From cca1e2148c34d93595c340c52bad7de7e9798c71 Mon Sep 17 00:00:00 2001 From: Esteban Ordano Date: Wed, 11 Feb 2015 19:10:57 -0300 Subject: [PATCH] Improve test coverage on multisig --- lib/transaction/input/multisigscripthash.js | 21 +++++--------------- test/transaction/input/multisigscripthash.js | 18 +++++++++++++++++ 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/lib/transaction/input/multisigscripthash.js b/lib/transaction/input/multisigscripthash.js index b6fb671..8b5db74 100644 --- a/lib/transaction/input/multisigscripthash.js +++ b/lib/transaction/input/multisigscripthash.js @@ -19,6 +19,9 @@ var TransactionSignature = require('../signature'); function MultiSigScriptHashInput(input, pubkeys, threshold, signatures) { Input.apply(this, arguments); var self = this; + pubkeys = pubkeys || input.publicKeys; + threshold = threshold || input.threshold; + signatures = signatures || input.signatures; this.publicKeys = _.sortBy(pubkeys, function(publicKey) { return publicKey.toString('hex'); }); this.redeemScript = Script.buildMultisigOut(this.publicKeys, threshold); $.checkState(Script.buildScriptHashOut(this.redeemScript).equals(this.output.script), @@ -46,14 +49,7 @@ MultiSigScriptHashInput.prototype._deserializeSignatures = function(signatures) if (!signature) { return undefined; } - return new TransactionSignature({ - publicKey: new PublicKey(signature.publicKey), - prevTxId: signature.txId, - outputIndex: signature.outputIndex, - inputIndex: signature.inputIndex, - signature: Signature.fromString(signature.signature), - sigtype: signature.sigtype - }); + return new TransactionSignature(signature); }); }; @@ -62,14 +58,7 @@ MultiSigScriptHashInput.prototype._serializeSignatures = function() { if (!signature) { return undefined; } - return new TransactionSignature({ - publicKey: signature.publicKey.toString(), - prevTxId: signature.txId, - outputIndex: signature.outputIndex, - inputIndex: signature.inputIndex, - signature: signature.signature.toString(), - sigtype: signature.sigtype - }); + return signature.toObject(); }); }; diff --git a/test/transaction/input/multisigscripthash.js b/test/transaction/input/multisigscripthash.js index c9a3621..60a82c7 100644 --- a/test/transaction/input/multisigscripthash.js +++ b/test/transaction/input/multisigscripthash.js @@ -11,6 +11,7 @@ var PrivateKey = bitcore.PrivateKey; var Address = bitcore.Address; var Script = bitcore.Script; var Signature = bitcore.crypto.Signature; +var MultiSigScriptHashInput = bitcore.Transaction.Input.MultiSigScriptHash; describe('MultiSigScriptHashInput', function() { @@ -93,4 +94,21 @@ describe('MultiSigScriptHashInput', function() { var sigs = input.getSignatures(transaction, privateKey1, 0); sigs[0].sigtype.should.equal(Signature.SIGHASH_ALL); }); + it('roundtrips to/from object', function() { + var transaction = new Transaction() + .from(output, [public1, public2, public3], 2) + .to(address, 1000000) + .sign(privateKey1); + var input = transaction.inputs[0]; + var roundtrip = new MultiSigScriptHashInput(input.toObject()); + roundtrip.toObject().should.deep.equal(input.toObject()); + }); + it('roundtrips to/from object when not signed', function() { + var transaction = new Transaction() + .from(output, [public1, public2, public3], 2) + .to(address, 1000000); + var input = transaction.inputs[0]; + var roundtrip = new MultiSigScriptHashInput(input.toObject()); + roundtrip.toObject().should.deep.equal(input.toObject()); + }); });