11import {
2+ BEEF ,
23 Beef ,
34 CreateActionArgs ,
45 CreateActionOptions ,
@@ -10,6 +11,8 @@ import {
1011 PublicKey ,
1112 SignActionArgs ,
1213 SignActionResult ,
14+ Transaction ,
15+ UnlockingScript ,
1316 WalletInterface
1417} from '@bsv/sdk'
1518import {
@@ -88,14 +91,6 @@ export abstract class SetupClient {
8891 */
8992 static getEnv ( chain : sdk . Chain ) : SetupEnv {
9093 // Identity keys of the lead maintainer of this repo...
91- const mainTaalApiKey = verifyTruthy (
92- process . env . MAIN_TAAL_API_KEY || '' ,
93- `.env value for 'mainTaalApiKey' is required.`
94- )
95- const testTaalApiKey = verifyTruthy (
96- process . env . TEST_TAAL_API_KEY || '' ,
97- `.env value for 'testTaalApiKey' is required.`
98- )
9994 const identityKey =
10095 chain === 'main'
10196 ? process . env . MY_MAIN_IDENTITY
@@ -106,6 +101,12 @@ export abstract class SetupClient {
106101 : process . env . MY_TEST_IDENTITY2
107102 const DEV_KEYS = process . env . DEV_KEYS || '{}'
108103 const mySQLConnection = process . env . MYSQL_CONNECTION || '{}'
104+ const taalApiKey =
105+ verifyTruthy ( chain === 'main'
106+ ? process . env . MAIN_TAAL_API_KEY
107+ : process . env . TEST_TAAL_API_KEY ,
108+ `.env value for '${ chain . toUpperCase ( ) } _TAAL_API_KEY' is required.`
109+ )
109110
110111 if ( ! identityKey || ! identityKey2 )
111112 throw new sdk . WERR_INVALID_OPERATION (
@@ -116,8 +117,7 @@ export abstract class SetupClient {
116117 chain,
117118 identityKey,
118119 identityKey2,
119- mainTaalApiKey,
120- testTaalApiKey,
120+ taalApiKey,
121121 devKeys : JSON . parse ( DEV_KEYS ) as Record < string , string > ,
122122 mySQLConnection
123123 }
@@ -143,7 +143,9 @@ export abstract class SetupClient {
143143 args . active ,
144144 args . backups
145145 )
146- if ( storage . stores . length > 0 ) await storage . makeAvailable ( )
146+ if ( storage . stores . length > 0 ) await storage . makeAvailable ( ) ;
147+ const serviceOptions = Services . createDefaultOptions ( chain )
148+ serviceOptions . taalApiKey = args . env . taalApiKey
147149 const services = new Services ( args . chain )
148150 const monopts = Monitor . createDefaultWalletMonitorOptions (
149151 chain ,
@@ -178,20 +180,55 @@ export abstract class SetupClient {
178180 return r
179181 }
180182
181- static async createWalletWithStorageClient ( args : SetupWalletClientArgs )
182- : Promise < SetupWalletClient >
183- {
184- const wo = await Setup . createWallet ( args )
185- if ( wo . chain === 'main' ) throw new sdk . WERR_INVALID_PARAMETER ( 'chain' , `'test' for now, 'main' is not yet supported.` ) ;
183+ static async createWalletWithStorageClient (
184+ args : SetupWalletClientArgs
185+ ) : Promise < SetupWalletClient > {
186+ const wo = await Setup . createWallet ( args )
187+ if ( wo . chain === 'main' )
188+ throw new sdk . WERR_INVALID_PARAMETER (
189+ 'chain' ,
190+ `'test' for now, 'main' is not yet supported.`
191+ )
186192
187- const endpointUrl = args . endpointUrl || 'https://staging-dojo.babbage.systems'
188- const client = new StorageClient ( wo . wallet , endpointUrl )
189- await wo . storage . addWalletStorageProvider ( client )
190- await wo . storage . makeAvailable ( )
191- return {
192- ...wo ,
193- endpointUrl
194- }
193+ const endpointUrl =
194+ args . endpointUrl || 'https://staging-dojo.babbage.systems'
195+ const client = new StorageClient ( wo . wallet , endpointUrl )
196+ await wo . storage . addWalletStorageProvider ( client )
197+ await wo . storage . makeAvailable ( )
198+ return {
199+ ...wo ,
200+ endpointUrl
201+ }
202+ }
203+
204+ static getKeyPair ( priv ?: string | PrivateKey ) : KeyPairAddress {
205+ if ( priv === undefined ) priv = PrivateKey . fromRandom ( )
206+ else if ( typeof priv === 'string' ) priv = new PrivateKey ( priv , 'hex' )
207+
208+ const pub = PublicKey . fromPrivateKey ( priv )
209+ const address = pub . toAddress ( )
210+ return { privateKey : priv , publicKey : pub , address }
211+ }
212+
213+ static getLockP2PKH ( address : string ) {
214+ const p2pkh = new P2PKH ( )
215+ const lock = p2pkh . lock ( address )
216+ return lock
217+ }
218+
219+ static getUnlockP2PKH (
220+ priv : PrivateKey ,
221+ satoshis : number
222+ ) : sdk . ScriptTemplateUnlock {
223+ const p2pkh = new P2PKH ( )
224+ const lock = Setup . getLockP2PKH ( Setup . getKeyPair ( priv ) . address )
225+ // Prepare to pay with SIGHASH_ALL and without ANYONE_CAN_PAY.
226+ // In otherwords:
227+ // - all outputs must remain in the current order, amount and locking scripts.
228+ // - all inputs must remain from the current outpoints and sequence numbers.
229+ // (unlock scripts are never signed)
230+ const unlock = p2pkh . unlock ( priv , 'all' , false , satoshis , lock )
231+ return unlock
195232 }
196233
197234 static createP2PKHOutputs (
@@ -255,36 +292,13 @@ export abstract class SetupClient {
255292 return { cr, outpoints }
256293 }
257294
258- /**
259- * TODO...
260- */
261- static async internalizeP2PKHOutpoints ( ) { }
262-
263- static getKeyPair ( priv ?: string | PrivateKey ) : KeyPairAddress {
264- if ( priv === undefined ) priv = PrivateKey . fromRandom ( )
265- else if ( typeof priv === 'string' ) priv = new PrivateKey ( priv , 'hex' )
266-
267- const pub = PublicKey . fromPrivateKey ( priv )
268- const address = pub . toAddress ( )
269- return { privateKey : priv , publicKey : pub , address }
270- }
271-
272- static getLockP2PKH ( address : string ) {
273- const p2pkh = new P2PKH ( )
274- const lock = p2pkh . lock ( address )
275- return lock
276- }
277-
278- static getUnlockP2PKH ( priv : PrivateKey , satoshis : number ) {
279- const p2pkh = new P2PKH ( )
280- const lock = Setup . getLockP2PKH ( Setup . getKeyPair ( priv ) . address )
281- // Prepare to pay with SIGHASH_ALL and without ANYONE_CAN_PAY.
282- // In otherwords:
283- // - all outputs must remain in the current order, amount and locking scripts.
284- // - all inputs must remain from the current outpoints and sequence numbers.
285- // (unlock scripts are never signed)
286- const unlock = p2pkh . unlock ( priv , 'all' , false , satoshis , lock )
287- return unlock
295+ static async fundWalletFromP2PKHOutpoints (
296+ wallet : WalletInterface ,
297+ outpoints : string [ ] ,
298+ p2pkhKey : KeyPairAddress ,
299+ inputBEEF ?: BEEF
300+ ) {
301+ // TODO
288302 }
289303}
290304
@@ -303,15 +317,14 @@ export interface SetupEnv {
303317 chain : sdk . Chain
304318 identityKey : string
305319 identityKey2 : string
306- mainTaalApiKey : string
307- testTaalApiKey : string
320+ taalApiKey : string
308321 devKeys : Record < string , string >
309322 mySQLConnection : string
310323}
311324
312325/**
313326 * Arguments used to construct a `Wallet`
314- *
327+ *
315328 * @param env Configuration "secrets" typically obtained by `Setup.makeEnv` and `Setup.getEnv` functions.
316329 * @param chain Optional. Which chain this wallet is on: 'main' or 'test'.
317330 * Defaults to `env.chain`.
@@ -343,9 +356,9 @@ export interface SetupWallet {
343356}
344357
345358export interface SetupWalletClientArgs extends SetupWalletArgs {
346- endpointUrl ?: string
359+ endpointUrl ?: string
347360}
348361
349362export interface SetupWalletClient extends SetupWallet {
350- endpointUrl : string
351- }
363+ endpointUrl : string
364+ }
0 commit comments