|
| 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