Skip to content

Commit 99707f1

Browse files
committed
Merge branch 'nextVersion'
2 parents d1c8193 + 4e6365e commit 99707f1

File tree

9 files changed

+481
-281
lines changed

9 files changed

+481
-281
lines changed

docs/setup.md

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,31 @@ export abstract class Setup extends SetupClient {
8181
chain?: sdk.Chain;
8282
rootKeyHex?: string;
8383
privKeyHex?: string;
84-
}): Promise<SetupWallet>
84+
}): Promise<SetupWallet> {
85+
const wo = await Setup.createWalletOnly({
86+
chain: args.chain,
87+
rootKeyHex: args.rootKeyHex,
88+
privKeyHex: args.privKeyHex
89+
});
90+
const activeStorage = new StorageKnex({
91+
chain: wo.chain,
92+
knex: args.knex,
93+
commissionSatoshis: 0,
94+
commissionPubKeyHex: undefined,
95+
feeModel: { model: "sat/kb", value: 1 }
96+
});
97+
await activeStorage.migrate(args.databaseName, wo.identityKey);
98+
await activeStorage.makeAvailable();
99+
await wo.storage.addWalletStorageProvider(activeStorage);
100+
const { user, isNew } = await activeStorage.findOrInsertUser(wo.identityKey);
101+
const userId = user.userId;
102+
const r: SetupWallet = {
103+
...wo,
104+
activeStorage,
105+
userId
106+
};
107+
return r;
108+
}
85109
static createSQLiteKnex(filename: string): Knex
86110
static createMySQLKnex(connection: string, database?: string): Knex
87111
static async createMySQLWallet(args: {
@@ -100,7 +124,7 @@ export abstract class Setup extends SetupClient {
100124
}
101125
```
102126

103-
See also: [Chain](#type-chain), [SetupClient](#class-setupclient), [SetupWallet](#interface-setupwallet)
127+
See also: [Chain](#type-chain), [SetupClient](#class-setupclient), [SetupWallet](#interface-setupwallet), [StorageKnex](#class-storageknex)
104128

105129
<details>
106130

@@ -117,9 +141,44 @@ static async createKnexWallet(args: {
117141
chain?: sdk.Chain;
118142
rootKeyHex?: string;
119143
privKeyHex?: string;
120-
}): Promise<SetupWallet>
144+
}): Promise<SetupWallet> {
145+
const wo = await Setup.createWalletOnly({
146+
chain: args.chain,
147+
rootKeyHex: args.rootKeyHex,
148+
privKeyHex: args.privKeyHex
149+
});
150+
const activeStorage = new StorageKnex({
151+
chain: wo.chain,
152+
knex: args.knex,
153+
commissionSatoshis: 0,
154+
commissionPubKeyHex: undefined,
155+
feeModel: { model: "sat/kb", value: 1 }
156+
});
157+
await activeStorage.migrate(args.databaseName, wo.identityKey);
158+
await activeStorage.makeAvailable();
159+
await wo.storage.addWalletStorageProvider(activeStorage);
160+
const { user, isNew } = await activeStorage.findOrInsertUser(wo.identityKey);
161+
const userId = user.userId;
162+
const r: SetupWallet = {
163+
...wo,
164+
activeStorage,
165+
userId
166+
};
167+
return r;
168+
}
121169
```
122-
See also: [Chain](#type-chain), [SetupWallet](#interface-setupwallet)
170+
See also: [Chain](#type-chain), [Setup](#class-setup), [SetupWallet](#interface-setupwallet), [StorageKnex](#class-storageknex)
171+
172+
Argument Details
173+
174+
+ **args.knex**
175+
+ `Knex` object configured for either MySQL or SQLite database access.
176+
Schema will be created and migrated as needed.
177+
For MySQL, a schema corresponding to databaseName must exist with full access permissions.
178+
+ **args.databaseName**
179+
+ Name for this storage. For MySQL, the schema name within the MySQL instance.
180+
+ **args.chain**
181+
+ Which chain this wallet is on: 'main' or 'test'. Defaults to 'test'.
123182

124183
</details>
125184

@@ -135,7 +194,7 @@ It serves as a starting point for experimentation and customization.
135194

136195
```ts
137196
export abstract class SetupClient {
138-
static makeEnv(chain: sdk.Chain): void {
197+
static makeEnv(): void {
139198
const testPrivKey1 = PrivateKey.fromRandom();
140199
const testIdentityKey1 = testPrivKey1.toPublicKey().toString();
141200
const testPrivKey2 = PrivateKey.fromRandom();
@@ -195,6 +254,24 @@ export abstract class SetupClient {
195254

196255
See also: [Chain](#type-chain), [KeyPairAddress](#type-keypairaddress), [SetupWalletOnly](#interface-setupwalletonly), [WalletStorageProvider](#interface-walletstorageprovider)
197256

257+
<details>
258+
259+
<summary>Class SetupClient Details</summary>
260+
261+
#### Method makeEnv
262+
263+
Create content for .env file with some private keys, identity keys, sample API keys, and sample MySQL connection string.
264+
265+
Private keys should never be included directly in you source code.
266+
267+
Loading them from a .env file is intended only for experimentation and getting started.
268+
269+
```ts
270+
static makeEnv(): void
271+
```
272+
273+
</details>
274+
198275
Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Variables](#variables)
199276

200277
---

src/Setup.ts

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,45 @@
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'
413
import { 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
*/
1426
export 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

Comments
 (0)