diff --git a/src/ethauth.ts b/src/ethauth.ts index 0b7e599..79c1bd0 100644 --- a/src/ethauth.ts +++ b/src/ethauth.ts @@ -12,6 +12,10 @@ export const ETHAuthEIP712Domain = { version: ETHAuthVersion, } +export interface EncodingOptions { + skipSignatureValidation?: boolean +} + export class ETHAuth { validators: ValidatorFunc[] ethereumJsonRpcURL: string @@ -21,7 +25,7 @@ export class ETHAuth { constructor(...validators: ValidatorFunc[]) { if (validators.length == 0) { this.validators = [ ValidateEOAProof, ValidateContractAccountProof ] - }else { + } else { this.validators = validators } } @@ -46,7 +50,7 @@ export class ETHAuth { this.validators = validators } - encodeProof = async (proof: Proof, skipSignatureValidation: boolean = false): Promise => { + encodeProof = async (proof: Proof, opts: EncodingOptions): Promise => { if (proof.address.length !== 42 || proof.address.slice(0,2) !== '0x') { throw new Error('ethauth: invalid address') } @@ -57,7 +61,7 @@ export class ETHAuth { throw new Error('ethauth: invalid extra encoding, expecting hex data') } - const isValid = await this.validateProof(proof, skipSignatureValidation) + const isValid = await this.validateProof(proof, opts) if (!isValid) { throw new Error(`ethauth: proof is invalid`) } @@ -77,7 +81,7 @@ export class ETHAuth { return proofString } - decodeProof = async (proofString: string, skipSignatureValidation: boolean = false): Promise => { + decodeProof = async (proofString: string, opts: EncodingOptions): Promise => { const parts = proofString.split('.') if (parts.length < 4 || parts.length > 5) { throw new Error('ethauth: invalid proof string') @@ -98,7 +102,7 @@ export class ETHAuth { const proof = new Proof({ address, claims, signature, extra }) // Validate proof signature and claims - const isValid = await this.validateProof(proof, skipSignatureValidation) + const isValid = await this.validateProof(proof, opts) if (!isValid) { throw new Error(`ethauth: proof is invalid`) } @@ -106,13 +110,13 @@ export class ETHAuth { return proof } - validateProof = async (proof: Proof, skipSignatureValidation: boolean = false): Promise => { + validateProof = async (proof: Proof, opts: EncodingOptions): Promise => { const isValidClaims = this.validateProofClaims(proof) if (isValidClaims.err) { throw new Error(`ethauth: proof claims are invalid ${isValidClaims.err}`) } - if (skipSignatureValidation !== true) { + if (opts.skipSignatureValidation !== true) { const isValidSig = await this.validateProofSignature(proof) if (isValidSig !== true) { throw new Error('ethauth: proof signature is invalid') diff --git a/src/validate.ts b/src/validate.ts index 2318572..2302e27 100644 --- a/src/validate.ts +++ b/src/validate.ts @@ -58,5 +58,12 @@ export const ValidateContractAccountProof: ValidatorFunc = async (provider: ethe } } +// ValidateTrue will always return a proof to be valid. This is useful if you'd like to have +// a noop validator, or to skip validation at this level. Such a feature is useful if you're +// validating in another code path and want to optimize double-validations. +export const ValidateTrue: ValidatorFunc = async () => { + return { isValid: true } +} + // IsValidSignatureBytes32 is the EIP-1271 magic value we test export const IsValidSignatureBytes32MagicValue = '0x1626ba7e'