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.payProUrl = opts.payProUrl;
x.changeAddress = opts.changeAddress;
x.inputs = [];
x.inputPaths = [];
x.setInputs(opts.inputs);
x.outputs = _.map(opts.outputs, function(output) {
return _.pick(output, ['amount', 'toAddress', 'message']);
});
x.outputOrder = _.shuffle(_.range(x.outputs.length + 1));
x.requiredSignatures = opts.requiredSignatures;
x.requiredRejections = opts.requiredRejections;
x.walletM = opts.walletM;
x.walletN = opts.walletN;
x.requiredSignatures = x.walletM;
x.requiredRejections = Math.min(x.walletM, x.walletN - x.walletM + 1),
x.status = 'temporary';
x.actions = [];
x.fee = null;
@ -80,9 +80,10 @@ TxProposal.fromObj = function(obj) {
x.payProUrl = obj.payProUrl;
x.changeAddress = obj.changeAddress;
x.inputs = obj.inputs;
x.walletM = obj.walletM;
x.walletN = obj.walletN;
x.requiredSignatures = obj.requiredSignatures;
x.requiredRejections = obj.requiredRejections;
x.walletN = obj.walletN;
x.status = obj.status;
x.txid = obj.txid;
x.broadcastedOn = obj.broadcastedOn;
@ -112,8 +113,8 @@ TxProposal.prototype.toObject = function() {
};
TxProposal.prototype.setInputs = function(inputs) {
this.inputs = inputs;
this.inputPaths = _.pluck(inputs, 'path');
this.inputs = inputs || [];
this.inputPaths = _.pluck(inputs, 'path') || [];
};
TxProposal.prototype._updateStatus = function() {
@ -144,21 +145,17 @@ TxProposal.prototype._buildTx = function() {
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);
}
});
}
_.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);
}
});
t.fee(self.fee);
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 {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.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.
*/
WalletService.prototype.createTx = function(opts, cb) {
@ -1378,25 +1379,26 @@ WalletService.prototype.createTx = function(opts, cb) {
if (err) return cb(err);
if (!canCreate) return cb(Errors.TX_CANNOT_CREATE);
var validationError = self._validateOutputs(opts, wallet);
if (validationError) {
return cb(validationError);
if (opts.validateOutputs !== false) {
var validationError = self._validateOutputs(opts, wallet);
if (validationError) {
return cb(validationError);
}
}
var txOpts = {
version: 3,
walletId: self.walletId,
creatorId: self.copayerId,
outputs: opts.outputs,
inputs: opts.inputs,
outputs: opts.outputs,
message: opts.message,
changeAddress: wallet.createAddress(true),
feePerKb: feePerKb,
payProUrl: opts.payProUrl,
requiredSignatures: wallet.m,
requiredRejections: Math.min(wallet.m, wallet.n - wallet.m + 1),
walletM: wallet.m,
walletN: wallet.n,
excludeUnconfirmedUtxos: !!opts.excludeUnconfirmedUtxos,
validateOutputs: !opts.validateOutputs,
addressType: wallet.addressType,
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() {
var server, wallet;
@ -2356,6 +2356,10 @@ describe('Wallet service', function() {
server.createTx(txOpts, function(err, tx) {
should.not.exist(err);
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.isRejected().should.equal.false;
tx.isPending().should.equal.true;

Loading…
Cancel
Save