diff --git a/lib/transaction/transaction.js b/lib/transaction/transaction.js index a76f396a2..38568fe01 100644 --- a/lib/transaction/transaction.js +++ b/lib/transaction/transaction.js @@ -49,6 +49,8 @@ function Transaction(serialized) { return Transaction.shallowCopy(serialized); } else if (util.isHexa(serialized)) { this.fromString(serialized); + } else if (util.isValidJSON(serialized)) { + this.fromJSON(serialized); } else if (bufferUtil.isBuffer(serialized)) { this.fromBuffer(serialized); } else if (_.isObject(serialized)) { @@ -221,7 +223,7 @@ Transaction.prototype.toObject = function toObject() { outputs.push(output.toObject()); }); return { - change: this._change ? this._change.toObject() : undefined, + change: this._change ? this._change.toString() : undefined, version: this.version, inputs: inputs, outputs: outputs, diff --git a/test/address.js b/test/address.js index 82d81898a..17218b8c1 100644 --- a/test/address.js +++ b/test/address.js @@ -456,6 +456,16 @@ describe('Address', function() { }); }); + it('throws an error if it couldn\'t instantiate', function() { + expect(function() { + return new Address(1); + }).to.throw(TypeError); + }); + it('can roundtrip from/to a object', function() { + var address = new Address(P2SHLivenet[0]); + expect(new Address(address.toObject()).toString()).to.equal(P2SHLivenet[0]); + }); + describe('creating a P2SH address from public keys', function() { var public1 = '02da5798ed0c055e31339eb9b5cef0d3c0ccdec84a62e2e255eb5c006d4f3e7f5b'; diff --git a/test/transaction/transaction.js b/test/transaction/transaction.js index 2a36acf3f..9a4ad0ed2 100644 --- a/test/transaction/transaction.js +++ b/test/transaction/transaction.js @@ -243,6 +243,18 @@ describe('Transaction', function() { }).to.throw(errors.Transaction.FeeError); }); }); + + describe('to and from JSON', function() { + it('takes a string that is a valid JSON and deserializes from it', function() { + var transaction = new Transaction(); + expect(new Transaction(transaction.toJSON()).serialize()).to.equal(transaction.serialize()); + }); + it('serializes the `change` information', function() { + var transaction = new Transaction(); + transaction.change(changeAddress); + expect(JSON.parse(transaction.toJSON()).change).to.equal(changeAddress.toString()); + }); + }); }); var tx_empty_hex = '01000000000000000000';