HDWallet: remove HmacBytesToBytes

This commit is contained in:
Daniel Cousens 2014-05-10 09:41:00 +10:00
parent da36003d09
commit 25514d6ac1
2 changed files with 11 additions and 20 deletions

View file

@ -13,9 +13,14 @@ var networks = require('./networks')
var sec = require('./sec') var sec = require('./sec')
var ecparams = sec("secp256k1") var ecparams = sec("secp256k1")
function HmacSHA512(buffer, secret) { function HmacSHA512(data, secret) {
var words = convert.bytesToWordArray(buffer) assert(Buffer.isBuffer(data))
var hash = CJS.HmacSHA512(words, secret) assert(Buffer.isBuffer(secret))
var dataWords = convert.bytesToWordArray(data)
var secretWords = convert.bytesToWordArray(secret)
var hash = CJS.HmacSHA512(dataWords, secretWords)
return new Buffer(convert.wordArrayToBytes(hash)) return new Buffer(convert.wordArrayToBytes(hash))
} }
@ -23,7 +28,7 @@ function HmacSHA512(buffer, secret) {
function HDWallet(seed, networkString) { function HDWallet(seed, networkString) {
if (seed == undefined) return; // FIXME: Boo, should be stricter if (seed == undefined) return; // FIXME: Boo, should be stricter
var I = HmacSHA512(seed, 'Bitcoin seed') var I = HmacSHA512(seed, new Buffer('Bitcoin seed'))
this.chaincode = I.slice(32) this.chaincode = I.slice(32)
this.network = networkString || 'bitcoin' this.network = networkString || 'bitcoin'
@ -185,7 +190,6 @@ HDWallet.prototype.derive = function(i) {
var cPar = this.chaincode var cPar = this.chaincode
var usePriv = i >= HDWallet.HIGHEST_BIT var usePriv = i >= HDWallet.HIGHEST_BIT
var I
if (usePriv) { if (usePriv) {
assert(this.priv, 'Missing private key') assert(this.priv, 'Missing private key')
@ -193,21 +197,14 @@ HDWallet.prototype.derive = function(i) {
// let I = HMAC-SHA512(Key = cpar, Data = 0x00 || kpar || i) [Note:] // let I = HMAC-SHA512(Key = cpar, Data = 0x00 || kpar || i) [Note:]
var kPar = this.priv.toBuffer().slice(0, 32) var kPar = this.priv.toBuffer().slice(0, 32)
iBuffer = Buffer.concat([new Buffer([0]), kPar, iBuffer], 37) iBuffer = Buffer.concat([new Buffer([0]), kPar, iBuffer], 37)
// FIXME: Dislikes buffers
I = HmacFromBytesToBytes(CJS.algo.SHA512, Array.prototype.slice.call(iBuffer), cPar)
} else { } else {
// If 0, public derivation is used: // If 0, public derivation is used:
// let I = HMAC-SHA512(Key = cpar, Data = χ(kpar*G) || i) // let I = HMAC-SHA512(Key = cpar, Data = χ(kpar*G) || i)
var KPar = this.pub.toBuffer() var KPar = this.pub.toBuffer()
iBuffer = Buffer.concat([KPar, iBuffer]) iBuffer = Buffer.concat([KPar, iBuffer])
// FIXME: Dislikes buffers
I = HmacFromBytesToBytes(CJS.algo.SHA512, Array.prototype.slice.call(iBuffer), cPar)
} }
// FIXME: Boo, CSJ.algo.SHA512 uses byte arrays var I = HmacSHA512(iBuffer, cPar)
I = new Buffer(I)
// Split I = IL || IR into two 32-byte sequences, IL and IR. // Split I = IL || IR into two 32-byte sequences, IL and IR.
var ILb = I.slice(0, 32) var ILb = I.slice(0, 32)
@ -250,10 +247,4 @@ HDWallet.prototype.getKeyVersion = function() {
HDWallet.prototype.toString = HDWallet.prototype.toBase58 HDWallet.prototype.toString = HDWallet.prototype.toBase58
function HmacFromBytesToBytes(hasher, message, key) {
var hmac = CJS.algo.HMAC.create(hasher, convert.bytesToWordArray(key))
hmac.update(convert.bytesToWordArray(message))
return convert.wordArrayToBytes(hmac.finalize())
}
module.exports = HDWallet module.exports = HDWallet

View file

@ -27,7 +27,7 @@ function Wallet(seed, options) {
// Make a new master key // Make a new master key
this.newMasterKey = function(seed, network) { this.newMasterKey = function(seed, network) {
if (!seed) seed = rng(32, { array: true }); seed = seed || new Buffer(rng(32))
masterkey = new HDNode(seed, network) masterkey = new HDNode(seed, network)
// HD first-level child derivation method should be private // HD first-level child derivation method should be private