Skip to content

Commit 594e038

Browse files
authored
signer/rules: use goja and remove otto (#20599)
* signer: replace otto with goja * go.mod: remove Otto
1 parent a903912 commit 594e038

4 files changed

Lines changed: 26 additions & 31 deletions

File tree

‎go.mod‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ require (
4949
github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7
5050
github.com/prometheus/tsdb v0.6.2-0.20190402121629-4f204dcbc150
5151
github.com/rjeczalik/notify v0.9.1
52-
github.com/robertkrimen/otto v0.0.0-20170205013659-6a77b7cbc37d
5352
github.com/rs/cors v0.0.0-20160617231935-a62a804a8a00
5453
github.com/rs/xhandler v0.0.0-20160618193221-ed27b6fd6521 // indirect
5554
github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4

‎go.sum‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,6 @@ github.com/prometheus/tsdb v0.6.2-0.20190402121629-4f204dcbc150 h1:ZeU+auZj1iNzN
167167
github.com/prometheus/tsdb v0.6.2-0.20190402121629-4f204dcbc150/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
168168
github.com/rjeczalik/notify v0.9.1 h1:CLCKso/QK1snAlnhNR/CNvNiFU2saUtjV0bx3EwNeCE=
169169
github.com/rjeczalik/notify v0.9.1/go.mod h1:rKwnCoCGeuQnwBtTSPL9Dad03Vh2n40ePRrjvIXnJho=
170-
github.com/robertkrimen/otto v0.0.0-20170205013659-6a77b7cbc37d h1:ouzpe+YhpIfnjR40gSkJHWsvXmB6TiPKqMtMpfyU9DE=
171-
github.com/robertkrimen/otto v0.0.0-20170205013659-6a77b7cbc37d/go.mod h1:xvqspoSXJTIpemEonrMDFq6XzwHYYgToXWj5eRX1OtY=
172170
github.com/rs/cors v0.0.0-20160617231935-a62a804a8a00 h1:8DPul/X0IT/1TNMIxoKLwdemEOBBHDC/K4EB16Cw5WE=
173171
github.com/rs/cors v0.0.0-20160617231935-a62a804a8a00/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
174172
github.com/rs/xhandler v0.0.0-20160618193221-ed27b6fd6521 h1:3hxavr+IHMsQBrYUPQM5v0CgENFktkkbg1sfpgM3h20=

‎signer/rules/rules.go‎

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3333
var (
@@ -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

134136
func (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

‎signer/rules/rules_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ func TestStorage(t *testing.T) {
337337
if err != nil {
338338
t.Errorf("Unexpected error %v", err)
339339
}
340-
retval, err := v.ToString()
340+
retval := v.ToString().String()
341341

342342
if err != nil {
343343
t.Errorf("Unexpected error %v", err)

0 commit comments

Comments
 (0)