add set function to bip32

This commit is contained in:
Ryan X. Charles 2014-09-17 15:11:16 -07:00
parent 0641184e84
commit 7390b15f89
2 changed files with 35 additions and 0 deletions

View File

@ -14,9 +14,25 @@ var BIP32 = function BIP32(obj) {
if (typeof obj === 'string') {
var str = obj;
this.fromString(str);
} else if (obj ) {
this.set(obj);
}
}
BIP32.prototype.set = function(obj) {
this.version = typeof obj.version !== 'undefined' ? obj.version : this.version;
this.depth = typeof obj.depth !== 'undefined' ? obj.depth : this.depth;
this.parentFingerprint = obj.parentFingerprint || this.parentFingerprint;
this.childIndex = obj.childIndex || this.childIndex;
this.chainCode = obj.chainCode || this.chainCode;
this.key = obj.key || this.key;
this.hasPrivateKey = typeof obj.hasPrivateKey !== 'undefined' ? obj.hasPrivateKey : this.hasPrivateKey;
this.pubKeyHash = obj.pubKeyHash || this.pubKeyHash;
this.extendedPublicKey = obj.extendedPublicKey || this.extendedPublicKey;
this.extendedPrivateKey = obj.extendedPrivateKey || this.extendedPrivateKey;
return this;
};
BIP32.prototype.fromRandom = function(networkstr) {
if (!networkstr)
networkstr = 'mainnet';

View File

@ -40,6 +40,7 @@ describe('BIP32', function() {
should.exist(bip32);
new BIP32(vector1_m_private).toString().should.equal(vector1_m_private);
BIP32(vector1_m_private).toString().should.equal(vector1_m_private);
BIP32(BIP32(vector1_m_private)).toString().should.equal(vector1_m_private);
});
it('should initialize test vector 1 from the extended public key', function() {
@ -281,6 +282,24 @@ describe('BIP32', function() {
});
});
describe('#set', function() {
var bip32 = BIP32(vector1_m_private);
var bip322 = BIP32().set({
version: bip32.version,
depth: bip32.depth,
parentFingerprint: bip32.parentFingerprint,
childIndex: bip32.childIndex,
chainCode: bip32.chainCode,
key: bip32.key,
hasPrivateKey: bip32.hasPrivateKey,
pubKeyHash: bip32.pubKeyhash,
extendedPublicKey: bip32.extendedPublicKey,
extendedPrivateKey: bip32.extendedPrivateKey
});
bip322.toString().should.equal(bip32.toString());
bip322.set({}).toString().should.equal(bip32.toString());
});
describe('#seed', function() {
it('should initialize a new BIP32 correctly from test vector 1 seed', function() {