wallet: better option handling. expose more txdb methods.

This commit is contained in:
Christopher Jeffrey 2016-10-05 04:31:17 -07:00
parent bc4e9e7387
commit 6f18d890f1
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
4 changed files with 77 additions and 27 deletions

View File

@ -3211,7 +3211,7 @@ RPC.prototype.importprivkey = co(function* importprivkey(args) {
if (!rescan)
return null;
yield this.walletdb.rescan(this.chain.db, 0);
yield this.node.scan(0);
return null;
});
@ -3262,7 +3262,7 @@ RPC.prototype.importwallet = co(function* importwallet(args) {
yield this.wallet.importKey(0, key, null);
}
yield this.walletdb.rescan(this.chain.db, 0);
yield this.node.scan(0);
return null;
});
@ -3305,7 +3305,7 @@ RPC.prototype.importpubkey = co(function* importpubkey(args) {
if (!rescan)
return null;
yield this.walletdb.rescan(this.chain.db, 0);
yield this.node.scan(0);
return null;
});
@ -3346,7 +3346,7 @@ RPC.prototype.listlockunspent = function listlockunspent(args) {
if (args.help || args.length > 0)
return Promise.reject(new RPCError('listlockunspent'));
outpoints = this.wallet.txdb.getLocked();
outpoints = this.wallet.getLocked();
out = [];
for (i = 0; i < outpoints.length; i++) {
@ -3728,7 +3728,7 @@ RPC.prototype.listunspent = co(function* listunspent(args) {
scriptPubKey: coin.script.toJSON(),
amount: +utils.btc(coin.value),
confirmations: depth,
spendable: !this.wallet.txdb.isLocked(coin),
spendable: !this.wallet.isLocked(coin),
solvable: true
});
}
@ -3748,7 +3748,7 @@ RPC.prototype.lockunspent = function lockunspent(args) {
if (args.length === 1) {
if (unlock)
this.wallet.txdb.unlockCoins();
this.wallet.unlockCoins();
return Promise.resolve(true);
}

View File

@ -272,6 +272,9 @@ Account.prototype.pushKey = function pushKey(key) {
if (HD.isExtended(key))
key = HD.fromBase58(key);
assert(key.network === this.network,
'Network mismatch for account key.');
if (!HD.isPublic(key))
throw new Error('Must add HD keys to wallet.');
@ -306,6 +309,9 @@ Account.prototype.spliceKey = function spliceKey(key) {
if (HD.isExtended(key))
key = HD.fromBase58(key);
assert(key.network === this.network,
'Network mismatch for account key.');
if (!HD.isHDPublicKey(key))
throw new Error('Must add HD keys to wallet.');

View File

@ -198,15 +198,15 @@ MasterKey.prototype._unlock = co(function* _unlock(passphrase, timeout) {
MasterKey.prototype.start = function start(timeout) {
if (!timeout)
timeout = 60000;
timeout = 60;
this.stop();
if (timeout === -1)
return;
this.until = utils.now() + (timeout / 1000 | 0);
this.timer = setTimeout(this._destroy, timeout);
this.until = utils.now() + timeout;
this.timer = setTimeout(this._destroy, timeout * 1000);
};
/**
@ -499,6 +499,7 @@ MasterKey.prototype.toJSON = function toJSON() {
if (this.encrypted) {
return {
encrypted: true,
until: this.until,
iv: this.iv.toString('hex'),
ciphertext: this.ciphertext.toString('hex'),
algorithm: MasterKey.algByVal[this.alg],

View File

@ -106,8 +106,14 @@ Wallet.prototype.fromOptions = function fromOptions(options) {
if (!master)
master = HD.fromMnemonic(null, this.network);
if (!HD.isHD(master))
master = HD.from(master, this.network);
if (HD.isExtended(master))
master = HD.fromBase58(master);
assert(HD.isPrivate(master),
'Must create wallet with hd private key.');
assert(master.network === this.network,
'Network mismatch for master key.');
master = MasterKey.fromKey(master);
}
@ -588,9 +594,9 @@ Wallet.prototype.lock = co(function* lock() {
});
/**
* Unlock the key for `timeout` milliseconds.
* Unlock the key for `timeout` seconds.
* @param {Buffer|String} passphrase
* @param {Number?} [timeout=60000] - ms.
* @param {Number?} [timeout=60]
*/
Wallet.prototype.unlock = function unlock(passphrase, timeout) {
@ -679,9 +685,6 @@ Wallet.prototype._createAccount = co(function* createAccount(options) {
var name = options.name;
var key, master, account, exists;
if (typeof options.account === 'string')
name = options.account;
if (!name)
name = this.accountDepth + '';
@ -695,11 +698,14 @@ Wallet.prototype._createAccount = co(function* createAccount(options) {
if (this.watchOnly && options.accountKey) {
key = options.accountKey;
if (!HD.isHD(key))
key = HD.from(key, this.network);
if (HD.isExtended(key))
key = HD.fromBase58(key);
if (HD.isPrivate(key))
throw new Error('Cannot add xprivkey to watch-only wallet.');
if (!HD.isPublic(key))
throw new Error('Must add HD public keys to watch only wallet.');
assert(key.network === this.network,
'Network mismatch for watch only key.');
} else {
key = master.deriveAccount44(this.accountDepth);
key = key.hdPublicKey;
@ -752,13 +758,8 @@ Wallet.prototype._createAccount = co(function* createAccount(options) {
*/
Wallet.prototype.ensureAccount = co(function* ensureAccount(options) {
var name = options.account;
var account;
if (typeof options.name === 'string')
name = options.name;
account = yield this.getAccount(name);
var name = options.name;
var account = yield this.getAccount(name);
if (!account)
return yield this.createAccount(options);
@ -1158,6 +1159,9 @@ Wallet.prototype._importKey = co(function* importKey(acct, ring, passphrase) {
if (acct == null)
acct = 0;
assert(ring.network === this.network,
'Network mismatch for key.');
if (!this.watchOnly) {
if (!ring.privateKey)
throw new Error('Cannot import pubkey into non watch-only wallet.');
@ -1240,6 +1244,9 @@ Wallet.prototype._importAddress = co(function* importAddress(acct, address) {
if (acct == null)
acct = 0;
assert(address.network === this.network,
'Network mismatch for address.');
if (!this.watchOnly)
throw new Error('Cannot import address into non watch-only wallet.');
@ -1949,6 +1956,42 @@ Wallet.prototype._abandon = function abandon(hash) {
return this.txdb.abandon(hash);
};
/**
* Lock a single coin.
* @param {Coin|Outpoint} coin
*/
Wallet.prototype.lockCoin = function lockCoin(coin) {
return this.txdb.lockCoin(coin);
};
/**
* Unlock a single coin.
* @param {Coin|Outpoint} coin
*/
Wallet.prototype.unlockCoin = function unlockCoin(coin) {
return this.txdb.unlockCoin(coin);
};
/**
* Test locked status of a single coin.
* @param {Coin|Outpoint} coin
*/
Wallet.prototype.isLocked = function isLocked(coin) {
return this.txdb.isLocked(coin);
};
/**
* Return an array of all locked outpoints.
* @returns {Outpoint[]}
*/
Wallet.prototype.getLocked = function getLocked() {
return this.txdb.getLocked();
};
/**
* Map a transactions' addresses to wallet IDs.
* @param {TX} tx