Skip to content

Commit

Permalink
Added verifyTypedData utility (reported on Farcaster).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Mar 20, 2023
1 parent e11d4c1 commit f06a445
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src.ts/ethers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ export {
ensNormalize, isValidName, namehash, dnsEncode,
hashMessage, verifyMessage,
solidityPacked, solidityPackedKeccak256, solidityPackedSha256,
TypedDataEncoder
TypedDataEncoder,
verifyTypedData
} from "./hash/index.js";

export {
Expand Down Expand Up @@ -149,7 +150,8 @@ export type {
ConstantContractMethod, ContractEvent, ContractEventArgs, ContractEventName,
ContractInterface, ContractMethod, ContractMethodArgs, ContractTransaction,
DeferredTopicFilter, Overrides,
BaseContractMethod, ContractDeployTransaction, PostfixOverrides
BaseContractMethod, ContractDeployTransaction, PostfixOverrides,
WrappedFallback
} from "./contract/index.js";

export type { ProgressCallback, SignatureLike } from "./crypto/index.js";
Expand Down
2 changes: 1 addition & 1 deletion src.ts/hash/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ export { hashMessage, verifyMessage } from "./message.js";
export {
solidityPacked, solidityPackedKeccak256, solidityPackedSha256
} from "./solidity.js";
export { TypedDataEncoder } from "./typed-data.js";
export { TypedDataEncoder, verifyTypedData } from "./typed-data.js";

export type { TypedDataDomain, TypedDataField } from "./typed-data.js";
8 changes: 8 additions & 0 deletions src.ts/hash/typed-data.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
//import { TypedDataDomain, TypedDataField } from "@ethersproject/providerabstract-signer";
import { getAddress } from "../address/index.js";
import { keccak256 } from "../crypto/index.js";
import { recoverAddress } from "../transaction/index.js";
import {
concat, defineProperties, getBigInt, getBytes, hexlify, isHexString, mask, toBeHex, toTwos, zeroPadValue,
assertArgument
} from "../utils/index.js";

import { id } from "./id.js";

import type { SignatureLike } from "../crypto/index.js";
import type { BigNumberish, BytesLike } from "../utils/index.js";


Expand Down Expand Up @@ -487,3 +489,9 @@ export class TypedDataEncoder {
}
}

/**
* Compute the address used to sign the typed data for the %%signature%%.
*/
export function verifyTypedData(domain: TypedDataDomain, types: Record<string, Array<TypedDataField>>, value: Record<string, any>, signature: SignatureLike): string {
return recoverAddress(TypedDataEncoder.hash(domain, types, value), signature);
}

0 comments on commit f06a445

Please sign in to comment.