From 3b8873b7a863f7c06af304ba472076b9cace2fca Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Sun, 22 Feb 2015 01:01:15 -0300 Subject: [PATCH] add output amount --- README.md | 6 ++++-- bit-wallet/bit-balance | 2 +- bit-wallet/bit-status | 19 +++---------------- bit-wallet/cli-utils.js | 35 +++++++++++++++++++++++++++++++++++ lib/server.js | 11 ++++++++++- 5 files changed, 53 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 828bcd0..c539bad 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,9 @@ A Multisig HD Wallet Service, with minimun server trust. # Spend coins. Amount can be specified in btc, bit or sat (default) ./bit send 1xxxxx 100bit "100 bits to mother" - + + # You can use 100bit or 0.00001btc or 10000sat. (Set up BIT_UNIT to btc/sat/bit so select output unit). + # List pending TX Proposals ./bit status @@ -57,7 +59,7 @@ A Multisig HD Wallet Service, with minimun server trust. # List all commands: ./bit --help - + ``` diff --git a/bit-wallet/bit-balance b/bit-wallet/bit-balance index 173f038..9f9cc40 100755 --- a/bit-wallet/bit-balance +++ b/bit-wallet/bit-balance @@ -13,5 +13,5 @@ var client = utils.getClient(program); client.getBalance(function(err, x) { utils.die(err); - console.log('* Wallet balance', x); + console.log('* Wallet balance %s (Locked %s)', utils.renderAmount(x.totalAmount), utils.renderAmount(x.lockedAmount) ); }); diff --git a/bit-wallet/bit-status b/bit-wallet/bit-status index 35cacc6..43faaa5 100755 --- a/bit-wallet/bit-status +++ b/bit-wallet/bit-status @@ -22,20 +22,7 @@ client.getStatus(function(err, res) { console.log('* Copayers:', _.pluck(x.copayers,'name').join(', ')); var x = res.balance; - console.log('* Balance %dSAT (Locked: %dSAT)', x.totalAmount, x.lockedAmount); - - if (!_.isEmpty(res.pendingTxps)) { - console.log("* TX Proposals:") - _.each(res.pendingTxps, function(x) { - missingSignatures = x.requiredSignatures - _.filter(_.values(x.actions), function (a) { return a.type == 'accept'; }).length; - console.log("\t%s [\"%s\" by %s] %dSAT => %s", utils.shortID(x.id), x.message, x.creatorName, x.amount, x.toAddress); - - if (!_.isEmpty(x.actions)) { - console.log('\t\tActions: ', _.map(x.actions, function(a) { - return a.copayerName + ' ' + (a.type == 'accept' ? '✓' : '✗') + (a.comment ? ' (' + a.comment + ')' : ''); - }).join('. ')); - } - console.log('\t\tMissing signatures: ' + missingSignatures); - }); - } + console.log('* Balance %s (Locked: %s)', utils.renderAmount(x.totalAmount), utils.renderAmount(x.lockedAmount)); + + utils.renderTxProposals(res.pendingTxps); }); diff --git a/bit-wallet/cli-utils.js b/bit-wallet/cli-utils.js index 5f5dcf9..222713e 100644 --- a/bit-wallet/cli-utils.js +++ b/bit-wallet/cli-utils.js @@ -99,4 +99,39 @@ Utils.configureCommander = function(program) { return program; }; +Utils.renderAmount = function(amount) { + var unit = process.env.BIT_UNIT || 'bit'; + if (unit === 'SAT') { + // Do nothing + } else if (process.env.BIT_UNIT === 'btc') { + amount = amount / 1e8; + } else { + amount = amount / 100; + } + amount = (parseFloat(amount.toPrecision(12))); + return amount.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + ' ' + unit; +}; + +Utils.renderTxProposals = function(txps) { + if (_.isEmpty(txps)) + return; + + console.log("* TX Proposals:") + + _.each(txps, function(x) { + var missingSignatures = x.requiredSignatures - _.filter(_.values(x.actions), function(a) { + return a.type == 'accept'; + }).length; + console.log("\t%s [\"%s\" by %s] %s => %s", Utils.shortID(x.id), x.message, x.creatorName, Utils.renderAmount(x.amount), x.toAddress); + + if (!_.isEmpty(x.actions)) { + console.log('\t\tActions: ', _.map(x.actions, function(a) { + return a.copayerName + ' ' + (a.type == 'accept' ? '✓' : '✗') + (a.comment ? ' (' + a.comment + ')' : ''); + }).join('. ')); + } + console.log('\t\tMissing signatures: ' + missingSignatures); + }); + +}; + module.exports = Utils; diff --git a/lib/server.js b/lib/server.js index bf2dedc..336af2d 100644 --- a/lib/server.js +++ b/lib/server.js @@ -363,7 +363,7 @@ WalletService.prototype._getUtxos = function(cb) { bc.getUnspentUtxos(addressStrs, function(err, inutxos) { if (err) return cb(err); var utxos = _.map(inutxos, function(i) { - return _.pick(i, ['txid', 'vout', 'address', 'scriptPubKey', 'amount', 'satoshis']); + return _.pick(i.toObject(), ['txid', 'vout', 'address', 'scriptPubKey', 'amount', 'satoshis']); }); self.getPendingTxs({}, function(err, txps) { if (err) return cb(err); @@ -432,22 +432,28 @@ WalletService.prototype.getBalance = function(opts, cb) { WalletService.prototype._selectUtxos = function(txp, utxos) { +console.log('[server.js.434:utxos:]',utxos); //TODO var i = 0; var total = 0; var selected = []; var inputs = _.sortBy(utxos, 'amount'); +console.log('[server.js.438:inputs:]',inputs); //TODO while (i < inputs.length) { selected.push(inputs[i]); total += inputs[i].satoshis; +console.log('[server.js.443]'); //TODO if (total >= txp.amount + Bitcore.Transaction.FEE_PER_KB) { try { // Check if there are enough fees txp.inputs = selected; + +console.log('[server.js.449]'); //TODO var raw = txp.getRawTx(); return; } catch (ex) { +console.log('[server.js.453:ex:]',ex); //TODO if (ex.name != 'bitcore.ErrorTransactionFeeError') { throw ex.message; } @@ -498,6 +504,7 @@ WalletService.prototype.createTx = function(opts, cb) { return cb(new ClientError('DUSTAMOUNT', 'Amount below dust threshold')); self._getUtxos(function(err, utxos) { +console.log('[server.js.506:utxos:]',utxos); //TODO if (err) return cb(err); var changeAddress = wallet.createAddress(true); @@ -520,6 +527,7 @@ WalletService.prototype.createTx = function(opts, cb) { try { self._selectUtxos(txp, utxos); } catch (ex) { +console.log('[server.js.523:ex:]',ex); //TODO return cb(new ClientError(ex)); } @@ -528,6 +536,7 @@ WalletService.prototype.createTx = function(opts, cb) { txp.inputPaths = _.pluck(txp.inputs, 'path'); +console.log('[server.js.530]'); //TODO self.storage.storeAddressAndWallet(wallet, changeAddress, function(err) { if (err) return cb(err);