Browse Source

simplify proposal creation + allow non-validated outputs (external txps)

activeAddress
Ivan Socolsky 9 years ago
parent
commit
0bb043d034
  1. 41
      lib/model/txproposal.js
  2. 18
      lib/server.js
  3. 6
      test/integration/server.js

41
lib/model/txproposal.js

@ -33,15 +33,15 @@ TxProposal.create = function(opts) {
x.message = opts.message; x.message = opts.message;
x.payProUrl = opts.payProUrl; x.payProUrl = opts.payProUrl;
x.changeAddress = opts.changeAddress; x.changeAddress = opts.changeAddress;
x.inputs = []; x.setInputs(opts.inputs);
x.inputPaths = [];
x.outputs = _.map(opts.outputs, function(output) { x.outputs = _.map(opts.outputs, function(output) {
return _.pick(output, ['amount', 'toAddress', 'message']); return _.pick(output, ['amount', 'toAddress', 'message']);
}); });
x.outputOrder = _.shuffle(_.range(x.outputs.length + 1)); x.outputOrder = _.shuffle(_.range(x.outputs.length + 1));
x.requiredSignatures = opts.requiredSignatures; x.walletM = opts.walletM;
x.requiredRejections = opts.requiredRejections;
x.walletN = opts.walletN; x.walletN = opts.walletN;
x.requiredSignatures = x.walletM;
x.requiredRejections = Math.min(x.walletM, x.walletN - x.walletM + 1),
x.status = 'temporary'; x.status = 'temporary';
x.actions = []; x.actions = [];
x.fee = null; x.fee = null;
@ -80,9 +80,10 @@ TxProposal.fromObj = function(obj) {
x.payProUrl = obj.payProUrl; x.payProUrl = obj.payProUrl;
x.changeAddress = obj.changeAddress; x.changeAddress = obj.changeAddress;
x.inputs = obj.inputs; x.inputs = obj.inputs;
x.walletM = obj.walletM;
x.walletN = obj.walletN;
x.requiredSignatures = obj.requiredSignatures; x.requiredSignatures = obj.requiredSignatures;
x.requiredRejections = obj.requiredRejections; x.requiredRejections = obj.requiredRejections;
x.walletN = obj.walletN;
x.status = obj.status; x.status = obj.status;
x.txid = obj.txid; x.txid = obj.txid;
x.broadcastedOn = obj.broadcastedOn; x.broadcastedOn = obj.broadcastedOn;
@ -112,8 +113,8 @@ TxProposal.prototype.toObject = function() {
}; };
TxProposal.prototype.setInputs = function(inputs) { TxProposal.prototype.setInputs = function(inputs) {
this.inputs = inputs; this.inputs = inputs || [];
this.inputPaths = _.pluck(inputs, 'path'); this.inputPaths = _.pluck(inputs, 'path') || [];
}; };
TxProposal.prototype._updateStatus = function() { TxProposal.prototype._updateStatus = function() {
@ -144,21 +145,17 @@ TxProposal.prototype._buildTx = function() {
break; break;
} }
if (self.toAddress && self.amount && !self.outputs) { _.each(self.outputs, function(o) {
t.to(self.toAddress, self.amount); $.checkState(o.script || o.toAddress, 'Output should have either toAddress or script specified');
} else if (self.outputs) { if (o.script) {
_.each(self.outputs, function(o) { t.addOutput(new Bitcore.Transaction.Output({
$.checkState(o.script || o.toAddress, 'Output should have either toAddress or script specified'); script: o.script,
if (o.script) { satoshis: o.amount
t.addOutput(new Bitcore.Transaction.Output({ }));
script: o.script, } else {
satoshis: o.amount t.to(o.toAddress, o.amount);
})); }
} else { });
t.to(o.toAddress, o.amount);
}
});
}
t.fee(self.fee); t.fee(self.fee);
t.change(self.changeAddress.address); t.change(self.changeAddress.address);

18
lib/server.js

@ -1356,7 +1356,8 @@ WalletService.prototype.createTxLegacy = function(opts, cb) {
* @param {Array} opts.inputs - Optional. Inputs for this TX * @param {Array} opts.inputs - Optional. Inputs for this TX
* @param {string} opts.feePerKb - Optional. Use an alternative fee per KB for this TX * @param {string} opts.feePerKb - Optional. Use an alternative fee per KB for this TX
* @param {string} opts.payProUrl - Optional. Paypro URL for peers to verify TX * @param {string} opts.payProUrl - Optional. Paypro URL for peers to verify TX
* @param {string} opts.excludeUnconfirmedUtxos - Optional. Do not use UTXOs of unconfirmed transactions as inputs (defaults to false) * @param {string} opts.excludeUnconfirmedUtxos[=false] - Optional. Do not use UTXOs of unconfirmed transactions as inputs
* @param {string} opts.validateOutputs[=true] - Optional. Perform validation on outputs.
* @returns {TxProposal} Transaction proposal. * @returns {TxProposal} Transaction proposal.
*/ */
WalletService.prototype.createTx = function(opts, cb) { WalletService.prototype.createTx = function(opts, cb) {
@ -1378,25 +1379,26 @@ WalletService.prototype.createTx = function(opts, cb) {
if (err) return cb(err); if (err) return cb(err);
if (!canCreate) return cb(Errors.TX_CANNOT_CREATE); if (!canCreate) return cb(Errors.TX_CANNOT_CREATE);
var validationError = self._validateOutputs(opts, wallet); if (opts.validateOutputs !== false) {
if (validationError) { var validationError = self._validateOutputs(opts, wallet);
return cb(validationError); if (validationError) {
return cb(validationError);
}
} }
var txOpts = { var txOpts = {
version: 3,
walletId: self.walletId, walletId: self.walletId,
creatorId: self.copayerId, creatorId: self.copayerId,
outputs: opts.outputs,
inputs: opts.inputs, inputs: opts.inputs,
outputs: opts.outputs,
message: opts.message, message: opts.message,
changeAddress: wallet.createAddress(true), changeAddress: wallet.createAddress(true),
feePerKb: feePerKb, feePerKb: feePerKb,
payProUrl: opts.payProUrl, payProUrl: opts.payProUrl,
requiredSignatures: wallet.m, walletM: wallet.m,
requiredRejections: Math.min(wallet.m, wallet.n - wallet.m + 1),
walletN: wallet.n, walletN: wallet.n,
excludeUnconfirmedUtxos: !!opts.excludeUnconfirmedUtxos, excludeUnconfirmedUtxos: !!opts.excludeUnconfirmedUtxos,
validateOutputs: !opts.validateOutputs,
addressType: wallet.addressType, addressType: wallet.addressType,
customData: opts.customData, customData: opts.customData,
}; };

6
test/integration/server.js

@ -1638,7 +1638,7 @@ describe('Wallet service', function() {
}); });
}); });
describe.only('#createTx', function() { describe('#createTx', function() {
describe('Legacy', function() { describe('Legacy', function() {
var server, wallet; var server, wallet;
@ -2356,6 +2356,10 @@ describe('Wallet service', function() {
server.createTx(txOpts, function(err, tx) { server.createTx(txOpts, function(err, tx) {
should.not.exist(err); should.not.exist(err);
should.exist(tx); should.exist(tx);
tx.walletM.should.equal(2);
tx.walletN.should.equal(3);
tx.requiredRejections.should.equal(2);
tx.requiredSignatures.should.equal(2);
tx.isAccepted().should.equal.false; tx.isAccepted().should.equal.false;
tx.isRejected().should.equal.false; tx.isRejected().should.equal.false;
tx.isPending().should.equal.true; tx.isPending().should.equal.true;

Loading…
Cancel
Save