Skip to content
Open
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
18 changes: 11 additions & 7 deletions src/ethauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export const ETHAuthEIP712Domain = {
version: ETHAuthVersion,
}

export interface EncodingOptions {
skipSignatureValidation?: boolean
}

export class ETHAuth {
validators: ValidatorFunc[]
ethereumJsonRpcURL: string
Expand All @@ -21,7 +25,7 @@ export class ETHAuth {
constructor(...validators: ValidatorFunc[]) {
if (validators.length == 0) {
this.validators = [ ValidateEOAProof, ValidateContractAccountProof ]
}else {
} else {
this.validators = validators
}
}
Expand All @@ -46,7 +50,7 @@ export class ETHAuth {
this.validators = validators
}

encodeProof = async (proof: Proof, skipSignatureValidation: boolean = false): Promise<string> => {
encodeProof = async (proof: Proof, opts: EncodingOptions): Promise<string> => {
if (proof.address.length !== 42 || proof.address.slice(0,2) !== '0x') {
throw new Error('ethauth: invalid address')
}
Expand All @@ -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`)
}
Expand All @@ -77,7 +81,7 @@ export class ETHAuth {
return proofString
}

decodeProof = async (proofString: string, skipSignatureValidation: boolean = false): Promise<Proof> => {
decodeProof = async (proofString: string, opts: EncodingOptions): Promise<Proof> => {
const parts = proofString.split('.')
if (parts.length < 4 || parts.length > 5) {
throw new Error('ethauth: invalid proof string')
Expand All @@ -98,21 +102,21 @@ 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`)
}

return proof
}

validateProof = async (proof: Proof, skipSignatureValidation: boolean = false): Promise<boolean> => {
validateProof = async (proof: Proof, opts: EncodingOptions): Promise<boolean> => {
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')
Expand Down
7 changes: 7 additions & 0 deletions src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'