Skip to content

Commit dd36a77

Browse files
committed
wdb: update types.
1 parent 343525a commit dd36a77

File tree

15 files changed

+287
-158
lines changed

15 files changed

+287
-158
lines changed

lib/coins/coinview.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ class CoinView extends View {
313313
* Get an HD path by prevout.
314314
* Implemented in {@link WalletCoinView}.
315315
* @param {Outpoint} prevout
316-
* @returns {null}
316+
* @returns {*}
317317
*/
318318

319319
getPath(prevout) {
@@ -404,7 +404,7 @@ class CoinView extends View {
404404
* Get a single path by input.
405405
* Implemented in {@link WalletCoinView}.
406406
* @param {Input} input
407-
* @returns {null}
407+
* @returns {*}
408408
*/
409409

410410
getPathFor(input) {

lib/hd/common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ common.isMaster = function isMaster(key) {
118118
/**
119119
* Test whether the key is (most likely) a BIP44 account key.
120120
* @param {HDPrivateKey|HDPublicKey} key
121-
* @param {Number?} account
121+
* @param {Number?} [account]
122122
* @returns {Boolean}
123123
*/
124124

lib/hd/public.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ class HDPublicKey extends bio.Struct {
237237
/**
238238
* Test whether the key is (most likely) a BIP44 account key.
239239
* @method
240-
* @param {Number?} account
240+
* @param {Number?} [account]
241241
* @returns {Boolean}
242242
*/
243243

lib/primitives/keyring.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class KeyRing extends bio.Struct {
106106
/**
107107
* Inject data from private key.
108108
* @param {Buffer} key
109+
* @returns {this}
109110
*/
110111

111112
fromPrivate(key) {

lib/wallet/account.js

Lines changed: 70 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@ const Path = require('./path');
1313
const common = require('./common');
1414
const Script = require('../script/script');
1515
const WalletKey = require('./walletkey');
16-
const {HDPublicKey} = require('../hd/hd');
16+
const HDPublicKey = require('../hd/public');
17+
18+
/** @typedef {import('bdb').DB} DB */
19+
/** @typedef {ReturnType<DB['batch']>} Batch */
20+
/** @typedef {import('../types').BufioWriter} BufioWriter */
21+
/** @typedef {import('./walletdb')} WalletDB */
22+
/** @typedef {import('./masterkey')} MasterKey */
23+
/** @typedef {import('../primitives/address')} Address */
1724

1825
/**
1926
* Account
@@ -28,6 +35,7 @@ class Account extends bio.Struct {
2835
/**
2936
* Create an account.
3037
* @constructor
38+
* @param {WalletDB} wdb
3139
* @param {Object} options
3240
*/
3341

@@ -36,22 +44,28 @@ class Account extends bio.Struct {
3644

3745
assert(wdb, 'Database is required.');
3846

47+
/** @type {WalletDB} */
3948
this.wdb = wdb;
4049
this.network = wdb.network;
4150

4251
this.wid = 0;
52+
/** @type {String|null} */
4353
this.id = null;
4454
this.accountIndex = 0;
55+
/** @type {String|null} */
4556
this.name = null;
4657
this.initialized = false;
4758
this.watchOnly = false;
59+
/** @type {Account.types} */
4860
this.type = Account.types.PUBKEYHASH;
4961
this.m = 1;
5062
this.n = 1;
5163
this.receiveDepth = 0;
5264
this.changeDepth = 0;
5365
this.lookahead = 200;
66+
/** @type {HDPublicKey|null} */
5467
this.accountKey = null;
68+
/** @type {HDPublicKey[]} */
5569
this.keys = [];
5670

5771
if (options)
@@ -60,8 +74,8 @@ class Account extends bio.Struct {
6074

6175
/**
6276
* Inject properties from options object.
63-
* @private
6477
* @param {Object} options
78+
* @returns {this}
6579
*/
6680

6781
fromOptions(options) {
@@ -155,7 +169,7 @@ class Account extends bio.Struct {
155169

156170
/**
157171
* Inject properties from options object.
158-
* @private
172+
* @param {WalletDB} wdb
159173
* @param {Object} options
160174
*/
161175

@@ -168,6 +182,7 @@ class Account extends bio.Struct {
168182
* the first addresses along with the lookahead
169183
* addresses). Called automatically from the
170184
* walletdb.
185+
* @param {Batch} b
171186
* @returns {Promise}
172187
*/
173188

@@ -193,6 +208,7 @@ class Account extends bio.Struct {
193208
* @param {HDPublicKey} key - Account (bip44)
194209
* key (can be in base58 form).
195210
* @throws Error on non-hdkey/non-accountkey.
211+
* @returns {Boolean} - Whether the key was added.
196212
*/
197213

198214
pushKey(key) {
@@ -230,6 +246,7 @@ class Account extends bio.Struct {
230246
* @param {HDPublicKey} key - Account (bip44)
231247
* key (can be in base58 form).
232248
* @throws Error on non-hdkey/non-accountkey.
249+
* @returns {Boolean} - Whether the key was removed.
233250
*/
234251

235252
spliceKey(key) {
@@ -254,8 +271,9 @@ class Account extends bio.Struct {
254271
/**
255272
* Add a public account key to the account (multisig).
256273
* Saves the key in the wallet database.
274+
* @param {Batch} b
257275
* @param {HDPublicKey} key
258-
* @returns {Promise}
276+
* @returns {Promise<Boolean>}
259277
*/
260278

261279
async addSharedKey(b, key) {
@@ -275,7 +293,7 @@ class Account extends bio.Struct {
275293
/**
276294
* Ensure accounts are not sharing keys.
277295
* @private
278-
* @returns {Promise}
296+
* @returns {Promise<Boolean>}
279297
*/
280298

281299
async hasDuplicate() {
@@ -291,8 +309,9 @@ class Account extends bio.Struct {
291309
/**
292310
* Remove a public account key from the account (multisig).
293311
* Remove the key from the wallet database.
312+
* @param {Batch} b
294313
* @param {HDPublicKey} key
295-
* @returns {Promise}
314+
* @returns {Boolean}
296315
*/
297316

298317
removeSharedKey(b, key) {
@@ -308,26 +327,29 @@ class Account extends bio.Struct {
308327

309328
/**
310329
* Create a new receiving address (increments receiveDepth).
311-
* @returns {WalletKey}
330+
* @param {Batch} b
331+
* @returns {Promise<WalletKey>}
312332
*/
313333

314-
createReceive() {
315-
return this.createKey(0);
334+
createReceive(b) {
335+
return this.createKey(b, 0);
316336
}
317337

318338
/**
319339
* Create a new change address (increments changeDepth).
320-
* @returns {WalletKey}
340+
* @param {Batch} b
341+
* @returns {Promise<WalletKey>}
321342
*/
322343

323-
createChange() {
324-
return this.createKey(1);
344+
createChange(b) {
345+
return this.createKey(b, 1);
325346
}
326347

327348
/**
328349
* Create a new address (increments depth).
329-
* @param {Boolean} change
330-
* @returns {Promise} - Returns {@link WalletKey}.
350+
* @param {Batch} b
351+
* @param {Number} branch
352+
* @returns {Promise<WalletKey>} - Returns {@link WalletKey}.
331353
*/
332354

333355
async createKey(b, branch) {
@@ -360,6 +382,7 @@ class Account extends bio.Struct {
360382
/**
361383
* Derive a receiving address at `index`. Do not increment depth.
362384
* @param {Number} index
385+
* @param {MasterKey} [master]
363386
* @returns {WalletKey}
364387
*/
365388

@@ -370,6 +393,7 @@ class Account extends bio.Struct {
370393
/**
371394
* Derive a change address at `index`. Do not increment depth.
372395
* @param {Number} index
396+
* @param {MasterKey} [master]
373397
* @returns {WalletKey}
374398
*/
375399

@@ -381,7 +405,7 @@ class Account extends bio.Struct {
381405
* Derive an address from `path` object.
382406
* @param {Path} path
383407
* @param {MasterKey} master
384-
* @returns {WalletKey}
408+
* @returns {WalletKey?}
385409
*/
386410

387411
derivePath(path, master) {
@@ -415,6 +439,7 @@ class Account extends bio.Struct {
415439
* Derive an address at `index`. Do not increment depth.
416440
* @param {Number} branch
417441
* @param {Number} index
442+
* @param {MasterKey} [master]
418443
* @returns {WalletKey}
419444
*/
420445

@@ -456,7 +481,8 @@ class Account extends bio.Struct {
456481
/**
457482
* Save the account to the database. Necessary
458483
* when address depth and keys change.
459-
* @returns {Promise}
484+
* @param {Batch} b
485+
* @returns {void}
460486
*/
461487

462488
save(b) {
@@ -465,7 +491,8 @@ class Account extends bio.Struct {
465491

466492
/**
467493
* Save addresses to path map.
468-
* @param {WalletKey[]} rings
494+
* @param {Batch} b
495+
* @param {WalletKey} ring
469496
* @returns {Promise}
470497
*/
471498

@@ -475,7 +502,8 @@ class Account extends bio.Struct {
475502

476503
/**
477504
* Save paths to path map.
478-
* @param {Path[]} rings
505+
* @param {Batch} b
506+
* @param {Path} path
479507
* @returns {Promise}
480508
*/
481509

@@ -485,6 +513,7 @@ class Account extends bio.Struct {
485513

486514
/**
487515
* Initialize address depths (including lookahead).
516+
* @param {Batch} b
488517
* @returns {Promise}
489518
*/
490519

@@ -510,8 +539,9 @@ class Account extends bio.Struct {
510539

511540
/**
512541
* Allocate new lookahead addresses if necessary.
513-
* @param {Number} receiveDepth
514-
* @param {Number} changeDepth
542+
* @param {Batch} b
543+
* @param {Number} receive
544+
* @param {Number} change
515545
* @returns {Promise<WalletKey?>}
516546
*/
517547

@@ -558,6 +588,7 @@ class Account extends bio.Struct {
558588

559589
/**
560590
* Allocate new lookahead addresses.
591+
* @param {Batch} b
561592
* @param {Number} lookahead
562593
* @returns {Promise}
563594
*/
@@ -607,7 +638,7 @@ class Account extends bio.Struct {
607638

608639
/**
609640
* Get current receive key.
610-
* @returns {WalletKey}
641+
* @returns {WalletKey?}
611642
*/
612643

613644
receiveKey() {
@@ -619,7 +650,7 @@ class Account extends bio.Struct {
619650

620651
/**
621652
* Get current change key.
622-
* @returns {WalletKey}
653+
* @returns {WalletKey?}
623654
*/
624655

625656
changeKey() {
@@ -631,7 +662,7 @@ class Account extends bio.Struct {
631662

632663
/**
633664
* Get current receive address.
634-
* @returns {Address}
665+
* @returns {Address?}
635666
*/
636667

637668
receiveAddress() {
@@ -645,7 +676,7 @@ class Account extends bio.Struct {
645676

646677
/**
647678
* Get current change address.
648-
* @returns {Address}
679+
* @returns {Address?}
649680
*/
650681

651682
changeAddress() {
@@ -690,6 +721,7 @@ class Account extends bio.Struct {
690721
/**
691722
* Convert the account to an object suitable for
692723
* serialization.
724+
* @param {Object} [balance=null]
693725
* @returns {Object}
694726
*/
695727

@@ -730,7 +762,8 @@ class Account extends bio.Struct {
730762

731763
/**
732764
* Serialize the account.
733-
* @returns {Buffer}
765+
* @param {BufioWriter} bw
766+
* @returns {BufioWriter}
734767
*/
735768

736769
write(bw) {
@@ -757,9 +790,7 @@ class Account extends bio.Struct {
757790

758791
/**
759792
* Inject properties from serialized data.
760-
* @private
761-
* @param {Buffer} data
762-
* @returns {Object}
793+
* @param {bio.BufferReader} br
763794
*/
764795

765796
read(br) {
@@ -837,6 +868,12 @@ function cmp(a, b) {
837868
return a.compare(b);
838869
}
839870

871+
/**
872+
* @param {HDPublicKey} key
873+
* @param {BufioWriter} bw
874+
* @returns {void}
875+
*/
876+
840877
function writeKey(key, bw) {
841878
bw.writeU8(key.depth);
842879
bw.writeU32BE(key.parentFingerPrint);
@@ -845,6 +882,11 @@ function writeKey(key, bw) {
845882
bw.writeBytes(key.publicKey);
846883
}
847884

885+
/**
886+
* @param {bio.BufferReader} br
887+
* @returns {HDPublicKey}
888+
*/
889+
848890
function readKey(br) {
849891
const key = new HDPublicKey();
850892
key.depth = br.readU8();

0 commit comments

Comments
 (0)