Skip to content

Commit

Permalink
whitelist vault sigs
Browse files Browse the repository at this point in the history
  • Loading branch information
taureau75 committed Nov 20, 2022
1 parent c7d3fc5 commit 76f6ba8
Showing 1 changed file with 5 additions and 18 deletions.
23 changes: 5 additions & 18 deletions src/cryptography.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { ethers } from 'ethers'
import type { AnyObject } from './types'
import { redis, publisher } from './redisClient'

const VALIDATOR_1271_ABI = [
'function isValidSignature(bytes32 hash, bytes signature) view returns (bytes4)'
]

const ON_CHAIN_ALLOWED_SIGNER_CACHE: AnyObject = {}

export function getEvmEIP712Types(chainId: number) {
if ([42161, 421613].includes(chainId)) {
return {
Expand Down Expand Up @@ -107,22 +106,10 @@ export async function verifyMessage(param: {
const recoveredAddress = recoverAddress(finalDigest, signature)
if (addrMatching(recoveredAddress, signer)) return true

// 2nd try: ON_CHAIN_ALLOWED_SIGNER_CACHE saves previus allowed signer
// optimistic assumtion: they are allowed to sign this time again.
// The contract does a real signature check anyway
const allowedAddress = ON_CHAIN_ALLOWED_SIGNER_CACHE[signer]
if (allowedAddress && addrMatching(recoveredAddress, allowedAddress)) return true

// 3st try: Getting code from deployed smart contract to call 1271 isValidSignature.
try {
if (await eip1271Check(provider, signer, finalDigest, signature)) {
ON_CHAIN_ALLOWED_SIGNER_CACHE[signer] = recoveredAddress
return true
}
} catch (err: any) {
console.error(`Failed to check signature on chain: ${err.message}`)
return true // better accept orders, as this check is optinal anyway
}
// 2nd try: Check registered vault address
// Requires manual whitelist
const vaultSigner = await redis.get(`vaultsigner:${signer}`);
if (vaultSigner && addrMatching(recoveredAddress, vaultSigner)) return true

return false
}

0 comments on commit 76f6ba8

Please sign in to comment.