1- import { AuthenticationErrorCommon , AuthenticationInstruction , AuthenticationProgramCommon , AuthenticationProgramStateCommon , AuthenticationVirtualMachine , ResolvedTransactionCommon , WalletTemplate , WalletTemplateScriptUnlocking , binToHex , createCompiler , createVirtualMachineBch2025 , decodeAuthenticationInstructions , encodeAuthenticationInstruction , walletTemplateToCompilerConfiguration } from '@bitauth/libauth' ;
1+ import { AuthenticationErrorCommon , AuthenticationInstruction , AuthenticationProgramCommon , AuthenticationProgramStateBchSpec , AuthenticationProgramStateCommon , AuthenticationVirtualMachine , AuthenticationVirtualMachineIdentifier , ResolvedTransactionCommon , WalletTemplate , WalletTemplateScriptUnlocking , binToHex , createCompiler , createVirtualMachineBch2023 , createVirtualMachineBch2025 , createVirtualMachineBch2026 , createVirtualMachineBchSpec , decodeAuthenticationInstructions , encodeAuthenticationInstruction , walletTemplateToCompilerConfiguration } from '@bitauth/libauth' ;
22import { Artifact , LogEntry , Op , PrimitiveType , StackItem , asmToBytecode , bytecodeToAsm , decodeBool , decodeInt , decodeString } from '@cashscript/utils' ;
33import { findLastIndex , toRegExp } from './utils.js' ;
44import { FailedRequireError , FailedTransactionError , FailedTransactionEvaluationError } from './Errors.js' ;
@@ -7,6 +7,21 @@ import { getBitauthUri } from './LibauthTemplate.js';
77export type DebugResult = AuthenticationProgramStateCommon [ ] ;
88export type DebugResults = Record < string , DebugResult > ;
99
10+ const createVirualMachine = ( vmTarget : AuthenticationVirtualMachineIdentifier ) => {
11+ switch ( vmTarget ) {
12+ case 'BCH_2023_05' :
13+ return createVirtualMachineBch2023 ( ) ;
14+ case 'BCH_2025_05' :
15+ return createVirtualMachineBch2025 ( ) ;
16+ case 'BCH_2026_05' :
17+ return createVirtualMachineBch2026 ( ) ;
18+ case 'BCH_SPEC' :
19+ return createVirtualMachineBchSpec ( ) ;
20+ default :
21+ throw new Error ( `Debugging is not supported for the ${ vmTarget } virtual machine.` ) ; ;
22+ }
23+ }
24+
1025// debugs the template, optionally logging the execution data
1126export const debugTemplate = ( template : WalletTemplate , artifacts : Artifact [ ] ) : DebugResults => {
1227 // If a contract has the same name, but a different bytecode, then it is considered a name collision
@@ -65,7 +80,7 @@ const debugSingleScenario = (
6580
6681 for ( const log of executedLogs ) {
6782 const inputIndex = extractInputIndexFromScenario ( scenarioId ) ;
68- logConsoleLogStatement ( log , executedDebugSteps , artifact , inputIndex ) ;
83+ logConsoleLogStatement ( log , executedDebugSteps , artifact , inputIndex , vm ) ;
6984 }
7085
7186 const lastExecutedDebugStep = executedDebugSteps [ executedDebugSteps . length - 1 ] ;
@@ -161,7 +176,7 @@ type CreateProgramResult = { vm: VM, program: Program };
161176// internal util. instantiates the virtual machine and compiles the template into a program
162177const createProgram = ( template : WalletTemplate , unlockingScriptId : string , scenarioId : string ) : CreateProgramResult => {
163178 const configuration = walletTemplateToCompilerConfiguration ( template ) ;
164- const vm = createVirtualMachineBch2025 ( ) ;
179+ const vm = createVirualMachine ( template . supported [ 0 ] ) ;
165180 const compiler = createCompiler ( configuration ) ;
166181
167182 if ( ! template . scripts [ unlockingScriptId ] ) {
@@ -186,21 +201,22 @@ const createProgram = (template: WalletTemplate, unlockingScriptId: string, scen
186201 throw new FailedTransactionError ( scenarioGeneration . scenario , getBitauthUri ( template ) ) ;
187202 }
188203
189- return { vm, program : scenarioGeneration . scenario . program } ;
204+ return { vm : vm as VM , program : scenarioGeneration . scenario . program } ;
190205} ;
191206
192207const logConsoleLogStatement = (
193208 log : LogEntry ,
194209 debugSteps : AuthenticationProgramStateCommon [ ] ,
195210 artifact : Artifact ,
196211 inputIndex : number ,
212+ vm : VM ,
197213) : void => {
198214 let line = `${ artifact . contractName } .cash:${ log . line } ` ;
199215 const decodedData = log . data . map ( ( element ) => {
200216 if ( typeof element === 'string' ) return element ;
201217
202218 const debugStep = debugSteps . find ( ( step ) => step . ip === element . ip ) ! ;
203- const transformedDebugStep = applyStackItemTransformations ( element , debugStep ) ;
219+ const transformedDebugStep = applyStackItemTransformations ( element , debugStep , vm ) ;
204220 return decodeStackItem ( element , transformedDebugStep . stack ) ;
205221 } ) ;
206222 console . log ( `[Input #${ inputIndex } ] ${ line } ${ decodedData . join ( ' ' ) } ` ) ;
@@ -209,6 +225,7 @@ const logConsoleLogStatement = (
209225const applyStackItemTransformations = (
210226 element : StackItem ,
211227 debugStep : AuthenticationProgramStateCommon ,
228+ vm : VM ,
212229) : AuthenticationProgramStateCommon => {
213230 if ( ! element . transformations ) return debugStep ;
214231
@@ -230,7 +247,6 @@ const applyStackItemTransformations = (
230247 functionCount : debugStep . functionCount ?? 0 ,
231248 } ;
232249
233- const vm = createVirtualMachineBch2025 ( ) ;
234250 const transformationsEndState = vm . stateEvaluate ( transformationsStartState ) ;
235251
236252 return transformationsEndState ;
0 commit comments