1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Merge pull request #1113 from MetaMask/RecoverTests

Add tests showing recovering public key from signature
This commit is contained in:
kumavis 2017-02-15 10:51:49 -08:00 committed by GitHub
commit f2539d125c
4 changed files with 47 additions and 2 deletions

View File

@ -2,6 +2,8 @@
## Current Master ## Current Master
- Improve test coverage of eth.sign behavior, including a code example of verifying a signature.
## 3.2.2 2017-2-8 ## 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 - 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

View File

@ -112,9 +112,11 @@ class HdKeyring extends EventEmitter {
_getWalletForAccount (account) { _getWalletForAccount (account) {
const targetAddress = sigUtil.normalize(account)
return this.wallets.find((w) => { return this.wallets.find((w) => {
const address = w.getAddress().toString('hex') const address = w.getAddress().toString('hex')
return ((address === account) || (sigUtil.normalize(address) === account)) return ((address === targetAddress) ||
(sigUtil.normalize(address) === targetAddress))
}) })
} }
} }

View File

@ -88,7 +88,8 @@ class SimpleKeyring extends EventEmitter {
/* PRIVATE METHODS */ /* PRIVATE METHODS */
_getWalletForAccount (account) { _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.') if (!wallet) throw new Error('Simple Keyring - Unable to find matching address.')
return wallet return wallet
} }

View File

@ -1,5 +1,7 @@
const assert = require('assert') const assert = require('assert')
const extend = require('xtend') const extend = require('xtend')
const Web3 = require('web3')
const web3 = new Web3()
const ethUtil = require('ethereumjs-util') const ethUtil = require('ethereumjs-util')
const SimpleKeyring = require('../../../app/scripts/keyrings/simple') const SimpleKeyring = require('../../../app/scripts/keyrings/simple')
const TYPE_STR = 'Simple Key Pair' const TYPE_STR = 'Simple Key Pair'
@ -65,6 +67,44 @@ describe('simple-keyring', function() {
done() 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() { describe('#addAccounts', function() {