Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapter.signTypedData #24

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions src/chains/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import {
isBytes,
SignableMessage,
verifyMessage,
verifyTypedData,
signatureToHex,
hashTypedData,
TypedData,
TypedDataDefinition,
} from "viem";
import {
BaseTx,
Expand Down Expand Up @@ -211,19 +215,48 @@ export class NearEthAdapter {
}
// Below code is inspired by https://github.com/Connor-ETHSeoul/near-viem

async signTypedData<
const typedData extends TypedData | Record<string, unknown>,
primaryType extends keyof typedData | "EIP712Domain" = keyof typedData,
>(typedData: TypedDataDefinition<typedData, primaryType>): Promise<Hash> {
const sigs = await this.sign(hashTypedData(typedData));

const common = {
address: this.ethPublicKey(),
types: typedData.types,
/* eslint-disable @typescript-eslint/no-explicit-any */
primaryType: typedData.primaryType as any,
message: typedData.message as any,
domain: typedData.domain as any,
/* eslint-enable @typescript-eslint/no-explicit-any */
};
const validity = await Promise.all([
verifyTypedData({
signature: sigs[0],
...common,
}),
verifyTypedData({
signature: sigs[1],
...common,
}),
]);
return this.pickValidSignature(validity, sigs);
}

async signMessage(message: SignableMessage): Promise<Hash> {
const sigs = await this.sign(hashMessage(message));
const address = this.ethPublicKey();
const common = {
address: this.ethPublicKey(),
message,
};
const validity = await Promise.all([
verifyMessage({
address,
message,
signature: sigs[0],
...common,
}),
verifyMessage({
address,
message: message,
signature: sigs[1],
...common,
}),
]);
return this.pickValidSignature(validity, sigs);
Expand Down
42 changes: 42 additions & 0 deletions tests/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,46 @@ describe("End To End", () => {
it("signMessage", async () => {
await expect(evm.signMessage("NearEth")).resolves.not.toThrow();
});

it("signTypedData", async () => {
const message = {
from: {
name: "Cow",
wallet: "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826",
},
to: {
name: "Bob",
wallet: "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB",
},
contents: "Hello, Bob!",
} as const;

const domain = {
name: "Ether Mail",
version: "1",
chainId: 1,
verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
} as const;

const types = {
Person: [
{ name: "name", type: "string" },
{ name: "wallet", type: "address" },
],
Mail: [
{ name: "from", type: "Person" },
{ name: "to", type: "Person" },
{ name: "contents", type: "string" },
],
} as const;

await expect(
evm.signTypedData({
types,
primaryType: "Mail",
message,
domain,
})
).resolves.not.toThrow();
});
});