From 41191a285fad812aa0305ef93e715011552e507c Mon Sep 17 00:00:00 2001 From: Esteban Ordano Date: Sat, 10 Jan 2015 13:33:52 -0300 Subject: [PATCH] Add alternative to create an Address * When by error one has two instances of an address, an address can't be instantiated from the other --- lib/address.js | 11 ++++++----- test/address.js | 11 +++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/address.js b/lib/address.js index 0f80a20..be9fc8a 100644 --- a/lib/address.js +++ b/lib/address.js @@ -143,12 +143,13 @@ Address._transformHash = function(hash){ * @return {Address} */ Address._transformObject = function(data) { - $.checkArgument(data.hash, 'Must provide a `hash` property'); + $.checkArgument(data.hash || data.hashBuffer, 'Must provide a `hash` or `hashBuffer` property'); $.checkArgument(data.type, 'Must provide a `type` property'); - data.hashBuffer = new Buffer(data.hash, 'hex'); - data.network = Networks.get(data.network) || Networks.defaultNetwork; - - return data; + return { + hashBuffer: data.hash ? new Buffer(data.hash, 'hex') : data.hashBuffer, + network: Networks.get(data.network) || Networks.defaultNetwork, + type: data.type + }; }; /** diff --git a/test/address.js b/test/address.js index 17218b8..85c7fc3 100644 --- a/test/address.js +++ b/test/address.js @@ -210,7 +210,18 @@ describe('Address', function() { should.not.exist(error); } }); + }); + describe('instantiation', function() { + it('can be instantiated from another address', function() { + var address = Address.fromBuffer(buf); + var address2 = new Address({ + hashBuffer: address.hashBuffer, + network: address.network, + type: address.type + }); + address.toString().should.equal(address2.toString()); + }); }); describe('encodings', function() {