Merge pull request #842 from eordano/feature/G7/hdprivkey

Adding G7 methods to HDPrivateKey
This commit is contained in:
Manuel Aráoz 2014-12-23 12:59:51 -03:00
commit 6ea9c7b005
2 changed files with 40 additions and 2 deletions

View File

@ -214,6 +214,10 @@ HDPrivateKey._validateNetwork = function(data, networkArg) {
return null;
};
HDPrivateKey.fromJSON = HDPrivateKey.fromObject = HDPrivateKey.fromString = function(arg) {
return new HDPrivateKey(arg);
};
HDPrivateKey.prototype._buildFromJSON = function(arg) {
return this._buildFromObject(JSON.parse(arg));
};

View File

@ -8,7 +8,7 @@ var bitcore = require('..');
var errors = bitcore.errors;
var hdErrors = errors.HDPrivateKey;
var buffer = require('buffer');
var bufferUtil = bitcore.util.buffer;
var BufferUtil = bitcore.util.buffer;
var HDPrivateKey = bitcore.HDPrivateKey;
var Base58Check = bitcore.encoding.Base58Check;
@ -168,7 +168,7 @@ describe('HDPrivate key interface', function() {
var error = null;
try {
var buffers = privKey._buffers;
buffers.checksum = bufferUtil.integerAsBuffer(0);
buffers.checksum = BufferUtil.integerAsBuffer(0);
var privateKey = new HDPrivateKey(buffers);
} catch (e) {
error = e;
@ -189,5 +189,39 @@ describe('HDPrivate key interface', function() {
var derivedByNumber = privateKey.derive(0, true).derive(1).derive(2, true);
derivedByNumber.xprivkey.should.equal(derivedByString.xprivkey);
});
describe('conversion to plain object/json', function() {
var plainObject = {
'network':'livenet',
'depth':0,
'fingerPrint':876747070,
'parentFingerPrint':0,
'childIndex':0,
'chainCode':'873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508',
'privateKey':'e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35',
'checksum':-411132559,
'xprivkey':'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi'
};
it('toObject leaves no Buffer instances', function() {
var privKey = new HDPrivateKey(xprivkey);
var object = privKey.toObject();
_.each(_.values(object), function(value) {
expect(BufferUtil.isBuffer(value)).to.equal(false);
});
});
it('roundtrips toJSON', function() {
expect(HDPrivateKey.fromJSON(new HDPrivateKey(xprivkey).toJSON()).xprivkey).to.equal(xprivkey);
});
it('roundtrips to JSON and to Object', function() {
var privkey = new HDPrivateKey(xprivkey);
expect(HDPrivateKey.fromJSON(privkey.toJSON()).xprivkey).to.equal(xprivkey);
});
it('recovers state from JSON', function() {
new HDPrivateKey(JSON.stringify(plainObject)).xprivkey.should.equal(xprivkey);
});
it('recovers state from Object', function() {
new HDPrivateKey(plainObject).xprivkey.should.equal(xprivkey);
});
});
});