From 01e8562b4385c281af992f794ccfd67ea8015e09 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Sat, 1 Nov 2014 09:42:56 -0300 Subject: [PATCH] fix async loop --- js/models/Identity.js | 1 - util/swipeWallet.js | 44 ++++++++++++++++++++++++++++++------------- views/home.html | 2 +- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/js/models/Identity.js b/js/models/Identity.js index 7fb67526c..4e6effb71 100644 --- a/js/models/Identity.js +++ b/js/models/Identity.js @@ -252,7 +252,6 @@ Identity.prototype.store = function(opts, cb) { storeFunction.call(self.storage, this.getId(), this.toObj(), function(err) { if (err) return cb(err); - console.log('[Identity.js.255:opts:]', opts); //TODO if (opts.noWallets) return cb(); diff --git a/util/swipeWallet.js b/util/swipeWallet.js index 388637c74..6185dcc18 100755 --- a/util/swipeWallet.js +++ b/util/swipeWallet.js @@ -9,6 +9,7 @@ var version = require('../version').version; var sinon = require('sinon'); var bitcore = require('bitcore'); var readline = require('readline'); +var async = require('async'); var rl = readline.createInterface({ input: process.stdin, @@ -38,6 +39,9 @@ var networkName = addr.network().name; console.log('\tNetwork: %s\n\tDestination Address:%s\n\tRequired copayers: %d\n\tTotal copayers: %d\n\tKeys:', networkName, destAddr, requiredCopayers, totalCopayers, extPrivKeys); //TODO console.log('\n ----------------------------'); +if (requiredCopayers > totalCopayers) + throw new Error('No enought private keys given'); + function createWallet(networkName, extPrivKeys, index) { @@ -120,27 +124,39 @@ firstWallet.updateIndexes(function() { } rl.question("\n\tShould I swipe the wallet (destination address" + destAddr + ")?\n\t(`yes` to continue)\n\t", function(answer) { - if (answer!== 'yes') + if (answer !== 'yes') process.exit(1); var amount = balance - DFLT_FEE; firstWallet.createTx(destAddr, amount, '', {}, function(err, ntxid) { - console.log('\n\t### Tx Proposal Created... With copayer 0 signature.'); - - if (requiredCopayers ===1) { + console.log('\n\t### Tx Proposal Created...\n\tWith copayer 0 signature.'); + if (requiredCopayers === 1) { firstWallet.sendTx(ntxid, function(txid) { console.log('\t ####### SENT TXID:', txid); process.exit(1); }); } - var l = w.length; - _.each(w, function(dummy, i) { - console.log('\t Signing with copayer', i + 1); - w[i].txProposals = firstWallet.txProposals; - w[i].sign(ntxid, function(err) { - console.log('\t Signed!'); - firstWallet.txProposals = firstWallet.txProposals; + var signers = w.slice(0, requiredCopayers - 1); + + console.log('Will add %d more signatures:', signers.length); + + var i = 0; + async.eachSeries(signers, function(dummy, cb) { + console.log('\t Signing with copayer', i + 1); + w[i].txProposals = firstWallet.txProposals; + w[i].sign(ntxid, function(signed) { + if (!signed) return cb('Could not sign'); + + console.log('\t Signed!'); + firstWallet.txProposals = firstWallet.txProposals; + i++; + return cb(null); + }) + }, + function(err) { + if (err) + throw new Error(err); var p = firstWallet.txProposals.getTxProposal(ntxid); if (p.builder.isFullySigned()) { @@ -149,9 +165,11 @@ firstWallet.updateIndexes(function() { console.log('\t ####### SENT TXID:', txid); process.exit(1); }); + } else { + throw new Error('Error: could not fully sign the TX'); } - }) - }) + } + ) }); }); }); diff --git a/views/home.html b/views/home.html index 2603190e0..1fa2887fb 100644 --- a/views/home.html +++ b/views/home.html @@ -2,7 +2,7 @@
- Retreiving information from storage... + Synchronizing with storage...