HDKeys: add toBuffer/fromBuffer to HDPublicKey

This commit is contained in:
Esteban Ordano 2015-01-27 12:24:00 -03:00
parent 0ff4c9a922
commit 7a73f582b4
3 changed files with 31 additions and 1 deletions

View File

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

View File

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

View File

@ -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',