From e55983b151e708bee1a7f2d0ae95755bf9069ac9 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Tue, 3 Nov 2015 13:01:12 -0300 Subject: [PATCH] mv Utils.buildTx -> TxProposal --- lib/common/utils.js | 72 ------------------------------------- lib/model/txproposal.js | 73 +++++++++++++++++++++++++++++++++++++- test/integration/server.js | 2 +- 3 files changed, 73 insertions(+), 74 deletions(-) diff --git a/lib/common/utils.js b/lib/common/utils.js index a6dde9c..01f1420 100644 --- a/lib/common/utils.js +++ b/lib/common/utils.js @@ -2,13 +2,9 @@ var $ = require('preconditions').singleton(); var _ = require('lodash'); var Bitcore = require('bitcore-lib'); -var Address = Bitcore.Address; var crypto = Bitcore.crypto; var encoding = Bitcore.encoding; -var Constants = require('./constants'); -var Defaults = require('./defaults'); - var Utils = {}; Utils.checkRequired = function(obj, args) { @@ -58,74 +54,6 @@ Utils.verifyMessage = function(text, signature, pubKey) { } }; -Utils.newBitcoreTransaction = function() { - return new Bitcore.Transaction(); -}; - -Utils.buildTx = function(txp) { - var t = Utils.newBitcoreTransaction(); - - $.checkState(_.contains(_.values(Constants.SCRIPT_TYPES), txp.addressType)); - - switch (txp.addressType) { - case Constants.SCRIPT_TYPES.P2SH: - _.each(txp.inputs, function(i) { - t.from(i, i.publicKeys, txp.requiredSignatures); - }); - break; - case Constants.SCRIPT_TYPES.P2PKH: - t.from(txp.inputs); - break; - } - - if (txp.toAddress && txp.amount && !txp.outputs) { - t.to(txp.toAddress, txp.amount); - } else if (txp.outputs) { - _.each(txp.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({ - script: o.script, - satoshis: o.amount - })); - } else { - t.to(o.toAddress, o.amount); - } - }); - } - - if (_.startsWith(txp.version, '1.')) { - Bitcore.Transaction.FEE_SECURITY_MARGIN = 1; - t.feePerKb(txp.feePerKb); - } else { - t.fee(txp.fee); - } - - t.change(txp.changeAddress.address); - - // Shuffle outputs for improved privacy - if (t.outputs.length > 1) { - $.checkState(t.outputs.length == txp.outputOrder.length); - t.sortOutputs(function(outputs) { - return _.map(txp.outputOrder, function(i) { - return outputs[i]; - }); - }); - } - - // Validate inputs vs outputs independently of Bitcore - var totalInputs = _.reduce(txp.inputs, function(memo, i) { - return +i.satoshis + memo; - }, 0); - var totalOutputs = _.reduce(t.outputs, function(memo, o) { - return +o.satoshis + memo; - }, 0); - - $.checkState(totalInputs - totalOutputs <= Defaults.MAX_TX_FEE); - - return t; -}; - Utils.formatAmount = function(satoshis, unit, opts) { var UNITS = { btc: { diff --git a/lib/model/txproposal.js b/lib/model/txproposal.js index c435d61..6f835c3 100644 --- a/lib/model/txproposal.js +++ b/lib/model/txproposal.js @@ -11,6 +11,7 @@ var Bitcore = require('bitcore-lib'); var Common = require('../common'); var Constants = Common.Constants; +var Defaults = Common.Defaults; var TxProposalAction = require('./txproposalaction'); @@ -148,6 +149,76 @@ TxProposal.prototype._updateStatus = function() { } }; +TxProposal.newBitcoreTransaction = function() { + return new Bitcore.Transaction(); +}; + +TxProposal.prototype._buildTx = function() { + var self = this; + + var t = TxProposal.newBitcoreTransaction(); + + $.checkState(_.contains(_.values(Constants.SCRIPT_TYPES), self.addressType)); + + switch (self.addressType) { + case Constants.SCRIPT_TYPES.P2SH: + _.each(self.inputs, function(i) { + t.from(i, i.publicKeys, self.requiredSignatures); + }); + break; + case Constants.SCRIPT_TYPES.P2PKH: + t.from(self.inputs); + break; + } + + if (self.toAddress && self.amount && !self.outputs) { + t.to(self.toAddress, self.amount); + } else if (self.outputs) { + _.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({ + script: o.script, + satoshis: o.amount + })); + } else { + t.to(o.toAddress, o.amount); + } + }); + } + + if (_.startsWith(self.version, '1.')) { + Bitcore.Transaction.FEE_SECURITY_MARGIN = 1; + t.feePerKb(self.feePerKb); + } else { + t.fee(self.fee); + } + + t.change(self.changeAddress.address); + + // Shuffle outputs for improved privacy + if (t.outputs.length > 1) { + $.checkState(t.outputs.length == self.outputOrder.length); + t.sortOutputs(function(outputs) { + return _.map(self.outputOrder, function(i) { + return outputs[i]; + }); + }); + } + + // Validate inputs vs outputs independently of Bitcore + var totalInputs = _.reduce(self.inputs, function(memo, i) { + return +i.satoshis + memo; + }, 0); + var totalOutputs = _.reduce(t.outputs, function(memo, o) { + return +o.satoshis + memo; + }, 0); + + $.checkState(totalInputs - totalOutputs <= Defaults.MAX_TX_FEE); + + return t; +}; + TxProposal.prototype._getCurrentSignatures = function() { var acceptedActions = _.filter(this.actions, { @@ -165,7 +236,7 @@ TxProposal.prototype._getCurrentSignatures = function() { TxProposal.prototype.getBitcoreTx = function() { var self = this; - var t = Common.Utils.buildTx(this); + var t = this._buildTx(); var sigs = this._getCurrentSignatures(); _.each(sigs, function(x) { diff --git a/test/integration/server.js b/test/integration/server.js index 73d1dcc..b5a2ad6 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -284,7 +284,7 @@ helpers.clientSign = function(txp, xPrivKey) { } }); - var t = Utils.buildTx(txp); + var t = txp.getBitcoreTx(); var signatures = _.map(privs, function(priv, i) { return t.getSignatures(priv);