diff --git a/lib/hdprivatekey.js b/lib/hdprivatekey.js index 7a2ca00..262f37d 100644 --- a/lib/hdprivatekey.js +++ b/lib/hdprivatekey.js @@ -52,6 +52,8 @@ function HDPrivateKey(arg) { this._buildFromSerialized(arg); } else if (JSUtil.isValidJSON(arg)) { this._buildFromJSON(arg); + } else if (BufferUtil.isBuffer(arg) && HDPrivateKey.isValidSerialized(arg.toString())) { + this._buildFromSerialized(arg.toString()); } else { throw HDPrivateKey.getSerializedError(arg); } @@ -377,12 +379,8 @@ HDPrivateKey.prototype._buildFromBuffers = function(arg) { } var xprivkey; - - if (!arg.xprivkey) { - xprivkey = Base58Check.encode(buffer.Buffer.concat(sequence)); - } else { - xprivkey = arg.xprivkey; - } + xprivkey = Base58Check.encode(buffer.Buffer.concat(sequence)); + arg.xprivkey = new Buffer(xprivkey); var privateKey = new PrivateKey(BN.fromBuffer(arg.privateKey)); var publicKey = privateKey.toPublicKey(); @@ -496,7 +494,7 @@ HDPrivateKey.prototype.toJSON = function toJSON() { * @return {HDPrivateKey} */ HDPrivateKey.fromBuffer = function(arg) { - return new HDPrivateKey(arg); + return new HDPrivateKey(arg.toString()); }; /** @@ -505,7 +503,7 @@ HDPrivateKey.fromBuffer = function(arg) { * @return {string} */ HDPrivateKey.prototype.toBuffer = function() { - return this._buffers.xprivkey; + return BufferUtil.copy(this._buffers.xprivkey); }; HDPrivateKey.DefaultDepth = 0; diff --git a/lib/hdpublickey.js b/lib/hdpublickey.js index 7126d50..7b38fdd 100644 --- a/lib/hdpublickey.js +++ b/lib/hdpublickey.js @@ -43,6 +43,8 @@ function HDPublicKey(arg) { return this._buildFromSerialized(arg); } else if (JSUtil.isValidJSON(arg)) { return this._buildFromJSON(arg); + } else if (BufferUtil.isBuffer(arg) && !HDPublicKey.getSerializedError(arg.toString())) { + return this._buildFromSerialized(arg.toString()); } else { if (error instanceof hdErrors.ArgumentIsPrivateExtended) { return new HDPrivateKey(arg).hdPublicKey; @@ -316,12 +318,8 @@ HDPublicKey.prototype._buildFromBuffers = function (arg) { } var xpubkey; - - if (!arg.xpubkey) { - xpubkey = Base58Check.encode(BufferUtil.concat(sequence)); - } else { - xpubkey = arg.xpubkey; - } + xpubkey = Base58Check.encode(BufferUtil.concat(sequence)); + arg.xpubkey = new Buffer(xpubkey); var publicKey = PublicKey.fromString(arg.publicKey); var size = HDPublicKey.ParentFingerPrintSize; @@ -433,7 +431,7 @@ HDPublicKey.fromBuffer = function(arg) { * @return {Buffer} */ HDPublicKey.prototype.toBuffer = function() { - return this._buffers.xpubkey; + return BufferUtil.copy(this._buffers.xpubkey); }; HDPublicKey.Hardened = 0x80000000; diff --git a/lib/util/buffer.js b/lib/util/buffer.js index f750b5e..332b4be 100644 --- a/lib/util/buffer.js +++ b/lib/util/buffer.js @@ -37,6 +37,18 @@ module.exports = { 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 * both node's Buffer and Uint8Array