wallet-toolbox-examples

Examples: BSV Wallet Toolbox API Documentation

The examples documentation is split into various pages, common bits are described here, more complex examples have their own pages:

Getting Started

Installation

To install the toolbox, run:

git clone https://github.com/bitcoin-sv/wallet-toolbox-examples

cd wallet-toolbox-examples

npm install

cd src

npx tsx makeEnv > .env

cat .env

Return To Top

API

Links: API, Functions

Functions

 
backup
backupToSQLite
backupWalletClient
balanceSpecOp
balances
makeEnv
runArgv2Function
walletBalance

Links: API, Functions


Function: backup
export async function backup(): Promise<void> {
    const env = Setup.getEnv("test");
    await backupWalletClient(env, env.identityKey);
}

See also: backupWalletClient

Links: API, Functions


Function: backupToSQLite
export async function backupToSQLite(setup: SetupWallet, filePath?: string, databaseName?: string): Promise<void> {
    const env = Setup.getEnv(setup.chain);
    filePath ||= `backup_${setup.identityKey}.sqlite`;
    databaseName ||= `${setup.identityKey} backup`;
    const backup = await Setup.createStorageKnex({
        env,
        knex: Setup.createSQLiteKnex(filePath),
        databaseName,
        rootKeyHex: setup.keyDeriver.rootKey.toHex()
    });
    await setup.storage.addWalletStorageProvider(backup);
    await setup.storage.updateBackups();
}

See also: backup

Links: API, Functions


Function: backupWalletClient
export async function backupWalletClient(env: SetupEnv, identityKey: string): Promise<void> {
    const setup = await Setup.createWalletClient({
        env,
        rootKeyHex: env.devKeys[identityKey]
    });
    await backupToSQLite(setup);
    await setup.wallet.destroy();
}

See also: backupToSQLite

Links: API, Functions


Function: balanceSpecOp

Special Operations (specOps) are extensions to the base BRC-100 Wallet standard operations.

This implementation of change balance computation uses specOpWalletBalance, which is a special ‘basket’ value that modifies the default behavior of the listOutputs method.

In the case of specOpWalletBalance, it automatically selects all the spendable, ‘default’ basket, change outputs and returns the sum of their satoshis properties, returning the sum as the totalOutputs property.

This is not only simpler to code, but more efficient as the outputs do not need to be sent to the client. Only the sum of satoshis is returned.

This function can be run from the command line as:

npx txs balances balanceSpecOp
export async function balanceSpecOp(): Promise<void> 

Links: API, Functions


Function: balances

The balance function demonstrates creating a ServerClient based wallet and calculating the wallet’s “balance” as the sum of spendable outputs in the ‘default’ basket.

The ‘default’ basket holds the outputs that are used to automatically fund new actions, and receives new outputs generated to recapture excess funding.

Run this function using the following command:

npx tsx balances
export async function balances(): Promise<void> {
    const env = Setup.getEnv("test");
    for (const identityKey of [env.identityKey, env.identityKey2]) {
        const setup = await Setup.createWalletClient({
            env,
            rootKeyHex: env.devKeys[identityKey]
        });
        let balance = 0;
        let offset = 0;
        for (;;) {
            const change = await setup.wallet.listOutputs({
                basket: "default",
                limit: 10,
                offset
            });
            balance += change.outputs.reduce((b, o) => (b += o.satoshis), 0);
            offset += change.outputs.length;
            if (change.outputs.length === 0 || offset >= change.totalOutputs)
                break;
        }
        console.log(`balance for ${identityKey} = ${balance}`);
    }
}

Links: API, Functions


Function: makeEnv

Running the makeEnv function generates several new private keys and related .env file initializers which simplify use of the Setup functions.

After running the function, copy or capture the output into a file named .env in the src folder of this repository.

Note that you can replace or add to the auto-generated keys.

The following command will run the function, capture the output into a file named ‘.env’, and display the file’s contents:

npx tsx makeEnv > .env; cat .env
export function makeEnv() {
    Setup.makeEnv();
}

Links: API, Functions


Function: runArgv2Function

Used to run a named function from a command line of the form:

npx txs filename.ts functionName

Where functionName is an exported async function taking no arguments returning void.

Does nothing if functionName doesn’t resolve to an exported function.

Optionally, if there is a functionName in module_exports that matches the filename, then ‘functionName’ can be ommitted.

export function runArgv2Function(module_exports: object): void 

Argument Details

Links: API, Functions


Function: walletBalance

And if your BRC-100 wallet supports the balance extension method based on specOpWalletBalance, this is the fastest and easiest way.

This function can be run from the command line as:

npx txs balances walletBalance
export async function walletBalance(): Promise<void> 

Links: API, Functions