generated from scaffold-eth/scaffold-eth
-
Notifications
You must be signed in to change notification settings - Fork 13
/
myTest.js
353 lines (313 loc) · 12.3 KB
/
myTest.js
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
const { expect } = require("chai");
const { ethers } = require("hardhat");
const friendlyTypes = require('../types');
const BigNumber = ethers.BigNumber;
const { generateUtil } = require('eth-delegatable-utils');
const createTypedMessage = require('../scripts/createTypedMessage');
const sigUtil = require('eth-sig-util');
const {
TypedDataUtils,
} = sigUtil;
const {
typedSignatureHash,
encodeData,
} = TypedDataUtils;
const { encode } = require("punycode");
const { TIMEOUT } = require("dns");
const { create } = require("domain");
const types = signTypedDataify(friendlyTypes);
const CONTRACT_NAME = 'YourContract';
const ownerHexPrivateKey = 'ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80';
const account1PrivKey = '59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d';
const account2PrivKey = '5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a';
require('../contracts/caveat-enforcers/index.test.js');
require('../contracts/examples/erc20.test.js');
require('./delegatable-utils-test.js');
describe(CONTRACT_NAME, function () {
it('setPurpose by owner changes purpose', async () => {
const targetString = 'A totally new purpose!'
const yourContract = await deployContract();
await yourContract.setPurpose(targetString);
expect(await yourContract.purpose()).to.equal(targetString);
});
it('other accounts cannot set purpose', async () => {
const [_owner, addr1] = await ethers.getSigners();
const targetString = 'A totally BAD purpose!'
const yourContract = await deployContract();
try {
await yourContract.connect(addr1).setPurpose(targetString);
} catch (err) {
expect(err.message).to.include('Ownable: caller is not the owner');
}
});
it('can sign a delegation to a second account', async () => {
const [owner, addr1, addr2] = await ethers.getSigners();
console.log(`owner: ${owner.address}`);
console.log(`addr1: ${addr1.address}`);
console.log(`addr2: ${addr2.address}`);
const targetString = 'A totally DELEGATED purpose!'
const yourContract = await deployContract();
const { chainId } = await yourContract.provider.getNetwork();
const contractInfo = {
chainId,
verifyingContract: yourContract.address,
name: CONTRACT_NAME,
};
// Prepare the delegation message:
// This message has no caveats, and authority 0,
// so it is a simple delegation to addr1 with no restrictions,
// and will allow the delegate to perform any action the signer could perform on this contract.
const delegation = {
delegate: addr1.address,
authority: '0x0000000000000000000000000000000000000000000000000000000000000000',
caveats: [],
};
const util = generateUtil(contractInfo);
// Owner signs the delegation:
const signedDelegation = util.signDelegation(delegation, ownerHexPrivateKey);
// Delegate signs the invocation message:
const desiredTx = await yourContract.populateTransaction.setPurpose(targetString);
const delegatePrivateKey = fromHexString(account1PrivKey);
const invocationMessage = {
replayProtection: {
nonce: '0x01',
queue: '0x00',
},
batch: [{
authority: [signedDelegation],
transaction: {
to: yourContract.address,
gasLimit: '10000000000000000',
data: desiredTx.data,
},
}],
};
const typedInvocationMessage = createTypedMessage(yourContract, invocationMessage, 'Invocations', CONTRACT_NAME);
const invocationSig = sigUtil.signTypedData_v4(
delegatePrivateKey,
typedInvocationMessage
);
const signedInvocation = {
signature: invocationSig,
invocations: invocationMessage,
}
// A third party can submit the invocation method to the chain:
const res = await yourContract.connect(addr2).invoke([signedInvocation]);
// Verify the change was made:
expect(await yourContract.purpose()).to.equal(targetString);
})
it('delegates can delegate', async () => {
const [owner, addr1, addr2, addr3] = await ethers.getSigners();
console.log(`owner: ${owner.address}`);
console.log(`addr1: ${addr1.address}`);
console.log(`addr2: ${addr2.address}`);
console.log(`addr3: ${addr3.address}`);
const targetString = 'A totally DELEGATED purpose!'
const yourContract = await deployContract();
const { chainId } = await yourContract.provider.getNetwork();
const contractInfo = {
chainId,
verifyingContract: yourContract.address,
name: CONTRACT_NAME,
};
const util = generateUtil(contractInfo);
// Prepare the delegation message:
// This message has no caveats, and authority 0,
// so it is a simple delegation to addr1 with no restrictions,
// and will allow the delegate to perform any action the signer could perform on this contract.
const delegation = {
delegate: addr1.address,
authority: '0x0000000000000000000000000000000000000000000000000000000000000000',
caveats: [],
};
// Owner signs the delegation:
const signedDelegation = util.signDelegation(delegation, ownerHexPrivateKey);
const delegationHash = TypedDataUtils.hashStruct('SignedDelegation', signedDelegation, types, true);
// First delegate signs the second delegation:
const delegation2 = {
delegate: addr2.address,
authority: delegationHash,
caveats: [],
};
const signedDelegation2 = util.signDelegation(delegation2, account1PrivKey);
// Second delegate signs the invocation message:
const desiredTx = await yourContract.populateTransaction.setPurpose(targetString);
const delegatePrivateKey = fromHexString(account2PrivKey);
const invocationMessage = {
replayProtection: {
nonce: '0x01',
queue: '0x00',
},
batch: [{
authority: [signedDelegation, signedDelegation2],
transaction: {
to: yourContract.address,
gasLimit: '10000000000000000',
data: desiredTx.data,
},
}],
};
const typedInvocationMessage = createTypedMessage(yourContract, invocationMessage, 'Invocations', CONTRACT_NAME);
const invocationSig = sigUtil.signTypedData_v4(
delegatePrivateKey,
typedInvocationMessage
);
const signedInvocation = {
signature: invocationSig,
invocations: invocationMessage,
}
// A third party can submit the invocation method to the chain:
const res = await yourContract.connect(addr3).invoke([signedInvocation]);
// Verify the change was made:
expect(await yourContract.purpose()).to.equal(targetString);
})
it('can allow-list a method with a caveat and it works', async () => {
const [owner, addr1, addr2] = await ethers.getSigners();
console.log(`owner: ${owner.address}`);
console.log(`addr1: ${addr1.address}`);
console.log(`addr2: ${addr2.address}`);
const targetString = 'A totally DELEGATED purpose!'
const yourContract = await deployContract();
const { chainId } = await yourContract.provider.getNetwork();
const contractInfo = {
chainId,
verifyingContract: yourContract.address,
name: CONTRACT_NAME,
};
const util = generateUtil(contractInfo);
const AllowListEnforcer = await ethers.getContractFactory('AllowedMethodsEnforcer');
const allowListEnforcer = await AllowListEnforcer.deploy();
await yourContract.deployed();
const desiredTx = await yourContract.populateTransaction.setPurpose(targetString);
const methodSig = desiredTx.data.substr(0, 10);
// Prepare the delegation message:
// This message has no caveats, and authority 0,
// so it is a simple delegation to addr1 with no restrictions,
// and will allow the delegate to perform any action the signer could perform on this contract.
const delegation = {
delegate: addr1.address,
authority: '0x0000000000000000000000000000000000000000000000000000000000000000',
caveats: [{
enforcer: allowListEnforcer.address,
terms: methodSig
}],
};
// Owner signs the delegation:
const signedDelegation = util.signDelegation(delegation, ownerHexPrivateKey);
// Delegate signs the invocation message:
const delegatePrivateKey = fromHexString(account1PrivKey);
const invocationMessage = {
replayProtection: {
nonce: '0x01',
queue: '0x00',
},
batch: [{
authority: [signedDelegation],
transaction: {
to: yourContract.address,
gasLimit: '10000000000000000',
data: desiredTx.data,
},
}],
};
const typedInvocationMessage = createTypedMessage(yourContract, invocationMessage, 'Invocations', CONTRACT_NAME);
const invocationSig = sigUtil.signTypedData_v4(
delegatePrivateKey,
typedInvocationMessage
);
const signedInvocation = {
signature: invocationSig,
invocations: invocationMessage,
}
// A third party can submit the invocation method to the chain:
const res = await yourContract.connect(addr2).invoke([signedInvocation]);
// Verify the change was made:
expect(await yourContract.purpose()).to.equal(targetString);
})
it('can allow-list a method with a caveat and disallows another method', async () => {
const [owner, addr1, addr2] = await ethers.getSigners();
console.log(`owner: ${owner.address}`);
console.log(`addr1: ${addr1.address}`);
console.log(`addr2: ${addr2.address}`);
const targetString = 'A totally DELEGATED purpose!'
const yourContract = await deployContract();
const { chainId } = await yourContract.provider.getNetwork();
const contractInfo = {
chainId,
verifyingContract: yourContract.address,
name: CONTRACT_NAME,
};
const util = generateUtil(contractInfo);
const AllowListEnforcer = await ethers.getContractFactory('AllowedMethodsEnforcer');
const allowListEnforcer = await AllowListEnforcer.deploy();
await yourContract.deployed();
const desiredTx = await yourContract.populateTransaction.setPurpose(targetString);
// Prepare the delegation message:
// This message has no caveats, and authority 0,
// so it is a simple delegation to addr1 with no restrictions,
// and will allow the delegate to perform any action the signer could perform on this contract.
const delegation = {
delegate: addr1.address,
authority: '0x0000000000000000000000000000000000000000000000000000000000000000',
caveats: [{
enforcer: allowListEnforcer.address,
terms: '0x00000000',
}],
};
// Owner signs the delegation:
const signedDelegation = util.signDelegation(delegation, ownerHexPrivateKey);
// Delegate signs the invocation message:
const delegatePrivateKey = fromHexString(account1PrivKey);
const invocationMessage = {
replayProtection: {
nonce: '0x01',
queue: '0x00',
},
batch: [{
authority: [signedDelegation],
transaction: {
to: yourContract.address,
gasLimit: '10000000000000000',
data: desiredTx.data,
},
}],
};
const typedInvocationMessage = createTypedMessage(yourContract, invocationMessage, 'Invocations', CONTRACT_NAME);
const invocationSig = sigUtil.signTypedData_v4(
delegatePrivateKey,
typedInvocationMessage
);
const signedInvocation = {
signature: invocationSig,
invocations: invocationMessage,
}
try {
const res = await yourContract.connect(addr2).invoke([signedInvocation]);
} catch (err) {
expect (err.message).to.include('Caveat rejected');
}
});
});
async function deployContract () {
const YourContract = await ethers.getContractFactory(CONTRACT_NAME);
const yourContract = await YourContract.deploy(CONTRACT_NAME);
return yourContract.deployed();
}
function fromHexString (hexString) {
return new Uint8Array(hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
}
function signTypedDataify (friendlyTypes) {
const types = {};
Object.keys(friendlyTypes).forEach(typeName => {
const type = friendlyTypes[typeName];
types[typeName] = [];
Object.keys(friendlyTypes[typeName]).forEach(subTypeName => {
const subType = friendlyTypes[typeName][subTypeName];
types[typeName].push({
name: subTypeName,
type: subType,
});
});
});
return types;
}