Browse Source

HDKeys: add toBuffer/fromBuffer to HDPublicKey

patch-2
Esteban Ordano 10 years ago
parent
commit
7a73f582b4
  1. 19
      lib/hdpublickey.js
  2. 2
      test/hdprivatekey.js
  3. 11
      test/hdpublickey.js

19
lib/hdpublickey.js

@ -417,6 +417,25 @@ HDPublicKey.prototype.toJSON = function toJSON() {
return JSON.stringify(this.toObject());
};
/**
* Create a HDPublicKey from a buffer argument
*
* @param {Buffer} arg
* @return {HDPublicKey}
*/
HDPublicKey.fromBuffer = function(arg) {
return new HDPublicKey(arg);
};
/**
* Return a buffer representation of the xpubkey
*
* @return {Buffer}
*/
HDPublicKey.prototype.toBuffer = function() {
return this._buffers.xpubkey;
};
HDPublicKey.Hardened = 0x80000000;
HDPublicKey.RootElementAlias = ['m', 'M'];

2
test/hdprivatekey.js

@ -262,7 +262,7 @@ describe('HDPrivate key interface', function() {
});
});
describe.only('conversion to/from buffer', function() {
describe('conversion to/from buffer', function() {
var str = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi';
it('should roundtrip to/from a buffer', function() {
var priv = new HDPrivateKey(str);

11
test/hdpublickey.js

@ -153,6 +153,17 @@ describe('HDPublicKey interface', function() {
pubKey.inspect().should.equal('<HDPublicKey: ' + pubKey.xpubkey + '>');
});
describe('conversion to/from buffer', function() {
it('should roundtrip to an equivalent object', function() {
var pubKey = new HDPublicKey(xpubkey);
var toBuffer = pubKey.toBuffer();
var fromBuffer = HDPublicKey.fromBuffer(toBuffer);
var roundTrip = new HDPublicKey(fromBuffer.toBuffer());
roundTrip.xpubkey.should.equal(xpubkey);
});
});
describe('conversion to different formats', function() {
var plainObject = {
'network':'livenet',

Loading…
Cancel
Save