11import { stringify } from './helpers/stringify.js'
22import type { DecodedTransaction } from '@3loop/transaction-decoder'
33import { Effect , Layer } from 'effect'
4- import { Interpreter } from './types.js'
4+ import { Interpreter , InterpreterOptions } from './types.js'
55import { getInterpreter } from './interpreters.js'
66import { TransactionInterpreter } from './interpreter.js'
7+ import { InterpreterError } from './quickjs.js'
78
8- function localEval ( code : string , input : string ) {
9+ async function localEval ( code : string , input : string ) {
910 const fn = new Function ( `with(this) { ${ code } ; return transformEvent(${ input } ) }` )
10- return fn . call ( { } )
11+ const result = fn . call ( { } )
12+
13+ // Check if result is a promise and await it
14+ if ( result && typeof result . then === 'function' ) {
15+ return await result
16+ }
17+
18+ return result
1119}
1220
13- const make = Effect . succeed ( {
21+ const make = {
1422 // NOTE: We could export this separately to allow bundling the interpreters separately
1523 findInterpreter : ( decodedTx : DecodedTransaction ) => {
1624 if ( ! decodedTx . toAddress ) return undefined
@@ -24,29 +32,38 @@ const make = Effect.succeed({
2432 }
2533 } ,
2634 interpretTx : (
27- decodedTx : DecodedTransaction ,
35+ decodedTransaction : DecodedTransaction ,
2836 interpreter : Interpreter ,
2937 options ?: {
3038 interpretAsUserAddress ?: string
3139 } ,
3240 ) =>
33- Effect . sync ( ( ) => {
34- // TODO: add ability to surpress warning on acknowledge
35- Effect . logWarning ( 'Using eval in production can result in security vulnerabilities. Use at your own risk.' )
36-
37- let input
38- if ( options ?. interpretAsUserAddress ) {
39- input = stringify ( {
40- ...decodedTx ,
41- fromAddress : options . interpretAsUserAddress ,
42- } )
43- } else {
44- input = stringify ( decodedTx )
45- }
46-
47- const result = localEval ( interpreter . schema , input )
48- return result
41+ Effect . tryPromise ( {
42+ try : async ( ) => {
43+ // TODO: add ability to surpress warning on acknowledge
44+ Effect . logWarning ( 'Using eval in production can result in security vulnerabilities. Use at your own risk.' )
45+ const input = stringify ( decodedTransaction ) + ( options ? `,${ stringify ( options ) } ` : '' )
46+ const result = await localEval ( interpreter . schema , input )
47+ return result
48+ } ,
49+ catch : ( error ) => new InterpreterError ( error ) ,
4950 } ) . pipe ( Effect . withSpan ( 'TransactionInterpreter.interpretTx' ) ) ,
50- } )
5151
52- export const EvalInterpreterLive = Layer . scoped ( TransactionInterpreter , make )
52+ interpretTransaction : (
53+ decodedTransaction : DecodedTransaction ,
54+ interpreter : Interpreter ,
55+ options ?: InterpreterOptions ,
56+ ) =>
57+ Effect . tryPromise ( {
58+ try : async ( ) => {
59+ // TODO: add ability to surpress warning on acknowledge
60+ Effect . logWarning ( 'Using eval in production can result in security vulnerabilities. Use at your own risk.' )
61+ const input = stringify ( decodedTransaction ) + ( options ? `,${ stringify ( options ) } ` : '' )
62+ const result = await localEval ( interpreter . schema , input )
63+ return result
64+ } ,
65+ catch : ( error ) => new InterpreterError ( error ) ,
66+ } ) . pipe ( Effect . withSpan ( 'TransactionInterpreter.interpretTransaction' ) ) ,
67+ }
68+
69+ export const EvalInterpreterLive = Layer . scoped ( TransactionInterpreter , Effect . succeed ( make ) )
0 commit comments