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"
1+ import {
2+ KeyDeriver ,
3+ PrivateKey ,
4+ } from '@bsv/sdk'
5+ import {
6+ Monitor ,
7+ sdk ,
8+ Services ,
9+ SetupClient ,
10+ Wallet ,
11+ WalletStorageManager
12+ } from './index.client'
413import { Knex , knex as makeKnex } from 'knex'
5- import { SetupWalletOnly , StorageKnex } from " ./index.all"
14+ import { SetupWallet , SetupWalletArgs , StorageKnex } from ' ./index.all'
615
716/**
8- * This class provides static setup functions to construct BRC-100 compatible
17+ * The 'Setup` class provides static setup functions to construct BRC-100 compatible
918 * wallets in a variety of configurations.
10- *
19+ *
1120 * It serves as a starting point for experimentation and customization.
12- *
21+ *
22+ * `SetupClient` references only browser compatible code including storage via `StorageClient`.
23+ * `Setup` extends `SetupClient` adding database storage via `Knex` and `StorageKnex`.
24+ *
1325 */
1426export abstract class Setup extends SetupClient {
15-
16- /**
17- * Adds `Knex` based storage to a `Wallet` configured by `Setup.createWalletOnly`
18- *
19- * @param args
20- * @returns
21- */
22- static async createKnexWallet ( args : {
23- /**
24- * `Knex` object configured for either MySQL or SQLite database access.
25- * Schema will be created and migrated as needed.
26- * For MySQL, a schema corresponding to databaseName must exist with full access permissions.
27- */
28- knex : Knex < any , any [ ] >
29- databaseName : string
30- chain ?: sdk . Chain
31- rootKeyHex ?: string
32- privKeyHex ?: string
33- } ) : Promise < SetupWallet > {
34- const wo = await Setup . createWalletOnly ( {
35- chain : args . chain ,
36- rootKeyHex : args . rootKeyHex ,
37- privKeyHex : args . privKeyHex
38- } )
27+ /**
28+ * Adds `Knex` based storage to a `Wallet` configured by `Setup.createWalletOnly`
29+ *
30+ * @param args.knex `Knex` object configured for either MySQL or SQLite database access.
31+ * Schema will be created and migrated as needed.
32+ * For MySQL, a schema corresponding to databaseName must exist with full access permissions.
33+ * @param args.databaseName Name for this storage. For MySQL, the schema name within the MySQL instance.
34+ * @param args.chain Which chain this wallet is on: 'main' or 'test'. Defaults to 'test'.
35+ * @param args.rootKeyHex
36+ *
37+ * @publicbody
38+ */
39+ static async createKnexWallet (
40+ args : SetupWalletKnexArgs
41+ ) : Promise < SetupWalletKnex > {
42+ const wo = await Setup . createWallet ( args )
3943 const activeStorage = new StorageKnex ( {
4044 chain : wo . chain ,
4145 knex : args . knex ,
@@ -48,7 +52,7 @@ export abstract class Setup extends SetupClient {
4852 await wo . storage . addWalletStorageProvider ( activeStorage )
4953 const { user, isNew } = await activeStorage . findOrInsertUser ( wo . identityKey )
5054 const userId = user . userId
51- const r : SetupWallet = {
55+ const r : SetupWalletKnex = {
5256 ...wo ,
5357 activeStorage,
5458 userId
@@ -69,7 +73,7 @@ export abstract class Setup extends SetupClient {
6973 static createMySQLKnex ( connection : string , database ?: string ) : Knex {
7074 const c : Knex . MySql2ConnectionConfig = JSON . parse ( connection )
7175 if ( database ) {
72- c . database = database
76+ c . database = database
7377 }
7478 const config : Knex . Config = {
7579 client : 'mysql2' ,
@@ -81,35 +85,40 @@ export abstract class Setup extends SetupClient {
8185 return knex
8286 }
8387
84- static async createMySQLWallet ( args : {
85- databaseName : string
86- chain ?: sdk . Chain
87- rootKeyHex ?: string
88- privKeyHex ?: string
89- } ) : Promise < SetupWallet > {
90- const env = Setup . getEnv ( args . chain || 'test' )
88+ static async createMySQLWallet (
89+ args : SetupWalletMySQLArgs
90+ ) : Promise < SetupWalletKnex > {
9191 return await this . createKnexWallet ( {
9292 ...args ,
93- knex : Setup . createMySQLKnex ( env . mySQLConnection , args . databaseName )
93+ knex : Setup . createMySQLKnex ( args . env . mySQLConnection , args . databaseName )
9494 } )
9595 }
9696
97- static async createSQLiteWallet ( args : {
98- filePath : string
99- databaseName : string
100- chain ?: sdk . Chain
101- rootKeyHex ?: string
102- privKeyHex ?: string
103- } ) : Promise < SetupWallet > {
97+ static async createSQLiteWallet (
98+ args : SetupWalletSQLiteArgs
99+ ) : Promise < SetupWalletKnex > {
104100 return await this . createKnexWallet ( {
105101 ...args ,
106102 knex : Setup . createSQLiteKnex ( args . filePath )
107103 } )
108104 }
105+ }
106+
107+ export interface SetupWalletKnexArgs extends SetupWalletArgs {
108+ knex : Knex < any , any [ ] >
109+ databaseName : string
110+ }
111+
112+ export interface SetupWalletMySQLArgs extends SetupWalletArgs {
113+ databaseName : string
114+ }
109115
116+ export interface SetupWalletSQLiteArgs extends SetupWalletArgs {
117+ filePath : string
118+ databaseName : string
110119}
111120
112- export interface SetupWallet extends SetupWalletOnly {
121+ export interface SetupWalletKnex extends SetupWallet {
113122 activeStorage : StorageKnex
114123 userId : number
115124
0 commit comments