diff --git a/lib/coins/coinview.js b/lib/coins/coinview.js index b958784b..93daef99 100644 --- a/lib/coins/coinview.js +++ b/lib/coins/coinview.js @@ -313,7 +313,7 @@ class CoinView extends View { * Get an HD path by prevout. * Implemented in {@link WalletCoinView}. * @param {Outpoint} prevout - * @returns {null} + * @returns {*} */ getPath(prevout) { @@ -404,7 +404,7 @@ class CoinView extends View { * Get a single path by input. * Implemented in {@link WalletCoinView}. * @param {Input} input - * @returns {null} + * @returns {*} */ getPathFor(input) { diff --git a/lib/hd/common.js b/lib/hd/common.js index d5b8482c..0240b0c3 100644 --- a/lib/hd/common.js +++ b/lib/hd/common.js @@ -118,7 +118,7 @@ common.isMaster = function isMaster(key) { /** * Test whether the key is (most likely) a BIP44 account key. * @param {HDPrivateKey|HDPublicKey} key - * @param {Number?} account + * @param {Number?} [account] * @returns {Boolean} */ diff --git a/lib/hd/public.js b/lib/hd/public.js index f8f26305..84e72007 100644 --- a/lib/hd/public.js +++ b/lib/hd/public.js @@ -237,7 +237,7 @@ class HDPublicKey extends bio.Struct { /** * Test whether the key is (most likely) a BIP44 account key. * @method - * @param {Number?} account + * @param {Number?} [account] * @returns {Boolean} */ diff --git a/lib/primitives/keyring.js b/lib/primitives/keyring.js index 902015b8..c0c44782 100644 --- a/lib/primitives/keyring.js +++ b/lib/primitives/keyring.js @@ -106,6 +106,7 @@ class KeyRing extends bio.Struct { /** * Inject data from private key. * @param {Buffer} key + * @returns {this} */ fromPrivate(key) { diff --git a/lib/wallet/account.js b/lib/wallet/account.js index 582d3a74..84f3f658 100644 --- a/lib/wallet/account.js +++ b/lib/wallet/account.js @@ -13,7 +13,14 @@ const Path = require('./path'); const common = require('./common'); const Script = require('../script/script'); const WalletKey = require('./walletkey'); -const {HDPublicKey} = require('../hd/hd'); +const HDPublicKey = require('../hd/public'); + +/** @typedef {import('bdb').DB} DB */ +/** @typedef {ReturnType} Batch */ +/** @typedef {import('../types').BufioWriter} BufioWriter */ +/** @typedef {import('./walletdb')} WalletDB */ +/** @typedef {import('./masterkey')} MasterKey */ +/** @typedef {import('../primitives/address')} Address */ /** * Account @@ -28,6 +35,7 @@ class Account extends bio.Struct { /** * Create an account. * @constructor + * @param {WalletDB} wdb * @param {Object} options */ @@ -36,22 +44,28 @@ class Account extends bio.Struct { assert(wdb, 'Database is required.'); + /** @type {WalletDB} */ this.wdb = wdb; this.network = wdb.network; this.wid = 0; + /** @type {String|null} */ this.id = null; this.accountIndex = 0; + /** @type {String|null} */ this.name = null; this.initialized = false; this.watchOnly = false; + /** @type {Account.types} */ this.type = Account.types.PUBKEYHASH; this.m = 1; this.n = 1; this.receiveDepth = 0; this.changeDepth = 0; this.lookahead = 200; + /** @type {HDPublicKey|null} */ this.accountKey = null; + /** @type {HDPublicKey[]} */ this.keys = []; if (options) @@ -60,8 +74,8 @@ class Account extends bio.Struct { /** * Inject properties from options object. - * @private * @param {Object} options + * @returns {this} */ fromOptions(options) { @@ -155,7 +169,7 @@ class Account extends bio.Struct { /** * Inject properties from options object. - * @private + * @param {WalletDB} wdb * @param {Object} options */ @@ -168,6 +182,7 @@ class Account extends bio.Struct { * the first addresses along with the lookahead * addresses). Called automatically from the * walletdb. + * @param {Batch} b * @returns {Promise} */ @@ -193,6 +208,7 @@ class Account extends bio.Struct { * @param {HDPublicKey} key - Account (bip44) * key (can be in base58 form). * @throws Error on non-hdkey/non-accountkey. + * @returns {Boolean} - Whether the key was added. */ pushKey(key) { @@ -230,6 +246,7 @@ class Account extends bio.Struct { * @param {HDPublicKey} key - Account (bip44) * key (can be in base58 form). * @throws Error on non-hdkey/non-accountkey. + * @returns {Boolean} - Whether the key was removed. */ spliceKey(key) { @@ -254,8 +271,9 @@ class Account extends bio.Struct { /** * Add a public account key to the account (multisig). * Saves the key in the wallet database. + * @param {Batch} b * @param {HDPublicKey} key - * @returns {Promise} + * @returns {Promise} */ async addSharedKey(b, key) { @@ -275,7 +293,7 @@ class Account extends bio.Struct { /** * Ensure accounts are not sharing keys. * @private - * @returns {Promise} + * @returns {Promise} */ async hasDuplicate() { @@ -291,8 +309,9 @@ class Account extends bio.Struct { /** * Remove a public account key from the account (multisig). * Remove the key from the wallet database. + * @param {Batch} b * @param {HDPublicKey} key - * @returns {Promise} + * @returns {Boolean} */ removeSharedKey(b, key) { @@ -308,26 +327,29 @@ class Account extends bio.Struct { /** * Create a new receiving address (increments receiveDepth). - * @returns {WalletKey} + * @param {Batch} b + * @returns {Promise} */ - createReceive() { - return this.createKey(0); + createReceive(b) { + return this.createKey(b, 0); } /** * Create a new change address (increments changeDepth). - * @returns {WalletKey} + * @param {Batch} b + * @returns {Promise} */ - createChange() { - return this.createKey(1); + createChange(b) { + return this.createKey(b, 1); } /** * Create a new address (increments depth). - * @param {Boolean} change - * @returns {Promise} - Returns {@link WalletKey}. + * @param {Batch} b + * @param {Number} branch + * @returns {Promise} - Returns {@link WalletKey}. */ async createKey(b, branch) { @@ -360,6 +382,7 @@ class Account extends bio.Struct { /** * Derive a receiving address at `index`. Do not increment depth. * @param {Number} index + * @param {MasterKey} [master] * @returns {WalletKey} */ @@ -370,6 +393,7 @@ class Account extends bio.Struct { /** * Derive a change address at `index`. Do not increment depth. * @param {Number} index + * @param {MasterKey} [master] * @returns {WalletKey} */ @@ -381,7 +405,7 @@ class Account extends bio.Struct { * Derive an address from `path` object. * @param {Path} path * @param {MasterKey} master - * @returns {WalletKey} + * @returns {WalletKey?} */ derivePath(path, master) { @@ -415,6 +439,7 @@ class Account extends bio.Struct { * Derive an address at `index`. Do not increment depth. * @param {Number} branch * @param {Number} index + * @param {MasterKey} [master] * @returns {WalletKey} */ @@ -456,7 +481,8 @@ class Account extends bio.Struct { /** * Save the account to the database. Necessary * when address depth and keys change. - * @returns {Promise} + * @param {Batch} b + * @returns {void} */ save(b) { @@ -465,7 +491,8 @@ class Account extends bio.Struct { /** * Save addresses to path map. - * @param {WalletKey[]} rings + * @param {Batch} b + * @param {WalletKey} ring * @returns {Promise} */ @@ -475,7 +502,8 @@ class Account extends bio.Struct { /** * Save paths to path map. - * @param {Path[]} rings + * @param {Batch} b + * @param {Path} path * @returns {Promise} */ @@ -485,6 +513,7 @@ class Account extends bio.Struct { /** * Initialize address depths (including lookahead). + * @param {Batch} b * @returns {Promise} */ @@ -510,8 +539,9 @@ class Account extends bio.Struct { /** * Allocate new lookahead addresses if necessary. - * @param {Number} receiveDepth - * @param {Number} changeDepth + * @param {Batch} b + * @param {Number} receive + * @param {Number} change * @returns {Promise} */ @@ -558,6 +588,7 @@ class Account extends bio.Struct { /** * Allocate new lookahead addresses. + * @param {Batch} b * @param {Number} lookahead * @returns {Promise} */ @@ -607,7 +638,7 @@ class Account extends bio.Struct { /** * Get current receive key. - * @returns {WalletKey} + * @returns {WalletKey?} */ receiveKey() { @@ -619,7 +650,7 @@ class Account extends bio.Struct { /** * Get current change key. - * @returns {WalletKey} + * @returns {WalletKey?} */ changeKey() { @@ -631,7 +662,7 @@ class Account extends bio.Struct { /** * Get current receive address. - * @returns {Address} + * @returns {Address?} */ receiveAddress() { @@ -645,7 +676,7 @@ class Account extends bio.Struct { /** * Get current change address. - * @returns {Address} + * @returns {Address?} */ changeAddress() { @@ -690,6 +721,7 @@ class Account extends bio.Struct { /** * Convert the account to an object suitable for * serialization. + * @param {Object} [balance=null] * @returns {Object} */ @@ -730,7 +762,8 @@ class Account extends bio.Struct { /** * Serialize the account. - * @returns {Buffer} + * @param {BufioWriter} bw + * @returns {BufioWriter} */ write(bw) { @@ -757,9 +790,7 @@ class Account extends bio.Struct { /** * Inject properties from serialized data. - * @private - * @param {Buffer} data - * @returns {Object} + * @param {bio.BufferReader} br */ read(br) { @@ -837,6 +868,12 @@ function cmp(a, b) { return a.compare(b); } +/** + * @param {HDPublicKey} key + * @param {BufioWriter} bw + * @returns {void} + */ + function writeKey(key, bw) { bw.writeU8(key.depth); bw.writeU32BE(key.parentFingerPrint); @@ -845,6 +882,11 @@ function writeKey(key, bw) { bw.writeBytes(key.publicKey); } +/** + * @param {bio.BufferReader} br + * @returns {HDPublicKey} + */ + function readKey(br) { const key = new HDPublicKey(); key.depth = br.readU8(); diff --git a/lib/wallet/common.js b/lib/wallet/common.js index 367f4b26..836df4b7 100644 --- a/lib/wallet/common.js +++ b/lib/wallet/common.js @@ -8,6 +8,10 @@ const {BufferMap} = require('buffer-map'); +/** @typedef {import('../primitives/tx')} TX */ +/** @typedef {import('../primitives/txmeta')} TXMeta */ +/** @typedef {import('../primitives/coin')} Coin */ + /** * @exports wallet/common */ @@ -52,8 +56,8 @@ common.isName = function isName(key) { /** * Sort an array of transactions by time. - * @param {TX[]} txs - * @returns {TX[]} + * @param {TXMeta[]} txs + * @returns {TXMeta[]} */ common.sortTX = function sortTX(txs) { @@ -64,15 +68,15 @@ common.sortTX = function sortTX(txs) { /** * Sort an array of coins by height. - * @param {Coin[]} txs + * @param {Coin[]} coins * @returns {Coin[]} */ common.sortCoins = function sortCoins(coins) { return coins.sort((a, b) => { - a = a.height === -1 ? 0x7fffffff : a.height; - b = b.height === -1 ? 0x7fffffff : b.height; - return a - b; + const ah = a.height === -1 ? 0x7fffffff : a.height; + const bh = b.height === -1 ? 0x7fffffff : b.height; + return ah - bh; }); }; diff --git a/lib/wallet/masterkey.js b/lib/wallet/masterkey.js index 487247f2..387321f6 100644 --- a/lib/wallet/masterkey.js +++ b/lib/wallet/masterkey.js @@ -22,6 +22,9 @@ const Mnemonic = require('../hd/mnemonic'); const pkg = require('../pkg'); const {encoding} = bio; +/** @typedef {import('../types').BufioWriter} BufioWriter */ +/** @typedef {import('../protocol/network')} Network */ + /** * Master Key * Master BIP32 key which can exist @@ -61,8 +64,8 @@ class MasterKey extends bio.Struct { /** * Inject properties from options object. - * @private * @param {Object} options + * @returns {this} */ fromOptions(options) { @@ -215,7 +218,7 @@ class MasterKey extends bio.Struct { /** * Derive an aes key based on params. - * @param {String|Buffer} passphrase + * @param {String|Buffer} passwd * @returns {Promise} */ @@ -276,7 +279,7 @@ class MasterKey extends bio.Struct { async lock() { const unlock = await this.locker.lock(); try { - return await this._lock(); + return this._lock(); } finally { unlock(); } @@ -320,6 +323,7 @@ class MasterKey extends bio.Struct { /** * Decrypt the key permanently. * @param {Buffer|String} passphrase - Zero this yourself. + * @param {Boolean} [clean=false] * @returns {Promise} */ @@ -336,6 +340,7 @@ class MasterKey extends bio.Struct { * Decrypt the key permanently without a lock. * @private * @param {Buffer|String} passphrase - Zero this yourself. + * @param {Boolean} [clean=false] * @returns {Promise} */ @@ -367,6 +372,7 @@ class MasterKey extends bio.Struct { /** * Encrypt the key permanently. * @param {Buffer|String} passphrase - Zero this yourself. + * @param {Boolean} [clean=false] * @returns {Promise} */ @@ -383,6 +389,7 @@ class MasterKey extends bio.Struct { * Encrypt the key permanently without a lock. * @private * @param {Buffer|String} passphrase - Zero this yourself. + * @param {Boolean} [clean=false] * @returns {Promise} */ @@ -497,7 +504,8 @@ class MasterKey extends bio.Struct { /** * Serialize the key in the form of: * `[enc-flag][iv?][ciphertext?][extended-key?]` - * @returns {Buffer} + * @param {BufioWriter} bw + * @returns {BufioWriter} */ write(bw) { @@ -531,8 +539,8 @@ class MasterKey extends bio.Struct { /** * Inject properties from serialized data. - * @private - * @param {Buffer} raw + * @param {bio.BufferReader} br + * @returns {this} */ read(br) { @@ -566,7 +574,6 @@ class MasterKey extends bio.Struct { /** * Inject properties from an HDPrivateKey. - * @private * @param {HDPrivateKey} key * @param {Mnemonic?} mnemonic */ @@ -593,8 +600,8 @@ class MasterKey extends bio.Struct { /** * Convert master key to a jsonifiable object. - * @param {Network?} network - * @param {Boolean?} unsafe - Whether to include + * @param {Network?} [network] + * @param {Boolean?} [unsafe] - Whether to include * the key data in the JSON. * @returns {Object} */ diff --git a/lib/wallet/path.js b/lib/wallet/path.js index 6b498945..3b70d819 100644 --- a/lib/wallet/path.js +++ b/lib/wallet/path.js @@ -13,6 +13,7 @@ const Network = require('../protocol/network'); const {encoding} = bio; /** @typedef {import('../types').NetworkType} NetworkType */ +/** @typedef {import('../types').Hash} Hash */ /** @typedef {import('./account')} Account */ /** @@ -36,6 +37,7 @@ class Path extends bio.Struct { this.keyType = Path.types.HD; + /** @type {String|null} */ this.name = null; // Passed in by caller. this.account = 0; @@ -46,6 +48,7 @@ class Path extends bio.Struct { this.encrypted = false; this.data = null; + /** @type {Hash|null} */ this.hash = null; // Passed in by caller. if (options) @@ -190,7 +193,6 @@ class Path extends bio.Struct { /** * Inject properties from address. - * @private * @param {Account} account * @param {Address} address */ @@ -247,7 +249,7 @@ class Path extends bio.Struct { /** * Convert path to a json-friendly object. - * @param {(String|Network)?} network - Network type. + * @param {(NetworkType|Network)?} [network] - Network type. * @returns {Object} */ @@ -273,7 +275,7 @@ class Path extends bio.Struct { /** * Inject properties from a json object. * @param {Object} json - * @returns {Path} + * @returns {this} */ fromJSON(json) { diff --git a/lib/wallet/paths.js b/lib/wallet/paths.js index af1035ea..9d3c60ae 100644 --- a/lib/wallet/paths.js +++ b/lib/wallet/paths.js @@ -8,6 +8,8 @@ const assert = require('bsert'); +/** @typedef {import('./path')} Path */ + /** * Paths * Represents the HD paths for coins in a single transaction. diff --git a/lib/wallet/records.js b/lib/wallet/records.js index f5c1f2b4..e45327b5 100644 --- a/lib/wallet/records.js +++ b/lib/wallet/records.js @@ -16,6 +16,10 @@ const util = require('../utils/util'); const TX = require('../primitives/tx'); const consensus = require('../protocol/consensus'); +/** @typedef {import('../types').BufioWriter} BufioWriter */ +/** @typedef {import('../types').Hash} Hash */ +/** @typedef {import('../blockchain/chainentry')} ChainEntry */ + /** * Chain State */ @@ -37,7 +41,8 @@ class ChainState extends bio.Struct { /** * Clone the state. - * @returns {ChainState} + * @param {ChainState} state + * @returns {this} */ inject(state) { @@ -59,8 +64,8 @@ class ChainState extends bio.Struct { /** * Inject properties from serialized data. - * @private - * @param {Buffer} data + * @param {bio.BufferReader} br + * @returns {this} */ read(br) { @@ -73,7 +78,8 @@ class ChainState extends bio.Struct { /** * Serialize the chain state. - * @returns {Buffer} + * @param {BufioWriter} bw + * @returns {BufioWriter} */ write(bw) { @@ -93,9 +99,9 @@ class BlockMeta extends bio.Struct { /** * Create block meta. * @constructor - * @param {Hash} hash - * @param {Number} height - * @param {Number} time + * @param {Hash} [hash] + * @param {Number} [height] + * @param {Number} [time] */ constructor(hash, height, time) { @@ -107,7 +113,8 @@ class BlockMeta extends bio.Struct { /** * Clone the block. - * @returns {BlockMeta} + * @param {BlockMeta} meta + * @returns {this} */ inject(meta) { @@ -179,8 +186,8 @@ class BlockMeta extends bio.Struct { /** * Instantiate block meta from serialized tip data. - * @private - * @param {Buffer} data + * @param {bio.BufferReader} br + * @returns {this} */ read(br) { @@ -201,7 +208,8 @@ class BlockMeta extends bio.Struct { /** * Serialize the block meta. - * @returns {Buffer} + * @param {BufioWriter} bw + * @returns {BufioWriter} */ write(bw) { @@ -262,6 +270,7 @@ class TXRecord extends bio.Struct { this.hash = null; this.mtime = mtime; this.height = -1; + /** @type {Hash?} */ this.block = null; this.index = -1; this.time = 0; @@ -272,9 +281,8 @@ class TXRecord extends bio.Struct { /** * Inject properties from tx and block. - * @private * @param {TX} tx - * @param {Block} [block] + * @param {BlockMeta} [block] * @returns {TXRecord} */ @@ -291,7 +299,7 @@ class TXRecord extends bio.Struct { /** * Instantiate tx record from tx and block. * @param {TX} [tx] - * @param {Block} [block] + * @param {BlockMeta} [block] * @param {Number} [mtime] * @returns {TXRecord} */ @@ -325,7 +333,7 @@ class TXRecord extends bio.Struct { /** * Convert tx record to a block meta. - * @returns {BlockMeta} + * @returns {BlockMeta?} */ getBlock() { @@ -377,7 +385,8 @@ class TXRecord extends bio.Struct { /** * Serialize a transaction to "extended format". - * @returns {Buffer} + * @param {BufioWriter} bw + * @returns {BufioWriter} */ write(bw) { @@ -405,8 +414,8 @@ class TXRecord extends bio.Struct { /** * Inject properties from "extended" format. - * @private - * @param {Buffer} data + * @param {bio.BufferReader} br + * @returns {this} */ read(br) { @@ -444,6 +453,11 @@ class MapRecord extends bio.Struct { this.wids = new Set(); } + /** + * @param {Number} wid + * @returns {Boolean} - Whether the map did not contain the wid. + */ + add(wid) { if (this.wids.has(wid)) return false; @@ -453,14 +467,29 @@ class MapRecord extends bio.Struct { return true; } + /** + * @param {Number} wid + * @returns {Boolean} - Whether the map contained the wid. + */ + remove(wid) { return this.wids.delete(wid); } + /** + * @param {Number} wid + * @returns {Boolean} - Whether the map contains the wid. + */ + has(wid) { return this.wids.has(wid); } + /** + * @param {BufioWriter} bw + * @returns {BufioWriter} + */ + write(bw) { bw.writeU32(this.wids.size); @@ -470,10 +499,19 @@ class MapRecord extends bio.Struct { return bw; } + /** + * @returns {Number} + */ + getSize() { return 4 + this.wids.size * 4; } + /** + * @param {bio.BufferReader} br + * @returns {this} + */ + read(br) { const count = br.readU32(); diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index 6b5fa1d6..040648b9 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -53,6 +53,7 @@ class TXDB { * Create a TXDB. * @constructor * @param {WalletDB} wdb + * @param {Number} [wid=0] */ constructor(wdb, wid) { diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index 75d3d536..64e1be88 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -22,6 +22,9 @@ const Script = require('../script/script'); const CoinView = require('../coins/coinview'); const WalletCoinView = require('./walletcoinview'); const WalletKey = require('./walletkey'); +const HDPrivateKey = require('../hd/private'); +const HDPublicKey = require('../hd/public'); +const Mnemonic = require('../hd/mnemonic'); const HD = require('../hd/hd'); const Output = require('../primitives/output'); const Account = require('./account'); @@ -35,15 +38,22 @@ const reserved = require('../covenants/reserved'); const {ownership} = require('../covenants/ownership'); const {states} = require('../covenants/namestate'); const {types} = rules; -const {Mnemonic} = HD; const {BufferSet} = require('buffer-map'); const Coin = require('../primitives/coin'); const Outpoint = require('../primitives/outpoint'); +/** @typedef {import('bdb').DB} DB */ +/** @typedef {ReturnType} Batch */ +/** @typedef {import('../types').Base58String} Base58String */ +/** @typedef {import('../types').Hash} Hash */ +/** @typedef {import('../types').Amount} Amount */ +/** @typedef {import('../types').Rate} Rate */ /** @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('./walletdb')} WalletDB */ /* * Constants @@ -67,6 +77,7 @@ class Wallet extends EventEmitter { /** * Create a wallet. * @constructor + * @param {WalletDB} wdb * @param {Object} options */ @@ -83,6 +94,7 @@ class Wallet extends EventEmitter { this.fundLock = new Lock(); this.wid = 0; + /** @type {String|null} */ this.id = null; this.watchOnly = false; this.accountDepth = 0; @@ -101,7 +113,6 @@ class Wallet extends EventEmitter { /** * Inject properties from options object. - * @private * @param {Object} options */ @@ -115,9 +126,9 @@ class Wallet extends EventEmitter { if (key) { if (typeof key === 'string') - key = HD.PrivateKey.fromBase58(key, this.network); + key = HDPrivateKey.fromBase58(key, this.network); - assert(HD.isPrivate(key), + assert(HDPrivateKey.isHDPrivateKey(key), 'Must create wallet with hd private key.'); } else { if (typeof mnemonic === 'string') @@ -126,7 +137,7 @@ class Wallet extends EventEmitter { if (!mnemonic) mnemonic = new Mnemonic({ language: options.language }); - key = HD.fromMnemonic(mnemonic, options.bip39Passphrase); + key = HDPrivateKey.fromMnemonic(mnemonic, options.bip39Passphrase); } this.master.fromKey(key, mnemonic); @@ -200,6 +211,8 @@ class Wallet extends EventEmitter { * the first addresses along with the lookahead * addresses). Called automatically from the * walletdb. + * @param {Object} options + * @param {(String|Buffer)?} [passphrase] * @returns {Promise} */ @@ -253,7 +266,7 @@ class Wallet extends EventEmitter { * Saves the key in the wallet database. * @param {(Number|String)} acct * @param {HDPublicKey} key - * @returns {Promise} + * @returns {Promise} */ async addSharedKey(acct, key) { @@ -270,7 +283,7 @@ class Wallet extends EventEmitter { * @private * @param {(Number|String)} acct * @param {HDPublicKey} key - * @returns {Promise} + * @returns {Promise} */ async _addSharedKey(acct, key) { @@ -290,7 +303,7 @@ class Wallet extends EventEmitter { * Remove a public account key from the wallet (multisig). * @param {(Number|String)} acct * @param {HDPublicKey} key - * @returns {Promise} + * @returns {Promise} */ async removeSharedKey(acct, key) { @@ -307,7 +320,7 @@ class Wallet extends EventEmitter { * @private * @param {(Number|String)} acct * @param {HDPublicKey} key - * @returns {Promise} + * @returns {Promise} */ async _removeSharedKey(acct, key) { @@ -317,7 +330,7 @@ class Wallet extends EventEmitter { throw new Error('Account not found.'); const b = this.db.batch(); - const result = await account.removeSharedKey(b, key); + const result = account.removeSharedKey(b, key); await b.write(); return result; @@ -414,7 +427,7 @@ class Wallet extends EventEmitter { /** * Generate a new token. * @param {(String|Buffer)?} passphrase - * @returns {Promise} + * @returns {Promise} */ async retoken(passphrase) { @@ -430,7 +443,7 @@ class Wallet extends EventEmitter { * Generate a new token without a lock. * @private * @param {(String|Buffer)?} passphrase - * @returns {Promise} + * @returns {Promise} */ async _retoken(passphrase) { @@ -465,7 +478,7 @@ class Wallet extends EventEmitter { /** * Rename account. - * @param {(String|Number)?} acct + * @param {String} acct * @param {String} name * @returns {Promise} */ @@ -482,7 +495,7 @@ class Wallet extends EventEmitter { /** * Rename account without a lock. * @private - * @param {(String|Number)?} acct + * @param {String} acct * @param {String} name * @returns {Promise} */ @@ -567,7 +580,6 @@ class Wallet extends EventEmitter { * Generate the wallet api key if none was passed in. * It is represented as BLAKE2b(m/44'->private|nonce). * @private - * @param {HDPrivateKey} master * @param {Number} nonce * @returns {Buffer} */ @@ -588,7 +600,8 @@ class Wallet extends EventEmitter { /** * Create an account. Requires passphrase if master key is encrypted. * @param {Object} options - See {@link Account} options. - * @returns {Promise} - Returns {@link Account}. + * @param {(String|Buffer)?} [passphrase] + * @returns {Promise} */ async createAccount(options, passphrase) { @@ -603,7 +616,8 @@ class Wallet extends EventEmitter { /** * Create an account without a lock. * @param {Object} options - See {@link Account} options. - * @returns {Promise} - Returns {@link Account}. + * @param {(String|Buffer)?} [passphrase] + * @returns {Promise} */ async _createAccount(options, passphrase) { @@ -622,9 +636,9 @@ class Wallet extends EventEmitter { key = options.accountKey; if (typeof key === 'string') - key = HD.PublicKey.fromBase58(key, this.network); + key = HDPublicKey.fromBase58(key, this.network); - if (!HD.isPublic(key)) + if (!HDPublicKey.isHDPublicKey(key)) throw new Error('Must add HD public keys to watch only wallet.'); } else { assert(this.master.key); @@ -690,7 +704,7 @@ class Wallet extends EventEmitter { * Create an account without a lock. * @param {String|Number} acct * @param {Object} options - * @param {String} [passphrase] + * @param {(String|Buffer)?} [passphrase] * @returns {Promise} */ @@ -716,7 +730,8 @@ class Wallet extends EventEmitter { /** * Ensure an account. Requires passphrase if master key is encrypted. * @param {Object} options - See {@link Account} options. - * @returns {Promise} - Returns {@link Account}. + * @param {(String|Buffer)?} [passphrase] + * @returns {Promise} */ async ensureAccount(options, passphrase) { @@ -731,7 +746,7 @@ class Wallet extends EventEmitter { /** * List account names and indexes from the db. - * @returns {Promise} - Returns Array. + * @returns {Promise} - Returns Array. */ getAccounts() { @@ -753,7 +768,7 @@ class Wallet extends EventEmitter { /** * Get all account address hashes. * @param {String|Number} acct - * @returns {Promise} - Returns Array. + * @returns {Promise} - Returns Array. */ async getAccountHashes(acct) { @@ -768,7 +783,7 @@ class Wallet extends EventEmitter { /** * Retrieve an account from the database. * @param {Number|String} acct - * @returns {Promise} + * @returns {Promise} */ async getAccount(acct) { @@ -792,10 +807,10 @@ class Wallet extends EventEmitter { /** * Lookup the corresponding account name's index. * @param {String|Number} acct - Account name/index. - * @returns {Promise} - Returns Number. + * @returns {Promise} */ - getAccountIndex(acct) { + async getAccountIndex(acct) { if (acct == null) return -1; @@ -826,8 +841,8 @@ class Wallet extends EventEmitter { /** * Lookup the corresponding account index's name. - * @param {Number} index - Account index. - * @returns {Promise} - Returns String. + * @param {(String|Number)} index - Account index. + * @returns {Promise} */ async getAccountName(index) { @@ -840,7 +855,7 @@ class Wallet extends EventEmitter { /** * Test whether an account exists. * @param {Number|String} acct - * @returns {Promise} - Returns {@link Boolean}. + * @returns {Promise} */ async hasAccount(acct) { @@ -855,7 +870,7 @@ class Wallet extends EventEmitter { /** * Create a new receiving address (increments receiveDepth). * @param {(Number|String)?} acct - * @returns {Promise} - Returns {@link WalletKey}. + * @returns {Promise} */ createReceive(acct = 0) { @@ -865,7 +880,7 @@ class Wallet extends EventEmitter { /** * Create a new change address (increments changeDepth). * @param {(Number|String)?} acct - * @returns {Promise} - Returns {@link WalletKey}. + * @returns {Promise} */ createChange(acct = 0) { @@ -876,7 +891,7 @@ class Wallet extends EventEmitter { * Create a new address (increments depth). * @param {(Number|String)?} acct * @param {Number} branch - * @returns {Promise} - Returns {@link WalletKey}. + * @returns {Promise} */ async createKey(acct, branch) { @@ -892,8 +907,8 @@ class Wallet extends EventEmitter { * Create a new address (increments depth) without a lock. * @private * @param {(Number|String)?} acct - * @param {Number} branche - * @returns {Promise} - Returns {@link WalletKey}. + * @param {Number} branch + * @returns {Promise} */ async _createKey(acct, branch) { @@ -912,7 +927,8 @@ class Wallet extends EventEmitter { /** * Save the wallet to the database. Necessary * when address depth and keys change. - * @returns {Promise} + * @param {Batch} b + * @returns {void} */ save(b) { @@ -921,7 +937,8 @@ class Wallet extends EventEmitter { /** * Increment the wid depth. - * @returns {Promise} + * @param {Batch} b + * @returns {void} */ increment(b) { @@ -931,7 +948,7 @@ class Wallet extends EventEmitter { /** * Test whether the wallet possesses an address. * @param {Address|Hash} address - * @returns {Promise} - Returns Boolean. + * @returns {Promise} */ async hasAddress(address) { @@ -943,7 +960,7 @@ class Wallet extends EventEmitter { /** * Get path by address hash. * @param {Address|Hash} address - * @returns {Promise} - Returns {@link Path}. + * @returns {Promise} */ async getPath(address) { @@ -955,7 +972,7 @@ class Wallet extends EventEmitter { * Get path by address hash (without account name). * @private * @param {Address|Hash} address - * @returns {Promise} - Returns {@link Path}. + * @returns {Promise} */ async readPath(address) { @@ -977,7 +994,7 @@ class Wallet extends EventEmitter { /** * Get all wallet paths. * @param {(String|Number)?} acct - * @returns {Promise} - Returns {@link Path}. + * @returns {Promise} */ async getPaths(acct) { @@ -990,7 +1007,7 @@ class Wallet extends EventEmitter { /** * Get all account paths. * @param {String|Number} acct - * @returns {Promise} - Returns {@link Path}. + * @returns {Promise} */ async getAccountPaths(acct) { @@ -1089,8 +1106,7 @@ class Wallet extends EventEmitter { * Import a keyring (will not exist on derivation chain). * Rescanning must be invoked manually. * @param {(String|Number)?} acct - * @param {WalletKey} ring - * @param {(String|Buffer)?} passphrase + * @param {Address} address * @returns {Promise} */ @@ -1107,8 +1123,7 @@ class Wallet extends EventEmitter { * Import a keyring (will not exist on derivation chain) without a lock. * @private * @param {(String|Number)?} acct - * @param {WalletKey} ring - * @param {(String|Buffer)?} passphrase + * @param {Address} address * @returns {Promise} */ @@ -1174,7 +1189,7 @@ class Wallet extends EventEmitter { * @see MTX#selectCoins * @see MTX#fill * @param {MTX} mtx - _Must_ be a mutable transaction. - * @param {Object?} options + * @param {Object} [options] * @param {(String|Number)?} options.account - If no account is * specified, coins from the entire wallet will be filled. * @param {String?} options.selection - Coin selection priority. Can @@ -1190,6 +1205,7 @@ class Wallet extends EventEmitter { * calculating one. * @param {Number|Boolean} options.subtractFee - Whether to subtract the * fee from existing outputs rather than adding more inputs. + * @param {Boolean} [force] */ async fund(mtx, options, force) { @@ -1206,6 +1222,8 @@ class Wallet extends EventEmitter { * @private * @see MTX#selectCoins * @see MTX#fill + * @param {MTX} mtx + * @param {Object} [options] */ async fill(mtx, options) { @@ -1357,7 +1375,8 @@ class Wallet extends EventEmitter { /** * Make a claim MTX. * @param {String} name - * @returns {Claim} + * @param {Object?} [options] + * @returns {Promise} */ async _createClaim(name, options) { @@ -1496,8 +1515,8 @@ class Wallet extends EventEmitter { /** * Make a claim proof. * @param {String} name - * @param {Object} options - * @returns {Claim} + * @param {Object?} [options] + * @returns {Promise} */ async makeFakeClaim(name, options) { diff --git a/lib/wallet/walletcoinview.js b/lib/wallet/walletcoinview.js index 711b97ca..ffce981b 100644 --- a/lib/wallet/walletcoinview.js +++ b/lib/wallet/walletcoinview.js @@ -11,14 +11,18 @@ const {BufferMap} = require('buffer-map'); const Paths = require('./paths'); const CoinView = require('../coins/coinview'); +/** @typedef {import('../types').Hash} Hash */ +/** @typedef {import('../primitives/outpoint')} Outpoint */ +/** @typedef {import('../primitives/input')} Input */ +/** @typedef {import('../primitives/coin')} Coin */ +/** @typedef {import('../coins/coins')} Coins */ +/** @typedef {import('./path')} Path */ + /** * Wallet Coin View * Represents a wallet, coin viewpoint: a snapshot of {@link Coins} objects * and the HD paths for their associated keys. * @alias module:wallet.WalletCoinView - * @property {Object} map - * @property {Object} paths - * @property {UndoCoins} undo */ class WalletCoinView extends CoinView { @@ -59,7 +63,7 @@ class WalletCoinView extends CoinView { /** * Add paths to the collection. * @param {Hash} hash - * @param {Paths} path + * @param {Paths} paths * @returns {Paths|null} */ @@ -91,7 +95,7 @@ class WalletCoinView extends CoinView { /** * Ensure existence of paths object in the collection. * @param {Hash} hash - * @returns {Coins} + * @returns {Paths} */ ensurePaths(hash) { @@ -105,7 +109,7 @@ class WalletCoinView extends CoinView { /** * Remove paths from the collection. - * @param {Paths} paths + * @param {Hash} hash * @returns {Paths|null} */ diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index 60705f15..d223ede6 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -35,9 +35,12 @@ const {states} = require('../covenants/namestate'); const util = require('../utils/util'); const {scanActions} = require('../blockchain/common'); -/** @typedef {import('../primitives/tx')} TX */ -/** @typedef {import('../blockchain/common').ScanAction} ScanAction */ +/** @typedef {ReturnType} Batch */ /** @typedef {import('../types').Hash} Hash */ +/** @typedef {import('../primitives/tx')} TX */ +/** @typedef {import('../primitives/claim')} Claim */ +/** @typedef {import('../blockchain/common').ScanAction} ScanAction */ +/** @typedef {import('./walletkey')} WalletKey */ const { ChainState, @@ -83,6 +86,7 @@ class WalletDB extends EventEmitter { this.workers = this.options.workers; this.client = this.options.client || new NullClient(this); this.feeRate = this.options.feeRate; + /** @type {bdb.DB} */ this.db = bdb.create(this.options); this.name = 'wallet'; this.version = 4; @@ -348,7 +352,7 @@ class WalletDB extends EventEmitter { return b.write(); } - const magic = raw.readUInt32LE(0, true); + const magic = raw.readUInt32LE(0); if (magic !== this.network.magic) throw new Error('Network mismatch for WalletDB.'); @@ -590,7 +594,6 @@ class WalletDB extends EventEmitter { /** * Rescan blockchain from a given height. * Needs this.rescanning = true to be set from the caller. - * @private * @param {Number} [height=this.state.startHeight] * @returns {Promise} */ @@ -716,7 +719,7 @@ class WalletDB extends EventEmitter { ); // remove all txdb data *except* blinds ('v') - const key = 'v'.charCodeAt(); + const key = 'v'.charCodeAt(0); const prefix = layout.t.encode(wallet.wid); await removeRange({ gte: Buffer.concat([prefix, Buffer.alloc(1)]), @@ -839,7 +842,7 @@ class WalletDB extends EventEmitter { /** * Get name state. * @param {Buffer} nameHash - * @returns {Object} + * @returns {Promise} */ async getNameStatus(nameHash) { @@ -850,7 +853,7 @@ class WalletDB extends EventEmitter { * Get UTXO from node. * @param {Hash} hash * @param {Number} index - * @returns {Object} + * @returns {Promise} */ async getCoin(hash, index) { @@ -860,7 +863,7 @@ class WalletDB extends EventEmitter { /** * Test whether name is available for CLAIM. * @param {Buffer} nameHash - * @returns {Boolean} + * @returns {Promise} */ async isAvailable(nameHash) { @@ -966,13 +969,13 @@ class WalletDB extends EventEmitter { if (!raw) return 0; - return raw.readUInt32LE(0, true); + return raw.readUInt32LE(0); } /** * Test the bloom filter against a tx or address hash. * @private - * @param {Hash} hash + * @param {Hash} data * @returns {Boolean} */ @@ -1025,7 +1028,7 @@ class WalletDB extends EventEmitter { /** * Register an object with the walletdb. - * @param {Object} object + * @param {Wallet} wallet */ register(wallet) { @@ -1035,8 +1038,7 @@ class WalletDB extends EventEmitter { /** * Unregister a object with the walletdb. - * @param {Object} object - * @returns {Boolean} + * @param {Wallet} wallet */ unregister(wallet) { @@ -1074,7 +1076,7 @@ class WalletDB extends EventEmitter { assert(data.length === 4); - return data.readUInt32LE(0, true); + return data.readUInt32LE(0); } /** @@ -1148,6 +1150,7 @@ class WalletDB extends EventEmitter { /** * Save a wallet to the database. + * @param {Batch} b * @param {Wallet} wallet */ @@ -1242,7 +1245,7 @@ class WalletDB extends EventEmitter { /** * Remove a wallet. * @param {Number|String} id - * @returns {Promise} + * @returns {Promise} */ async remove(id) { @@ -1269,7 +1272,7 @@ class WalletDB extends EventEmitter { * Remove a wallet (without a lock). * @private * @param {Number} wid - * @returns {Promise} + * @returns {Promise} */ async _remove(wid) { @@ -1480,7 +1483,6 @@ class WalletDB extends EventEmitter { /** * Get an account from the database by wid. - * @private * @param {Number} wid * @param {Number} index - Account index. * @returns {Promise} - Returns {@link Wallet}. @@ -1506,7 +1508,7 @@ class WalletDB extends EventEmitter { /** * List account names and indexes from the db. * @param {Number} wid - * @returns {Promise} - Returns Array. + * @returns {Promise} - Returns Array. */ async getAccounts(wid) { @@ -1521,7 +1523,7 @@ class WalletDB extends EventEmitter { * Lookup the corresponding account name's index. * @param {Number} wid * @param {String} name - Account name/index. - * @returns {Promise} - Returns Number. + * @returns {Promise} */ async getAccountIndex(wid, name) { @@ -1530,14 +1532,14 @@ class WalletDB extends EventEmitter { if (!index) return -1; - return index.readUInt32LE(0, true); + return index.readUInt32LE(0); } /** * Lookup the corresponding account index's name. * @param {Number} wid * @param {Number} index - * @returns {Promise} - Returns Number. + * @returns {Promise} */ async getAccountName(wid, index) { @@ -1551,8 +1553,8 @@ class WalletDB extends EventEmitter { /** * Save an account to the database. + * @param {Batch} b * @param {Account} account - * @returns {Promise} */ saveAccount(b, account) { @@ -1573,8 +1575,8 @@ class WalletDB extends EventEmitter { /** * Test for the existence of an account. * @param {Number} wid - * @param {String|Number} acct - * @returns {Promise} - Returns Boolean. + * @param {Number} index + * @returns {Promise} */ async hasAccount(wid, index) { @@ -1583,7 +1585,8 @@ class WalletDB extends EventEmitter { /** * Save an address to the path map. - * @param {Wallet} wallet + * @param {Batch} b + * @param {Number} wid * @param {WalletKey} ring * @returns {Promise} */ @@ -1600,7 +1603,8 @@ class WalletDB extends EventEmitter { * - `P[wid][address-hash] -> path data` * - `r[wid][account-index][address-hash] -> dummy` * - * @param {Wallet} wallet + * @param {Batch} b + * @param {Number} wid * @param {Path} path * @returns {Promise} */ @@ -1620,7 +1624,7 @@ class WalletDB extends EventEmitter { * Retrieve path by hash. * @param {Number} wid * @param {Hash} hash - * @returns {Promise} + * @returns {Promise} */ async getPath(wid, hash) { @@ -1639,7 +1643,7 @@ class WalletDB extends EventEmitter { * Retrieve path by hash. * @param {Number} wid * @param {Hash} hash - * @returns {Promise} + * @returns {Promise} */ async readPath(wid, hash) { @@ -1658,7 +1662,7 @@ class WalletDB extends EventEmitter { * Test whether a wallet contains a path. * @param {Number} wid * @param {Hash} hash - * @returns {Promise} + * @returns {Promise} */ async hasPath(wid, hash) { @@ -1712,7 +1716,7 @@ class WalletDB extends EventEmitter { * Get all account address hashes. * @param {Number} wid * @param {Number} account - * @returns {Promise} + * @returns {Promise} */ async getAccountHashes(wid, account) { @@ -1726,7 +1730,7 @@ class WalletDB extends EventEmitter { /** * Get all paths for a wallet. * @param {Number} wid - * @returns {Promise} + * @returns {Promise} */ async getWalletPaths(wid) { @@ -2068,9 +2072,10 @@ class WalletDB extends EventEmitter { /** * Add wid to a wallet map. - * @param {Wallet} wallet + * @param {Batch} b * @param {Buffer} key * @param {Number} wid + * @returns {Promise} */ async addMap(b, key, wid) { @@ -2095,7 +2100,7 @@ class WalletDB extends EventEmitter { /** * Remove wid from a wallet map. - * @param {Wallet} wallet + * @param {Batch} b * @param {Buffer} key * @param {Number} wid */ diff --git a/lib/wallet/walletkey.js b/lib/wallet/walletkey.js index a8c1e988..4fa5d29b 100644 --- a/lib/wallet/walletkey.js +++ b/lib/wallet/walletkey.js @@ -9,6 +9,11 @@ const KeyRing = require('../primitives/keyring'); const Path = require('./path'); +/** @typedef {import('../hd/private')} HDPrivateKey */ +/** @typedef {import('../hd/public')} HDPublicKey */ +/** @typedef {import('../protocol/network')} Network */ +/** @typedef {import('./account')} Account */ + /** * Wallet Key * Represents a key ring which amounts to an address. @@ -36,6 +41,7 @@ class WalletKey extends KeyRing { /** * Convert an WalletKey to a more json-friendly object. + * @param {Network} [network] * @returns {Object} */ @@ -53,12 +59,11 @@ class WalletKey extends KeyRing { /** * Inject properties from hd key. - * @private * @param {Account} account * @param {HDPrivateKey|HDPublicKey} key * @param {Number} branch * @param {Number} index - * @returns {WalletKey} + * @returns {this} */ fromHD(account, key, branch, index) { @@ -89,7 +94,6 @@ class WalletKey extends KeyRing { /** * Inject properties from imported data. - * @private * @param {Account} account * @param {Buffer} data * @returns {WalletKey}