-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetacoin.js
61 lines (56 loc) · 2.23 KB
/
metacoin.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 MetaCoin = artifacts.require('MetaCoin');
const util = require('ethereumjs-util')
// S.U.: Copied from ethereumjs-util `fromRpcSig`, with adjustment: do not increment v.
function fromRpcSig(sig) {
var buf = util.toBuffer(sig);
// NOTE: with potential introduction of chainId this might need to be updated
if (buf.length !== 65) {
throw new Error('Invalid signature length');
}
var v = buf[64];
// support both versions of `eth_sign` responses
// S.U.: Actually, let me comment that out for illustrative purposes.
// if (v < 27) {
// v += 27;
// }
return {
v: v,
r: buf.slice(0, 32),
s: buf.slice(32, 64),
};
}
function incrementV(signature) {
const parts = util.fromRpcSig(signature) // It increases "v" by 27 if v<27
return util.toRpcSig(parts.v, parts.r, parts.s)
}
contract('MetaCoin', (accounts) => {
const account = accounts[0]
const digest = web3.utils.soliditySha3({t: 'string', v: 'something'})
it('does not sign', async () => {
const signature = await web3.eth.sign(digest, account)
const metaCoinInstance = await MetaCoin.deployed();
const recovered = await metaCoinInstance.recover.call(digest, signature)
console.log('---------- DOES NOT SIGN')
console.log('signature', signature)
// console.log('signature parts', fromRpcSig(signature))
console.log('v=', fromRpcSig(signature).v)
console.log('account', account)
console.log('recovered', recovered)
console.log('----------')
assert.equal(recovered, account, 'Recovered incorrect address') // Failure
})
it('signs right after setting v to 27/28', async () => {
const signature = await web3.eth.sign(digest, account)
const properSignature = incrementV(signature)
const metaCoinInstance = await MetaCoin.deployed();
const recovered = await metaCoinInstance.recover.call(digest, properSignature)
console.log('---------- SIGNS AFTER INCREASED V')
console.log('signature', signature)
// console.log('signature parts', fromRpcSig(properSignature))
console.log('v=', fromRpcSig(properSignature).v)
console.log('account', account)
console.log('recovered', recovered)
console.log('----------')
assert.equal(recovered, account, 'Recovered incorrect address') // Pass
})
});