From 29d79e57ad94e79f5cecdd235fe2ca400717e433 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 2 Jun 2016 10:35:39 -0700 Subject: [PATCH] sign created tx. --- bin/bcoin-cli | 84 +++++++++++++++++++++++++++++++++++++--- lib/bcoin/http/client.js | 1 + lib/bcoin/http/server.js | 35 +++++++---------- 3 files changed, 94 insertions(+), 26 deletions(-) diff --git a/bin/bcoin-cli b/bin/bcoin-cli index d7de8081..0dd9d718 100755 --- a/bin/bcoin-cli +++ b/bin/bcoin-cli @@ -225,6 +225,11 @@ function listenWallet(callback) { utils.print(tx); utils.print(map); }); + client.on('address', function(receive, change) { + utils.print('New addresses allocated:'); + utils.print(receive); + utils.print(change); + }); client.on('balance', function(tx, map) { utils.print('Balance:'); utils.print(tx); @@ -253,7 +258,7 @@ function getMempool(callback) { }); } -function send(callback) { +function sendTX(callback) { var id = getID(); var options = { account: argv.account, passphrase: argv.passphrase }; var output = {}; @@ -268,6 +273,40 @@ function send(callback) { if (err) return callback(err); utils.print(tx); + utils.print(tx.toRaw('hex')); + callback(); + }); +} + +function createTX(callback) { + var id = getID(); + var options = { account: argv.account, passphrase: argv.passphrase }; + var output = {}; + if (argv.script) { + output.script = new bcoin.script(new Buffer(argv.script, 'hex')); + output.value = utils.satoshi(argv.value || argv.args[0]); + } else { + output.address = argv.address || argv.args[0]; + output.value = utils.satoshi(argv.value || argv.args[1]); + } + client.walletCreate(id, options, [output], function(err, tx) { + if (err) + return callback(err); + utils.print(tx); + utils.print(tx.toRaw('hex')); + callback(); + }); +} + +function signTX(callback) { + var id = getID(); + var options = { passphrase: argv.passphrase }; + var tx = bcoin.tx.fromRaw(options.tx || argv.args[0], 'hex'); + client.walletSign(id, tx, options, function(err, tx) { + if (err) + return callback(err); + utils.print(tx); + utils.print(tx.toRaw('hex')); callback(); }); } @@ -283,6 +322,23 @@ function zap(callback) { }); } +function broadcast(callback) { + var tx = bcoin.tx.fromRaw(argv.args[0] || argv.tx, 'hex'); + client.broadcast(tx, function(err, tx) { + if (err) + return callback(err); + utils.print('Broadcasted:'); + utils.print(tx); + callback(); + }); +} + +function view(callback) { + var tx = bcoin.tx.fromRaw(argv.args[0] || argv.tx, 'hex'); + utils.print(tx); + callback(); +} + function main(callback) { switch (argv.args.shift()) { case 'wallet': @@ -299,10 +355,22 @@ function main(callback) { return getBalance(callback); case 'history': return getWalletHistory(callback); + case 'account': + return createAccount(callback); + case 'accounts': + return getAccounts(callback); + case 'sign': + return signTX(callback); + case 'create': + return createTX(callback); case 'send': return send(callback); case 'zap': return zap(callback); + case 'broadcast': + return broadcast(callback); + case 'view': + return view(callback); case 'mempool': return getMempool(callback); case 'tx': @@ -311,27 +379,33 @@ function main(callback) { return getCoin(callback); case 'block': return getBlock(callback); - case 'account': - return createAccount(callback); - case 'accounts': - return getAccounts(callback); default: utils.print('Unrecognized command.'); utils.print('Commands:'); utils.print(' $ wallet [id] --keys [hdkeys]' + ' --type [pubkeyhash/multisig] -m [m-value]' + ' -n [n-value] --witness: View or create wallet by ID.'); + utils.print(' $ listen [id]: Listen for wallet events.'); utils.print(' $ getwallet [id]: View wallet by ID.'); utils.print(' $ addkey [id] --keys [hdkeys]: Add keys to wallet.'); utils.print(' $ rmkey [id] --keys [hdkeys]: Remove keys from wallet.'); utils.print(' $ balance [id]: Get wallet balance.'); utils.print(' $ history [id]: View wallet TX history.'); + utils.print(' $ accounts [id]: List account names.'); + utils.print(' $ account [id] [acct]: Get account details.'); utils.print(' $ send [id] [address] [value] --script [code]: Send transaction.'); + utils.print(' $ create [id] [address] [value] --script [code]: Create transaction.'); + utils.print(' $ sign [id] [tx-hex]: Sign transaction.'); utils.print(' $ zap [id] --age [age]: Zap pending wallet TXs.'); + utils.print(' $ broadcast [tx-hex]: Broadcast transaction.'); + utils.print(' $ view [tx-hex]: View transaction.'); utils.print(' $ mempool: Get mempool snapshot.'); utils.print(' $ tx [hash/address]: View transactions.'); utils.print(' $ coin [hash+index/address]: View coins.'); utils.print(' $ block [hash/height]: View block.'); + utils.print('Other Options:'); + utils.print(' --passphrase [passphrase]: For signing and account creation.'); + utils.print(' --account [acctname]: Account name.'); return callback(); } } diff --git a/lib/bcoin/http/client.js b/lib/bcoin/http/client.js index 96581bb8..81cff474 100644 --- a/lib/bcoin/http/client.js +++ b/lib/bcoin/http/client.js @@ -236,6 +236,7 @@ HTTPClient.prototype._request = function _request(method, endpoint, json, callba networkType = res.headers['x-bcoin-network']; assert(networkType === self.network.type, 'Wrong network.'); + self.network.updateHeight(+res.headers['x-bcoin-height']); if (res.statusCode === 404) return callback(); diff --git a/lib/bcoin/http/server.js b/lib/bcoin/http/server.js index b35ad047..e80692da 100644 --- a/lib/bcoin/http/server.js +++ b/lib/bcoin/http/server.js @@ -108,12 +108,6 @@ HTTPServer.prototype._init = function _init() { if (params.limit != null) options.limit = params.limit >>> 0; - if (params.changeDepth) - options.changeDepth = params.changeDepth >>> 0; - - if (params.receiveDepth) - options.receiveDepth = params.receiveDepth >>> 0; - if (params.address) { params.addresses = params.address; options.address = params.address; @@ -127,7 +121,7 @@ HTTPServer.prototype._init = function _init() { value: utils.satoshi(output.value) }; }); - } else if (options.value) { + } else if (params.value) { options.outputs = [{ address: params.address, script: decodeScript(params.script), @@ -156,9 +150,6 @@ HTTPServer.prototype._init = function _init() { if (params.name) options.name = params.name; - if (params.now) - options.now = params.now >>> 0; - if (params.age) options.age = params.age >>> 0; @@ -175,9 +166,6 @@ HTTPServer.prototype._init = function _init() { if (params.passphrase) options.passphrase = params.passphrase; - if (params.bin) - options.bin = true; - req.options = options; next(); @@ -273,7 +261,7 @@ HTTPServer.prototype._init = function _init() { if (!txs.length) return send(404); - utils.forEach(txs, function(tx, next) { + utils.forEachSerial(txs, function(tx, next) { self.node.fillHistory(tx, next); }, function(err) { if (err) @@ -295,7 +283,7 @@ HTTPServer.prototype._init = function _init() { if (!txs.length) return send(404); - utils.forEach(txs, function(tx, next) { + utils.forEachSerial(txs, function(tx, next) { self.node.fillHistory(tx, next); }, function(err) { if (err) @@ -331,7 +319,7 @@ HTTPServer.prototype._init = function _init() { if (!txs.length) return send(404); - utils.forEach(txs, function(tx, next) { + utils.forEachSerial(txs, function(tx, next) { self.node.fillHistory(tx, next); }, function(err) { if (err) @@ -455,7 +443,12 @@ HTTPServer.prototype._init = function _init() { if (err) return next(err); - send(200, tx.toJSON()); + self.walletdb.sign(id, tx, options, function(err) { + if (err) + return next(err); + + send(200, tx.toJSON()); + }); }); }); @@ -577,7 +570,7 @@ HTTPServer.prototype._init = function _init() { if (!txs.length) return send(404); - utils.forEach(txs, function(tx, next) { + utils.forEachSerial(txs, function(tx, next) { self.walletdb.fillHistory(tx, next); }, function(err) { if (err) @@ -601,7 +594,7 @@ HTTPServer.prototype._init = function _init() { if (!txs.length) return send(404); - utils.forEach(txs, function(tx, next) { + utils.forEachSerial(txs, function(tx, next) { self.walletdb.fillHistory(tx, next); }, function(err) { if (err) @@ -626,7 +619,7 @@ HTTPServer.prototype._init = function _init() { if (!txs.length) return send(404); - utils.forEach(txs, function(tx, next) { + utils.forEachSerial(txs, function(tx, next) { self.walletdb.fillHistory(tx, next); }, function(err) { if (err) @@ -651,7 +644,7 @@ HTTPServer.prototype._init = function _init() { if (!txs.length) return send(404); - utils.forEach(txs, function(tx, next) { + utils.forEachSerial(txs, function(tx, next) { self.walletdb.fillHistory(tx, next); }, function(err) { if (err)