@@ -25,7 +25,7 @@ import type {
25
25
WalletTemplate ,
26
26
} from '../lib.js' ;
27
27
import {
28
- encodeDataPush ,
28
+ createVirtualMachineBchSpec ,
29
29
generateBytecodeMap ,
30
30
Opcodes ,
31
31
OpcodesBchSpec ,
@@ -125,7 +125,6 @@ export const compilerConfigurationToCompiler =
125
125
compilerConfigurationToCompilerBch ;
126
126
127
127
const nullHashLength = 32 ;
128
- const maximumValidOpReturnPushLength = 9996 ;
129
128
130
129
/**
131
130
* A common {@link createAuthenticationProgram} implementation for
@@ -165,10 +164,7 @@ export const createAuthenticationProgramEvaluationCommon = (
165
164
locktime : 0 ,
166
165
outputs : [
167
166
{
168
- lockingBytecode : flattenBinArray ( [
169
- Uint8Array . of ( Opcodes . OP_RETURN ) ,
170
- encodeDataPush ( new Uint8Array ( maximumValidOpReturnPushLength ) ) ,
171
- ] ) ,
167
+ lockingBytecode : flattenBinArray ( [ Uint8Array . of ( Opcodes . OP_RETURN ) ] ) ,
172
168
valueSatoshis : 0n ,
173
169
} ,
174
170
] ,
@@ -233,6 +229,122 @@ export const compileCashAssembly = (script: string) => {
233
229
) } `;
234
230
} ;
235
231
232
+ const defaultVm = createVirtualMachineBchSpec ( ) ;
233
+
234
+ /**
235
+ * Compile a CashAssembly script with detailed debugging information.
236
+ *
237
+ * If no VM override is provided, Libauth's default `BCH_SPEC` VM is used.
238
+ *
239
+ * @param script - the CashAssembly script to compile
240
+ * @param scriptsAndOverrides - a compiler configuration from which properties
241
+ * will be used to override properties of the default common compiler
242
+ * configuration
243
+ */
244
+ export const debugCashAssemblyCompilation = <
245
+ Configuration extends CompilerConfiguration < CompilationContextBch > ,
246
+ Overrides extends Configuration & {
247
+ vm ?: NonNullable < Configuration [ 'vm' ] > ;
248
+ } ,
249
+ > (
250
+ script : string ,
251
+ scriptsAndOverrides : Overrides = { scripts : { } , vm : defaultVm } as Overrides ,
252
+ ) => {
253
+ const merged = {
254
+ ...scriptsAndOverrides ,
255
+ scripts : {
256
+ script,
257
+ ...scriptsAndOverrides . scripts ,
258
+ } ,
259
+ ...( scriptsAndOverrides . vm ? scriptsAndOverrides . vm : { vm : defaultVm } ) ,
260
+ } ;
261
+ return createCompilerCommon ( merged ) . generateScenario ( {
262
+ debug : true ,
263
+ lockingScriptId : 'script' ,
264
+ } ) ;
265
+ } ;
266
+
267
+ /**
268
+ * A simple, compile-and-evaluate utility for debugging CashAssembly scripts.
269
+ * Returns the full compilation results, program trace, and verification result;
270
+ * to return only the final program state following evaluation,
271
+ * use {@link evaluateCashAssembly}.
272
+ *
273
+ * If no VM override is provided, Libauth's default `BCH_SPEC` VM is used.
274
+ *
275
+ * @param script - the CashAssembly script to debug
276
+ * @param scriptsAndOverrides - a compiler configuration from which properties
277
+ * will be used to override properties of the default common compiler
278
+ * configuration
279
+ */
280
+ export const debugCashAssembly = <
281
+ Configuration extends CompilerConfiguration < CompilationContextBch > ,
282
+ ProgramState extends AuthenticationProgramStateCommon ,
283
+ Overrides extends Configuration & {
284
+ vm ?: NonNullable < Configuration [ 'vm' ] > ;
285
+ } ,
286
+ > (
287
+ script : string ,
288
+ scriptsAndOverrides : Overrides = { scripts : { } , vm : defaultVm } as Overrides ,
289
+ ) => {
290
+ const vm = scriptsAndOverrides . vm ?? defaultVm ;
291
+ const result = debugCashAssemblyCompilation < Configuration , Overrides > (
292
+ script ,
293
+ { ...scriptsAndOverrides , vm } ,
294
+ ) ;
295
+ if ( typeof result === 'string' )
296
+ return {
297
+ error : result ,
298
+ success : false ,
299
+ } as { error : string ; success : false } ;
300
+ if ( typeof result . scenario === 'string' )
301
+ return {
302
+ error : result . scenario ,
303
+ result,
304
+ success : false as const ,
305
+ } as { error : string ; result : typeof result ; success : false } ;
306
+ const trace = vm . debug ( result . scenario . program ) as ProgramState [ ] ;
307
+ const verify = vm . verify ( result . scenario . program ) ;
308
+ return { compilation : result , success : true , trace, verify } as {
309
+ compilation : typeof result ;
310
+ success : true ;
311
+ trace : typeof trace ;
312
+ verify : typeof verify ;
313
+ } ;
314
+ } ;
315
+
316
+ /**
317
+ * A simple, compile-and-evaluate utility for testing CashAssembly scripts.
318
+ * Returns the final program state following evaluation; for full compilation
319
+ * debugging and a program trace, use {@link debugCashAssembly}.
320
+ *
321
+ * If no VM override is provided, Libauth's default `BCH_SPEC` VM is used.
322
+ *
323
+ * @param script - the CashAssembly script to evaluate
324
+ * @param scriptsAndOverrides - a compiler configuration from which properties
325
+ * will be used to override properties of the default common compiler
326
+ * configuration
327
+ */
328
+ export const evaluateCashAssembly = <
329
+ Configuration extends CompilerConfiguration < CompilationContextBch > ,
330
+ ProgramState extends AuthenticationProgramStateCommon ,
331
+ Overrides extends Configuration & {
332
+ vm ?: NonNullable < Configuration [ 'vm' ] > ;
333
+ } ,
334
+ > (
335
+ script : string ,
336
+ scriptsAndOverrides : Overrides = { scripts : { } , vm : defaultVm } as Overrides ,
337
+ ) => {
338
+ const vm = scriptsAndOverrides . vm ?? defaultVm ;
339
+ const debug = debugCashAssembly < Configuration , ProgramState , Overrides > (
340
+ script ,
341
+ { ...scriptsAndOverrides , vm } ,
342
+ ) ;
343
+ if ( ! debug . success ) return debug . error ;
344
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
345
+ return debug . trace [ debug . trace . length - 1 ] ! ;
346
+ } ;
347
+
236
348
/**
237
349
* Re-assemble a string of disassembled bytecode
238
350
* (see {@link disassembleBytecode}).
0 commit comments