-
Notifications
You must be signed in to change notification settings - Fork 5k
/
Copy patheth.accounts.sign.js
159 lines (121 loc) · 6.57 KB
/
eth.accounts.sign.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
var Accounts = require("./../packages/web3-eth-accounts");
var chai = require('chai');
var assert = chai.assert;
var Web3 = require('../packages/web3');
var web3 = new Web3();
var tests = [
{
address: '0xEB014f8c8B418Db6b45774c326A0E64C78914dC0',
privateKey: '0xbe6383dad004f233317e46ddb46ad31b16064d14447a95cc1d8c8d4bc61c3728',
data: 'Some data',
// signature done with personal_sign
signature: '0xa8037a6116c176a25e6fc224947fde9e79a2deaa0dd8b67b366fbdfdbffc01f953e41351267b20d4a89ebfe9c8f03c04de9b345add4a52f15bd026b63c8fb1501b'
}, {
address: '0xEB014f8c8B418Db6b45774c326A0E64C78914dC0',
privateKey: '0xbe6383dad004f233317e46ddb46ad31b16064d14447a95cc1d8c8d4bc61c3728',
data: 'Some data!%$$%&@*',
// signature done with personal_sign
signature: '0x05252412b097c5d080c994d1ea12abcee6f1cae23feb225517a0b691a66e12866b3f54292f9cfef98f390670b4d010fc4af7fcd46e41d72870602c117b14921c1c'
}, {
address: '0xEB014f8c8B418Db6b45774c326A0E64C78914dC0',
privateKey: '0xbe6383dad004f233317e46ddb46ad31b16064d14447a95cc1d8c8d4bc61c3728',
data: '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470',
// signature done with personal_sign
signature: '0xddd493679d80c9c74e0e5abd256a496dfb31b51cd39ea2c7c9e8a2a07de94a90257107a00d9cb631bacb85b208d66bfa7a80c639536b34884505eff352677dd01c'
}
];
describe("eth", function () {
describe("accounts", function () {
tests.forEach(function (test, i) {
it("sign data using a string", function() {
var ethAccounts = new Accounts();
var data = ethAccounts.sign(test.data, test.privateKey);
assert.equal(data.signature, test.signature);
});
it("sign data using a utf8 encoded hex string", function() {
var ethAccounts = new Accounts();
var data = web3.utils.isHexStrict(test.data) ? test.data : web3.utils.utf8ToHex(test.data);
var data = ethAccounts.sign(data, test.privateKey);
assert.equal(data.signature, test.signature);
});
it("recover signature using a string", function() {
var ethAccounts = new Accounts();
var address = ethAccounts.recover(test.data, test.signature);
assert.equal(address, test.address);
});
it("recover signature using a string and preFixed", function() {
var ethAccounts = new Accounts();
var address = ethAccounts.recover(ethAccounts.hashMessage(test.data), test.signature, true);
assert.equal(address, test.address);
});
it("recover signature using a hash and r s v values and preFixed", function() {
var ethAccounts = new Accounts();
var sig = ethAccounts.sign(test.data, test.privateKey);
var address = ethAccounts.recover(ethAccounts.hashMessage(test.data), sig.v, sig.r, sig.s, true);
assert.equal(address, test.address);
});
it("recover signature (pre encoded) using a signature object", function() {
var ethAccounts = new Accounts();
var data = web3.utils.isHexStrict(test.data) ? test.data : web3.utils.utf8ToHex(test.data);
var sig = ethAccounts.sign(data, test.privateKey);
var address = ethAccounts.recover(sig);
assert.equal(address, test.address);
});
it("recover signature using a signature object", function() {
var ethAccounts = new Accounts();
var sig = ethAccounts.sign(test.data, test.privateKey);
var address = ethAccounts.recover(sig);
assert.equal(address, test.address);
});
it("recover signature (pre encoded) using a hash and r s v values", function() {
var ethAccounts = new Accounts();
var data = web3.utils.isHexStrict(test.data) ? test.data : web3.utils.utf8ToHex(test.data);
var sig = ethAccounts.sign(data, test.privateKey);
var address = ethAccounts.recover(test.data, sig.v, sig.r, sig.s);
assert.equal(address, test.address);
});
it("recover signature using a hash and r s v values", function() {
var ethAccounts = new Accounts();
var sig = ethAccounts.sign(test.data, test.privateKey);
var address = ethAccounts.recover(test.data, sig.v, sig.r, sig.s);
assert.equal(address, test.address);
});
});
});
it('should add the "0x" prefix and sign the given message correctly', function() {
assert.equal(
'0xa8037a6116c176a25e6fc224947fde9e79a2deaa0dd8b67b366fbdfdbffc01f953e41351267b20d4a89ebfe9c8f03c04de9b345add4a52f15bd026b63c8fb1501b',
new Accounts().sign('Some data', 'be6383dad004f233317e46ddb46ad31b16064d14447a95cc1d8c8d4bc61c3728').signature
);
});
it('should add the "0x" prefix to the privateKey', function() {
assert.equal(
'0xbe6383dad004f233317e46ddb46ad31b16064d14447a95cc1d8c8d4bc61c3728',
new Accounts().privateKeyToAccount('be6383dad004f233317e46ddb46ad31b16064d14447a95cc1d8c8d4bc61c3728').privateKey
);
});
it('should throw if a privateKey is given with a invalid length', function() {
try {
new Accounts().privateKeyToAccount('0000be6383dad004f233317e46ddb46ad31b16064d14447a95cc1d8c8d4bc61c3728');
assert.fail();
} catch(err) {
assert(err.message.includes('Private key must be 32 bytes long'));
}
});
it('should throw if a privateKey is given with a invalid length', function() {
try {
new Accounts().sign('data', '00be6383dad004f233317e46ddb46ad31b16064d14447a95cc1d8c8d4bc61c3728');
assert.fail();
} catch(err) {
assert(err.message.includes('Private key must be 32 bytes long'));
}
});
it('should sign data correctly using an emoji character', function() {
const message = '🤗'
const ethAccounts = new Accounts();
const hashMessage = ethAccounts.hashMessage(message)
assert.equal(hashMessage, '0x716ce69c5d2d629c168bc02e24a961456bdc5a362d366119305aea73978a0332')
const hashMessageHex = ethAccounts.hashMessage(web3.utils.utf8ToHex(message))
assert.equal(hashMessageHex, '0x716ce69c5d2d629c168bc02e24a961456bdc5a362d366119305aea73978a0332')
});
});