forked from danfinlay/eip712-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
218 lines (183 loc) · 6.98 KB
/
test.js
File metadata and controls
218 lines (183 loc) · 6.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const { expect } = require('chai');
const ganache = require('ganache-cli');
const ethers = require('ethers');
const testEnv = require('@openzeppelin/test-environment');
const testContract = testEnv.contract;
const { deploy } = testEnv;
const sigUtil = require('@metamask/eth-sig-util');
const {
TypedDataUtils,
} = sigUtil;
const hre = require('hardhat');
// Import the generateSolidity function from the module
const { generateSolidity } = require('./index.js');
// Define your types and messages for testing
const MessageTypes = {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' },
],
Person: [
{ name: 'name', type: 'string' },
{ name: 'age', type: 'uint256' },
],
};
const message = {
types: MessageTypes,
primaryType: 'Person',
domain: {
name: 'MockEIP712Decoder',
version: '1',
chainId: 1,
},
message: {
name: 'Alice',
age: 30,
},
};
// Generate the Solidity code
const entryTypes = ['Person'];
const solidityCode = generateSolidity(message, false, entryTypes);
// Write the Solidity code to a file (for testing purposes)
const fs = require('fs');
fs.writeFileSync('./contracts/EIP712Decoder.sol', solidityCode);
// Run the tests
describe('EIP712Decoder', function () {
let contract, accounts, signer, typedData, eip712Decoder, privateKey, wallet;
before(async function () {
// Compile the contract using Hardhat
await hre.run('compile');
// Set up a ganache provider with the generated Solidity code
const ganacheProvider = ganache.provider({})
const provider = new ethers.providers.Web3Provider(ganacheProvider);
const mnemonic = ganacheProvider.options.mnemonic;
wallet = ethers.Wallet.fromMnemonic(mnemonic);
privateKey = wallet.privateKey;
accounts = await provider.listAccounts();
signer = provider.getSigner(accounts[0]);
// Load up the compiled contract artifact
const EIP712Decoder = await hre.artifacts.readArtifact('MockEIP712Decoder');
// Deploy the contract
const EIP712DecoderFactory = new ethers.ContractFactory(EIP712Decoder.abi, EIP712Decoder.bytecode, signer);
contract = await EIP712DecoderFactory.deploy([1]);
message.domain.verifyingContract = contract.address;
await contract.deployed();
// Create the typed data for testing
typedData = JSON.parse(JSON.stringify(message));
typedData.domain.verifyingContract = contract.address;
});
it('should generate the correct person type hash', async function () {
const solidityPersonPackethash = await contract.personTypehash();
const personTypestring = TypedDataUtils.encodeType('Person', typedData.types).toString('hex');
const jsPersonPackethash = ethers.utils.keccak256(ethers.utils.toUtf8Bytes(personTypestring));
expect(solidityPersonPackethash).to.equal(jsPersonPackethash);
});
it('should generate the correct person packet', async function () {
const person = {
name: 'Alice',
age: 30,
};
const encoded = TypedDataUtils.encodeData(
'Person',
person,
typedData.types,
'V4'
);
const solEncoded = await contract.getPersonPacket(person);
expect(solEncoded).to.equal('0x' + encoded.toString('hex'));
});
it('should generate the correct person packet hash', async function () {
const person = {
name: 'Alice',
age: 30,
};
const domainHash = await contract.getPersonPacketHash(person);
// Check if the domain hash is correct
expect(domainHash).to.equal('0x' + TypedDataUtils.hashStruct(
'Person',
person,
typedData.types,
'V4'
).toString('hex'));
});
it('should recover the correct signer', async function () {
const signedStruct = signStruct(privateKey);
// Call the verifySignedPerson function
const isValid = await contract.verifySignedPerson(signedStruct);
// Check if the signer is valid
expect(isValid).to.equal(wallet.address);
});
});
describe('EIP-1271', () => {
let contract, eip1271, owner, other, accounts, signer, typedData, eip712Decoder, privateKey, wallet;
beforeEach(async () => {
// Compile the contract using Hardhat
await hre.run('compile');
// Set up a ganache provider with the generated Solidity code
const ganacheProvider = ganache.provider({})
const provider = new ethers.providers.Web3Provider(ganacheProvider);
const mnemonic = ganacheProvider.options.mnemonic;
wallet = ethers.Wallet.fromMnemonic(mnemonic);
privateKey = wallet.privateKey;
accounts = await provider.listAccounts();
signer = provider.getSigner(accounts[0]);
// Load up the compiled contract artifact
const EIP712Decoder = await hre.artifacts.readArtifact('MockEIP712Decoder');
// Deploy the contract
const EIP712DecoderFactory = new ethers.ContractFactory(EIP712Decoder.abi, EIP712Decoder.bytecode, signer);
contract = await EIP712DecoderFactory.deploy([1]);
message.domain.verifyingContract = contract.address;
await contract.deployed();
// Create the typed data for testing
typedData = JSON.parse(JSON.stringify(message));
typedData.domain.verifyingContract = contract.address;
// Deploy the EIP-1271 contract
const EIP1271 = await hre.artifacts.readArtifact('EIP1271');
const EIP1271ContractFactory = new ethers.ContractFactory(EIP1271.abi, EIP1271.bytecode, signer);
const EIP1271Contract = await EIP1271ContractFactory.deploy([]);
eip1271 = await EIP1271Contract.deployed();
await eip1271.deployed();
await eip1271.addOwner(wallet.address);
});
describe('contract account signature recovery', () => {
it('should recover the correct signer', async () => {
// Sign the typed data using eth-sig-util
const signedPerson = signStruct(privateKey);
signedPerson.signer = eip1271.address;
// Call the verifySigned method with the EIP-1271 contract address as the signer
const result = await contract.verifySignedPerson(signedPerson);
// Verify that the returned address is equal to the EIP-1271 contract address
expect(result).to.equal(eip1271.address);
});
});
});
function signStruct (privateKey) {
const signature = sigUtil.signTypedData({
privateKey: fromHexString(privateKey.indexOf('0x') === 0 ? privateKey.substring(2) : privateKey),
data: message,
version: 'V4',
});
const signedStruct = {
signature,
signer: '0x0000000000000000000000000000000000000000',
message: message.message,
}
return signedStruct;
}
function fromHexString (_hexString) {
const hexString = _hexString.toLowerCase();
if (!hexString || typeof hexString !== 'string') {
throw new Error('Expected a hex string.');
}
const matched = hexString.match(/.{1,2}/g)
if (!matched) {
throw new Error('Expected a hex string.');
}
const mapped = matched.map(byte => parseInt(byte, 16));
if (!mapped || mapped.length !== 32) {
throw new Error('Expected a hex string.');
}
return new Uint8Array(mapped);
}