@@ -111,12 +111,6 @@ export function TestOnLocalnet () {
111111 ) ;
112112}
113113
114- interface TestProgram extends Pick < Btc , 'rpc' | 'rest' | 'esplora' > {
115- ID ,
116- ASSETS ,
117- P2WPKH ,
118- }
119-
120114/** Define example program. */
121115function TestProgram ( name : string , src : string , {
122116 /** Program runs that should fail. */
@@ -134,73 +128,78 @@ function TestProgram (name: string, src: string, {
134128 /** Function that provides witness data. */
135129 provideWits = null as null | Fn < [ Uint8Array < ArrayBufferLike > ] , Fn . Async < object > > ,
136130} = { } ) {
137- const cost = fee ;
131+
132+ const commitAmount = 1_00000000n ;
133+ const redeemFee = 1e-4 ;
134+ const redeemAmount = 1. - redeemFee ;
135+
138136 return Fn . Name ( `${ name } (${ p2tr || 'unspecified P2TR' } )` , testProgram , {
139- shouldFail, name, src, cost , cmr, p2tr, argTypes, witTypes, provideArgs, provideWits,
137+ shouldFail, name, src, fee , cmr, p2tr, argTypes, witTypes, provideArgs, provideWits,
140138 } ) ;
141- async function testProgram ( chain : Btc ) {
142- // Need chain's genesis hash to compile for the chain.
143- const genesis = await chain . getBlockHash ( 0 ) ;
144139
145- // Parameter values are specified by the test case.
146- // It's a function so they can be made context-dependent,
147- // but for now they are constant.
148- const args = provideArgs ? await provideArgs ( ) : undefined ;
140+ // Test the SimplicityHL program specified above on the given chain.
141+ async function testProgram ( chain : Btc ) {
149142
150- // Ok, compile this program for this chain with these arguments.
151- const prog = await SimplicityHL . Program ( src , { chain : chain . ID , genesis, args } ) ;
143+ // Compile this program with these arguments for this chain.
144+ const program = await SimplicityHL . Program ( src , {
145+ // Expected program address, optional. Makes it safer.
146+ address : p2tr ,
147+ // Represents config such as HRP, prefix bytes...
148+ // TODO expose
149+ chain : chain . ID ,
150+ // Need chain's genesis hash to compile for the chain.
151+ genesis : await chain . getBlockHash ( 0 ) ,
152+ // Parameter values are specified by the test case.
153+ // It's a function so they can be made context-dependent,
154+ // but for now they are constant.
155+ args : provideArgs ? await provideArgs ( ) : undefined ,
156+ } ) ;
152157
153158 // Check against expected program address, if provided.
154- if ( p2tr ) equal ( prog . p2tr , p2tr ) ;
159+ if ( p2tr ) equal ( program . p2tr , p2tr ) ;
155160
156161 // Fund program from deployer:
157- // TODO: Use sendSigned
158- const commitAmount = 1_00000000n ;
159162 const commitSource = await chain . getUtxo ( chain . P2WPKH ( keypair1 . publicKey ( ) ) . address ) ;
160- const commitTxid = await SimplicityHL . Spend ( )
163+ const commitTxid = await SimplicityHL . Spend ( ) // TODO wrap as program.commit() ?
161164 . asset ( commitSource . asset )
162165 . input ( commitSource , keypair1 )
163166 . output ( p2tr , commitAmount )
164167 . fee ( fee )
165168 . broadcast ( chain ) ;
166169
167- // Create local spender wallet and import it to RPC :
170+ // Note current recipient balance :
168171 const recipient = chain . P2WPKH ( keypair1 . publicKey ( ) ) . address ;
169- await rpc . importaddress ( recipient ) ;
170-
171- // Note current balance:
172- await rpc . rescanblockchain ( ) ;
173- const balance = ( ( await rpc . getreceivedbyaddress ( recipient , 0 ) ) as { bitcoin : number } ) . bitcoin ;
172+ const balance = ( await chain . getBalance ( recipient , 0 ) ) [ 'bitcoin' ] ;
174173
175174 // Find commit (deploy) output = redeem (spend) input:
176- const asset = ASSETS . DEFAULT ;
177- const prev = await rest . tx ( id ) ;
178- assertTxOuts ( prev , p2tr , 1 , cost ) ;
175+ const asset = commitSource . asset ;
176+ const prev = await chain . getTxInfo ( commitTxid ) ;
177+ assertTxOuts ( prev , p2tr , 1 , fee ) ;
179178 const txid = prev . txid ;
180179 const vout = prev . vout . filter ( x => x . scriptPubKey . address === p2tr ) [ 0 ] ;
181180 if ( ! vout ) throw new Error ( 'no corresponding vout found' ) ;
182181 const utxos = [ { txid, asset, vout : vout . n , address : vout . scriptPubKey . address , amount : vout . value } ] ;
183182
184183 // To get SIGHASH_ALL for signing, first the rest of the transaction must be specified:
185- const redeemFee = 1e-4 ;
186- const redeemAmount = 1. - redeemFee ;
187184 const sighashOpts = { asset, utxos, recipient, amount : redeemAmount , fee : redeemFee } ;
188- const sighash = prog . redeemSighash ( sighashOpts ) ;
185+ const sighash = program . redeemSighash ( sighashOpts ) ;
189186 ok ( sighash instanceof Uint8Array , 'sighash expected to be returned from WASM as Uint8Array' )
190187 ok ( Base16 . encode ( sighash ) , 'sighash expected to be base16-encodable' ) ;
191- // Try spending from program:
192- const redeemArgs = { rpc, rest, ...sighashOpts , witness : provideWits ? await provideWits ( sighash ) : { } } ;
188+
189+ // Construct redeem transaction with witnesses:
190+ const witness = provideWits ? await provideWits ( sighash ) : { } ;
193191 if ( shouldFail ) {
194- // TX is expected to fail
195- rejects ( ( ) => prog . rpcRedeem ( redeemArgs ) ) ;
196- // Balance is expected to remain the same
197- equal ( await rpc . getreceivedbyaddress ( recipient , 0 ) , { bitcoin : balance } ) ;
198- } else {
199- // TX is expected to pass
200- await prog . rpcRedeem ( redeemArgs ) ;
201- // Balance is expected to increase
202- equal ( await rpc . getreceivedbyaddress ( recipient , 0 ) , { bitcoin : balance + redeemAmount } ) ;
192+ // Program runtime failure is caught at redeem TX construction.
193+ throws ( ( ) => program . redeemTx ( { ...sighashOpts , witness } ) ) ;
194+ return
203195 }
196+
197+ // Spend from program:
198+ const redeemTx = program . redeemTx ( { ...sighashOpts , witness } ) ;
199+ // TX is expected to pass
200+ await chain . broadcast ( redeemTx . hex ) ;
201+ // Balance is expected to increase
202+ equal ( await chain . getBalance ( recipient , 0 ) , { bitcoin : balance + redeemAmount } ) ;
204203 }
205204}
206205
@@ -213,7 +212,7 @@ function assertTxOuts (
213212 debug = console . debug ,
214213) {
215214 equal ( tx . vout . length , 3 ) ;
216- debug ( 'TX:' , tx ) ;
215+ // debug('TX:', tx);
217216 hasVout ( isBalance , _ => `balance: program ${ p2tr } must receive ${ amount } ` ) ;
218217 hasVout ( isFee , _ => `fee: no deploy fee matching ${ cost } ` ) ;
219218 //hasVout((x: Btc.Vout)=>x.value===remaining, v => `remaining: must be ${v}`);
0 commit comments