Skip to content

Commit

Permalink
feat: Support EIP-6492 signatures (#1052)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaituVR committed Sep 18, 2024
1 parent 940316b commit d6f0fad
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 2 deletions.
58 changes: 58 additions & 0 deletions src/verify/evm.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { test, expect, describe } from 'vitest';
import evmMessage from '../../test/fixtures/evm/message-alias.json';
import eip6492Message from '../../test/fixtures/evm/eip-6492.json';
import eip1271Message from '../../test/fixtures/evm/eip-1271.json';
import verify, { getHash } from './evm';

describe('verify/evm', () => {
Expand Down Expand Up @@ -28,5 +30,61 @@ describe('verify/evm', () => {
)
).rejects.toThrowError(/isValidSignature/);
});

test('should return true if the eip-1271 message signature is valid', async () => {
expect(
verify(eip1271Message.address, eip1271Message.sig, eip1271Message.data)
).resolves.toBe(true);
});

test('should reject if the eip-1271 message signer address is wrong', async () => {
expect(
verify(
'0xDD983E11Cf84746f3b7589ee1Dc2081c08c40Cb3',
eip1271Message.sig,
eip1271Message.data
)
).rejects.toThrowError(/isValidSignature/);
});

test('should reject if the eip-1271 message signature is invalid', async () => {
const invalidMessageData = Object.assign({}, eip1271Message.data, {
message: {
...eip1271Message.data.message,
choices: [false]
}
});
expect(
verify(eip1271Message.address, eip1271Message.sig, invalidMessageData)
).rejects.toThrowError(/Hash not approved/);
});

test('should return true if the eip-6492 message signature is valid', async () => {
expect(
verify(eip6492Message.address, eip6492Message.sig, eip6492Message.data)
).resolves.toBe(true);
});

test('should reject if the eip-6492 message signer address is wrong', async () => {
expect(
verify(
'0xDD983E11Cf84746f3b7589ee1Dc2081c08c40Cb3',
eip6492Message.sig,
eip6492Message.data
)
).resolves.toBe(false);
});

test('should reject if the eip-6492 message signature is invalid', async () => {
const invalidMessageData = Object.assign({}, eip6492Message.data, {
message: {
...eip6492Message.data.message,
choice: 3
}
});
expect(
verify(eip6492Message.address, eip6492Message.sig, invalidMessageData)
).resolves.toBe(false);
});
});
});
34 changes: 32 additions & 2 deletions src/verify/evm.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { verifyTypedData } from '@ethersproject/wallet';
import { _TypedDataEncoder } from '@ethersproject/hash';
import { arrayify } from '@ethersproject/bytes';
import { arrayify, concat } from '@ethersproject/bytes';
import getProvider, { type ProviderOptions } from '../utils/provider';
import { call } from '../utils';
import type { SignaturePayload } from '.';
import type { StaticJsonRpcProvider } from '@ethersproject/providers';
import { AbiCoder } from '@ethersproject/abi';

const ERC6492_DETECTION_SUFFIX =
'6492649264926492649264926492649264926492649264926492649264926492';

function isEqual(a: string, b: string): boolean {
return a.toLowerCase() === b.toLowerCase();
Expand Down Expand Up @@ -32,8 +36,34 @@ export default async function verify(
const provider = getProvider(network, options);
const hash = getHash(data);

if (await verifyDefault(address, sig, hash, provider)) return true;
// Handle EIP-6492
// https://eips.ethereum.org/EIPS/eip-6492
//
// We can actually replace verifyTypedData and verifyDefault with the following code,
// but https://github.com/AmbireTech/signature-validator/blob/main/contracts/DeploylessUniversalSigValidator.sol
// also can send an extra network request to the provider. (with verifyTypedData we don't send any extra request)
//
if (sig.endsWith(ERC6492_DETECTION_SUFFIX)) {
try {
return (
'0x01' ===
(await provider.call({
data: concat([
'0x60806040523480156200001157600080fd5b50604051620007003803806200070083398101604081905262000034916200056f565b6000620000438484846200004f565b9050806000526001601ff35b600080846001600160a01b0316803b806020016040519081016040528181526000908060200190933c90507f6492649264926492649264926492649264926492649264926492649264926492620000a68462000451565b036200021f57600060608085806020019051810190620000c79190620005ce565b8651929550909350915060000362000192576000836001600160a01b031683604051620000f5919062000643565b6000604051808303816000865af19150503d806000811462000134576040519150601f19603f3d011682016040523d82523d6000602084013e62000139565b606091505b5050905080620001905760405162461bcd60e51b815260206004820152601e60248201527f5369676e617475726556616c696461746f723a206465706c6f796d656e74000060448201526064015b60405180910390fd5b505b604051630b135d3f60e11b808252906001600160a01b038a1690631626ba7e90620001c4908b90869060040162000661565b602060405180830381865afa158015620001e2573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906200020891906200069d565b6001600160e01b031916149450505050506200044a565b805115620002b157604051630b135d3f60e11b808252906001600160a01b03871690631626ba7e9062000259908890889060040162000661565b602060405180830381865afa15801562000277573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906200029d91906200069d565b6001600160e01b031916149150506200044a565b8251604114620003195760405162461bcd60e51b815260206004820152603a6024820152600080516020620006e083398151915260448201527f3a20696e76616c6964207369676e6174757265206c656e677468000000000000606482015260840162000187565b620003236200046b565b506020830151604080850151855186939260009185919081106200034b576200034b620006c9565b016020015160f81c9050601b81148015906200036b57508060ff16601c14155b15620003cf5760405162461bcd60e51b815260206004820152603b6024820152600080516020620006e083398151915260448201527f3a20696e76616c6964207369676e617475726520762076616c75650000000000606482015260840162000187565b6040805160008152602081018083528a905260ff83169181019190915260608101849052608081018390526001600160a01b038a169060019060a0016020604051602081039080840390855afa1580156200042e573d6000803e3d6000fd5b505050602060405103516001600160a01b031614955050505050505b9392505050565b60006020825110156200046357600080fd5b508051015190565b60405180606001604052806003906020820280368337509192915050565b6001600160a01b03811681146200049f57600080fd5b50565b634e487b7160e01b600052604160045260246000fd5b60005b83811015620004d5578181015183820152602001620004bb565b50506000910152565b600082601f830112620004f057600080fd5b81516001600160401b03808211156200050d576200050d620004a2565b604051601f8301601f19908116603f01168101908282118183101715620005385762000538620004a2565b816040528381528660208588010111156200055257600080fd5b62000565846020830160208901620004b8565b9695505050505050565b6000806000606084860312156200058557600080fd5b8351620005928162000489565b6020850151604086015191945092506001600160401b03811115620005b657600080fd5b620005c486828701620004de565b9150509250925092565b600080600060608486031215620005e457600080fd5b8351620005f18162000489565b60208501519093506001600160401b03808211156200060f57600080fd5b6200061d87838801620004de565b935060408601519150808211156200063457600080fd5b50620005c486828701620004de565b6000825162000657818460208701620004b8565b9190910192915050565b828152604060208201526000825180604084015262000688816060850160208701620004b8565b601f01601f1916919091016060019392505050565b600060208284031215620006b057600080fd5b81516001600160e01b0319811681146200044a57600080fd5b634e487b7160e01b600052603260045260246000fdfe5369676e617475726556616c696461746f72237265636f7665725369676e6572',
new AbiCoder().encode(
['address', 'bytes32', 'bytes'],
[address, arrayify(hash), sig]
)
])
}))
);
} catch (error) {
return false;
}
}

// Handle EIP-1271
if (await verifyDefault(address, sig, hash, provider)) return true;
return await verifyOldVersion(address, sig, hash, provider);
}

Expand Down
85 changes: 85 additions & 0 deletions test/fixtures/evm/eip-1271.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"address": "0x052cbB9395b350b15540537Fb7E1b434A2701958",
"sig": "0x",
"hash": "0x5bb1dabff7dd20555101d712b36de6f60728d6ebb9cb96f96c4d5ec8a9b77391",
"data": {
"types": {
"Proposal": [
{
"name": "from",
"type": "address"
},
{
"name": "space",
"type": "string"
},
{
"name": "timestamp",
"type": "uint64"
},
{
"name": "type",
"type": "string"
},
{
"name": "title",
"type": "string"
},
{
"name": "body",
"type": "string"
},
{
"name": "discussion",
"type": "string"
},
{
"name": "choices",
"type": "string[]"
},
{
"name": "start",
"type": "uint64"
},
{
"name": "end",
"type": "uint64"
},
{
"name": "snapshot",
"type": "uint64"
},
{
"name": "plugins",
"type": "string"
},
{
"name": "app",
"type": "string"
}
]
},
"domain": {
"name": "snapshot",
"version": "0.1.4"
},
"message": {
"app": "snapshot",
"end": 1724545965,
"body": "### Abstract\n\nThis proposal aims to put an end date for the JPEG to JPGD migration program. All JPEG token after the migration end date won’t be able to be migrated. The proposed end date for the migration is *October 30th 2024.*\n\n### Background\n\nThere is currently ~16 billion JPEG that has not yet been migrated to JPGD. \n\nIn order to consolidate liquidity, we suggest to amend PIP-80 “Migrate $JPEG to $JPEGD,” and set an end date to the migration of JPEG tokens to JPGD tokens.\n\nEffective *October 30th 2024*, holders of JPEG token will no longer have the ability to migrate their tokens and receive JPGD.\n\n### Specification\n\nEffective *October 30th 2024*, JPEG token will no longer have the ability to be migrated to JPGD.",
"from": "0x052cbB9395b350b15540537Fb7E1b434A2701958",
"type": "single-choice",
"space": "jpeg’d.eth",
"start": 1724286765,
"title": "PIP-92 - End JPEG to JPGD Token Migration",
"choices": [
"yes, end JPEG to JPGD migration ",
"no, keep JPEG to JPGD migration"
],
"plugins": "{}",
"snapshot": 20579390,
"timestamp": 1724272365,
"discussion": ""
}
}
}
56 changes: 56 additions & 0 deletions test/fixtures/evm/eip-6492.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"address": "0xd0AdFF3256B686137E7c382f6c338999a99b237e",
"sig": "0x000000000000000000000000ca11bde05977b3631167028862be2a173976ca110000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000028000000000000000000000000000000000000000000000000000000000000001e482ad56cb0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000ba5ed0c6aa8c49038f819e587e2633c4a9f428a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000e43ffba36f00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040a09bbeb80f20da2eee5bf137d86f150f0cbe5999bb915813f5a177d3be1cb0d907ec074efa515825d19c4017b6315e70b431bfa058c60b5f89f190203e900cce000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000000170000000000000000000000000000000000000000000000000000000000000001827d25d6619ff31c602f0edcebd04e2f45d5115b507e5b0acc19c7a3bf05b11102c49fde8c71f4dd5e0d3a0140c12b468d323e3a3a089d9a85981b91d89725ca0000000000000000000000000000000000000000000000000000000000000025f198086b2db17256731bc456673b96bcef23f51d1fbacdd7c4379ef65465572f1d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008a7b2274797065223a22776562617574686e2e676574222c226368616c6c656e6765223a225267687057735861394d6231544e6d714d5f754a71316b5a38636e7a704f6a65677051702d4477484e2d49222c226f726967696e223a2268747470733a2f2f6b6579732e636f696e626173652e636f6d222c2263726f73734f726967696e223a66616c73657d000000000000000000000000000000000000000000006492649264926492649264926492649264926492649264926492649264926492",
"data": {
"domain": {
"name": "snapshot",
"version": "0.1.4"
},
"types": {
"Vote": [
{
"name": "from",
"type": "address"
},
{
"name": "space",
"type": "string"
},
{
"name": "timestamp",
"type": "uint64"
},
{
"name": "proposal",
"type": "string"
},
{
"name": "choice",
"type": "uint32"
},
{
"name": "reason",
"type": "string"
},
{
"name": "app",
"type": "string"
},
{
"name": "metadata",
"type": "string"
}
]
},
"message": {
"from": "0xd0AdFF3256B686137E7c382f6c338999a99b237e",
"timestamp": 1724919393,
"space": "pistachiodao.eth",
"proposal": "0x38c654c0f81b63ea1839ec3b221fad6ecba474aa0c4e8b4e8bc957f70100e753",
"choice": 1,
"reason": "",
"app": "",
"metadata": ""
}
}
}

0 comments on commit d6f0fad

Please sign in to comment.