Skip to content

Commit

Permalink
Adapter.signTypedData (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
bh2smith authored Apr 25, 2024
1 parent a8b122d commit 888371e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 5 deletions.
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();
});
});

0 comments on commit 888371e

Please sign in to comment.