Allow null values for TypedData domain (#3623).

This commit is contained in:
Richard Moore 2023-03-03 17:59:58 -07:00
parent 287d94fc45
commit a32af3adc1

@ -20,11 +20,11 @@ const BN_1 = BigInt(1);
const BN_MAX_UINT256 = BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); const BN_MAX_UINT256 = BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
export interface TypedDataDomain { export interface TypedDataDomain {
name?: string; name?: null | string;
version?: string; version?: null | string;
chainId?: BigNumberish; chainId?: null | BigNumberish;
verifyingContract?: string; verifyingContract?: null | string;
salt?: BytesLike; salt?: null | BytesLike;
}; };
export interface TypedDataField { export interface TypedDataField {
@ -355,6 +355,7 @@ export class TypedDataEncoder {
static hashDomain(domain: TypedDataDomain): string { static hashDomain(domain: TypedDataDomain): string {
const domainFields: Array<TypedDataField> = [ ]; const domainFields: Array<TypedDataField> = [ ];
for (const name in domain) { for (const name in domain) {
if ((<Record<string, any>>domain)[name] == null) { continue; }
const type = domainFieldTypes[name]; const type = domainFieldTypes[name];
assertArgument(type, `invalid typed-data domain key: ${ JSON.stringify(name) }`, "domain", domain); assertArgument(type, `invalid typed-data domain key: ${ JSON.stringify(name) }`, "domain", domain);
domainFields.push({ name, type }); domainFields.push({ name, type });
@ -384,6 +385,13 @@ export class TypedDataEncoder {
// Make a copy to isolate it from the object passed in // Make a copy to isolate it from the object passed in
domain = Object.assign({ }, domain); domain = Object.assign({ }, domain);
// Allow passing null to ignore value
for (const key in domain) {
if ((<Record<string, any>>domain)[key] == null) {
delete (<Record<string, any>>domain)[key];
}
}
// Look up all ENS names // Look up all ENS names
const ensCache: Record<string, string> = { }; const ensCache: Record<string, string> = { };