check tx already exists based on foreign id

This commit is contained in:
Ivan Socolsky 2016-07-26 11:01:14 -03:00
parent 49929942e7
commit 04fdc090bd
No known key found for this signature in database
GPG Key ID: FAECE6A05FAA4F56
2 changed files with 84 additions and 65 deletions

View File

@ -23,6 +23,7 @@ var errors = {
NOT_AUTHORIZED: 'Not authorized',
TOO_MANY_KEYS: 'Too many keys registered',
TX_ALREADY_BROADCASTED: 'The transaction proposal is already broadcasted',
TX_ALREADY_EXISTS: 'A transaction proposal with the same id already exists',
TX_CANNOT_CREATE: 'Cannot create TX proposal during backoff time',
TX_CANNOT_REMOVE: 'Cannot remove this tx proposal during locktime',
TX_MAX_SIZE_EXCEEDED: 'TX exceeds maximum allowed size',

View File

@ -1934,6 +1934,7 @@ WalletService.prototype._validateAndSanitizeTxOpts = function(wallet, opts, cb)
/**
* Creates a new transaction proposal.
* @param {Object} opts
* @param {string} opts.txProposalId - Optional. If provided it will be used as this TX proposal ID. Should be unique in the scope of the wallet.
* @param {Array} opts.outputs - List of outputs.
* @param {string} opts.outputs[].toAddress - Destination address.
* @param {number} opts.outputs[].amount - Amount to transfer in satoshi.
@ -1973,18 +1974,31 @@ WalletService.prototype.createTx = function(opts, cb) {
}
};
function checkTxpAlreadyExists(txProposalId, cb) {
if (!txProposalId) return cb();
self.storage.fetchTx(self.walletId, txProposalId, function(err, txp) {
if (err || !txp) return cb(err);
if (txp.status == 'temporary') {
return cb(null, txp);
} else {
return cb(Errors.TX_ALREADY_EXISTS);
}
});
};
self._runLocked(cb, function(cb) {
var wallet, txp, changeAddress;
var txp, changeAddress;
self.getWallet({}, function(err, wallet) {
if (err) return cb(err);
if (!wallet.isComplete()) return cb(Errors.WALLET_NOT_COMPLETE);
checkTxpAlreadyExists(opts.txProposalId, function(err, txp) {
if (err) return cb(err);
if (txp) return cb(null, txp);
async.series([
function(next) {
self.getWallet({}, function(err, w) {
if (err) return next(err);
if (!w.isComplete()) return next(Errors.WALLET_NOT_COMPLETE);
wallet = w;
next();
});
},
function(next) {
self._validateAndSanitizeTxOpts(wallet, opts, next);
},
@ -2005,6 +2019,7 @@ WalletService.prototype.createTx = function(opts, cb) {
},
function(next) {
var txOpts = {
id: opts.txProposalId,
walletId: self.walletId,
creatorId: self.copayerId,
outputs: opts.outputs,
@ -2038,9 +2053,12 @@ WalletService.prototype.createTx = function(opts, cb) {
self.storage.storeTx(wallet.id, txp, next);
},
], function(err) {
if (err) return cb(err);
if (err && err !== 'break') return cb(err);
return cb(null, txp);
});
});
});
});
};