diff --git a/lib/http/rpc.js b/lib/http/rpc.js index 31d52242..b053c03a 100644 --- a/lib/http/rpc.js +++ b/lib/http/rpc.js @@ -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); } diff --git a/lib/wallet/account.js b/lib/wallet/account.js index 47882951..31b76b38 100644 --- a/lib/wallet/account.js +++ b/lib/wallet/account.js @@ -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.'); diff --git a/lib/wallet/masterkey.js b/lib/wallet/masterkey.js index 37ffea69..94489d04 100644 --- a/lib/wallet/masterkey.js +++ b/lib/wallet/masterkey.js @@ -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], diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index e0d3d481..67d21088 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -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