wallet: import key and subtract fee.

This commit is contained in:
Christopher Jeffrey 2016-08-21 13:44:32 -07:00
parent e5d23945b1
commit 1e2d85e7db
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 58 additions and 37 deletions

View File

@ -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) {

View File

@ -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;

View File

@ -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);
});
});
};