use buffer functions

...instead of adhoc uint functions
This commit is contained in:
Ryan X. Charles 2014-09-17 14:49:17 -07:00
parent 5da964739d
commit c41419b6ef
2 changed files with 3 additions and 31 deletions

View File

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

View File

@ -324,7 +324,6 @@ describe('BIP32', function() {
tip32b.toString().slice(0, 4).should.equal('tpub');
});
});
});