diff options
author | kumavis <kumavis@users.noreply.github.com> | 2017-02-16 02:51:49 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-16 02:51:49 +0800 |
commit | f2539d125c2cfe240511f8505e222a9893bf7748 (patch) | |
tree | f58eaa54c0fd5818735ab6a282093bc8eb7bcd9d | |
parent | 07422ae8350c08a68ecf17b8ca2ec08f9ea1805f (diff) | |
parent | 0b0eec9c52667148fc69e1a3446bcd208dab3858 (diff) | |
download | tangerine-wallet-browser-f2539d125c2cfe240511f8505e222a9893bf7748.tar tangerine-wallet-browser-f2539d125c2cfe240511f8505e222a9893bf7748.tar.gz tangerine-wallet-browser-f2539d125c2cfe240511f8505e222a9893bf7748.tar.bz2 tangerine-wallet-browser-f2539d125c2cfe240511f8505e222a9893bf7748.tar.lz tangerine-wallet-browser-f2539d125c2cfe240511f8505e222a9893bf7748.tar.xz tangerine-wallet-browser-f2539d125c2cfe240511f8505e222a9893bf7748.tar.zst tangerine-wallet-browser-f2539d125c2cfe240511f8505e222a9893bf7748.zip |
Merge pull request #1113 from MetaMask/RecoverTests
Add tests showing recovering public key from signature
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | app/scripts/keyrings/hd.js | 4 | ||||
-rw-r--r-- | app/scripts/keyrings/simple.js | 3 | ||||
-rw-r--r-- | test/unit/keyrings/simple-test.js | 40 |
4 files changed, 47 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a29e3c25..00ab548ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Current Master +- Improve test coverage of eth.sign behavior, including a code example of verifying a signature. + ## 3.2.2 2017-2-8 - Revert eth.sign behavior to the previous one with a big warning. We will be gradually implementing the new behavior over the coming time. https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sign diff --git a/app/scripts/keyrings/hd.js b/app/scripts/keyrings/hd.js index 7b10f925a..3a66f7868 100644 --- a/app/scripts/keyrings/hd.js +++ b/app/scripts/keyrings/hd.js @@ -112,9 +112,11 @@ class HdKeyring extends EventEmitter { _getWalletForAccount (account) { + const targetAddress = sigUtil.normalize(account) return this.wallets.find((w) => { const address = w.getAddress().toString('hex') - return ((address === account) || (sigUtil.normalize(address) === account)) + return ((address === targetAddress) || + (sigUtil.normalize(address) === targetAddress)) }) } } diff --git a/app/scripts/keyrings/simple.js b/app/scripts/keyrings/simple.js index b6ffc606e..82881aa2d 100644 --- a/app/scripts/keyrings/simple.js +++ b/app/scripts/keyrings/simple.js @@ -88,7 +88,8 @@ class SimpleKeyring extends EventEmitter { /* PRIVATE METHODS */ _getWalletForAccount (account) { - let wallet = this.wallets.find(w => ethUtil.bufferToHex(w.getAddress()) === account) + const address = sigUtil.normalize(account) + let wallet = this.wallets.find(w => ethUtil.bufferToHex(w.getAddress()) === address) if (!wallet) throw new Error('Simple Keyring - Unable to find matching address.') return wallet } diff --git a/test/unit/keyrings/simple-test.js b/test/unit/keyrings/simple-test.js index 77eeb834c..ba7dd448a 100644 --- a/test/unit/keyrings/simple-test.js +++ b/test/unit/keyrings/simple-test.js @@ -1,5 +1,7 @@ const assert = require('assert') const extend = require('xtend') +const Web3 = require('web3') +const web3 = new Web3() const ethUtil = require('ethereumjs-util') const SimpleKeyring = require('../../../app/scripts/keyrings/simple') const TYPE_STR = 'Simple Key Pair' @@ -65,6 +67,44 @@ describe('simple-keyring', function() { done() }) }) + + it('reliably can decode messages it signs', function (done) { + + const message = 'hello there!' + const msgHashHex = web3.sha3(message) + let address + let addresses = [] + + keyring.deserialize([ privateKey ]) + .then(() => { + keyring.addAccounts(9) + }) + .then(() => { + return keyring.getAccounts() + }) + .then((addrs) => { + addresses = addrs + return Promise.all(addresses.map((address) => { + return keyring.signMessage(address, msgHashHex) + })) + }) + .then((signatures) => { + + signatures.forEach((sgn, index) => { + const address = addresses[index] + + var r = ethUtil.toBuffer(sgn.slice(0,66)) + var s = ethUtil.toBuffer('0x' + sgn.slice(66,130)) + var v = ethUtil.bufferToInt(ethUtil.toBuffer('0x' + sgn.slice(130,132))) + var m = ethUtil.toBuffer(msgHashHex) + var pub = ethUtil.ecrecover(m, v, r, s) + var adr = '0x' + ethUtil.pubToAddress(pub).toString('hex') + + assert.equal(adr, address, 'recovers address from signature correctly') + }) + done() + }) + }) }) describe('#addAccounts', function() { |