@@ -22,12 +22,12 @@ import (
2222 "os"
2323 "strings"
2424
25+ "github.com/dop251/goja"
2526 "github.com/ethereum/go-ethereum/internal/ethapi"
2627 "github.com/ethereum/go-ethereum/log"
2728 "github.com/ethereum/go-ethereum/signer/core"
2829 "github.com/ethereum/go-ethereum/signer/rules/deps"
2930 "github.com/ethereum/go-ethereum/signer/storage"
30- "github.com/robertkrimen/otto"
3131)
3232
3333var (
@@ -36,13 +36,13 @@ var (
3636
3737// consoleOutput is an override for the console.log and console.error methods to
3838// stream the output into the configured output stream instead of stdout.
39- func consoleOutput (call otto .FunctionCall ) otto .Value {
39+ func consoleOutput (call goja .FunctionCall ) goja .Value {
4040 output := []string {"JS:> " }
41- for _ , argument := range call .ArgumentList {
41+ for _ , argument := range call .Arguments {
4242 output = append (output , fmt .Sprintf ("%v" , argument ))
4343 }
4444 fmt .Fprintln (os .Stderr , strings .Join (output , " " ))
45- return otto. Value {}
45+ return goja . Undefined ()
4646}
4747
4848// rulesetUI provides an implementation of UIClientAPI that evaluates a javascript
@@ -70,45 +70,47 @@ func (r *rulesetUI) Init(javascriptRules string) error {
7070 r .jsRules = javascriptRules
7171 return nil
7272}
73- func (r * rulesetUI ) execute (jsfunc string , jsarg interface {}) (otto .Value , error ) {
73+ func (r * rulesetUI ) execute (jsfunc string , jsarg interface {}) (goja .Value , error ) {
7474
7575 // Instantiate a fresh vm engine every time
76- vm := otto .New ()
76+ vm := goja .New ()
7777
7878 // Set the native callbacks
79- consoleObj , _ := vm .Get ("console" )
80- consoleObj .Object ().Set ("log" , consoleOutput )
81- consoleObj .Object ().Set ("error" , consoleOutput )
79+ consoleObj := vm .NewObject ()
80+ consoleObj .Set ("log" , consoleOutput )
81+ consoleObj .Set ("error" , consoleOutput )
82+ vm .Set ("console" , consoleObj )
8283
83- vm .Set ("storage" , struct {}{})
84- storageObj , _ := vm .Get ("storage" )
85- storageObj .Object ().Set ("put" , func (call otto.FunctionCall ) otto.Value {
84+ storageObj := vm .NewObject ()
85+ storageObj .Set ("put" , func (call goja.FunctionCall ) goja.Value {
8686 key , val := call .Argument (0 ).String (), call .Argument (1 ).String ()
8787 if val == "" {
8888 r .storage .Del (key )
8989 } else {
9090 r .storage .Put (key , val )
9191 }
92- return otto . NullValue ()
92+ return goja . Null ()
9393 })
94- storageObj .Object (). Set ("get" , func (call otto .FunctionCall ) otto .Value {
94+ storageObj .Set ("get" , func (call goja .FunctionCall ) goja .Value {
9595 goval , _ := r .storage .Get (call .Argument (0 ).String ())
96- jsval , _ := otto .ToValue (goval )
96+ jsval := vm .ToValue (goval )
9797 return jsval
9898 })
99+ vm .Set ("storage" , storageObj )
100+
99101 // Load bootstrap libraries
100- script , err := vm .Compile ("bignumber.js" , BigNumber_JS )
102+ script , err := goja .Compile ("bignumber.js" , string ( BigNumber_JS ), true )
101103 if err != nil {
102104 log .Warn ("Failed loading libraries" , "err" , err )
103- return otto . UndefinedValue (), err
105+ return goja . Undefined (), err
104106 }
105- vm .Run (script )
107+ vm .RunProgram (script )
106108
107109 // Run the actual rule implementation
108- _ , err = vm .Run (r .jsRules )
110+ _ , err = vm .RunString (r .jsRules )
109111 if err != nil {
110112 log .Warn ("Execution failed" , "err" , err )
111- return otto . UndefinedValue (), err
113+ return goja . Undefined (), err
112114 }
113115
114116 // And the actual call
@@ -119,7 +121,7 @@ func (r *rulesetUI) execute(jsfunc string, jsarg interface{}) (otto.Value, error
119121 jsonbytes , err := json .Marshal (jsarg )
120122 if err != nil {
121123 log .Warn ("failed marshalling data" , "data" , jsarg )
122- return otto . UndefinedValue (), err
124+ return goja . Undefined (), err
123125 }
124126 // Now, we call foobar(JSON.parse(<jsondata>)).
125127 var call string
@@ -128,7 +130,7 @@ func (r *rulesetUI) execute(jsfunc string, jsarg interface{}) (otto.Value, error
128130 } else {
129131 call = fmt .Sprintf ("%v()" , jsfunc )
130132 }
131- return vm .Run (call )
133+ return vm .RunString (call )
132134}
133135
134136func (r * rulesetUI ) checkApproval (jsfunc string , jsarg []byte , err error ) (bool , error ) {
@@ -140,11 +142,7 @@ func (r *rulesetUI) checkApproval(jsfunc string, jsarg []byte, err error) (bool,
140142 log .Info ("error occurred during execution" , "error" , err )
141143 return false , err
142144 }
143- result , err := v .ToString ()
144- if err != nil {
145- log .Info ("error occurred during response unmarshalling" , "error" , err )
146- return false , err
147- }
145+ result := v .ToString ().String ()
148146 if result == "Approve" {
149147 log .Info ("Op approved" )
150148 return true , nil
0 commit comments