Promote getHash into a static method of the Wallet.

This commit is contained in:
Antoni Kedracki 2018-05-19 17:47:45 +02:00
parent 417065bad6
commit 3bf39b3bee
2 changed files with 14 additions and 4 deletions

@ -172,6 +172,7 @@ describe('Test Signing Messages', function() {
address: '0x14791697260E4c9A71f18484C9f997B308e59325',
name: 'string("hello world")',
message: 'hello world',
messageHash: '0xd9eba16ed0ecae432b71fe008c98cc872bb4cc214d3220a36f365326cf807d68',
privateKey: '0x0123456789012345678901234567890123456789012345678901234567890123',
signature: '0xddd0a7290af9526056b4e35a077b9a11b513aa0028ec6c9880948544508f3c63265e99e47ad31bb2cab9646c504576b3abc6939a1710afc08cbf3034d73214b81c'
},
@ -181,6 +182,7 @@ describe('Test Signing Messages', function() {
address: '0xD351c7c627ad5531Edb9587f4150CaF393c33E87',
name: 'bytes(0x47173285...4cb01fad)',
message: arrayify('0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad'),
messageHash: '0x93100cc9477ba6522a2d7d5e83d0e075b167224ed8aa0c5860cfd47fa9f22797',
privateKey: '0x51d1d6047622bca92272d36b297799ecc152dc2ef91b229debf84fc41e8c73ee',
signature: '0x546f0c996fa4cfbf2b68fd413bfb477f05e44e66545d7782d87d52305831cd055fc9943e513297d0f6755ad1590a5476bf7d1761d4f9dc07dfe473824bbdec751b'
},
@ -190,6 +192,7 @@ describe('Test Signing Messages', function() {
address: '0xe7deA7e64B62d1Ca52f1716f29cd27d4FE28e3e1',
name: 'zero-prefixed signature',
message: arrayify(id('0x7f23b5eed5bc7e89f267f339561b2697faab234a2')),
messageHash: '0x06c9d148d268f9a13d8f94f4ce351b0beff3b9ba69f23abbf171168202b2dd67',
privateKey: '0x09a11afa58d6014843fd2c5fd4e21e7fadf96ca2d8ce9934af6b8e204314f25c',
signature: '0x7222038446034a0425b6e3f0cc3594f0d979c656206408f937c37a8180bb1bea047d061e4ded4aeac77fa86eb02d42ba7250964ac3eb9da1337090258ce798491c'
}
@ -209,4 +212,11 @@ describe('Test Signing Messages', function() {
assert.equal(address, test.address, 'verifies message signature');
});
});
tests.forEach(function(test) {
it(('hashes a message "' + test.name + '"'), function() {
var hash = Wallet.hashMessage(test.message);
assert.equal(hash, test.messageHash, 'calculates message hash');
});
});
});

@ -312,18 +312,18 @@ utils.defineProperty(Wallet.prototype, 'send', function(addressOrName, amountWei
});
});
function getHash(message) {
utils.defineProperty(Wallet, 'hashMessage', function (message) {
var payload = utils.concat([
utils.toUtf8Bytes('\x19Ethereum Signed Message:\n'),
utils.toUtf8Bytes(String(message.length)),
((typeof(message) === 'string') ? utils.toUtf8Bytes(message): message)
]);
return utils.keccak256(payload);
}
});
utils.defineProperty(Wallet.prototype, 'signMessage', function(message) {
var signingKey = new SigningKey(this.privateKey);
var sig = signingKey.signDigest(getHash(message));
var sig = signingKey.signDigest(Wallet.hashMessage(message));
return (utils.hexZeroPad(sig.r, 32) + utils.hexZeroPad(sig.s, 32).substring(2) + (sig.recoveryParam ? '1c': '1b'));
});
@ -331,7 +331,7 @@ utils.defineProperty(Wallet.prototype, 'signMessage', function(message) {
utils.defineProperty(Wallet, 'verifyMessage', function(message, signature) {
signature = utils.hexlify(signature);
if (signature.length != 132) { throw new Error('invalid signature'); }
var digest = getHash(message);
var digest = Wallet.hashMessage(message);
var recoveryParam = parseInt(signature.substring(130), 16);
if (recoveryParam >= 27) { recoveryParam -= 27; }