From d99d1c9cc240acb3a6c5be1750225d90ee1b2e2b Mon Sep 17 00:00:00 2001 From: Esteban Ordano Date: Thu, 8 Jan 2015 17:41:15 -0300 Subject: [PATCH] Add tests for serialization roundtrip of inputs --- .jshintrc | 2 +- test/transaction/transaction.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/.jshintrc b/.jshintrc index 1d70b69..40fd8c6 100644 --- a/.jshintrc +++ b/.jshintrc @@ -28,7 +28,7 @@ "maxcomplexity": 6, // Cyclomatic complexity (http://en.wikipedia.org/wiki/Cyclomatic_complexity) "maxdepth": 4, // Maximum depth of nested control structures "maxlen": 120, // Maximum number of cols in a line - "multistr": true // Allow use of multiline EOL escaping + "multistr": true, // Allow use of multiline EOL escaping "predef": [ // Extra globals. "after", diff --git a/test/transaction/transaction.js b/test/transaction/transaction.js index 9a4ad0e..00b4f57 100644 --- a/test/transaction/transaction.js +++ b/test/transaction/transaction.js @@ -222,6 +222,13 @@ describe('Transaction', function() { var deserialized = new Transaction(serialized); expect(deserialized._change.toString()).to.equal(changeAddress); }); + it('can avoid checked serialize', function() { + var transaction = new Transaction() + .from(simpleUtxoWith1BTC) + .to(fromAddress, 1); + expect(function() { return transaction.serialize(); }).to.throw(); + expect(function() { return transaction.serialize(true); }).to.not.throw(); + }); }); describe('checked serialize', function() { @@ -255,6 +262,30 @@ describe('Transaction', function() { expect(JSON.parse(transaction.toJSON()).change).to.equal(changeAddress.toString()); }); }); + + describe('serialization of inputs', function() { + it('can serialize and deserialize a P2PKH input', function() { + var transaction = new Transaction() + .from(simpleUtxoWith1BTC); + var deserialized = new Transaction(transaction.toObject()); + expect(deserialized.inputs[0] instanceof Transaction.Input.PublicKeyHash).to.equal(true); + }); + it('can serialize and deserialize a P2SH input', function() { + var private1 = '6ce7e97e317d2af16c33db0b9270ec047a91bff3eff8558afb5014afb2bb5976'; + var private2 = 'c9b26b0f771a0d2dad88a44de90f05f416b3b385ff1d989343005546a0032890'; + var public1 = new PrivateKey(private1).publicKey; + var public2 = new PrivateKey(private2).publicKey; + var transaction = new Transaction() + .from({ + txId: private1, + outputIndex: 0, + script: Script.buildScriptHashOut(Script.buildMultisigOut([public1, public2], 2)), + satoshis: 10000 + }, [public1, public2], 2); + var deserialized = new Transaction(transaction.toObject()); + expect(deserialized.inputs[0] instanceof Transaction.Input.MultiSigScriptHash).to.equal(true); + }); + }); }); var tx_empty_hex = '01000000000000000000';