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

got hash test passing

This commit is contained in:
Dan Finlay 2017-02-13 21:25:02 -08:00
parent b862d94209
commit f2486fbdd3
3 changed files with 28 additions and 16 deletions

View File

@ -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))
})
}
}

View File

@ -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
}

View File

@ -71,31 +71,40 @@ describe('simple-keyring', function() {
it('reliably can decode messages it signs', function (done) {
const message = 'hello there!'
let address, msgHex
let address, msgHashHex
keyring.addAccounts(10)
keyring.deserialize([ privateKey ])
.then(() => {
return keyring.getAccounts()
})
.then((addresses) => {
address = addresses[0]
msgHex = web3.sha3(message)
return keyring.signMessage(address, msgHex)
msgHashHex = web3.sha3(message)
return keyring.signMessage(address, msgHashHex)
})
.then((signature) => {
var sgn = signature
.then((sgn) => {
var r = ethUtil.toBuffer(sgn.slice(0,66))
var s = ethUtil.toBuffer('0x' + sgn.slice(66,130))
var v = parseInt(sgn.slice(130,132)) + 27
var msgBuffer = ethUtil.toBuffer(msgHex)
var msgHash = ethUtil.hashPersonalMessage(msgBuffer)
var pub = ethUtil.ecrecover(msgHash, v, r, s)
var v = ethUtil.bufferToInt(ethUtil.toBuffer('0x' + sgn.slice(130,132)))
var m = ethUtil.toBuffer(msgHashHex)
console.dir({ r, s, v, m })
var pub = ethUtil.ecrecover(m, v, r, s)
var adr = '0x' + ethUtil.pubToAddress(pub).toString('hex')
assert.equal(adr, address)
/*
var sgn = ethUtil.stripHexPrefix(signature)
var r = ethUtil.toBuffer(sgn.slice(0,64))
var s = ethUtil.toBuffer(sgn.slice(64,128))
var v = parseInt(sgn.slice(128,130)) + 27
var msgHashBuffer = ethUtil.toBuffer(msgHashHex)
var pub = ethUtil.ecrecover(msgHashBuffer, v, r, s)
var adr = '0x' + ethUtil.pubToAddress(pub).toString('hex')
*/
assert.equal(adr, address, 'recovers address from signature correctly')
done()
})
.catch((reason) => {
console.log('failed for reason:')
console.dir(reason)
assert.equal(true, false, reason)
done()
})
})
})