Skip to content

Commit

Permalink
Add binary message support for wallet.signMessage (#80).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Dec 2, 2017
1 parent ca24905 commit fa4db2c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
16 changes: 14 additions & 2 deletions tests/test-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,26 +153,38 @@ describe('Test Transaction Signing and Parsing', function() {
describe('Test Signing Messages', function() {
var Wallet = require('../wallet/wallet');

var arrayify = require('../utils/convert').arrayify;

var tests = [
// See: https://etherscan.io/verifySig/57
{
address: '0x14791697260E4c9A71f18484C9f997B308e59325',
name: 'string("hello world")',
message: 'hello world',
privateKey: '0x0123456789012345678901234567890123456789012345678901234567890123',
signature: '0xddd0a7290af9526056b4e35a077b9a11b513aa0028ec6c9880948544508f3c63265e99e47ad31bb2cab9646c504576b3abc6939a1710afc08cbf3034d73214b81c'
},

// See: https://github.com/ethers-io/ethers.js/issues/80
{
address: '0xD351c7c627ad5531Edb9587f4150CaF393c33E87',
name: 'bytes(0x47173285...4cb01fad)',
message: arrayify('0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad'),
privateKey: '0x51d1d6047622bca92272d36b297799ecc152dc2ef91b229debf84fc41e8c73ee',
signature: '0x546f0c996fa4cfbf2b68fd413bfb477f05e44e66545d7782d87d52305831cd055fc9943e513297d0f6755ad1590a5476bf7d1761d4f9dc07dfe473824bbdec751b'
}
];

tests.forEach(function(test) {
it(('signs a message "' + test.message + '"'), function() {
it(('signs a message "' + test.name + '"'), function() {
var wallet = new Wallet(test.privateKey);
var signature = wallet.signMessage(test.message);
assert.equal(signature, test.signature, 'computes message signature');
});
});

tests.forEach(function(test) {
it(('verifies a message "' + test.message + '"'), function() {
it(('verifies a message "' + test.name + '"'), function() {
var address = Wallet.verifyMessage(test.message, test.signature);
assert.equal(address, test.address, 'verifies message signature');
});
Expand Down
2 changes: 1 addition & 1 deletion wallet/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ethers-wallet",
"version": "2.1.4",
"version": "2.1.5",
"description": "Wallet and signing library for Ethereum.",
"bugs": {
"url": "http://github.com/ethers-io/ethers.js/issues",
Expand Down
2 changes: 1 addition & 1 deletion wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ function getHash(message) {
var payload = utils.concat([
utils.toUtf8Bytes('\x19Ethereum Signed Message:\n'),
utils.toUtf8Bytes(String(message.length)),
utils.toUtf8Bytes(message)
((typeof(message) === 'string') ? utils.toUtf8Bytes(message): message)
]);
return utils.keccak256(payload);
}
Expand Down

0 comments on commit fa4db2c

Please sign in to comment.