wallet: import key and subtract fee.
This commit is contained in:
parent
e5d23945b1
commit
1e2d85e7db
@ -3790,7 +3790,7 @@ RPC.prototype.sendfrom = function sendfrom(args, callback) {
|
|||||||
|
|
||||||
RPC.prototype.sendmany = function sendmany(args, callback) {
|
RPC.prototype.sendmany = function sendmany(args, callback) {
|
||||||
var account, sendTo, minDepth, comment, subtractFee;
|
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;
|
var key, value, address, hash, output, options;
|
||||||
|
|
||||||
if (args.help || args.length < 2 || args.length > 5) {
|
if (args.help || args.length < 2 || args.length > 5) {
|
||||||
@ -3819,7 +3819,6 @@ RPC.prototype.sendmany = function sendmany(args, callback) {
|
|||||||
subtractFee = toArray(args[4]);
|
subtractFee = toArray(args[4]);
|
||||||
|
|
||||||
outputs = [];
|
outputs = [];
|
||||||
subtractIndex = null;
|
|
||||||
keys = Object.keys(sendTo);
|
keys = Object.keys(sendTo);
|
||||||
uniq = {};
|
uniq = {};
|
||||||
|
|
||||||
@ -3834,26 +3833,17 @@ RPC.prototype.sendmany = function sendmany(args, callback) {
|
|||||||
|
|
||||||
uniq[hash] = true;
|
uniq[hash] = true;
|
||||||
|
|
||||||
for (j = 0; j < subtractFee.length; j++) {
|
|
||||||
if (subtractFee[j] === key)
|
|
||||||
subtractIndex = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
output = new bcoin.output();
|
output = new bcoin.output();
|
||||||
output.value = value;
|
output.value = value;
|
||||||
output.script.fromAddress(address);
|
output.script.fromAddress(address);
|
||||||
outputs.push(output);
|
outputs.push(output);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't do this for now. We sort the outputs.
|
|
||||||
if (subtractIndex != null)
|
|
||||||
return callback(new RPCError('Cannot subtract.'));
|
|
||||||
|
|
||||||
options = {
|
options = {
|
||||||
outputs: outputs,
|
outputs: outputs,
|
||||||
subtractFee: subtractIndex,
|
subtractFee: subtractFee,
|
||||||
account: account,
|
account: account,
|
||||||
confirmations: minDepth // todo: addme
|
confirmations: minDepth
|
||||||
};
|
};
|
||||||
|
|
||||||
this.wallet.send(options, function(err, tx) {
|
this.wallet.send(options, function(err, tx) {
|
||||||
|
|||||||
@ -1312,7 +1312,19 @@ MTX.prototype.selectCoins = function selectCoins(coins, options) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
MTX.prototype.subtractFee = function subtractFee(fee, index) {
|
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') {
|
if (typeof index === 'number') {
|
||||||
output = this.outputs[index];
|
output = this.outputs[index];
|
||||||
@ -1333,6 +1345,17 @@ MTX.prototype.subtractFee = function subtractFee(fee, index) {
|
|||||||
for (i = 0; i < this.outputs.length; i++) {
|
for (i = 0; i < this.outputs.length; i++) {
|
||||||
output = this.outputs[i];
|
output = this.outputs[i];
|
||||||
min = fee + output.getDustThreshold();
|
min = fee + output.getDustThreshold();
|
||||||
|
|
||||||
|
if (addrs) {
|
||||||
|
hash = output.getHash();
|
||||||
|
|
||||||
|
if (!hash)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (utils.indexOf(addrs, hash) === -1)
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (output.value >= min) {
|
if (output.value >= min) {
|
||||||
output.value -= fee;
|
output.value -= fee;
|
||||||
break;
|
break;
|
||||||
|
|||||||
@ -820,39 +820,47 @@ Wallet.prototype.importKey = function importKey(account, ring, passphrase, callb
|
|||||||
if (!callback)
|
if (!callback)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
this.getAccount(account, function(err, account) {
|
this.getPath(ring.getHash('hex'), function(err, exists) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
if (!account)
|
if (exists)
|
||||||
return callback(new Error('Account not found.'));
|
return callback(new Error('Key already exists.'));
|
||||||
|
|
||||||
self.unlock(passphrase, null, function(err) {
|
self.getAccount(account, function(err, account) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
raw = ring.toRaw();
|
if (!account)
|
||||||
path = bcoin.path.fromAccount(account, ring);
|
return callback(new Error('Account not found.'));
|
||||||
|
|
||||||
if (self.master.encrypted) {
|
self.unlock(passphrase, null, function(err) {
|
||||||
raw = self.master.encipher(raw, path.hash);
|
if (err)
|
||||||
assert(raw);
|
|
||||||
path.encrypted = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
path.imported = raw;
|
|
||||||
ring.path = path;
|
|
||||||
|
|
||||||
self.start();
|
|
||||||
|
|
||||||
account.saveAddress([ring], function(err) {
|
|
||||||
if (err) {
|
|
||||||
self.drop();
|
|
||||||
return callback(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);
|
|
||||||
});
|
path.imported = raw;
|
||||||
}, true);
|
ring.path = path;
|
||||||
|
|
||||||
|
self.start();
|
||||||
|
|
||||||
|
account.saveAddress([ring], function(err) {
|
||||||
|
if (err) {
|
||||||
|
self.drop();
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
self.commit(callback);
|
||||||
|
});
|
||||||
|
}, true);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user