github.com/daeglee/go-ethereum@v0.0.0-20190504220456-cad3e8d18e9b/signer/rules/rules.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // This file is part of go-ethereum. 3 // 4 // go-ethereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // go-ethereum is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 16 17 package rules 18 19 import ( 20 "encoding/json" 21 "fmt" 22 "os" 23 "strings" 24 25 "github.com/ethereum/go-ethereum/internal/ethapi" 26 "github.com/ethereum/go-ethereum/log" 27 "github.com/ethereum/go-ethereum/signer/core" 28 "github.com/ethereum/go-ethereum/signer/rules/deps" 29 "github.com/ethereum/go-ethereum/signer/storage" 30 "github.com/robertkrimen/otto" 31 ) 32 33 var ( 34 BigNumber_JS = deps.MustAsset("bignumber.js") 35 ) 36 37 // consoleOutput is an override for the console.log and console.error methods to 38 // stream the output into the configured output stream instead of stdout. 39 func consoleOutput(call otto.FunctionCall) otto.Value { 40 output := []string{"JS:> "} 41 for _, argument := range call.ArgumentList { 42 output = append(output, fmt.Sprintf("%v", argument)) 43 } 44 fmt.Fprintln(os.Stderr, strings.Join(output, " ")) 45 return otto.Value{} 46 } 47 48 // rulesetUI provides an implementation of UIClientAPI that evaluates a javascript 49 // file for each defined UI-method 50 type rulesetUI struct { 51 next core.UIClientAPI // The next handler, for manual processing 52 storage storage.Storage 53 jsRules string // The rules to use 54 } 55 56 func NewRuleEvaluator(next core.UIClientAPI, jsbackend storage.Storage) (*rulesetUI, error) { 57 c := &rulesetUI{ 58 next: next, 59 storage: jsbackend, 60 jsRules: "", 61 } 62 63 return c, nil 64 } 65 func (r *rulesetUI) RegisterUIServer(api *core.UIServerAPI) { 66 // TODO, make it possible to query from js 67 } 68 69 func (r *rulesetUI) Init(javascriptRules string) error { 70 r.jsRules = javascriptRules 71 return nil 72 } 73 func (r *rulesetUI) execute(jsfunc string, jsarg interface{}) (otto.Value, error) { 74 75 // Instantiate a fresh vm engine every time 76 vm := otto.New() 77 // Set the native callbacks 78 consoleObj, _ := vm.Get("console") 79 consoleObj.Object().Set("log", consoleOutput) 80 consoleObj.Object().Set("error", consoleOutput) 81 vm.Set("storage", r.storage) 82 83 // Load bootstrap libraries 84 script, err := vm.Compile("bignumber.js", BigNumber_JS) 85 if err != nil { 86 log.Warn("Failed loading libraries", "err", err) 87 return otto.UndefinedValue(), err 88 } 89 vm.Run(script) 90 91 // Run the actual rule implementation 92 _, err = vm.Run(r.jsRules) 93 if err != nil { 94 log.Warn("Execution failed", "err", err) 95 return otto.UndefinedValue(), err 96 } 97 98 // And the actual call 99 // All calls are objects with the parameters being keys in that object. 100 // To provide additional insulation between js and go, we serialize it into JSON on the Go-side, 101 // and deserialize it on the JS side. 102 103 jsonbytes, err := json.Marshal(jsarg) 104 if err != nil { 105 log.Warn("failed marshalling data", "data", jsarg) 106 return otto.UndefinedValue(), err 107 } 108 // Now, we call foobar(JSON.parse(<jsondata>)). 109 var call string 110 if len(jsonbytes) > 0 { 111 call = fmt.Sprintf("%v(JSON.parse(%v))", jsfunc, string(jsonbytes)) 112 } else { 113 call = fmt.Sprintf("%v()", jsfunc) 114 } 115 return vm.Run(call) 116 } 117 118 func (r *rulesetUI) checkApproval(jsfunc string, jsarg []byte, err error) (bool, error) { 119 if err != nil { 120 return false, err 121 } 122 v, err := r.execute(jsfunc, string(jsarg)) 123 if err != nil { 124 log.Info("error occurred during execution", "error", err) 125 return false, err 126 } 127 result, err := v.ToString() 128 if err != nil { 129 log.Info("error occurred during response unmarshalling", "error", err) 130 return false, err 131 } 132 if result == "Approve" { 133 log.Info("Op approved") 134 return true, nil 135 } else if result == "Reject" { 136 log.Info("Op rejected") 137 return false, nil 138 } 139 return false, fmt.Errorf("Unknown response") 140 } 141 142 func (r *rulesetUI) ApproveTx(request *core.SignTxRequest) (core.SignTxResponse, error) { 143 jsonreq, err := json.Marshal(request) 144 approved, err := r.checkApproval("ApproveTx", jsonreq, err) 145 if err != nil { 146 log.Info("Rule-based approval error, going to manual", "error", err) 147 return r.next.ApproveTx(request) 148 } 149 150 if approved { 151 return core.SignTxResponse{ 152 Transaction: request.Transaction, 153 Approved: true}, 154 nil 155 } 156 return core.SignTxResponse{Approved: false}, err 157 } 158 159 func (r *rulesetUI) ApproveSignData(request *core.SignDataRequest) (core.SignDataResponse, error) { 160 jsonreq, err := json.Marshal(request) 161 approved, err := r.checkApproval("ApproveSignData", jsonreq, err) 162 if err != nil { 163 log.Info("Rule-based approval error, going to manual", "error", err) 164 return r.next.ApproveSignData(request) 165 } 166 if approved { 167 return core.SignDataResponse{Approved: true}, nil 168 } 169 return core.SignDataResponse{Approved: false}, err 170 } 171 172 // OnInputRequired not handled by rules 173 func (r *rulesetUI) OnInputRequired(info core.UserInputRequest) (core.UserInputResponse, error) { 174 return r.next.OnInputRequired(info) 175 } 176 177 func (r *rulesetUI) ApproveListing(request *core.ListRequest) (core.ListResponse, error) { 178 jsonreq, err := json.Marshal(request) 179 approved, err := r.checkApproval("ApproveListing", jsonreq, err) 180 if err != nil { 181 log.Info("Rule-based approval error, going to manual", "error", err) 182 return r.next.ApproveListing(request) 183 } 184 if approved { 185 return core.ListResponse{Accounts: request.Accounts}, nil 186 } 187 return core.ListResponse{}, err 188 } 189 190 func (r *rulesetUI) ApproveNewAccount(request *core.NewAccountRequest) (core.NewAccountResponse, error) { 191 // This cannot be handled by rules, requires setting a password 192 // dispatch to next 193 return r.next.ApproveNewAccount(request) 194 } 195 196 func (r *rulesetUI) ShowError(message string) { 197 log.Error(message) 198 r.next.ShowError(message) 199 } 200 201 func (r *rulesetUI) ShowInfo(message string) { 202 log.Info(message) 203 r.next.ShowInfo(message) 204 } 205 206 func (r *rulesetUI) OnSignerStartup(info core.StartupInfo) { 207 jsonInfo, err := json.Marshal(info) 208 if err != nil { 209 log.Warn("failed marshalling data", "data", info) 210 return 211 } 212 r.next.OnSignerStartup(info) 213 _, err = r.execute("OnSignerStartup", string(jsonInfo)) 214 if err != nil { 215 log.Info("error occurred during execution", "error", err) 216 } 217 } 218 219 func (r *rulesetUI) OnApprovedTx(tx ethapi.SignTransactionResult) { 220 jsonTx, err := json.Marshal(tx) 221 if err != nil { 222 log.Warn("failed marshalling transaction", "tx", tx) 223 return 224 } 225 _, err = r.execute("OnApprovedTx", string(jsonTx)) 226 if err != nil { 227 log.Info("error occurred during execution", "error", err) 228 } 229 }