made wallet#sign synchronous
This commit is contained in:
parent
68208f7610
commit
0db8c1008b
4 changed files with 64 additions and 68 deletions
|
|
@ -445,20 +445,23 @@ angular.module('copayApp.controllers').controller('SendController',
|
||||||
|
|
||||||
$scope.sign = function(ntxid) {
|
$scope.sign = function(ntxid) {
|
||||||
$scope.loading = true;
|
$scope.loading = true;
|
||||||
w.sign(ntxid, function(ret) {
|
|
||||||
if (!ret) {
|
try {
|
||||||
$scope.error = 'There was an error signing the transaction';
|
w.sign(ntxid);
|
||||||
|
} catch (e) {
|
||||||
|
$scope.error = 'There was an error signing the transaction';
|
||||||
|
$scope.loadTxs();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var p = w.txProposals.getTxProposal(ntxid);
|
||||||
|
if (p.builder.isFullySigned()) {
|
||||||
|
$scope.send(ntxid, function() {
|
||||||
$scope.loadTxs();
|
$scope.loadTxs();
|
||||||
} else {
|
});
|
||||||
var p = w.txProposals.getTxProposal(ntxid);
|
} else {
|
||||||
if (p.builder.isFullySigned()) {
|
$scope.loadTxs();
|
||||||
$scope.send(ntxid, function() {
|
}
|
||||||
$scope.loadTxs();
|
|
||||||
});
|
|
||||||
} else
|
|
||||||
$scope.loadTxs();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.reject = function(ntxid) {
|
$scope.reject = function(ntxid) {
|
||||||
|
|
|
||||||
|
|
@ -1311,49 +1311,40 @@ Wallet.prototype.reject = function(ntxid) {
|
||||||
this.emitAndKeepAlive('txProposalsUpdated');
|
this.emitAndKeepAlive('txProposalsUpdated');
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* @callback signCallback
|
|
||||||
* @param {boolean} ret - true if it was successfully signed
|
|
||||||
*/
|
|
||||||
/**
|
/**
|
||||||
* @desc Sign a proposal
|
* @desc Sign a proposal
|
||||||
* @param {string} ntxid the id of the transaction proposal to sign
|
* @param {string} ntxid the id of the transaction proposal to sign
|
||||||
* @param {signCallback} cb - a callback to be called on successful signing
|
|
||||||
* @emits txProposalsUpdated
|
* @emits txProposalsUpdated
|
||||||
|
* @throws {Error} Could not sign proposal
|
||||||
|
* @throws {Error} Bad payment request
|
||||||
|
* @return {boolean} true if signing actually incremented the number of signatures
|
||||||
*/
|
*/
|
||||||
Wallet.prototype.sign = function(ntxid, cb) {
|
Wallet.prototype.sign = function(ntxid) {
|
||||||
preconditions.checkState(!_.isUndefined(this.getMyCopayerId()));
|
preconditions.checkState(!_.isUndefined(this.getMyCopayerId()));
|
||||||
var self = this;
|
|
||||||
setTimeout(function() {
|
|
||||||
var myId = self.getMyCopayerId();
|
|
||||||
var txp = self.txProposals.get(ntxid);
|
|
||||||
// if (!txp || txp.rejectedBy[myId] || txp.signedBy[myId]) {
|
|
||||||
// if (cb) cb(false);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
|
|
||||||
// If this is a payment protocol request,
|
var myId = this.getMyCopayerId();
|
||||||
// ensure it hasn't been tampered with.
|
var txp = this.txProposals.get(ntxid);
|
||||||
if (!self.verifyPaymentRequest(ntxid)) {
|
|
||||||
if (cb) cb(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var keys = self.privateKey.getForPaths(txp.inputChainPaths);
|
// If this is a payment protocol request,
|
||||||
|
// ensure it hasn't been tampered with.
|
||||||
|
if (!this.verifyPaymentRequest(ntxid)) {
|
||||||
|
throw new Error('Bad payment request');
|
||||||
|
}
|
||||||
|
|
||||||
var b = txp.builder;
|
var before = txp.countSignatures();
|
||||||
var before = txp.countSignatures();
|
|
||||||
b.sign(keys);
|
|
||||||
|
|
||||||
var ret = false;
|
var keys = this.privateKey.getForPaths(txp.inputChainPaths);
|
||||||
if (txp.countSignatures() > before) {
|
txp.builder.sign(keys);
|
||||||
txp.signedBy[myId] = Date.now();
|
|
||||||
self.sendTxProposal(ntxid);
|
if (txp.countSignatures() <= before) {
|
||||||
self.emitAndKeepAlive('txProposalsUpdated');
|
return false;
|
||||||
ret = true;
|
}
|
||||||
}
|
|
||||||
if (cb) return cb(ret);
|
txp.signedBy[myId] = Date.now();
|
||||||
}, 10);
|
this.sendTxProposal(ntxid);
|
||||||
|
this.emitAndKeepAlive('txProposalsUpdated');
|
||||||
|
|
||||||
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -800,9 +800,7 @@ describe('Wallet model', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
w.privateKey = k2;
|
w.privateKey = k2;
|
||||||
w.sign(ntxid, function(success) {
|
w.sign(ntxid).should.equal.true;
|
||||||
success.should.equal(true);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it('should fail to reject a signed transaction', function() {
|
it('should fail to reject a signed transaction', function() {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -43,13 +44,13 @@ if (amount && program.fee) {
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!requiredCopayers || !extPrivKeys || !extPrivKeys.length || !destAddr){
|
if (!requiredCopayers || !extPrivKeys || !extPrivKeys.length || !destAddr) {
|
||||||
program.outputHelp();
|
program.outputHelp();
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fee to asign to the tx. Please put a bigger number if you get 'unsufficient unspent'
|
// Fee to asign to the tx. Please put a bigger number if you get 'unsufficient unspent'
|
||||||
var fee = program.fee || 0.0001;
|
var fee = program.fee || 0.0001;
|
||||||
|
|
||||||
|
|
||||||
var totalCopayers = extPrivKeys.length;
|
var totalCopayers = extPrivKeys.length;
|
||||||
|
|
@ -64,8 +65,8 @@ if (!addr.isValid()) {
|
||||||
|
|
||||||
var networkName = addr.network().name;
|
var networkName = addr.network().name;
|
||||||
console.log('\tNetwork: %s\n\tDestination Address:%s\n\tRequired copayers: %d\n\tTotal copayers: %d\n\tFee: %d\n\tKeys:',
|
console.log('\tNetwork: %s\n\tDestination Address:%s\n\tRequired copayers: %d\n\tTotal copayers: %d\n\tFee: %d\n\tKeys:',
|
||||||
networkName, destAddr, requiredCopayers,
|
networkName, destAddr, requiredCopayers,
|
||||||
totalCopayers, fee, extPrivKeys); //TODO
|
totalCopayers, fee, extPrivKeys); //TODO
|
||||||
console.log('\n ----------------------------');
|
console.log('\n ----------------------------');
|
||||||
|
|
||||||
if (requiredCopayers > totalCopayers)
|
if (requiredCopayers > totalCopayers)
|
||||||
|
|
@ -155,16 +156,16 @@ firstWallet.updateIndexes(function() {
|
||||||
if (amount && amount >= balance)
|
if (amount && amount >= balance)
|
||||||
throw ('Not enought balance fund to fullfill ' + amount + ' satoshis');
|
throw ('Not enought balance fund to fullfill ' + amount + ' satoshis');
|
||||||
|
|
||||||
rl.question("\n\tShould I swipe the wallet (destination address is:" + destAddr + " Amount: "+ amount + "Satoshis )?\n\t(`yes` to continue)\n\t", function(answer) {
|
rl.question("\n\tShould I swipe the wallet (destination address is:" + destAddr + " Amount: " + (amount || balance) + " satoshis)?\n\t(`yes` to continue)\n\t", function(answer) {
|
||||||
if (answer !== 'yes')
|
if (answer !== 'yes')
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
|
|
||||||
amount = amount || balance - fee * bitcore.util.COIN;
|
amount = amount || balance - fee * bitcore.util.COIN;
|
||||||
|
|
||||||
firstWallet.createTx(destAddr, amount, '', {}, function(err, ntxid) {
|
firstWallet.createTx(destAddr, amount, '', {}, function(err, ntxid) {
|
||||||
if (err || !ntxid) {
|
if (err || !ntxid) {
|
||||||
if (err && err.toString().match('BIG')) {
|
if (err && err.toString().match('BIG')) {
|
||||||
throw new Error('Could not create tx' + err );
|
throw new Error('Could not create tx' + err);
|
||||||
} else {
|
} else {
|
||||||
throw new Error('Could not create tx' + err + '. Try a bigger fee (--fee).');
|
throw new Error('Could not create tx' + err + '. Try a bigger fee (--fee).');
|
||||||
}
|
}
|
||||||
|
|
@ -187,14 +188,17 @@ firstWallet.updateIndexes(function() {
|
||||||
async.eachSeries(signers, function(dummy, cb) {
|
async.eachSeries(signers, function(dummy, cb) {
|
||||||
console.log('\t Signing with copayer', i + 1);
|
console.log('\t Signing with copayer', i + 1);
|
||||||
w[i].txProposals = firstWallet.txProposals;
|
w[i].txProposals = firstWallet.txProposals;
|
||||||
w[i].sign(ntxid, function(signed) {
|
|
||||||
if (!signed) return cb('Could not sign');
|
|
||||||
|
|
||||||
console.log('\t Signed!');
|
try {
|
||||||
firstWallet.txProposals = firstWallet.txProposals;
|
w[i].sign(ntxid);
|
||||||
i++;
|
} catch (e) {
|
||||||
return cb(null);
|
return cb('Could not sign');
|
||||||
})
|
}
|
||||||
|
|
||||||
|
console.log('\t Signed!');
|
||||||
|
firstWallet.txProposals = firstWallet.txProposals;
|
||||||
|
i++;
|
||||||
|
return cb(null);
|
||||||
},
|
},
|
||||||
function(err) {
|
function(err) {
|
||||||
if (err)
|
if (err)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue