1- import { AuthenticationErrorCommon , AuthenticationInstruction , AuthenticationProgramCommon , AuthenticationProgramStateCommon , AuthenticationVirtualMachine , ResolvedTransactionCommon , WalletTemplate , WalletTemplateScriptUnlocking , binToHex , createCompiler , createVirtualMachineBch2025 , decodeAuthenticationInstructions , encodeAuthenticationInstruction , walletTemplateToCompilerConfiguration } from '@bitauth/libauth' ;
1+ import { AuthenticationErrorCommon , AuthenticationInstruction , AuthenticationProgramCommon , 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
@@ -61,7 +76,7 @@ const debugSingleScenario = (
6176
6277 for ( const log of executedLogs ) {
6378 const inputIndex = extractInputIndexFromScenario ( scenarioId ) ;
64- logConsoleLogStatement ( log , executedDebugSteps , artifact , inputIndex ) ;
79+ logConsoleLogStatement ( log , executedDebugSteps , artifact , inputIndex , vm ) ;
6580 }
6681 }
6782
@@ -171,7 +186,7 @@ type CreateProgramResult = { vm: VM, program: Program };
171186// internal util. instantiates the virtual machine and compiles the template into a program
172187const createProgram = ( template : WalletTemplate , unlockingScriptId : string , scenarioId : string ) : CreateProgramResult => {
173188 const configuration = walletTemplateToCompilerConfiguration ( template ) ;
174- const vm = createVirtualMachineBch2025 ( ) ;
189+ const vm = createVirualMachine ( template . supported [ 0 ] ) ;
175190 const compiler = createCompiler ( configuration ) ;
176191
177192 if ( ! template . scripts [ unlockingScriptId ] ) {
@@ -196,21 +211,22 @@ const createProgram = (template: WalletTemplate, unlockingScriptId: string, scen
196211 throw new FailedTransactionError ( scenarioGeneration . scenario , getBitauthUri ( template ) ) ;
197212 }
198213
199- return { vm, program : scenarioGeneration . scenario . program } ;
214+ return { vm : vm as VM , program : scenarioGeneration . scenario . program } ;
200215} ;
201216
202217const logConsoleLogStatement = (
203218 log : LogEntry ,
204219 debugSteps : AuthenticationProgramStateCommon [ ] ,
205220 artifact : Artifact ,
206221 inputIndex : number ,
222+ vm : VM ,
207223) : void => {
208224 let line = `${ artifact . contractName } .cash:${ log . line } ` ;
209225 const decodedData = log . data . map ( ( element ) => {
210226 if ( typeof element === 'string' ) return element ;
211227
212228 const debugStep = debugSteps . find ( ( step ) => step . ip === element . ip ) ! ;
213- const transformedDebugStep = applyStackItemTransformations ( element , debugStep ) ;
229+ const transformedDebugStep = applyStackItemTransformations ( element , debugStep , vm ) ;
214230 return decodeStackItem ( element , transformedDebugStep . stack ) ;
215231 } ) ;
216232 console . log ( `[Input #${ inputIndex } ] ${ line } ${ decodedData . join ( ' ' ) } ` ) ;
@@ -219,6 +235,7 @@ const logConsoleLogStatement = (
219235const applyStackItemTransformations = (
220236 element : StackItem ,
221237 debugStep : AuthenticationProgramStateCommon ,
238+ vm : VM ,
222239) : AuthenticationProgramStateCommon => {
223240 if ( ! element . transformations ) return debugStep ;
224241
@@ -240,7 +257,6 @@ const applyStackItemTransformations = (
240257 functionCount : debugStep . functionCount ?? 0 ,
241258 } ;
242259
243- const vm = createVirtualMachineBch2025 ( ) ;
244260 const transformationsEndState = vm . stateEvaluate ( transformationsStartState ) ;
245261
246262 return transformationsEndState ;
0 commit comments