Skip to content

Commit

Permalink
wdb: update types.
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Jan 15, 2025
1 parent 343525a commit dd36a77
Show file tree
Hide file tree
Showing 15 changed files with 287 additions and 158 deletions.
4 changes: 2 additions & 2 deletions lib/coins/coinview.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion lib/hd/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/

Expand Down
2 changes: 1 addition & 1 deletion lib/hd/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/

Expand Down
1 change: 1 addition & 0 deletions lib/primitives/keyring.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class KeyRing extends bio.Struct {
/**
* Inject data from private key.
* @param {Buffer} key
* @returns {this}
*/

fromPrivate(key) {
Expand Down
98 changes: 70 additions & 28 deletions lib/wallet/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<DB['batch']>} Batch */
/** @typedef {import('../types').BufioWriter} BufioWriter */
/** @typedef {import('./walletdb')} WalletDB */
/** @typedef {import('./masterkey')} MasterKey */
/** @typedef {import('../primitives/address')} Address */

/**
* Account
Expand All @@ -28,6 +35,7 @@ class Account extends bio.Struct {
/**
* Create an account.
* @constructor
* @param {WalletDB} wdb
* @param {Object} options
*/

Expand All @@ -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)
Expand All @@ -60,8 +74,8 @@ class Account extends bio.Struct {

/**
* Inject properties from options object.
* @private
* @param {Object} options
* @returns {this}
*/

fromOptions(options) {
Expand Down Expand Up @@ -155,7 +169,7 @@ class Account extends bio.Struct {

/**
* Inject properties from options object.
* @private
* @param {WalletDB} wdb
* @param {Object} options
*/

Expand All @@ -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}
*/

Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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<Boolean>}
*/

async addSharedKey(b, key) {
Expand All @@ -275,7 +293,7 @@ class Account extends bio.Struct {
/**
* Ensure accounts are not sharing keys.
* @private
* @returns {Promise}
* @returns {Promise<Boolean>}
*/

async hasDuplicate() {
Expand All @@ -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) {
Expand All @@ -308,26 +327,29 @@ class Account extends bio.Struct {

/**
* Create a new receiving address (increments receiveDepth).
* @returns {WalletKey}
* @param {Batch} b
* @returns {Promise<WalletKey>}
*/

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<WalletKey>}
*/

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<WalletKey>} - Returns {@link WalletKey}.
*/

async createKey(b, branch) {
Expand Down Expand Up @@ -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}
*/

Expand All @@ -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}
*/

Expand All @@ -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) {
Expand Down Expand Up @@ -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}
*/

Expand Down Expand Up @@ -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) {
Expand All @@ -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}
*/

Expand All @@ -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}
*/

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

/**
* Initialize address depths (including lookahead).
* @param {Batch} b
* @returns {Promise}
*/

Expand All @@ -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<WalletKey?>}
*/

Expand Down Expand Up @@ -558,6 +588,7 @@ class Account extends bio.Struct {

/**
* Allocate new lookahead addresses.
* @param {Batch} b
* @param {Number} lookahead
* @returns {Promise}
*/
Expand Down Expand Up @@ -607,7 +638,7 @@ class Account extends bio.Struct {

/**
* Get current receive key.
* @returns {WalletKey}
* @returns {WalletKey?}
*/

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

/**
* Get current change key.
* @returns {WalletKey}
* @returns {WalletKey?}
*/

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

/**
* Get current receive address.
* @returns {Address}
* @returns {Address?}
*/

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

/**
* Get current change address.
* @returns {Address}
* @returns {Address?}
*/

changeAddress() {
Expand Down Expand Up @@ -690,6 +721,7 @@ class Account extends bio.Struct {
/**
* Convert the account to an object suitable for
* serialization.
* @param {Object} [balance=null]
* @returns {Object}
*/

Expand Down Expand Up @@ -730,7 +762,8 @@ class Account extends bio.Struct {

/**
* Serialize the account.
* @returns {Buffer}
* @param {BufioWriter} bw
* @returns {BufioWriter}
*/

write(bw) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down
Loading

0 comments on commit dd36a77

Please sign in to comment.