From 58cb560b790eeb42080fec7ce5c8b1c34dbc4943 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Fri, 25 Aug 2017 16:23:27 -0300 Subject: [PATCH] select appropriate Bitcore when building/signing tx proposal --- lib/model/txproposal.js | 17 +++++++++++------ lib/model/txproposal_legacy.js | 1 + test/integration/helpers.js | 4 ++++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/model/txproposal.js b/lib/model/txproposal.js index 925c78b..7db3770 100644 --- a/lib/model/txproposal.js +++ b/lib/model/txproposal.js @@ -7,7 +7,10 @@ var log = require('npmlog'); log.debug = log.verbose; log.disableColor(); -var Bitcore = require('bitcore-lib'); +var Bitcore = { + 'btc': require('bitcore-lib'), + 'bch': require('bitcore-lib-cash'), +}; var Common = require('../common'); var Constants = Common.Constants, @@ -139,7 +142,7 @@ TxProposal.prototype._updateStatus = function() { TxProposal.prototype._buildTx = function() { var self = this; - var t = new Bitcore.Transaction(); + var t = new Bitcore[self.coin].Transaction(); $.checkState(Utils.checkValueInCollection(self.addressType, Constants.SCRIPT_TYPES)); @@ -158,7 +161,7 @@ TxProposal.prototype._buildTx = function() { _.each(self.outputs, function(o) { $.checkState(o.script || o.toAddress, 'Output should have either toAddress or script specified'); if (o.script) { - t.addOutput(new Bitcore.Transaction.Output({ + t.addOutput(new Bitcore[self.coin].Transaction.Output({ script: o.script, satoshis: o.amount })); @@ -322,21 +325,23 @@ TxProposal.prototype.addAction = function(copayerId, type, comment, signatures, TxProposal.prototype._addSignaturesToBitcoreTx = function(tx, signatures, xpub) { var self = this; + var bitcore = Bitcore[self.coin]; + if (signatures.length != this.inputs.length) throw new Error('Number of signatures does not match number of inputs'); var i = 0, - x = new Bitcore.HDPublicKey(xpub); + x = new bitcore.HDPublicKey(xpub); _.each(signatures, function(signatureHex) { var input = self.inputs[i]; try { - var signature = Bitcore.crypto.Signature.fromString(signatureHex); + var signature = bitcore.crypto.Signature.fromString(signatureHex); var pub = x.deriveChild(self.inputPaths[i]).publicKey; var s = { inputIndex: i, signature: signature, - sigtype: Bitcore.crypto.Signature.SIGHASH_ALL, + sigtype: bitcore.crypto.Signature.SIGHASH_ALL, publicKey: pub, }; tx.inputs[i].addSignature(tx, s); diff --git a/lib/model/txproposal_legacy.js b/lib/model/txproposal_legacy.js index c21576f..787a78a 100644 --- a/lib/model/txproposal_legacy.js +++ b/lib/model/txproposal_legacy.js @@ -54,6 +54,7 @@ TxProposal.fromObj = function(obj) { return TxProposalAction.fromObj(action); }); x.outputOrder = obj.outputOrder; + x.coin = obj.coin || Defaults.COIN; x.network = obj.network; x.fee = obj.fee; x.feePerKb = obj.feePerKb; diff --git a/test/integration/helpers.js b/test/integration/helpers.js index 825d0af..2ab3041 100644 --- a/test/integration/helpers.js +++ b/test/integration/helpers.js @@ -13,6 +13,10 @@ var tingodb = require('tingodb')({ }); var Bitcore = require('bitcore-lib'); +var Bitcore_ = { + btc: Bitcore, + bch: require('bitcore-lib-cash') +}; var Common = require('../../lib/common'); var Utils = Common.Utils;