1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 18:00:18 +01:00

Fixed signing of hashes

Signing now always takes a 64 digit hex string, and returns a message signature which appropriately pads r, s, and v with zeroes.

Need to verify with Denis that this is the behavior he requires.
This commit is contained in:
Dan Finlay 2016-06-16 11:46:35 -07:00
parent 6b0a99a09a
commit 408addb1b2
2 changed files with 16 additions and 17 deletions

View File

@ -41,14 +41,12 @@ function IdManagement(opts) {
this.signMsg = function (address, message) { this.signMsg = function (address, message) {
// sign message // sign message
var privKeyHex = this.exportPrivateKey(address) var privKeyHex = this.exportPrivateKey(address);
var privKey = ethUtil.toBuffer(privKeyHex) var privKey = ethUtil.toBuffer(privKeyHex);
var msgHash = ethUtil.sha3(message) var msgSig = ethUtil.ecsign(new Buffer(message.replace('0x',''), 'hex'), privKey);
var msgBuffer = new Buffer(message.replace('0x',''), 'hex') var rawMsgSig = ethUtil.bufferToHex(concatSig(msgSig.v, msgSig.r, msgSig.s));
var msgSig = ethUtil.ecsign(msgBuffer, privKey) return rawMsgSig;
var rawMsgSig = ethUtil.bufferToHex(concatSig(msgSig.v, msgSig.r, msgSig.s)) };
return rawMsgSig
}
this.getSeed = function(){ this.getSeed = function(){
return this.keyStore.getSeed(this.derivedKey) return this.keyStore.getSeed(this.derivedKey)
@ -71,9 +69,8 @@ function pad_with_zeroes(number, length){
function concatSig(v, r, s) { function concatSig(v, r, s) {
r = pad_with_zeroes(ethUtil.fromSigned(r), 64) r = pad_with_zeroes(ethUtil.fromSigned(r), 64)
s = pad_with_zeroes(ethUtil.fromSigned(s), 64) s = pad_with_zeroes(ethUtil.fromSigned(s), 64)
v = ethUtil.bufferToInt(v) r = ethUtil.stripHexPrefix(r.toString('hex'))
r = ethUtil.toUnsigned(r).toString('hex') s = ethUtil.stripHexPrefix(s.toString('hex'))
s = ethUtil.toUnsigned(s).toString('hex')
v = ethUtil.stripHexPrefix(ethUtil.intToHex(v)) v = ethUtil.stripHexPrefix(ethUtil.intToHex(v))
return ethUtil.addHexPrefix(r.concat(s, v).toString("hex")) return ethUtil.addHexPrefix(r.concat(s, v))
} }

View File

@ -17,15 +17,17 @@ describe('IdManagement', function() {
describe('#signMsg', function () { describe('#signMsg', function () {
const address = '0x926cD0393816429a580037475ec23eD65fDC893B' const address = '0x926cD0393816429a580037475ec23eD65fDC893B'
const message = '0x0987654321abcdef' const message = '0x96b8d442f4c09a08d266bf37b18219465cfb341c1b3ab9792a6103a93583fdf7'
const privateKey = '0xd291f7aa01b94941b446f260bca42c0752762571428ad4ed6239613c66365cf4' const privateKey = '0xd291f7aa01b94941b446f260bca42c0752762571428ad4ed6239613c66365cf4'
const expectedResult = 'foo' const expectedResult = '0x04881196121781472543750166203264808665659193717384627772472141185319786561270240926993050673320157359365329096037150419976876479876332927284781689204045461c'
const idManagement = new IdManagement() const idManagement = new IdManagement()
const exportKeyStub = sinon.stub(idManagement, 'exportPrivateKey', () => privateKey) const exportKeyStub = sinon.stub(idManagement, 'exportPrivateKey', (addr) => {
assert.equal(addr, address)
return privateKey
})
const result = idManagement.signMsg(address, message) const result = idManagement.signMsg(address, message)
console.log(result)
assert.equal(result, expectedResult) assert.equal(result, expectedResult)
}) })
}) })