got hash test passing

feature/default_network_editable
Dan Finlay 8 years ago
parent b862d94209
commit f2486fbdd3
  1. 4
      app/scripts/keyrings/hd.js
  2. 3
      app/scripts/keyrings/simple.js
  3. 37
      test/unit/keyrings/simple-test.js

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

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

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

Loading…
Cancel
Save