forked from makerdao/helper-scripts
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerateSig.js
61 lines (56 loc) · 2.24 KB
/
generateSig.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
const ethUtil = require('ethereumjs-util');
const sigUtil = require('eth-sig-util');
const utils = sigUtil.TypedDataUtils;
//Our lad Cal wants to send 2 dai to del, by signing a cheque and paying a 1 dai fee to msg.sender
const calprivKeyHex = '4af1bceebf7f3634ec3cff8a2c38e51178d5d4ce585c52d6043e5e2cc3418bb0'
const calprivKey = new Buffer.from(calprivKeyHex, 'hex')
const cal = ethUtil.privateToAddress(calprivKey);
const del = new Buffer.from('dd2d5d3f7f1b35b7a0601d6a00dbb7d44af58479', 'hex');
const dai = new Buffer.from('11Ee1eeF5D446D07Cf26941C7F2B4B1Dfb9D030B', 'hex');
console.log('cals address: ' + '0x' + cal.toString('hex'));
console.log('dels address: ' + '0x' + del.toString('hex'));
let typedData = {
types: {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' },
],
Permit: [
{ name: 'holder', type: 'address' },
{ name: 'spender', type: 'address' },
{ name: 'nonce', type: 'uint256' },
{ name: 'expiry', type: 'uint256' },
{ name: 'allowed', type: 'bool' },
],
},
primaryType: 'Permit',
domain: {
name: 'Dai Stablecoin',
version: '1',
chainId: 99,
verifyingContract: '0x11Ee1eeF5D446D07Cf26941C7F2B4B1Dfb9D030B', //in hevm
},
message: {
holder: '0x'+cal.toString('hex'),
spender: '0x'+del.toString('hex'),
nonce: 0,
expiry: 604411200 + 3600,
allowed: true
},
};
let hash = ethUtil.bufferToHex(utils.hashStruct('EIP712Domain', typedData.domain, typedData.types))
console.log('EIP712DomainHash: ' + hash);
hash = ethUtil.bufferToHex(utils.hashType('Permit', typedData.types))
console.log('Permit Typehash: ' + hash);
hash = ethUtil.bufferToHex(utils.hashStruct('Permit', typedData.message, typedData.types))
console.log('Permit (from cal to del) hash: ' + hash);
const sig = sigUtil.signTypedData(calprivKey, { data: typedData });
console.log('signed permit: ' + sig);
let r = sig.slice(0,66);
let s = '0x'+ sig.slice(66,130);
let v = ethUtil.bufferToInt(ethUtil.toBuffer('0x'+sig.slice(130,132),'hex'));
console.log('r: ' + r)
console.log('s: ' + s)
console.log('v: ' + v)