diff --git a/lib/primitives/coin.js b/lib/primitives/coin.js index cb83b7f36..fb97f2730 100644 --- a/lib/primitives/coin.js +++ b/lib/primitives/coin.js @@ -314,6 +314,7 @@ class Coin extends Output { * @param {TX} tx * @param {Number} index * @param {Number} height + * @returns {this} */ fromTX(tx, index, height) { diff --git a/lib/wallet/records.js b/lib/wallet/records.js index e45327b58..3d17dc2ef 100644 --- a/lib/wallet/records.js +++ b/lib/wallet/records.js @@ -450,6 +450,7 @@ class MapRecord extends bio.Struct { constructor() { super(); + /** @type {Set} */ this.wids = new Set(); } diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index 040648b93..761a8975e 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -24,8 +24,20 @@ const NameUndo = require('../covenants/undo'); const {TXRecord} = require('./records'); const {types} = rules; +/** @typedef {import('bdb').DB} DB */ +/** @typedef {ReturnType} Batch */ +/** @typedef {import('../types').Hash} Hash */ +/** @typedef {import('../types').BufioWriter} BufioWriter */ +/** @typedef {import('../types').NetworkType} NetworkType */ +/** @typedef {import('../types').Amount} AmountValue */ +/** @typedef {import('../types').Rate} Rate */ +/** @typedef {import('../protocol/network')} Network */ +/** @typedef {import('../primitives/output')} Output */ +/** @typedef {import('../primitives/tx')} TX */ /** @typedef {import('./records').BlockMeta} BlockMeta */ /** @typedef {import('./walletdb')} WalletDB */ +/** @typedef {import('./wallet')} Wallet */ +/** @typedef {import('./path')} Path */ /** * @typedef {Object} BlockExtraInfo @@ -71,6 +83,7 @@ class TXDB { /** * Open TXDB. + * @param {Wallet} wallet * @returns {Promise} */ @@ -87,7 +100,7 @@ class TXDB { * @private * @param {String} event * @param {Object} data - * @param {Details} details + * @param {Details} [details] */ emit(event, data, details) { @@ -116,7 +129,7 @@ class TXDB { * @returns {Promise} */ - hasPath(output) { + async hasPath(output) { const hash = output.getHash(); if (!hash) @@ -127,6 +140,7 @@ class TXDB { /** * Save credit. + * @param {Batch} b * @param {Credit} credit * @param {Path} path */ @@ -142,6 +156,7 @@ class TXDB { /** * Remove credit. + * @param {Batch} b * @param {Credit} credit * @param {Path} path */ @@ -157,6 +172,7 @@ class TXDB { /** * Spend credit. + * @param {Batch} b * @param {Credit} credit * @param {TX} tx * @param {Number} index @@ -171,6 +187,7 @@ class TXDB { /** * Unspend credit. + * @param {Batch} b * @param {TX} tx * @param {Number} index */ @@ -185,6 +202,7 @@ class TXDB { /** * Spend credit by spender/input record. * Add undo coin to the input record. + * @param {Batch} b * @param {Credit} credit * @param {Outpoint} spender */ @@ -195,6 +213,7 @@ class TXDB { /** * Write input record. + * @param {Batch} b * @param {TX} tx * @param {Number} index */ @@ -208,6 +227,7 @@ class TXDB { /** * Remove input record. + * @param {Batch} b * @param {TX} tx * @param {Number} index */ @@ -220,6 +240,7 @@ class TXDB { /** * Update wallet balance. + * @param {Batch} b * @param {BalanceDelta} state */ @@ -234,7 +255,8 @@ class TXDB { * Update account balance. * @param {Batch} b * @param {Number} acct - * @param {BalanceDelta} delta + * @param {Balance} delta - account balance + * @returns {Promise} */ async updateAccountBalance(b, acct, delta) { @@ -273,6 +295,7 @@ class TXDB { /** * Append to global map. + * @param {Batch} b * @param {Number} height * @returns {Promise} */ @@ -283,6 +306,7 @@ class TXDB { /** * Remove from global map. + * @param {Batch} b * @param {Number} height * @returns {Promise} */ @@ -293,6 +317,7 @@ class TXDB { /** * Append to global map. + * @param {Batch} b * @param {Hash} hash * @returns {Promise} */ @@ -303,6 +328,7 @@ class TXDB { /** * Remove from global map. + * @param {Batch} b * @param {Hash} hash * @returns {Promise} */ @@ -313,6 +339,7 @@ class TXDB { /** * Append to global map. + * @param {Batch} b * @param {Hash} hash * @param {Number} index * @returns {Promise} @@ -324,6 +351,7 @@ class TXDB { /** * Remove from global map. + * @param {Batch} b * @param {Hash} hash * @param {Number} index * @returns {Promise} @@ -335,8 +363,8 @@ class TXDB { /** * Append to global map. - * @param {Hash} hash - * @param {Number} index + * @param {Batch} b + * @param {Hash} nameHash * @returns {Promise} */ @@ -346,8 +374,8 @@ class TXDB { /** * Remove from global map. - * @param {Hash} hash - * @param {Number} index + * @param {Batch} b + * @param {Hash} nameHash * @returns {Promise} */ @@ -420,8 +448,8 @@ class TXDB { const raw = Buffer.allocUnsafe(data.length + 32); data.copy(raw, 0); - const size = raw.readUInt32LE(40, true); - raw.writeUInt32LE(size + 1, 40, true); + const size = raw.readUInt32LE(40); + raw.writeUInt32LE(size + 1, 40); hash.copy(raw, data.length); b.put(key, raw); @@ -429,6 +457,7 @@ class TXDB { /** * Remove from the global block record. + * @param {Batch} b * @param {Hash} hash * @param {Number} height * @returns {Promise} @@ -459,6 +488,7 @@ class TXDB { /** * Remove from the global block record. + * @param {Batch} b * @param {Hash} hash * @param {Number} height * @returns {Promise} @@ -600,7 +630,7 @@ class TXDB { /** * Get all bids for name. - * @param {Buffer} nameHash + * @param {Buffer} [nameHash] * @returns {Promise} */ @@ -633,6 +663,7 @@ class TXDB { /** * Remove all bids for name. + * @param {Batch} b * @param {Buffer} nameHash * @returns {Promise} */ @@ -719,8 +750,8 @@ class TXDB { * @param {Buffer} nameHash * @param {Outpoint} outpoint * @param {Object} options - * @param {String} options.name - * @param {Amount} options.value + * @param {Buffer} options.name + * @param {AmountValue} options.value * @param {Number} options.height * @param {Boolean} options.own * @param {Outpoint} options.bidPrevout @@ -758,7 +789,7 @@ class TXDB { /** * Get all reveals by name. - * @param {Buffer} nameHash + * @param {Buffer} [nameHash] * @returns {Promise} */ @@ -918,7 +949,7 @@ class TXDB { /** * Test whether the transaction * has a duplicate open. - * @param {TX} + * @param {TX} tx * @returns {Promise} */ @@ -1591,7 +1622,6 @@ class TXDB { * remove all of its spenders. * @private * @param {TXRecord} wtx - * @param {Number} medianTime * @returns {Promise} */ @@ -1994,11 +2024,11 @@ class TXDB { b.put(layout.Oc.encode(hash), count.encode()); b.put(layout.Oe.encode(hash), fromU32(time)); - b.put(layout.Om.encode(time, count.index, hash)); + b.put(layout.Om.encode(time, count.index, hash), null); for (const [acct] of accounts) { b.put(layout.OT.encode(acct, count.height, count.index), hash); - b.put(layout.OM.encode(acct, time, count.index, hash)); + b.put(layout.OM.encode(acct, time, count.index, hash), null); } this.incrementLatestUnconfirmedTXCount(b, count.index); @@ -2068,11 +2098,11 @@ class TXDB { b.del(layout.Ou.encode(hash)); const time = await this.getUnconfirmedTimeForTX(hash); - b.put(layout.Om.encode(time, count.index, hash)); + b.put(layout.Om.encode(time, count.index, hash), null); for (const [acct] of accounts) { b.put(layout.OT.encode(acct, count.height, count.index), hash); - b.put(layout.OM.encode(acct, time, count.index, hash)); + b.put(layout.OM.encode(acct, time, count.index, hash), null); } } @@ -2135,11 +2165,11 @@ class TXDB { b.put(layout.Oc.encode(hash), count.encode()); const time = blockextra.medianTime; - b.put(layout.Oi.encode(time, height, index, hash)); + b.put(layout.Oi.encode(time, height, index, hash), null); for (const [acct] of accounts) { b.put(layout.OT.encode(acct, height, index), hash); - b.put(layout.OI.encode(acct, time, height, index, hash)); + b.put(layout.OI.encode(acct, time, height, index, hash), null); } } @@ -2229,7 +2259,7 @@ class TXDB { * configurable max of transactions. * @param {Number} acct * @param {Object} options - * @param {Buffer} options.time + * @param {Number} options.time * @param {Number} options.limit * @param {Boolean} options.reverse * @returns {Promise} @@ -2347,7 +2377,7 @@ class TXDB { * and limited at a configurable max of transactions. * @private * @param {Number} acct - * @param {Number} options + * @param {Object} options * @param {Buffer} options.hash * @param {Number} options.limit * @param {Boolean} options.reverse @@ -2455,6 +2485,7 @@ class TXDB { * max transactions. * @param {Number} acct * @param {Object} options + * @param {Number} options.time * @param {Number} options.limit * @param {Boolean} options.reverse * @returns {Promise} @@ -2575,7 +2606,7 @@ class TXDB { * of transactions * @private * @param {Number} acct - * @param {Number} options + * @param {Object} options * @param {Buffer} options.hash * @param {Number} options.limit * @param {Boolean} options.reverse @@ -2644,9 +2675,8 @@ class TXDB { * of that coin that are _not_ confirmed will be removed from * the database. * @private - * @param {Hash} hash - * @param {TX} ref - Reference tx, the tx that double-spent. - * @returns {Promise} - Returns Boolean. + * @param {TXRecord} wtx + * @returns {Promise} */ async removeConflict(wtx) { @@ -2721,7 +2751,7 @@ class TXDB { * Lock balances according to covenant. * Inserting or confirming: TX outputs. * Removing or undoing: Coins spent by the wallet in tx inputs. - * @param {State} state + * @param {BalanceDelta} state * @param {Credit} credit * @param {Path} path * @param {Number} height @@ -2757,7 +2787,7 @@ class TXDB { * Unlock balances according to covenants. * Inserting or confirming: Coins spent by the wallet in TX inputs. * Removing or undoing: TX outputs. - * @param {State} state + * @param {BalanceDelta} state * @param {Credit} credit * @param {Path} path * @param {Number} height @@ -2793,8 +2823,7 @@ class TXDB { * This does not check if the name is owned by the wallet. * @private * @param {Batch} b - * @param {Output} tx - * @param {Path} path + * @param {Output} output * @returns {Promise} */ @@ -3141,9 +3170,6 @@ class TXDB { * Handle reorg'd covenant. * @param {Object} b * @param {TX} tx - * @param {Number} i - * @param {Path} path - * @param {Number} height * @returns {Promise} applied undo. */ @@ -3347,7 +3373,8 @@ class TXDB { /** * Filter array of coins or outpoints * for only unlocked ones. - * @param {Coin[]|Outpoint[]} + * jsdoc can't express this type. + * @param {Coin[]|Outpoint[]} coins * @returns {Coin[]|Outpoint[]} */ @@ -3380,7 +3407,7 @@ class TXDB { * Test whether an account owns a coin. * @param {Number} acct * @param {Hash} hash - * @param {Index} number + * @param {Number} index * @returns {Promise} */ @@ -3538,20 +3565,10 @@ class TXDB { }); } - /** - * Get TX hashes by height. - * @param {Number} height - * @returns {Promise} - */ - - getHeightHashes(height) { - return this.getHeightRangeHashes({ start: height, end: height }); - } - /** * Get unconfirmed transactions. * @param {Number} acct - * @returns {Promise} + * @returns {Promise} */ async getPending(acct) { @@ -3649,7 +3666,7 @@ class TXDB { /** * Get coins. * @param {Number} acct - * @returns {Promise} + * @returns {Promise} */ async getCoins(acct) { @@ -3926,6 +3943,7 @@ class TXDB { /** * Update spent coin height in storage. + * @param {Batch} b * @param {TX} tx - Sending transaction. * @param {Number} index * @param {Number} height @@ -3952,6 +3970,7 @@ class TXDB { /** * Test whether the database has a transaction. * @param {Hash} hash + * @param {Number} index * @returns {Promise} */ @@ -3961,7 +3980,7 @@ class TXDB { /** * Calculate balance. - * @param {Number?} account + * @param {Number} acct * @returns {Promise} */ @@ -4059,7 +4078,7 @@ class TXDB { /** * Dump database (for debugging). - * @returns {bdb.Iterator} + * @returns {Promise} */ async dump() { @@ -4085,7 +4104,7 @@ class Balance extends bio.Struct { /** * Create a balance. * @constructor - * @param {Number} account + * @param {Number} acct */ constructor(acct = -1) { @@ -4134,7 +4153,8 @@ class Balance extends bio.Struct { /** * Serialize balance. - * @returns {Buffer} + * @param {BufioWriter} bw + * @returns {BufioWriter} */ write(bw) { @@ -4149,9 +4169,8 @@ class Balance extends bio.Struct { /** * Inject properties from serialized data. - * @private - * @param {Buffer} data - * @returns {Balance} + * @param {bio.BufferReader} br + * @returns {this} */ read(br) { @@ -4184,7 +4203,7 @@ class Balance extends bio.Struct { /** * Inspect balance. - * @param {String} + * @returns {String} */ format() { @@ -4205,19 +4224,16 @@ class Balance extends bio.Struct { */ class BalanceDelta { - /** @type {Balance} */ - wallet; - - /** @type {Map} */ - accounts; - /** * Create a balance delta. * @constructor */ constructor() { + /** @type {Balance} */ this.wallet = new Balance(); + + /** @type {Map} */ this.accounts = new Map(); } @@ -4225,6 +4241,10 @@ class BalanceDelta { return this.wallet.tx !== 0; } + /** + * @param {Balance} balance + */ + applyTo(balance) { this.wallet.applyTo(balance); } @@ -4284,8 +4304,8 @@ class Credit extends bio.Struct { /** * Create a credit. * @constructor - * @param {Coin} coin - * @param {Boolean?} spent + * @param {Coin?} [coin] + * @param {Boolean?} [spent] */ constructor(coin, spent) { @@ -4297,8 +4317,8 @@ class Credit extends bio.Struct { /** * Inject properties from serialized data. - * @private - * @param {Buffer} data + * @param {bio.BufferReader} br + * @returns {this} */ read(br) { @@ -4319,7 +4339,8 @@ class Credit extends bio.Struct { /** * Serialize credit. - * @returns {Buffer} + * @param {BufioWriter} bw + * @returns {BufioWriter} */ write(bw) { @@ -4334,6 +4355,7 @@ class Credit extends bio.Struct { * @private * @param {TX} tx * @param {Number} index + * @param {Number} height * @returns {Credit} */ @@ -4348,6 +4370,7 @@ class Credit extends bio.Struct { * Instantiate credit from transaction. * @param {TX} tx * @param {Number} index + * @param {Number} height * @returns {Credit} */ @@ -4447,6 +4470,7 @@ class Details { /** * Calculate confirmations. + * @param {Number} height * @returns {Number} */ @@ -4468,7 +4492,7 @@ class Details { /** * Calculate fee. Only works if wallet * owns all inputs. Returns 0 otherwise. - * @returns {Amount} + * @returns {AmountValue} */ getFee() { @@ -4491,7 +4515,7 @@ class Details { /** * Calculate fee rate. Only works if wallet * owns all inputs. Returns 0 otherwise. - * @param {Amount} fee + * @param {AmountValue} fee * @returns {Rate} */ @@ -4501,6 +4525,8 @@ class Details { /** * Convert details to a more json-friendly object. + * @param {(Network|NetworkType)?} [network] + * @param {Number} [height] * @returns {Object} */ @@ -4572,7 +4598,7 @@ class DetailsMember { /** * Convert the member to a more json-friendly object. - * @param {Network} network + * @param {(Network|NetworkType)?} [network] * @returns {Object} */ @@ -4601,9 +4627,9 @@ class BlockRecord extends bio.Struct { /** * Create a block record. * @constructor - * @param {Hash} hash - * @param {Number} height - * @param {Number} time + * @param {Hash} [hash] + * @param {Number} [height] + * @param {Number} [time] */ constructor(hash, height, time) { @@ -4641,8 +4667,8 @@ class BlockRecord extends bio.Struct { /** * Instantiate wallet block from serialized tip data. - * @private * @param {bio.BufferReader} br + * @returns {this} */ read(br) { @@ -4671,7 +4697,8 @@ class BlockRecord extends bio.Struct { /** * Serialize the wallet block as a tip (hash and height). - * @returns {Buffer} + * @param {BufioWriter} bw + * @returns {BufioWriter} */ write(bw) { @@ -4751,10 +4778,19 @@ class BlindBid extends bio.Struct { this.own = false; } + /** + * @returns {Number} + */ + getSize() { return 1 + this.name.length + 45; } + /** + * @param {BufioWriter} bw + * @returns {BufioWriter} + */ + write(bw) { let height = this.height; @@ -4770,6 +4806,11 @@ class BlindBid extends bio.Struct { return bw; } + /** + * @param {bio.BufferReader} br + * @returns {this} + */ + read(br) { this.name = br.readBytes(br.readU8()); this.lockup = br.readU64(); @@ -4812,12 +4853,22 @@ class BlindValue extends bio.Struct { return 40; } + /** + * @param {BufioWriter} bw + * @returns {BufioWriter} + */ + write(bw) { bw.writeU64(this.value); bw.writeBytes(this.nonce); return bw; } + /** + * @param {bio.BufferReader} br + * @returns {this} + */ + read(br) { this.value = br.readU64(); this.nonce = br.readBytes(32); @@ -4852,6 +4903,11 @@ class BidReveal extends bio.Struct { return 1 + this.name.length + 13 + 36; } + /** + * @param {BufioWriter} bw + * @returns {BufioWriter} + */ + write(bw) { let height = this.height; @@ -4868,6 +4924,11 @@ class BidReveal extends bio.Struct { return bw; } + /** + * @param {bio.BufferReader} br + * @returns {this} + */ + read(br) { this.name = br.readBytes(br.readU8()); this.value = br.readU64(); @@ -4906,13 +4967,13 @@ class TXCount { /** * Create tx count record. * @constructor - * @param {Number} height - * @param {Number} index + * @param {Number} [height] + * @param {Number} [index] */ constructor(height, index) { - this.height = height; - this.index = index; + this.height = height || 0; + this.index = index || 0; } /** @@ -4962,7 +5023,7 @@ class TXCount { function fromU32(num) { const data = Buffer.allocUnsafe(4); - data.writeUInt32LE(num, 0, true); + data.writeUInt32LE(num, 0); return data; } diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index 64e1be887..32f85a70f 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -48,11 +48,18 @@ const Outpoint = require('../primitives/outpoint'); /** @typedef {import('../types').Hash} Hash */ /** @typedef {import('../types').Amount} Amount */ /** @typedef {import('../types').Rate} Rate */ +/** @typedef {import('../covenants/namestate')} NameState */ +/** @typedef {import('../primitives/tx')} TX */ /** @typedef {import('./records').BlockMeta} BlockMeta */ /** @typedef {import('./records').TXRecord} TXRecord */ -/** @typedef {import('../primitives/tx')} TX */ /** @typedef {import('./txdb').BlockExtraInfo} BlockExtraInfo */ /** @typedef {import('./txdb').Details} Details */ +/** @typedef {import('./txdb').Credit} Credit */ +/** @typedef {import('./txdb').Balance} Balance */ +/** @typedef {import('./txdb').BlindBid} BlindBid */ +/** @typedef {import('./txdb').BidReveal} BidReveal */ +/** @typedef {import('./txdb').BlindValue} BlindValue */ +/** @typedef {import('./txdb').BlockRecord} BlockRecord */ /** @typedef {import('./walletdb')} WalletDB */ /* @@ -756,7 +763,7 @@ class Wallet extends EventEmitter { /** * Get all wallet address hashes. * @param {(String|Number)?} acct - * @returns {Promise} - Returns Array. + * @returns {Promise} */ getAddressHashes(acct) { @@ -769,6 +776,7 @@ class Wallet extends EventEmitter { * Get all account address hashes. * @param {String|Number} acct * @returns {Promise} - Returns Array. + * @throws on non-existent account */ async getAccountHashes(acct) { @@ -822,8 +830,8 @@ class Wallet extends EventEmitter { /** * Lookup the corresponding account name's index. - * @param {String|Number} acct - Account name/index. - * @returns {Promise} - Returns Number. + * @param {(String|Number)?} [acct] - Account name/index. + * @returns {Promise} * @throws on non-existent account */ @@ -983,7 +991,7 @@ class Wallet extends EventEmitter { /** * Test whether the wallet contains a path. * @param {Address|Hash} address - * @returns {Promise} - Returns {Boolean}. + * @returns {Promise} */ async hasPath(address) { @@ -1501,6 +1509,7 @@ class Wallet extends EventEmitter { * Create and send a claim MTX. * @param {String} name * @param {Object} options + * @returns {Promise} */ async createClaim(name, options) { @@ -1583,7 +1592,7 @@ class Wallet extends EventEmitter { * Make a claim proof. * @param {String} name * @param {Object} options - * @returns {Claim} + * @returns {Promise} */ async makeClaim(name, options) { @@ -1630,6 +1639,7 @@ class Wallet extends EventEmitter { * Create and send a claim proof. * @param {String} name * @param {Object} options + * @returns {Promise} */ async _sendClaim(name, options) { @@ -1642,6 +1652,7 @@ class Wallet extends EventEmitter { * Create and send a claim proof. * @param {String} name * @param {Object} options + * @returns {Promise} */ async sendClaim(name, options) { @@ -1657,7 +1668,7 @@ class Wallet extends EventEmitter { * Make a open MTX. * @param {String} name * @param {Number|String} acct - * @param {MTX?} mtx + * @param {MTX?} [mtx] * @returns {Promise} */ @@ -1708,6 +1719,7 @@ class Wallet extends EventEmitter { if (!mtx) mtx = new MTX(); + mtx.outputs.push(output); if (await this.txdb.isDoubleOpen(mtx)) @@ -1785,8 +1797,8 @@ class Wallet extends EventEmitter { * @param {Number} value * @param {Number} lockup * @param {Number|String} acct - * @param {MTX?} mtx - * @param {Address?} addr + * @param {MTX?} [mtx] + * @param {Address?} [addr] * @returns {Promise} */ @@ -1849,7 +1861,7 @@ class Wallet extends EventEmitter { * @param {Number} value * @param {Number} lockup * @param {Object} options - * @returns {MTX} + * @returns {Promise} */ async _createBid(name, value, lockup, options) { @@ -1866,7 +1878,7 @@ class Wallet extends EventEmitter { * @param {Number} value * @param {Number} lockup * @param {Object} options - * @returns {MTX} + * @returns {Promise} */ async createBid(name, value, lockup, options) { @@ -1898,6 +1910,7 @@ class Wallet extends EventEmitter { * @param {Number} value * @param {Number} lockup * @param {Object} options + * @returns {Promise} */ async sendBid(name, value, lockup, options) { @@ -1909,6 +1922,12 @@ class Wallet extends EventEmitter { } } + /** + * @typedef {Object} CreateAuctionResults + * @param {MTX} bid + * @param {MTX} reveal + */ + /** * Create and finalize a bid & a reveal (in advance) * MTX with a lock. @@ -1916,9 +1935,7 @@ class Wallet extends EventEmitter { * @param {Number} value * @param {Number} lockup * @param {Object} options - * @returns {Object} output - * @returns {MTX} output.bid - * @returns {MTX} output.reveal + * @returns {Promise} */ async createAuctionTXs(name, value, lockup, options) { @@ -1937,9 +1954,7 @@ class Wallet extends EventEmitter { * @param {Number} value * @param {Number} lockup * @param {Object} options - * @returns {Object} output - * @returns {MTX} output.bid - * @returns {MTX} output.reveal + * @returns {Promise} */ async _createAuctionTXs(name, value, lockup, options) { @@ -1984,17 +1999,19 @@ class Wallet extends EventEmitter { /** * Make a reveal MTX. * @param {String} name - * @param {(Number|String)?} acct - * @param {MTX?} mtx + * @param {(Number|String)?} [acct] + * @param {MTX?} [mtx] * @returns {Promise} */ async makeReveal(name, acct, mtx) { assert(typeof name === 'string'); + let acctno; + if (acct != null) { assert((acct >>> 0) === acct || typeof acct === 'string'); - acct = await this.getAccountIndex(acct); + acctno = await this.getAccountIndex(acct); } if (!rules.verifyName(name)) @@ -2035,8 +2052,10 @@ class Wallet extends EventEmitter { if (!coin) continue; - if (acct != null && !await this.txdb.hasCoinByAccount(acct, hash, index)) - continue; + if (acctno != null) { + if (!await this.txdb.hasCoinByAccount(acctno, hash, index)) + continue; + } // Is local? if (coin.height < ns.height) @@ -2073,7 +2092,7 @@ class Wallet extends EventEmitter { * MTX without a lock. * @param {String} name * @param {Object} options - * @returns {MTX} + * @returns {Promise} */ async _createReveal(name, options) { @@ -2088,7 +2107,7 @@ class Wallet extends EventEmitter { * MTX with a lock. * @param {String} name * @param {Object} options - * @returns {MTX} + * @returns {Promise} */ async createReveal(name, options) { @@ -2104,6 +2123,7 @@ class Wallet extends EventEmitter { * Create and send a reveal MTX. * @param {String} name * @param {Object} options + * @returns {Promise} */ async _sendReveal(name, options) { @@ -2116,6 +2136,7 @@ class Wallet extends EventEmitter { * Create and send a bid MTX. * @param {String} name * @param {Object} options + * @returns {Promise} */ async sendReveal(name, options) { @@ -2129,8 +2150,8 @@ class Wallet extends EventEmitter { /** * Make a reveal MTX. - * @param {MTX?} mtx - * @param {Number?} witnessSize + * @param {MTX?} [mtx] + * @param {Number?} [witnessSize] * @returns {Promise} */ @@ -2138,6 +2159,7 @@ class Wallet extends EventEmitter { const height = this.wdb.height + 1; const network = this.network; const bids = await this.getBids(); + if (!mtx) mtx = new MTX(); else @@ -2208,7 +2230,7 @@ class Wallet extends EventEmitter { * Create and finalize a reveal all * MTX without a lock. * @param {Object} options - * @returns {MTX} + * @returns {Promise} */ async _createRevealAll(options) { @@ -2221,7 +2243,7 @@ class Wallet extends EventEmitter { * Create and finalize a reveal all * MTX with a lock. * @param {Object} options - * @returns {MTX} + * @returns {Promise} */ async createRevealAll(options) { @@ -2236,6 +2258,7 @@ class Wallet extends EventEmitter { /** * Create and send a reveal all MTX. * @param {Object} options + * @returns {Promise} */ async _sendRevealAll(options) { @@ -2247,6 +2270,7 @@ class Wallet extends EventEmitter { /** * Create and send a bid MTX. * @param {Object} options + * @returns {Promise} */ async sendRevealAll(options) { @@ -2261,8 +2285,8 @@ class Wallet extends EventEmitter { /** * Make a redeem MTX. * @param {String} name - * @param {(Number|String)?} acct - * @param {MTX?} mtx + * @param {(Number|String)?} [acct] + * @param {MTX?} [mtx] * @returns {Promise} */ @@ -2272,9 +2296,11 @@ class Wallet extends EventEmitter { if (!rules.verifyName(name)) throw new Error(`Invalid name: ${name}.`); + let acctno; + if (acct != null) { assert((acct >>> 0) === acct || typeof acct === 'string'); - acct = await this.getAccountIndex(acct); + acctno = await this.getAccountIndex(acct); } const rawName = Buffer.from(name, 'ascii'); @@ -2313,8 +2339,10 @@ class Wallet extends EventEmitter { if (!coin) continue; - if (acct != null && !await this.txdb.hasCoinByAccount(acct, hash, index)) - continue; + if (acctno != null) { + if (!await this.txdb.hasCoinByAccount(acctno, hash, index)) + continue; + } // Is local? if (coin.height < ns.height) @@ -2342,7 +2370,7 @@ class Wallet extends EventEmitter { * MTX without a lock. * @param {String} name * @param {Object} options - * @returns {MTX} + * @returns {Promise} */ async _createRedeem(name, options) { @@ -2357,7 +2385,7 @@ class Wallet extends EventEmitter { * MTX with a lock. * @param {String} name * @param {Object} options - * @returns {MTX} + * @returns {Promise} */ async createRedeem(name, options) { @@ -2374,6 +2402,7 @@ class Wallet extends EventEmitter { * MTX without a lock. * @param {String} name * @param {Object} options + * @returns {Promise} */ async _sendRedeem(name, options) { @@ -2387,6 +2416,7 @@ class Wallet extends EventEmitter { * MTX with a lock. * @param {String} name * @param {Object} options + * @returns {Promise} */ async sendRedeem(name, options) { @@ -2400,9 +2430,8 @@ class Wallet extends EventEmitter { /** * Make a redeem MTX. - * @param {String} name - * @param {MTX?} mtx - * @param {Number?} witnessSize + * @param {MTX?} [mtx] + * @param {Number?} [witnessSize] * @returns {Promise} */ @@ -2410,6 +2439,7 @@ class Wallet extends EventEmitter { const height = this.wdb.height + 1; const network = this.network; const reveals = await this.txdb.getReveals(); + if (!mtx) mtx = new MTX(); else @@ -2474,7 +2504,7 @@ class Wallet extends EventEmitter { * Create and finalize a redeem * all MTX without a lock. * @param {Object} options - * @returns {MTX} + * @returns {Promise} */ async _createRedeemAll(options) { @@ -2487,7 +2517,7 @@ class Wallet extends EventEmitter { * Create and finalize a redeem * all MTX with a lock. * @param {Object} options - * @returns {MTX} + * @returns {Promise} */ async createRedeemAll(options) { @@ -2503,6 +2533,7 @@ class Wallet extends EventEmitter { * Create and send a redeem all * MTX without a lock. * @param {Object} options + * @returns {Promise} */ async _sendRedeemAll(options) { @@ -2515,6 +2546,7 @@ class Wallet extends EventEmitter { * Create and send a redeem all * MTX with a lock. * @param {Object} options + * @returns {Promise} */ async sendRedeemAll(options) { @@ -2531,8 +2563,8 @@ class Wallet extends EventEmitter { * @private * @param {String} name * @param {Resource?} resource - * @param {MTX?} mtx - * @returns {MTX} + * @param {MTX?} [mtx] + * @returns {Promise} */ async _makeRegister(name, resource, mtx) { @@ -2615,7 +2647,7 @@ class Wallet extends EventEmitter { * @param {String} name * @param {Resource} resource * @param {(Number|String)?} acct - * @param {MTX?} mtx + * @param {MTX?} [mtx] * @returns {Promise} */ @@ -2626,9 +2658,11 @@ class Wallet extends EventEmitter { if (!rules.verifyName(name)) throw new Error('Invalid name.'); + let acctno; + if (acct != null) { assert((acct >>> 0) === acct || typeof acct === 'string'); - acct = await this.getAccountIndex(acct); + acctno = await this.getAccountIndex(acct); } const rawName = Buffer.from(name, 'ascii'); @@ -2649,8 +2683,10 @@ class Wallet extends EventEmitter { if (credit.spent) throw new Error(`Credit is already pending for: ${name}.`); - if (acct != null && !await this.txdb.hasCoinByAccount(acct, hash, index)) - throw new Error(`Account does not own name: ${name}.`); + if (acctno != null) { + if (!await this.txdb.hasCoinByAccount(acctno, hash, index)) + throw new Error(`Account does not own name: ${name}.`); + } const coin = credit.coin; @@ -2698,7 +2734,7 @@ class Wallet extends EventEmitter { * @param {String} name * @param {Resource} resource * @param {Object} options - * @returns {MTX} + * @returns {Promise} */ async _createUpdate(name, resource, options) { @@ -2714,7 +2750,7 @@ class Wallet extends EventEmitter { * @param {String} name * @param {Resource} resource * @param {Object} options - * @returns {MTX} + * @returns {Promise} */ async createUpdate(name, resource, options) { @@ -2732,6 +2768,7 @@ class Wallet extends EventEmitter { * @param {String} name * @param {Resource} resource * @param {Object} options + * @returns {Promise} */ async _sendUpdate(name, resource, options) { @@ -2746,6 +2783,7 @@ class Wallet extends EventEmitter { * @param {String} name * @param {Resource} resource * @param {Object} options + * @returns {Promise} */ async sendUpdate(name, resource, options) { @@ -2762,7 +2800,7 @@ class Wallet extends EventEmitter { * @private * @param {String} name * @param {(Number|String)?} acct - * @param {MTX?} mtx + * @param {MTX?} [mtx] * @returns {Promise} */ @@ -2772,9 +2810,11 @@ class Wallet extends EventEmitter { if (!rules.verifyName(name)) throw new Error(`Invalid name: ${name}.`); + let acctno; + if (acct != null) { assert((acct >>> 0) === acct || typeof acct === 'string'); - acct = await this.getAccountIndex(acct); + acctno = await this.getAccountIndex(acct); } const rawName = Buffer.from(name, 'ascii'); @@ -2805,8 +2845,10 @@ class Wallet extends EventEmitter { if (coin.height < ns.height) throw new Error(`Wallet does not own name: ${name}.`); - if (acct != null && !await this.txdb.hasCoinByAccount(acct, hash, index)) - throw new Error(`Account does not own name: ${name}.`); + if (acctno != null) { + if (!await this.txdb.hasCoinByAccount(acctno, hash, index)) + throw new Error(`Account does not own name: ${name}.`); + } if (!ns.isClosed(height, network)) throw new Error(`Auction is not yet closed: ${name}.`); @@ -2921,7 +2963,7 @@ class Wallet extends EventEmitter { * MTX without a lock. * @param {String} name * @param {Object} options - * @returns {MTX} + * @returns {Promise} */ async _createRenewal(name, options) { @@ -2936,7 +2978,7 @@ class Wallet extends EventEmitter { * MTX with a lock. * @param {String} name * @param {Object} options - * @returns {MTX} + * @returns {Promise} */ async createRenewal(name, options) { @@ -2953,6 +2995,7 @@ class Wallet extends EventEmitter { * MTX without a lock. * @param {String} name * @param {Object} options + * @returns {Promise} */ async _sendRenewal(name, options) { @@ -2966,6 +3009,7 @@ class Wallet extends EventEmitter { * MTX with a lock. * @param {String} name * @param {Object} options + * @returns {Promise} */ async sendRenewal(name, options) { @@ -2982,7 +3026,7 @@ class Wallet extends EventEmitter { * @param {String} name * @param {Address} address * @param {(Number|String)?} acct - * @param {MTX?} mtx + * @param {MTX?} [mtx] * @returns {Promise} */ @@ -2993,9 +3037,11 @@ class Wallet extends EventEmitter { if (!rules.verifyName(name)) throw new Error(`Invalid name: ${name}.`); + let acctno; + if (acct != null) { assert((acct >>> 0) === acct || typeof acct === 'string'); - acct = await this.getAccountIndex(acct); + acctno = await this.getAccountIndex(acct); } const rawName = Buffer.from(name, 'ascii'); @@ -3025,8 +3071,10 @@ class Wallet extends EventEmitter { if (coin.height < ns.height) throw new Error(`Wallet does not own name: ${name}.`); - if (acct != null && !await this.txdb.hasCoinByAccount(acct, hash, index)) - throw new Error(`Account does not own name: ${name}.`); + if (acctno != null) { + if (!await this.txdb.hasCoinByAccount(acctno, hash, index)) + throw new Error(`Account does not own name: ${name}.`); + } if (!ns.isClosed(height, network)) throw new Error(`Auction is not yet closed: ${name}.`); @@ -3060,7 +3108,7 @@ class Wallet extends EventEmitter { * @param {String} name * @param {Address} address * @param {Object} options - * @returns {MTX} + * @returns {Promise} */ async _createTransfer(name, address, options) { @@ -3076,7 +3124,7 @@ class Wallet extends EventEmitter { * @param {String} name * @param {Address} address * @param {Object} options - * @returns {MTX} + * @returns {Promise} */ async createTransfer(name, address, options) { @@ -3094,6 +3142,7 @@ class Wallet extends EventEmitter { * @param {String} name * @param {Address} address * @param {Object} options + * @returns {Promise} */ async _sendTransfer(name, address, options) { @@ -3112,6 +3161,7 @@ class Wallet extends EventEmitter { * @param {String} name * @param {Address} address * @param {Object} options + * @returns {Promise} */ async sendTransfer(name, address, options) { @@ -3128,7 +3178,7 @@ class Wallet extends EventEmitter { * @private * @param {String} name * @param {(Number|String)?} acct - * @param {MTX?} mtx + * @param {MTX?} [mtx] * @returns {Promise} */ @@ -3138,9 +3188,11 @@ class Wallet extends EventEmitter { if (!rules.verifyName(name)) throw new Error(`Invalid name: ${name}.`); + let acctno; + if (acct != null) { assert((acct >>> 0) === acct || typeof acct === 'string'); - acct = await this.getAccountIndex(acct); + acctno = await this.getAccountIndex(acct); } const rawName = Buffer.from(name, 'ascii'); @@ -3165,8 +3217,10 @@ class Wallet extends EventEmitter { if (coin.height < ns.height) throw new Error(`Wallet does not own name: ${name}.`); - if (acct != null && !await this.txdb.hasCoinByAccount(acct, hash, index)) - throw new Error(`Account does not own name: ${name}.`); + if (acctno != null) { + if (!await this.txdb.hasCoinByAccount(acctno, hash, index)) + throw new Error(`Account does not own name: ${name}.`); + } if (!ns.isClosed(height, network)) throw new Error(`Auction is not yet closed: ${name}.`); @@ -3192,7 +3246,7 @@ class Wallet extends EventEmitter { * MTX without a lock. * @param {String} name * @param {Object} options - * @returns {MTX} + * @returns {Promise} */ async _createCancel(name, options) { @@ -3207,7 +3261,7 @@ class Wallet extends EventEmitter { * MTX with a lock. * @param {String} name * @param {Object} options - * @returns {MTX} + * @returns {Promise} */ async createCancel(name, options) { @@ -3224,6 +3278,7 @@ class Wallet extends EventEmitter { * MTX without a lock. * @param {String} name * @param {Object} options + * @returns {Promise} */ async _sendCancel(name, options) { @@ -3237,6 +3292,7 @@ class Wallet extends EventEmitter { * MTX with a lock. * @param {String} name * @param {Object} options + * @returns {Promise} */ async sendCancel(name, options) { @@ -3253,7 +3309,7 @@ class Wallet extends EventEmitter { * @private * @param {String} name * @param {(Number|String)?} acct - * @param {MTX?} mtx + * @param {MTX?} [mtx] * @returns {Promise} */ @@ -3263,9 +3319,11 @@ class Wallet extends EventEmitter { if (!rules.verifyName(name)) throw new Error(`Invalid name: ${name}.`); + let acctno; + if (acct != null) { assert((acct >>> 0) === acct || typeof acct === 'string'); - acct = await this.getAccountIndex(acct); + acctno = await this.getAccountIndex(acct); } const rawName = Buffer.from(name, 'ascii'); @@ -3295,8 +3353,10 @@ class Wallet extends EventEmitter { if (coin.height < ns.height) throw new Error(`Wallet does not own name: ${name}.`); - if (acct != null && !await this.txdb.hasCoinByAccount(acct, hash, index)) - throw new Error(`Account does not own name: ${name}.`); + if (acctno != null) { + if (!await this.txdb.hasCoinByAccount(acctno, hash, index)) + throw new Error(`Account does not own name: ${name}.`); + } if (!ns.isClosed(height, network)) throw new Error(`Auction is not yet closed: ${name}.`); @@ -3420,7 +3480,7 @@ class Wallet extends EventEmitter { * MTX without a lock. * @param {String} name * @param {Object} options - * @returns {MTX} + * @returns {Promise} */ async _createFinalize(name, options) { @@ -3435,7 +3495,7 @@ class Wallet extends EventEmitter { * MTX with a lock. * @param {String} name * @param {Object} options - * @returns {MTX} + * @returns {Promise} */ async createFinalize(name, options) { @@ -3452,6 +3512,7 @@ class Wallet extends EventEmitter { * MTX without a lock. * @param {String} name * @param {Object} options + * @returns {Promise} */ async _sendFinalize(name, options) { @@ -3465,6 +3526,7 @@ class Wallet extends EventEmitter { * MTX with a lock. * @param {String} name * @param {Object} options + * @returns {Promise} */ async sendFinalize(name, options) { @@ -3480,7 +3542,7 @@ class Wallet extends EventEmitter { * Make a revoke MTX. * @param {String} name * @param {(Number|String)?} acct - * @param {MTX?} mtx + * @param {MTX?} [mtx] * @returns {Promise} */ @@ -3490,9 +3552,11 @@ class Wallet extends EventEmitter { if (!rules.verifyName(name)) throw new Error(`Invalid name: ${name}.`); + let acctno; + if (acct != null) { assert((acct >>> 0) === acct || typeof acct === 'string'); - acct = await this.getAccountIndex(acct); + acctno = await this.getAccountIndex(acct); } const rawName = Buffer.from(name, 'ascii'); @@ -3513,8 +3577,10 @@ class Wallet extends EventEmitter { if (credit.spent) throw new Error(`Credit is already pending for: ${name}.`); - if (acct != null && !await this.txdb.hasCoinByAccount(acct, hash, index)) - throw new Error(`Account does not own name: ${name}.`); + if (acctno != null) { + if (!await this.txdb.hasCoinByAccount(acctno, hash, index)) + throw new Error(`Account does not own name: ${name}.`); + } const coin = credit.coin; @@ -3555,7 +3621,7 @@ class Wallet extends EventEmitter { * MTX without a lock. * @param {String} name * @param {Object} options - * @returns {MTX} + * @returns {Promise} */ async _createRevoke(name, options) { @@ -3570,7 +3636,7 @@ class Wallet extends EventEmitter { * MTX with a lock. * @param {String} name * @param {Object} options - * @returns {MTX} + * @returns {Promise} */ async createRevoke(name, options) { @@ -3587,6 +3653,7 @@ class Wallet extends EventEmitter { * MTX without a lock. * @param {String} name * @param {Object} options + * @returns {Promise} */ async _sendRevoke(name, options) { @@ -3600,6 +3667,7 @@ class Wallet extends EventEmitter { * MTX with a lock. * @param {String} name * @param {Object} options + * @returns {Promise} */ async sendRevoke(name, options) { @@ -3686,7 +3754,7 @@ class Wallet extends EventEmitter { /** * Make a transaction with normal outputs. * @param {Object[]} outputs - See {@link MTX#addOutput} - * @param {MTX} [mtx=null] - MTX to modify instead of new one. + * @param {MTX?} [mtx] - MTX to modify instead of new one. * @returns {MTX} - MTX with populated outputs. */ @@ -3815,18 +3883,23 @@ class Wallet extends EventEmitter { } case 'OPEN': { assert(action.length === 1, 'Bad arguments for OPEN.'); - await this.makeOpen(...action, acct, mtx); + const name = action[0]; + await this.makeOpen(name, acct, mtx); break; } case 'BID': { assert(action.length === 3, 'Bad arguments for BID.'); const address = account.deriveReceive(receiveIndex++).getAddress(); - await this.makeBid(...action, acct, mtx, address); + const name = action[0]; + const value = action[1]; + const lockup = action[2]; + await this.makeBid(name, value, lockup, acct, mtx, address); break; } case 'REVEAL': { if (action.length === 1) { - await this.makeReveal(...action, acct, mtx); + const name = action[0]; + await this.makeReveal(name, acct, mtx); break; } @@ -3836,7 +3909,8 @@ class Wallet extends EventEmitter { } case 'REDEEM': { if (action.length === 1) { - await this.makeRedeem(...action, acct, mtx); + const name = action[0]; + await this.makeRedeem(name, acct, mtx); break; } @@ -3846,12 +3920,15 @@ class Wallet extends EventEmitter { } case 'UPDATE': { assert(action.length === 2, 'Bad arguments for UPDATE.'); - await this.makeUpdate(...action, acct, mtx); + const name = action[0]; + const resource = action[1]; + await this.makeUpdate(name, resource, acct, mtx); break; } case 'RENEW': { if (action.length === 1) { - await this.makeRenewal(...action, acct, mtx); + const name = action[0]; + await this.makeRenewal(name, acct, mtx); break; } @@ -3861,12 +3938,15 @@ class Wallet extends EventEmitter { } case 'TRANSFER': { assert(action.length === 2, 'Bad arguments for TRANSFER.'); - await this.makeTransfer(...action, acct, mtx); + const name = action[0]; + const address = action[1]; + await this.makeTransfer(name, address, acct, mtx); break; } case 'FINALIZE': { if (action.length === 1) { - await this.makeFinalize(...action, acct, mtx); + const name = action[0]; + await this.makeFinalize(name, acct, mtx); break; } @@ -3876,12 +3956,14 @@ class Wallet extends EventEmitter { } case 'CANCEL': { assert(action.length === 1, 'Bad arguments for CANCEL.'); - await this.makeCancel(...action, acct, mtx); + const name = action[0]; + await this.makeCancel(name, acct, mtx); break; } case 'REVOKE': { assert(action.length === 1, 'Bad arguments for REVOKE.'); - await this.makeRevoke(...action, acct, mtx); + const name = action[0]; + await this.makeRevoke(name, acct, mtx); break; } default: @@ -3958,7 +4040,7 @@ class Wallet extends EventEmitter { * Make a batch transaction with multiple actions. * @param {Array} actions * @param {Object} options - * @returns {MTX} + * @returns {Promise} */ async createBatch(actions, options) { @@ -3974,7 +4056,7 @@ class Wallet extends EventEmitter { * Create and send a batch transaction with multiple actions. * @param {Array} actions * @param {Object} options - * @returns {TX} + * @returns {Promise} */ async _sendBatch(actions, options) { @@ -3987,7 +4069,7 @@ class Wallet extends EventEmitter { * Create and send a batch transaction with multiple actions. * @param {Array} actions * @param {Object} options - * @returns {TX} + * @returns {Promise} */ async sendBatch(actions, options) { @@ -4068,7 +4150,8 @@ class Wallet extends EventEmitter { * coins from being double spent. * @param {Object} options - See {@link Wallet#fund options}. * @param {Object[]} options.outputs - See {@link MTX#addOutput}. - * @returns {Promise} - Returns {@link TX}. + * @param {String} options.passphrase + * @returns {Promise} */ async send(options) { @@ -4085,7 +4168,8 @@ class Wallet extends EventEmitter { * @private * @param {Object} options - See {@link Wallet#fund options}. * @param {Object[]} options.outputs - See {@link MTX#addOutput}. - * @returns {Promise} - Returns {@link TX}. + * @param {String} options.passphrase + * @returns {Promise} */ async _send(options) { @@ -4163,7 +4247,7 @@ class Wallet extends EventEmitter { * @param {Hash} hash * @param {Rate} rate * @param {(String|Buffer)?} passphrase - * @returns {Promise} - Returns {@link TX}. + * @returns {Promise} */ async increaseFee(hash, rate, passphrase) { @@ -4285,8 +4369,7 @@ class Wallet extends EventEmitter { /** * Derive necessary addresses for signing a transaction. * @param {MTX} mtx - * @param {Number?} index - Input index. - * @returns {Promise} - Returns {@link WalletKey}[]. + * @returns {Promise} */ async deriveInputs(mtx) { @@ -4312,7 +4395,7 @@ class Wallet extends EventEmitter { /** * Retrieve a single keyring by address. - * @param {Address|Hash} hash + * @param {Address|Hash} address * @returns {Promise} */ @@ -4349,7 +4432,7 @@ class Wallet extends EventEmitter { /** * Retrieve a single keyring by address * (with the private key reference). - * @param {Address|Hash} hash + * @param {Address|Hash} address * @param {(Buffer|String)?} passphrase * @returns {Promise} */ @@ -4403,7 +4486,7 @@ class Wallet extends EventEmitter { /** * Map output addresses to paths. * @param {TX} tx - * @returns {Promise} - Returns {@link Path}[]. + * @returns {Promise} */ async getOutputPaths(tx) { @@ -4487,8 +4570,7 @@ class Wallet extends EventEmitter { * sign, only creates signature slots). Only builds scripts * for inputs that are redeemable by this wallet. * @param {MTX} mtx - * @returns {Promise} - Returns Number - * (total number of scripts built). + * @returns {Promise} - total number of scripts built. */ async template(mtx) { @@ -4499,8 +4581,8 @@ class Wallet extends EventEmitter { /** * Build input scripts and sign inputs for a transaction. Only attempts * to build/sign inputs that are redeemable by this wallet. - * @param {MTX} tx - * @param {Object|String|Buffer} options - Options or passphrase. + * @param {MTX} mtx + * @param {String|Buffer} passphrase * @returns {Promise} - Returns Number (total number * of inputs scripts built and signed). */ @@ -4519,7 +4601,7 @@ class Wallet extends EventEmitter { /** * Get pending ancestors up to the policy limit * @param {TX} tx - * @returns {Promise} - Returns {BufferSet} with Hash + * @returns {Promise} - Returns {BufferSet} with Hash */ async getPendingAncestors(tx) { @@ -4529,8 +4611,8 @@ class Wallet extends EventEmitter { /** * Get pending ancestors up to the policy limit. * @param {TX} tx - * @param {Object} set - * @returns {Promise} - Returns {BufferSet} with Hash + * @param {BufferSet} set + * @returns {Promise} */ async _getPendingAncestors(tx, set) { @@ -4561,7 +4643,7 @@ class Wallet extends EventEmitter { /** * Test whether the database has a pending transaction. * @param {Hash} hash - * @returns {Promise} - Returns Boolean. + * @returns {Promise} */ hasPending(hash) { @@ -4571,7 +4653,7 @@ class Wallet extends EventEmitter { /** * Get a coin viewpoint. * @param {TX} tx - * @returns {Promise} - Returns {@link CoinView}. + * @returns {Promise} */ getCoinView(tx) { @@ -4581,8 +4663,8 @@ class Wallet extends EventEmitter { /** * Get a wallet coin viewpoint with HD paths. * @param {TX} tx - * @param {CoinView?} view - Coins to be used in wallet coin viewpoint. - * @returns {Promise} - Returns {@link WalletCoinView}. + * @param {CoinView?} [view] - Coins to be used in wallet coin viewpoint. + * @returns {Promise} */ async getWalletCoinView(tx, view) { @@ -4592,11 +4674,11 @@ class Wallet extends EventEmitter { if (!tx.hasCoins(view)) view = await this.txdb.getCoinView(tx); - view = WalletCoinView.fromCoinView(view); + const wview = WalletCoinView.fromCoinView(view); for (const input of tx.inputs) { const prevout = input.prevout; - const coin = view.getCoin(prevout); + const coin = wview.getCoin(prevout); if (!coin) continue; @@ -4627,16 +4709,16 @@ class Wallet extends EventEmitter { path.account ^= HD.common.HARDENED; // Add path to the viewpoint. - view.addPath(prevout, path); + wview.addPath(prevout, path); } - return view; + return wview; } /** * Get a historical coin viewpoint. * @param {TX} tx - * @returns {Promise} - Returns {@link CoinView}. + * @returns {Promise} */ getSpentView(tx) { @@ -4646,7 +4728,7 @@ class Wallet extends EventEmitter { /** * Convert transaction to transaction details. * @param {TXRecord} wtx - * @returns {Promise} - Returns {@link Details}. + * @returns {Promise
} */ toDetails(wtx) { @@ -4656,7 +4738,7 @@ class Wallet extends EventEmitter { /** * Get transaction details. * @param {Hash} hash - * @returns {Promise} - Returns {@link Details}. + * @returns {Promise
} */ getDetails(hash) { @@ -4667,7 +4749,7 @@ class Wallet extends EventEmitter { * Get a coin from the wallet. * @param {Hash} hash * @param {Number} index - * @returns {Promise} - Returns {@link Coin}. + * @returns {Promise} */ getCoin(hash, index) { @@ -4678,7 +4760,7 @@ class Wallet extends EventEmitter { * Get an unspent coin from the wallet. * @param {Hash} hash * @param {Number} index - * @returns {Promise} - Returns {@link Coin}. + * @returns {Promise} */ async getUnspentCoin(hash, index) { @@ -4704,7 +4786,7 @@ class Wallet extends EventEmitter { /** * Get a transaction from the wallet. * @param {Hash} hash - * @returns {Promise} - Returns {@link TX}. + * @returns {Promise} */ getTX(hash) { @@ -4713,7 +4795,7 @@ class Wallet extends EventEmitter { /** * List blocks for the wallet. - * @returns {Promise} - Returns {@link BlockRecord}. + * @returns {Promise} */ getBlocks() { @@ -4742,7 +4824,7 @@ class Wallet extends EventEmitter { /** * Get a name if present. * @param {Buffer} nameHash - * @returns {NameState} + * @returns {Promise} */ async getNameState(nameHash) { @@ -4752,7 +4834,7 @@ class Wallet extends EventEmitter { /** * Get a name if present. * @param {String|Buffer} name - * @returns {NameState} + * @returns {Promise} */ async getNameStateByName(name) { @@ -4762,7 +4844,7 @@ class Wallet extends EventEmitter { /** * Get a blind value if present. * @param {Buffer} blind - Blind hash. - * @returns {BlindValue} + * @returns {Promise} */ async getBlind(blind) { @@ -4782,7 +4864,7 @@ class Wallet extends EventEmitter { /** * Get all bids for name. - * @param {Buffer} nameHash + * @param {Buffer} [nameHash] * @returns {Promise} */ @@ -4792,8 +4874,8 @@ class Wallet extends EventEmitter { /** * Get all bids for name. - * @param {String|Buffer} name - * @returns {BlindBid[]} + * @param {String|Buffer} [name] + * @returns {Promise} */ async getBidsByName(name) { @@ -4815,7 +4897,7 @@ class Wallet extends EventEmitter { * Get reveal. * @param {Buffer} nameHash * @param {Outpoint} outpoint - * @returns {BidReveal?} + * @returns {Promise} */ async getReveal(nameHash, outpoint) { @@ -4825,7 +4907,7 @@ class Wallet extends EventEmitter { /** * Get all reveals by name. * @param {Buffer} nameHash - * @returns {BidReveal[]} + * @returns {Promise} */ async getReveals(nameHash) { @@ -4834,8 +4916,8 @@ class Wallet extends EventEmitter { /** * Get all reveals by name. - * @param {String} name - * @returns {BidReveal[]} + * @param {String|Buffer} name + * @returns {Promise} */ async getRevealsByName(name) { @@ -4846,7 +4928,7 @@ class Wallet extends EventEmitter { * Get reveal for bid. * @param {Buffer} nameHash * @param {Outpoint} outpoint - bid outpoint - * @returns {Promise} + * @returns {Promise} */ async getRevealByBid(nameHash, outpoint) { @@ -4917,7 +4999,7 @@ class Wallet extends EventEmitter { /** * Remove a wallet transaction. * @param {Hash} hash - * @returns {Promise} + * @returns {Promise} */ async remove(hash) { @@ -4950,7 +5032,7 @@ class Wallet extends EventEmitter { * Zap stale TXs from wallet. * @param {(Number|String)?} acct * @param {Number} age - Age threshold (unix time, default=72 hours). - * @returns {Promise} + * @returns {Promise} */ async zap(acct, age) { @@ -4967,7 +5049,7 @@ class Wallet extends EventEmitter { * @private * @param {(Number|String)?} acct * @param {Number} age - * @returns {Promise} + * @returns {Promise} */ async _zap(acct, age) { @@ -4978,7 +5060,7 @@ class Wallet extends EventEmitter { /** * Abandon transaction. * @param {Hash} hash - * @returns {Promise} + * @returns {Promise
} - removed tx details. */ async abandon(hash) { @@ -4994,7 +5076,7 @@ class Wallet extends EventEmitter { * Abandon transaction without a lock. * @private * @param {Hash} hash - * @returns {Promise} + * @returns {Promise
} - removed tx details. */ _abandon(hash) { @@ -5013,6 +5095,7 @@ class Wallet extends EventEmitter { /** * Unlock a single coin. * @param {Coin|Outpoint} coin + * @returns {Boolean} */ unlockCoin(coin) { @@ -5030,6 +5113,7 @@ class Wallet extends EventEmitter { /** * Test locked status of a single coin. * @param {Coin|Outpoint} coin + * @returns {Boolean} */ isLocked(coin) { @@ -5047,8 +5131,8 @@ class Wallet extends EventEmitter { /** * Get all available coins. - * @param {(String|Number)?} account - * @returns {Promise} - Returns {@link Coin}[]. + * @param {(String|Number)?} [acct] + * @returns {Promise} */ async getCoins(acct) { @@ -5058,8 +5142,8 @@ class Wallet extends EventEmitter { /** * Get all available credits. - * @param {(String|Number)?} account - * @returns {Promise} - Returns {@link Credit}[]. + * @param {(String|Number)?} [acct] + * @returns {Promise} */ async getCredits(acct) { @@ -5069,8 +5153,8 @@ class Wallet extends EventEmitter { /** * Get "smart" coins. - * @param {(String|Number)?} account - * @returns {Promise} - Returns {@link Coin}[]. + * @param {(String|Number)?} acct + * @returns {Promise} */ async getSmartCoins(acct) { @@ -5108,8 +5192,8 @@ class Wallet extends EventEmitter { /** * Get all pending/unconfirmed transactions. - * @param {(String|Number)?} acct - * @returns {Promise} - Returns {@link TX}[]. + * @param {(String|Number)?} [acct] + * @returns {Promise} */ async getPending(acct) { @@ -5119,8 +5203,8 @@ class Wallet extends EventEmitter { /** * Get wallet balance. - * @param {(String|Number)?} acct - * @returns {Promise} - Returns {@link Balance}. + * @param {(String|Number)?} [acct] + * @returns {Promise} */ async getBalance(acct) { @@ -5129,7 +5213,7 @@ class Wallet extends EventEmitter { } /** - * @param {(String|Number)} [acct] + * @param {(String|Number)?} acct * @param {Object} options * @param {Number} options.limit * @param {Boolean} options.reverse @@ -5142,9 +5226,9 @@ class Wallet extends EventEmitter { } /** - * @param {(String|Number)} [acct] + * @param {(String|Number)?} acct * @param {Object} options - * @param {String} options.hash + * @param {Buffer} options.hash * @param {Number} options.limit * @param {Boolean} options.reverse * @returns {Promise} @@ -5156,9 +5240,9 @@ class Wallet extends EventEmitter { } /** - * @param {(String|Number)} [acct] + * @param {(String|Number)?} acct * @param {Object} options - * @param {String} options.hash + * @param {Buffer} options.hash * @param {Number} options.limit * @param {Boolean} options.reverse * @returns {Promise} @@ -5170,7 +5254,7 @@ class Wallet extends EventEmitter { } /** - * @param {(String|Number)} [acct] + * @param {(String|Number)?} acct * @param {Object} options * @param {Number} options.time - Time in seconds. * @param {Number} options.limit @@ -5184,7 +5268,7 @@ class Wallet extends EventEmitter { } /** - * @param {(String|Number)} [acct] + * @param {(String|Number)?} acct * @param {Object} options * @param {Number} options.limit * @param {Boolean} options.reverse @@ -5197,7 +5281,7 @@ class Wallet extends EventEmitter { } /** - * @param {(String|Number)} [acct] + * @param {(String|Number)?} acct * @param {Object} options * @param {Buffer} options.hash * @param {Number} options.limit @@ -5211,7 +5295,7 @@ class Wallet extends EventEmitter { } /** - * @param {(String|Number)} [acct] + * @param {(String|Number)?} acct * @param {Object} options * @param {Buffer} options.hash * @param {Number} options.limit @@ -5225,7 +5309,7 @@ class Wallet extends EventEmitter { } /** - * @param {(String|Number)} [acct] + * @param {(String|Number)?} acct * @param {Object} options * @param {Number} options.time - Time in seconds. * @param {Number} options.limit @@ -5241,39 +5325,45 @@ class Wallet extends EventEmitter { /** * Get account key. * @param {Number} [acct=0] - * @returns {HDPublicKey} + * @returns {Promise} */ async accountKey(acct = 0) { const account = await this.getAccount(acct); + if (!account) throw new Error('Account not found.'); + return account.accountKey; } /** * Get current receive depth. * @param {Number} [acct=0] - * @returns {Number} + * @returns {Promise} */ async receiveDepth(acct = 0) { const account = await this.getAccount(acct); + if (!account) throw new Error('Account not found.'); + return account.receiveDepth; } /** * Get current change depth. * @param {Number} [acct=0] - * @returns {Number} + * @returns {Promise} */ async changeDepth(acct = 0) { const account = await this.getAccount(acct); + if (!account) throw new Error('Account not found.'); + return account.changeDepth; } @@ -5285,8 +5375,10 @@ class Wallet extends EventEmitter { async receiveAddress(acct = 0) { const account = await this.getAccount(acct); + if (!account) throw new Error('Account not found.'); + return account.receiveAddress(); } @@ -5298,34 +5390,40 @@ class Wallet extends EventEmitter { async changeAddress(acct = 0) { const account = await this.getAccount(acct); + if (!account) throw new Error('Account not found.'); + return account.changeAddress(); } /** * Get current receive key. * @param {Number} [acct=0] - * @returns {WalletKey} + * @returns {Promise} */ async receiveKey(acct = 0) { const account = await this.getAccount(acct); + if (!account) throw new Error('Account not found.'); + return account.receiveKey(); } /** * Get current change key. * @param {Number} [acct=0] - * @returns {WalletKey} + * @returns {Promise} */ async changeKey(acct = 0) { const account = await this.getAccount(acct); + if (!account) throw new Error('Account not found.'); + return account.changeKey(); } @@ -5358,8 +5456,9 @@ class Wallet extends EventEmitter { /** * Convert the wallet to an object suitable for * serialization. - * @param {Boolean?} unsafe - Whether to include + * @param {Boolean?} [unsafe] - Whether to include * the master key in the JSON. + * @param {Balance?} [balance] * @returns {Object} */ @@ -5373,20 +5472,18 @@ class Wallet extends EventEmitter { token: this.token.toString('hex'), tokenDepth: this.tokenDepth, master: this.master.getJSON(this.network, unsafe), - balance: balance ? balance.toJSON(true) : null + balance: balance ? balance.getJSON(true) : null }; } /** * Convert the wallet to an object suitable for * serialization. - * @param {Boolean?} unsafe - Whether to include - * the master key in the JSON. * @returns {Object} */ toJSON() { - return this.getJSON(); + return this.getJSON(false); } /** @@ -5426,8 +5523,8 @@ class Wallet extends EventEmitter { /** * Inject properties from serialized data. - * @private * @param {Buffer} data + * @returns {this} */ decode(data) { @@ -5446,6 +5543,7 @@ class Wallet extends EventEmitter { /** * Instantiate a wallet from serialized data. + * @param {WalletDB} wdb * @param {Buffer} data * @returns {Wallet} */ diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index d223ede63..eb0dc6e8a 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -40,6 +40,9 @@ const {scanActions} = require('../blockchain/common'); /** @typedef {import('../primitives/tx')} TX */ /** @typedef {import('../primitives/claim')} Claim */ /** @typedef {import('../blockchain/common').ScanAction} ScanAction */ +/** @typedef {import('../blockchain/chainentry')} ChainEntry */ +/** @typedef {import('./records').BlockMeta} BlockMeta */ +/** @typedef {import('./txdb').BlockExtraInfo} BlockExtraInfo */ /** @typedef {import('./walletkey')} WalletKey */ const { @@ -49,9 +52,6 @@ const { MapRecord } = records; -/** @typedef {import('./records').BlockMeta} BlockMeta */ -/** @typedef {import('./txdb').BlockExtraInfo} BlockExtraInfo */ - /** * @typedef {Object} AddBlockResult * @property {Number} txs - Number of transactions added on this add. @@ -60,7 +60,7 @@ const { /** * @typedef {Object} AddTXResult - * @property {Number} wids - Wallet IDs affected. + * @property {Set} wids - Wallet IDs affected. * @property {Boolean} filterUpdated - Whether the bloom filter was updated. /** @@ -1222,6 +1222,7 @@ class WalletDB extends EventEmitter { /** * Rename an account. + * @param {Batch} b * @param {Account} account * @param {String} name */ @@ -1390,7 +1391,7 @@ class WalletDB extends EventEmitter { * Get a wallet with token auth first. * @param {Number|String} id * @param {Buffer} token - * @returns {Promise} - Returns {@link Wallet}. + * @returns {Promise} */ async auth(id, token) { @@ -1456,7 +1457,7 @@ class WalletDB extends EventEmitter { /** * Test for the existence of a wallet. * @param {Number|String} id - * @returns {Promise} + * @returns {Promise} */ async has(id) { @@ -1467,7 +1468,7 @@ class WalletDB extends EventEmitter { /** * Attempt to create wallet, return wallet if already exists. * @param {Object} options - See {@link Wallet}. - * @returns {Promise} + * @returns {Promise} */ async ensure(options) { @@ -1485,7 +1486,7 @@ class WalletDB extends EventEmitter { * Get an account from the database by wid. * @param {Number} wid * @param {Number} index - Account index. - * @returns {Promise} - Returns {@link Wallet}. + * @returns {Promise} */ async getAccount(wid, index) { @@ -1671,7 +1672,7 @@ class WalletDB extends EventEmitter { /** * Get all address hashes. - * @returns {Promise} + * @returns {Promise} */ async getHashes() { @@ -1684,7 +1685,7 @@ class WalletDB extends EventEmitter { /** * Get all outpoints. - * @returns {Promise} + * @returns {Promise} */ async getOutpoints() { @@ -1701,7 +1702,7 @@ class WalletDB extends EventEmitter { /** * Get all address hashes. * @param {Number} wid - * @returns {Promise} + * @returns {Promise} */ async getWalletHashes(wid) { @@ -1770,6 +1771,7 @@ class WalletDB extends EventEmitter { /** * Encrypt all imported keys for a wallet. + * @param {Batch} b * @param {Number} wid * @param {Buffer} key * @returns {Promise} @@ -1802,6 +1804,7 @@ class WalletDB extends EventEmitter { /** * Decrypt all imported keys for a wallet. + * @param {Batch} b * @param {Number} wid * @param {Buffer} key * @returns {Promise} @@ -1897,11 +1900,12 @@ class WalletDB extends EventEmitter { /** * Get all wallet ids by output addresses and outpoints. - * @param {Hash[]} hashes - * @returns {Promise} + * @param {TX} tx + * @returns {Promise>} */ async getWalletsByTX(tx) { + /** @type {Set} */ const wids = new Set(); if (!tx.isCoinbase()) { @@ -1962,7 +1966,7 @@ class WalletDB extends EventEmitter { /** * Get the best block hash. - * @returns {Promise} + * @returns {Promise} */ async getState() { @@ -2042,7 +2046,7 @@ class WalletDB extends EventEmitter { /** * Get a wallet map. * @param {Buffer} key - * @returns {Promise} + * @returns {Promise} */ async getMap(key) { @@ -2103,6 +2107,7 @@ class WalletDB extends EventEmitter { * @param {Batch} b * @param {Buffer} key * @param {Number} wid + * @returns {Promise} */ async removeMap(b, key, wid) { @@ -2124,8 +2129,8 @@ class WalletDB extends EventEmitter { /** * Get a wallet map. - * @param {Buffer} key - * @returns {Promise} + * @param {Hash} hash + * @returns {Promise} */ async getPathMap(hash) { @@ -2134,9 +2139,10 @@ class WalletDB extends EventEmitter { /** * Add wid to a wallet map. - * @param {Wallet} wallet - * @param {Buffer} key + * @param {Batch} b + * @param {Hash} hash * @param {Number} wid + * @returns {Promise} */ async addPathMap(b, hash, wid) { @@ -2146,9 +2152,10 @@ class WalletDB extends EventEmitter { /** * Remove wid from a wallet map. - * @param {Wallet} wallet - * @param {Buffer} key + * @param {Batch} b + * @param {Hash} hash * @param {Number} wid + * @returns {Promise} */ async removePathMap(b, hash, wid) { @@ -2157,8 +2164,8 @@ class WalletDB extends EventEmitter { /** * Get a wallet map. - * @param {Buffer} key - * @returns {Promise} + * @param {Number} height + * @returns {Promise} */ async getBlockMap(height) { @@ -2167,9 +2174,10 @@ class WalletDB extends EventEmitter { /** * Add wid to a wallet map. - * @param {Wallet} wallet - * @param {Buffer} key + * @param {Batch} b + * @param {Number} height * @param {Number} wid + * @returns {Promise} */ async addBlockMap(b, height, wid) { @@ -2178,9 +2186,10 @@ class WalletDB extends EventEmitter { /** * Remove wid from a wallet map. - * @param {Wallet} wallet - * @param {Buffer} key + * @param {Batch} b + * @param {Number} height * @param {Number} wid + * @returns {Promise} */ async removeBlockMap(b, height, wid) { @@ -2189,8 +2198,8 @@ class WalletDB extends EventEmitter { /** * Get a wallet map. - * @param {Buffer} key - * @returns {Promise} + * @param {Hash} hash + * @returns {Promise} */ async getTXMap(hash) { @@ -2199,9 +2208,10 @@ class WalletDB extends EventEmitter { /** * Add wid to a wallet map. - * @param {Wallet} wallet - * @param {Buffer} key + * @param {Batch} b + * @param {Hash} hash * @param {Number} wid + * @returns {Promise} */ async addTXMap(b, hash, wid) { @@ -2210,9 +2220,10 @@ class WalletDB extends EventEmitter { /** * Remove wid from a wallet map. - * @param {Wallet} wallet - * @param {Buffer} key + * @param {Batch} b + * @param {Hash} hash * @param {Number} wid + * @returns {Promise} */ async removeTXMap(b, hash, wid) { @@ -2221,8 +2232,9 @@ class WalletDB extends EventEmitter { /** * Get a wallet map. - * @param {Buffer} key - * @returns {Promise} + * @param {Hash} hash + * @param {Number} index + * @returns {Promise} */ async getOutpointMap(hash, index) { @@ -2231,9 +2243,11 @@ class WalletDB extends EventEmitter { /** * Add wid to a wallet map. - * @param {Wallet} wallet - * @param {Buffer} key + * @param {Batch} b + * @param {Hash} hash + * @param {Number} index * @param {Number} wid + * @returns {Promise} */ async addOutpointMap(b, hash, index, wid) { @@ -2243,9 +2257,11 @@ class WalletDB extends EventEmitter { /** * Remove wid from a wallet map. - * @param {Wallet} wallet - * @param {Buffer} key + * @param {Batch} b + * @param {Hash} hash + * @param {Number} index * @param {Number} wid + * @returns {Promise} */ async removeOutpointMap(b, hash, index, wid) { @@ -2254,8 +2270,8 @@ class WalletDB extends EventEmitter { /** * Get a wallet map. - * @param {Buffer} key - * @returns {Promise} + * @param {Hash} nameHash + * @returns {Promise} */ async getNameMap(nameHash) { @@ -2275,9 +2291,10 @@ class WalletDB extends EventEmitter { /** * Add wid to a wallet map. - * @param {Wallet} wallet - * @param {Buffer} key + * @param {Batch} b + * @param {Hash} nameHash * @param {Number} wid + * @returns {Promise} */ async addNameMap(b, nameHash, wid) { @@ -2287,9 +2304,10 @@ class WalletDB extends EventEmitter { /** * Remove wid from a wallet map. - * @param {Wallet} wallet - * @param {Buffer} key + * @param {Batch} b + * @param {Hash} nameHash * @param {Number} wid + * @returns {Promise} */ async removeNameMap(b, nameHash, wid) { @@ -2313,7 +2331,6 @@ class WalletDB extends EventEmitter { /** * Get wallet tip. - * @param {Hash} hash * @returns {Promise} */ @@ -2328,7 +2345,7 @@ class WalletDB extends EventEmitter { /** * Get renewal block hash. - * @returns {Buffer} + * @returns {Promise} */ async getRenewalBlock() { @@ -3018,12 +3035,22 @@ class WalletOptions { * Helpers */ +/** + * @param {Number} num + * @returns {Buffer} + */ + function fromU32(num) { const data = Buffer.allocUnsafe(4); - data.writeUInt32LE(num, 0, true); + data.writeUInt32LE(num, 0); return data; } +/** + * @param {String} str + * @returns {Buffer} + */ + function fromString(str) { const buf = Buffer.alloc(1 + str.length); buf[0] = str.length;