diff --git a/lib/bcoin/hd.js b/lib/bcoin/hd.js index c4f3770f..d6395802 100644 --- a/lib/bcoin/hd.js +++ b/lib/bcoin/hd.js @@ -77,6 +77,8 @@ function HDSeed(options) { this.entropy = options.entropy; this.mnemonic = options.mnemonic; this.passphrase = options.passphrase || ''; + + assert(this.bits % 8 === 0); } HDSeed.prototype.createSeed = function createSeed(passphrase) { @@ -126,6 +128,8 @@ HD.fromSeed = function fromSeed(options) { return HDPrivateKey.fromSeed(options); }; +HD.cache = new bcoin.lru(500); + /** * HD Private Key */ @@ -719,7 +723,7 @@ HDPrivateKey.prototype.derive = function derive(index, hardened) { if (typeof index === 'string') return this.derivePath(index); - cached = cache.get(this.xprivkey, index); + cached = HD.cache.get(this.xprivkey + '/' + index); if (cached) return cached; @@ -766,7 +770,7 @@ HDPrivateKey.prototype.derive = function derive(index, hardened) { } }); - cache.set(this.xprivkey, index, child); + HD.cache.set(this.xprivkey + '/' + index, child); return child; }; @@ -1087,7 +1091,7 @@ HDPublicKey.prototype.derive = function derive(index, hardened) { if (typeof index === 'string') return this.derivePath(index); - cached = cache.get(this.xpubkey, index); + cached = HD.cache.get(this.xpubkey + '/' + index); if (cached) return cached; @@ -1128,7 +1132,7 @@ HDPublicKey.prototype.derive = function derive(index, hardened) { } }); - cache.set(this.xpubkey, index, child); + HD.cache.set(this.xpubkey + '/' + index, child); return child; }; @@ -1263,34 +1267,6 @@ function pbkdf2(key, salt, iterations, dkLen) { return DK; } -HD.cache = new bcoin.lru(500, function(key, value) { - return 1; -}); - -var cache = { - data: {}, - count: 0 -}; - -cache.set = function(key, index, value) { - key = key + '/' + index; - - if (this.count > 500) { - this.data = {}; - this.count = 0; - } - - if (this.data[key] === undefined) - this.count++; - - this.data[key] = value; -}; - -cache.get = function(key, index) { - key = key + '/' + index; - return this.data[key]; -}; - /** * Expose */