Skip to content

Commit

Permalink
small updates
Browse files Browse the repository at this point in the history
  • Loading branch information
0xtrooper committed Nov 29, 2022
1 parent 816092f commit 07c3caa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
5 changes: 1 addition & 4 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,6 @@ export default class API extends EventEmitter {
delete zktx.signature

const result = await verifyMessage ({
provider: this.ETHERS_PROVIDERS[chainId] as ethers.providers.Provider,
signer: zktx.user,
typedData: {
domain: networkProviderConfig.domain,
Expand Down Expand Up @@ -1480,7 +1479,6 @@ export default class API extends EventEmitter {
if (signerAddress !== userId) throw new Error('Unauthorized')
} else {
const res = await verifyMessage({
provider: this.ETHERS_PROVIDERS[chainId] as ethers.providers.Provider,
signer: userId as string,
message: `cancelall2:${chainId}:${validUntil}`,
signature
Expand Down Expand Up @@ -1599,8 +1597,7 @@ export default class API extends EventEmitter {
signerAddress = res.result.accountId.toString()
if (signerAddress !== select.rows[0].userid) throw new Error('Unauthorized')
} else {
const res = await verifyMessage({
provider: this.ETHERS_PROVIDERS[chainId] as ethers.providers.Provider,
const res = await verifyMessage({verifyMessage
signer: select.rows[0].userid as string,
message: `cancelorder2:${chainId}:${orderId}`,
signature
Expand Down
15 changes: 7 additions & 8 deletions src/cryptography.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { ethers } from 'ethers'
import type { AnyObject } from './types'
import { redis, publisher } from './redisClient'
import { redis } from './redisClient'

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

export function getEvmEIP712Types(chainId: number) {
if ([42161, 421613].includes(chainId)) {
Expand Down Expand Up @@ -46,6 +44,7 @@ function addrMatching(recoveredAddr: string, targetAddr: string) {
return recoveredAddr.toLowerCase() === targetAddr.toLowerCase()
}

/*
// EIP 1271 check
async function eip1271Check(
provider: ethers.providers.Provider,
Expand All @@ -70,16 +69,16 @@ async function eip1271Check(
}
return false
}
*/

// you only need to pass one of: typedData or message
export async function verifyMessage(param: {
provider: ethers.providers.Provider
signer: string
message?: string
typedData?: AnyObject
signature: string
}): Promise<boolean> {
const { message, typedData, provider, signer } = param
const { message, typedData, signer } = param
const signature = modifyOldSignature(param.signature)
let finalDigest: string

Expand Down Expand Up @@ -108,9 +107,9 @@ export async function verifyMessage(param: {

// 2nd try: Check registered vault address
// Requires manual whitelist
const vaultSigner = await redis.get(`vaultsigner:${signer.toLowerCase()}`);
const vaultSigner = await redis.get(`vaultsigner:${signer.toLowerCase()}`)
if (vaultSigner && addrMatching(recoveredAddress, vaultSigner)) return true
console.log(`Expected ${signer}, recovered ${recoveredAddress}`);
console.log(`Expected ${signer}, recovered ${recoveredAddress}`)

return false
}

0 comments on commit 07c3caa

Please sign in to comment.