Skip to content

Commit ecf8993

Browse files
committed
Add Setup and SetupClient
1 parent 4228d12 commit ecf8993

File tree

7 files changed

+405
-14
lines changed

7 files changed

+405
-14
lines changed

README.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ The BSV Wallet Toolbox builds on the [SDK](https://bitcoin-sv.github.io/ts-sdk)
99

1010
# Table of Contents
1111

12-
- [Objective](#objective)
13-
- [Getting Started](#getting-started)
14-
- [Features \& Deliverables](#features--deliverables)
15-
- [Documentation](#documentation)
16-
- [Contribution Guidelines](#contribution-guidelines)
17-
- [Support \& Contacts](#support--contacts)
18-
- [License](#license)
12+
- [Objective](#objective)
13+
- [Getting Started](#getting-started)
14+
- [Features \& Deliverables](#features--deliverables)
15+
- [Documentation](#documentation)
16+
- [Contribution Guidelines](#contribution-guidelines)
17+
- [Support \& Contacts](#support--contacts)
18+
- [License](#license)
1919

2020
## Objective
2121

@@ -37,14 +37,14 @@ npm install @bsv/wallet-toolbox
3737

3838
Here's a simple example of using the toolbox to create and fund a testnet wallet using SQLite for persistent storage:
3939

40-
```javascript
40+
```ts
4141
import { InternalizeActionArgs, PrivateKey, Utils } from '@bsv/sdk'
42-
import { test } from '@bsv/wallet-toolbox'
42+
import { Setup } from '@bsv/wallet-toolbox'
4343

4444
const rootKeyHex = PrivateKey.fromRandom().toString()
4545
console.log(`MAKE A SECURE COPY OF YOUR WALLET PRIVATE ROOT KEY: ${rootKeyHex}`)
4646

47-
const { wallet } = await test._tu.createSQLiteTestWallet({
47+
const { wallet } = await Setup.createSQLiteWallet({
4848
filePath: './myTestWallet.sqlite',
4949
databaseName: 'myTestWallet',
5050
chain: 'test',
@@ -121,4 +121,3 @@ For questions, bug reports, or feature requests, please open an issue on GitHub
121121
The license for the code in this repository is the Open BSV License. Refer to [LICENSE.txt](./LICENSE.txt) for the license text.
122122

123123
Thank you for being a part of the BSV Blockchain Libraries Project. Let's build the future of BSV Blockchain together!
124-

src/Setup.ts

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { Beef, CreateActionArgs, CreateActionOutput, CreateActionResult, KeyDeriver, P2PKH, PrivateKey, PublicKey, SignActionArgs, SignActionResult, WalletInterface } from "@bsv/sdk"
2+
import { Monitor, sdk, Services, SetupClient, StorageClient, verifyTruthy, Wallet, WalletStorageManager } from "./index.client"
3+
import { PrivilegedKeyManager } from "./sdk"
4+
import { Knex, knex as makeKnex } from 'knex'
5+
import { SetupWalletOnly, StorageKnex } from "./index.all"
6+
7+
/**
8+
* This class provides static setup functions to construct BRC-100 compatible
9+
* wallets in a variety of configurations.
10+
*
11+
* It serves as a starting point for experimentation and customization.
12+
*
13+
*/
14+
export abstract class Setup extends SetupClient {
15+
16+
static async createKnexWallet(args: {
17+
knex: Knex<any, any[]>
18+
databaseName: string
19+
chain?: sdk.Chain
20+
rootKeyHex?: string
21+
privKeyHex?: string
22+
}): Promise<SetupWallet> {
23+
const wo = await Setup.createWalletOnly({
24+
chain: args.chain,
25+
rootKeyHex: args.rootKeyHex,
26+
privKeyHex: args.privKeyHex
27+
})
28+
const activeStorage = new StorageKnex({
29+
chain: wo.chain,
30+
knex: args.knex,
31+
commissionSatoshis: 0,
32+
commissionPubKeyHex: undefined,
33+
feeModel: { model: 'sat/kb', value: 1 }
34+
})
35+
await activeStorage.migrate(args.databaseName, wo.identityKey)
36+
await activeStorage.makeAvailable()
37+
await wo.storage.addWalletStorageProvider(activeStorage)
38+
const { user, isNew } = await activeStorage.findOrInsertUser(wo.identityKey)
39+
const userId = user.userId
40+
const r: SetupWallet = {
41+
...wo,
42+
activeStorage,
43+
userId
44+
}
45+
return r
46+
}
47+
48+
static createSQLiteKnex(filename: string): Knex {
49+
const config: Knex.Config = {
50+
client: 'sqlite3',
51+
connection: { filename },
52+
useNullAsDefault: true
53+
}
54+
const knex = makeKnex(config)
55+
return knex
56+
}
57+
58+
static createMySQLKnex(connection: string, database?: string): Knex {
59+
const c: Knex.MySql2ConnectionConfig = JSON.parse(connection)
60+
if (database) {
61+
c.database = database
62+
}
63+
const config: Knex.Config = {
64+
client: 'mysql2',
65+
connection: c,
66+
useNullAsDefault: true,
67+
pool: { min: 0, max: 7, idleTimeoutMillis: 15000 }
68+
}
69+
const knex = makeKnex(config)
70+
return knex
71+
}
72+
73+
static async createMySQLWallet(args: {
74+
databaseName: string
75+
chain?: sdk.Chain
76+
rootKeyHex?: string
77+
privKeyHex?: string
78+
}): Promise<SetupWallet> {
79+
const env = Setup.getEnv(args.chain || 'test')
80+
return await this.createKnexWallet({
81+
...args,
82+
knex: Setup.createMySQLKnex(env.mySQLConnection, args.databaseName)
83+
})
84+
}
85+
86+
static async createSQLiteWallet(args: {
87+
filePath: string
88+
databaseName: string
89+
chain?: sdk.Chain
90+
rootKeyHex?: string
91+
privKeyHex?: string
92+
}): Promise<SetupWallet> {
93+
return await this.createKnexWallet({
94+
...args,
95+
knex: Setup.createSQLiteKnex(args.filePath)
96+
})
97+
}
98+
99+
}
100+
101+
export interface SetupWallet extends SetupWalletOnly {
102+
activeStorage: StorageKnex
103+
userId: number
104+
105+
rootKey: PrivateKey
106+
identityKey: string
107+
keyDeriver: KeyDeriver
108+
chain: sdk.Chain
109+
storage: WalletStorageManager
110+
services: Services
111+
monitor: Monitor
112+
wallet: Wallet
113+
}

0 commit comments

Comments
 (0)