From c41419b6ef6d33db27a04563197afedd86410735 Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Wed, 17 Sep 2014 14:49:17 -0700 Subject: [PATCH] use buffer functions ...instead of adhoc uint functions --- lib/bip32.js | 33 +++------------------------------ test/bip32.js | 1 - 2 files changed, 3 insertions(+), 31 deletions(-) diff --git a/lib/bip32.js b/lib/bip32.js index 1444071..3264e76 100644 --- a/lib/bip32.js +++ b/lib/bip32.js @@ -68,10 +68,10 @@ BIP32.prototype.initFromBytes = function(bytes) { if (bytes.length != 78) throw new Error('not enough data'); - this.version = u32(bytes.slice(0, 4)); - this.depth = u8(bytes.slice(4, 5)); + this.version = bytes.slice(0, 4).readUInt32BE(0); + this.depth = bytes.slice(4, 5).readUInt8(0); this.parentFingerprint = bytes.slice(5, 9); - this.childIndex = u32(bytes.slice(9, 13)); + this.childIndex = bytes.slice(9, 13).readUInt32BE(0); this.chainCode = bytes.slice(13, 45); var keyBytes = bytes.slice(45, 78); @@ -303,31 +303,4 @@ BIP32.prototype.toString = function() { return this.extendedPublicKeyString(); }; -function uint(f, size) { - if (f.length < size) - throw new Error('not enough data'); - var n = 0; - for (var i = 0; i < size; i++) { - n *= 256; - n += f[i]; - } - return n; -} - -function u8(f) { - return uint(f, 1); -} - -function u16(f) { - return uint(f, 2); -} - -function u32(f) { - return uint(f, 4); -} - -function u64(f) { - return uint(f, 8); -} - module.exports = BIP32; diff --git a/test/bip32.js b/test/bip32.js index 82643b7..96f0d33 100644 --- a/test/bip32.js +++ b/test/bip32.js @@ -324,7 +324,6 @@ describe('BIP32', function() { tip32b.toString().slice(0, 4).should.equal('tpub'); }); - }); });