From 1e2d85e7db22605defa8f5bdeae38ac4aa5b6cf0 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 21 Aug 2016 13:44:32 -0700 Subject: [PATCH] wallet: import key and subtract fee. --- lib/bcoin/http/rpc.js | 16 +++---------- lib/bcoin/mtx.js | 25 +++++++++++++++++++- lib/bcoin/wallet.js | 54 +++++++++++++++++++++++++------------------ 3 files changed, 58 insertions(+), 37 deletions(-) diff --git a/lib/bcoin/http/rpc.js b/lib/bcoin/http/rpc.js index 347c5b76..8f646b38 100644 --- a/lib/bcoin/http/rpc.js +++ b/lib/bcoin/http/rpc.js @@ -3790,7 +3790,7 @@ RPC.prototype.sendfrom = function sendfrom(args, callback) { RPC.prototype.sendmany = function sendmany(args, callback) { var account, sendTo, minDepth, comment, subtractFee; - var i, j, outputs, subtractIndex, keys, uniq; + var i, outputs, keys, uniq; var key, value, address, hash, output, options; if (args.help || args.length < 2 || args.length > 5) { @@ -3819,7 +3819,6 @@ RPC.prototype.sendmany = function sendmany(args, callback) { subtractFee = toArray(args[4]); outputs = []; - subtractIndex = null; keys = Object.keys(sendTo); uniq = {}; @@ -3834,26 +3833,17 @@ RPC.prototype.sendmany = function sendmany(args, callback) { uniq[hash] = true; - for (j = 0; j < subtractFee.length; j++) { - if (subtractFee[j] === key) - subtractIndex = i; - } - output = new bcoin.output(); output.value = value; output.script.fromAddress(address); outputs.push(output); } - // Don't do this for now. We sort the outputs. - if (subtractIndex != null) - return callback(new RPCError('Cannot subtract.')); - options = { outputs: outputs, - subtractFee: subtractIndex, + subtractFee: subtractFee, account: account, - confirmations: minDepth // todo: addme + confirmations: minDepth }; this.wallet.send(options, function(err, tx) { diff --git a/lib/bcoin/mtx.js b/lib/bcoin/mtx.js index 434cbec6..f4a2881f 100644 --- a/lib/bcoin/mtx.js +++ b/lib/bcoin/mtx.js @@ -1312,7 +1312,19 @@ MTX.prototype.selectCoins = function selectCoins(coins, options) { */ MTX.prototype.subtractFee = function subtractFee(fee, index) { - var i, min, output; + var i, min, output, hash, addrs; + + if (Buffer.isBuffer(index) || typeof index === 'string') + index = [index]; + + if (Array.isArray(index)) { + addrs = []; + for (i = 0; i < index.length; i++) { + hash = bcoin.address.getHash(index[i]); + if (hash) + addrs.push(hash); + } + } if (typeof index === 'number') { output = this.outputs[index]; @@ -1333,6 +1345,17 @@ MTX.prototype.subtractFee = function subtractFee(fee, index) { for (i = 0; i < this.outputs.length; i++) { output = this.outputs[i]; min = fee + output.getDustThreshold(); + + if (addrs) { + hash = output.getHash(); + + if (!hash) + continue; + + if (utils.indexOf(addrs, hash) === -1) + continue; + } + if (output.value >= min) { output.value -= fee; break; diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index deb0953b..3359decb 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -820,39 +820,47 @@ Wallet.prototype.importKey = function importKey(account, ring, passphrase, callb if (!callback) return; - this.getAccount(account, function(err, account) { + this.getPath(ring.getHash('hex'), function(err, exists) { if (err) return callback(err); - if (!account) - return callback(new Error('Account not found.')); + if (exists) + return callback(new Error('Key already exists.')); - self.unlock(passphrase, null, function(err) { + self.getAccount(account, function(err, account) { if (err) return callback(err); - raw = ring.toRaw(); - path = bcoin.path.fromAccount(account, ring); + if (!account) + return callback(new Error('Account not found.')); - if (self.master.encrypted) { - raw = self.master.encipher(raw, path.hash); - assert(raw); - path.encrypted = true; - } - - path.imported = raw; - ring.path = path; - - self.start(); - - account.saveAddress([ring], function(err) { - if (err) { - self.drop(); + self.unlock(passphrase, null, function(err) { + if (err) return callback(err); + + raw = ring.toRaw(); + path = bcoin.path.fromAccount(account, ring); + + if (self.master.encrypted) { + raw = self.master.encipher(raw, path.hash); + assert(raw); + path.encrypted = true; } - self.commit(callback); - }); - }, true); + + path.imported = raw; + ring.path = path; + + self.start(); + + account.saveAddress([ring], function(err) { + if (err) { + self.drop(); + return callback(err); + } + self.commit(callback); + }); + }, true); + }); }); };