Skip to content

Commit 1c313cf

Browse files
committed
work on Setup and docs
1 parent d6f031e commit 1c313cf

File tree

6 files changed

+432
-247
lines changed

6 files changed

+432
-247
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: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,54 @@
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+
Beef,
3+
CreateActionArgs,
4+
CreateActionOutput,
5+
CreateActionResult,
6+
KeyDeriver,
7+
P2PKH,
8+
PrivateKey,
9+
PublicKey,
10+
SignActionArgs,
11+
SignActionResult,
12+
WalletInterface
13+
} from '@bsv/sdk'
14+
import {
15+
Monitor,
16+
sdk,
17+
Services,
18+
SetupClient,
19+
StorageClient,
20+
verifyTruthy,
21+
Wallet,
22+
WalletStorageManager
23+
} from './index.client'
24+
import { PrivilegedKeyManager } from './sdk'
425
import { Knex, knex as makeKnex } from 'knex'
5-
import { SetupWalletOnly, StorageKnex } from "./index.all"
26+
import { SetupWallet, StorageKnex } from './index.all'
627

728
/**
8-
* This class provides static setup functions to construct BRC-100 compatible
29+
* The 'Setup` class provides static setup functions to construct BRC-100 compatible
930
* wallets in a variety of configurations.
10-
*
31+
*
1132
* It serves as a starting point for experimentation and customization.
12-
*
33+
*
34+
* `SetupClient` references only browser compatible code including storage via `StorageClient`.
35+
* `Setup` extends `SetupClient` adding database storage via `Knex` and `StorageKnex`.
36+
*
1337
*/
1438
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-
*/
39+
/**
40+
* Adds `Knex` based storage to a `Wallet` configured by `Setup.createWalletOnly`
41+
*
42+
* @param args.knex `Knex` object configured for either MySQL or SQLite database access.
43+
* Schema will be created and migrated as needed.
44+
* For MySQL, a schema corresponding to databaseName must exist with full access permissions.
45+
* @param args.databaseName Name for this storage. For MySQL, the schema name within the MySQL instance.
46+
* @param args.chain Which chain this wallet is on: 'main' or 'test'. Defaults to 'test'.
47+
* @param args.rootKeyHex
48+
*
49+
* @publicbody
50+
*/
2251
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-
*/
2852
knex: Knex<any, any[]>
2953
databaseName: string
3054
chain?: sdk.Chain
@@ -69,7 +93,7 @@ export abstract class Setup extends SetupClient {
6993
static createMySQLKnex(connection: string, database?: string): Knex {
7094
const c: Knex.MySql2ConnectionConfig = JSON.parse(connection)
7195
if (database) {
72-
c.database = database
96+
c.database = database
7397
}
7498
const config: Knex.Config = {
7599
client: 'mysql2',
@@ -106,10 +130,9 @@ export abstract class Setup extends SetupClient {
106130
knex: Setup.createSQLiteKnex(args.filePath)
107131
})
108132
}
109-
110133
}
111134

112-
export interface SetupWallet extends SetupWalletOnly {
135+
export interface SetupWalletKnex extends SetupWallet {
113136
activeStorage: StorageKnex
114137
userId: number
115138

0 commit comments

Comments
 (0)