Merge pull request #1005 from eordano/g7/hdkeys
HDKeys: add toBuffer/fromBuffer
This commit is contained in:
commit
ebcf3ae991
@ -52,6 +52,8 @@ function HDPrivateKey(arg) {
|
|||||||
this._buildFromSerialized(arg);
|
this._buildFromSerialized(arg);
|
||||||
} else if (JSUtil.isValidJSON(arg)) {
|
} else if (JSUtil.isValidJSON(arg)) {
|
||||||
this._buildFromJSON(arg);
|
this._buildFromJSON(arg);
|
||||||
|
} else if (BufferUtil.isBuffer(arg) && HDPrivateKey.isValidSerialized(arg.toString())) {
|
||||||
|
this._buildFromSerialized(arg.toString());
|
||||||
} else {
|
} else {
|
||||||
throw HDPrivateKey.getSerializedError(arg);
|
throw HDPrivateKey.getSerializedError(arg);
|
||||||
}
|
}
|
||||||
@ -377,12 +379,8 @@ HDPrivateKey.prototype._buildFromBuffers = function(arg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var xprivkey;
|
var xprivkey;
|
||||||
|
xprivkey = Base58Check.encode(buffer.Buffer.concat(sequence));
|
||||||
if (!arg.xprivkey) {
|
arg.xprivkey = new Buffer(xprivkey);
|
||||||
xprivkey = Base58Check.encode(buffer.Buffer.concat(sequence));
|
|
||||||
} else {
|
|
||||||
xprivkey = arg.xprivkey;
|
|
||||||
}
|
|
||||||
|
|
||||||
var privateKey = new PrivateKey(BN.fromBuffer(arg.privateKey));
|
var privateKey = new PrivateKey(BN.fromBuffer(arg.privateKey));
|
||||||
var publicKey = privateKey.toPublicKey();
|
var publicKey = privateKey.toPublicKey();
|
||||||
@ -480,10 +478,34 @@ HDPrivateKey.prototype.toObject = function toObject() {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a JSON representation of the HDPrivateKey
|
||||||
|
*
|
||||||
|
* @return {string}
|
||||||
|
*/
|
||||||
HDPrivateKey.prototype.toJSON = function toJSON() {
|
HDPrivateKey.prototype.toJSON = function toJSON() {
|
||||||
return JSON.stringify(this.toObject());
|
return JSON.stringify(this.toObject());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build a HDPrivateKey from a buffer
|
||||||
|
*
|
||||||
|
* @param {Buffer} arg
|
||||||
|
* @return {HDPrivateKey}
|
||||||
|
*/
|
||||||
|
HDPrivateKey.fromBuffer = function(arg) {
|
||||||
|
return new HDPrivateKey(arg.toString());
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a buffer representation of the HDPrivateKey
|
||||||
|
*
|
||||||
|
* @return {string}
|
||||||
|
*/
|
||||||
|
HDPrivateKey.prototype.toBuffer = function() {
|
||||||
|
return BufferUtil.copy(this._buffers.xprivkey);
|
||||||
|
};
|
||||||
|
|
||||||
HDPrivateKey.DefaultDepth = 0;
|
HDPrivateKey.DefaultDepth = 0;
|
||||||
HDPrivateKey.DefaultFingerprint = 0;
|
HDPrivateKey.DefaultFingerprint = 0;
|
||||||
HDPrivateKey.DefaultChildIndex = 0;
|
HDPrivateKey.DefaultChildIndex = 0;
|
||||||
|
|||||||
@ -43,6 +43,8 @@ function HDPublicKey(arg) {
|
|||||||
return this._buildFromSerialized(arg);
|
return this._buildFromSerialized(arg);
|
||||||
} else if (JSUtil.isValidJSON(arg)) {
|
} else if (JSUtil.isValidJSON(arg)) {
|
||||||
return this._buildFromJSON(arg);
|
return this._buildFromJSON(arg);
|
||||||
|
} else if (BufferUtil.isBuffer(arg) && !HDPublicKey.getSerializedError(arg.toString())) {
|
||||||
|
return this._buildFromSerialized(arg.toString());
|
||||||
} else {
|
} else {
|
||||||
if (error instanceof hdErrors.ArgumentIsPrivateExtended) {
|
if (error instanceof hdErrors.ArgumentIsPrivateExtended) {
|
||||||
return new HDPrivateKey(arg).hdPublicKey;
|
return new HDPrivateKey(arg).hdPublicKey;
|
||||||
@ -316,12 +318,8 @@ HDPublicKey.prototype._buildFromBuffers = function (arg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var xpubkey;
|
var xpubkey;
|
||||||
|
xpubkey = Base58Check.encode(BufferUtil.concat(sequence));
|
||||||
if (!arg.xpubkey) {
|
arg.xpubkey = new Buffer(xpubkey);
|
||||||
xpubkey = Base58Check.encode(BufferUtil.concat(sequence));
|
|
||||||
} else {
|
|
||||||
xpubkey = arg.xpubkey;
|
|
||||||
}
|
|
||||||
|
|
||||||
var publicKey = PublicKey.fromString(arg.publicKey);
|
var publicKey = PublicKey.fromString(arg.publicKey);
|
||||||
var size = HDPublicKey.ParentFingerPrintSize;
|
var size = HDPublicKey.ParentFingerPrintSize;
|
||||||
@ -417,6 +415,25 @@ HDPublicKey.prototype.toJSON = function toJSON() {
|
|||||||
return JSON.stringify(this.toObject());
|
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 BufferUtil.copy(this._buffers.xpubkey);
|
||||||
|
};
|
||||||
|
|
||||||
HDPublicKey.Hardened = 0x80000000;
|
HDPublicKey.Hardened = 0x80000000;
|
||||||
HDPublicKey.RootElementAlias = ['m', 'M'];
|
HDPublicKey.RootElementAlias = ['m', 'M'];
|
||||||
|
|
||||||
|
|||||||
@ -37,6 +37,18 @@ module.exports = {
|
|||||||
return buffer;
|
return buffer;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a copy of a buffer
|
||||||
|
*
|
||||||
|
* @param {Buffer} original
|
||||||
|
* @return {Buffer}
|
||||||
|
*/
|
||||||
|
copy: function(original) {
|
||||||
|
var buffer = new Buffer(original.length);
|
||||||
|
original.copy(buffer);
|
||||||
|
return buffer;
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the given argument is an instance of a buffer. Tests for
|
* Returns true if the given argument is an instance of a buffer. Tests for
|
||||||
* both node's Buffer and Uint8Array
|
* both node's Buffer and Uint8Array
|
||||||
|
|||||||
@ -262,6 +262,17 @@ describe('HDPrivate key interface', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('conversion to/from buffer', function() {
|
||||||
|
var str = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi';
|
||||||
|
it('should roundtrip to/from a buffer', function() {
|
||||||
|
var priv = new HDPrivateKey(str);
|
||||||
|
var toBuffer = priv.toBuffer();
|
||||||
|
var fromBuffer = HDPrivateKey.fromBuffer(toBuffer);
|
||||||
|
var roundTrip = new HDPrivateKey(fromBuffer.toBuffer());
|
||||||
|
roundTrip.xprivkey.should.equal(str);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('conversion to plain object/json', function() {
|
describe('conversion to plain object/json', function() {
|
||||||
var plainObject = {
|
var plainObject = {
|
||||||
'network':'livenet',
|
'network':'livenet',
|
||||||
|
|||||||
@ -153,6 +153,17 @@ describe('HDPublicKey interface', function() {
|
|||||||
pubKey.inspect().should.equal('<HDPublicKey: ' + pubKey.xpubkey + '>');
|
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() {
|
describe('conversion to different formats', function() {
|
||||||
var plainObject = {
|
var plainObject = {
|
||||||
'network':'livenet',
|
'network':'livenet',
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user