github.com/klaytn/klaytn@v1.10.2/console/console.go (about) 1 // Modifications Copyright 2018 The klaytn Authors 2 // Copyright 2016 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from console/console.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 package console 22 23 import ( 24 "fmt" 25 "io" 26 "io/ioutil" 27 "os" 28 "os/signal" 29 "path/filepath" 30 "regexp" 31 "sort" 32 "strings" 33 "syscall" 34 35 "github.com/klaytn/klaytn/networks/rpc" 36 37 "github.com/klaytn/klaytn/console/jsre" 38 "github.com/klaytn/klaytn/console/web3ext" 39 "github.com/mattn/go-colorable" 40 "github.com/peterh/liner" 41 "github.com/robertkrimen/otto" 42 ) 43 44 var ( 45 passwordRegexp = regexp.MustCompile(`personal.[nus]`) 46 onlyWhitespace = regexp.MustCompile(`^\s*$`) 47 exit = regexp.MustCompile(`^\s*exit\s*;*\s*$`) 48 ) 49 50 // HistoryFile is the file within the data directory to store input scrollback. 51 const HistoryFile = "history" 52 53 // DefaultPrompt is the default prompt line prefix to use for user input querying. 54 const DefaultPrompt = "> " 55 56 // Config is the collection of configurations to fine tune the behavior of the 57 // JavaScript console. 58 type Config struct { 59 DataDir string // Data directory to store the console history at 60 DocRoot string // Filesystem path from where to load JavaScript files from 61 Client *rpc.Client // RPC client to execute Klaytn requests through 62 Prompt string // Input prompt prefix string (defaults to DefaultPrompt) 63 Prompter UserPrompter // Input prompter to allow interactive user feedback (defaults to TerminalPrompter) 64 Printer io.Writer // Output writer to serialize any display strings to (defaults to os.Stdout) 65 Preload []string // Absolute paths to JavaScript files to preload 66 } 67 68 // Console is a JavaScript interpreted runtime environment. It is a fully fleged 69 // JavaScript console attached to a running node via an external or in-process RPC 70 // client. 71 type Console struct { 72 client *rpc.Client // RPC client to execute Klaytn requests through 73 jsre *jsre.JSRE // JavaScript runtime environment running the interpreter 74 prompt string // Input prompt prefix string 75 prompter UserPrompter // Input prompter to allow interactive user feedback 76 histPath string // Absolute path to the console scrollback history 77 history []string // Scroll history maintained by the console 78 printer io.Writer // Output writer to serialize any display strings to 79 } 80 81 func New(config Config) (*Console, error) { 82 // Handle unset config values gracefully 83 if config.Prompter == nil { 84 config.Prompter = Stdin 85 } 86 if config.Prompt == "" { 87 config.Prompt = DefaultPrompt 88 } 89 if config.Printer == nil { 90 config.Printer = colorable.NewColorableStdout() 91 } 92 // Initialize the console and return 93 console := &Console{ 94 client: config.Client, 95 jsre: jsre.New(config.DocRoot, config.Printer), 96 prompt: config.Prompt, 97 prompter: config.Prompter, 98 printer: config.Printer, 99 histPath: filepath.Join(config.DataDir, HistoryFile), 100 } 101 if err := os.MkdirAll(config.DataDir, 0o700); err != nil { 102 return nil, err 103 } 104 if err := console.init(config.Preload); err != nil { 105 return nil, err 106 } 107 return console, nil 108 } 109 110 // init retrieves the available APIs from the remote RPC provider and initializes 111 // the console's JavaScript namespaces based on the exposed modules. 112 func (c *Console) init(preload []string) error { 113 // Initialize the JavaScript <-> Go RPC bridge 114 bridge := newBridge(c.client, c.prompter, c.printer) 115 c.jsre.Set("jeth", struct{}{}) 116 117 jethObj, _ := c.jsre.Get("jeth") 118 jethObj.Object().Set("send", bridge.Send) 119 jethObj.Object().Set("sendAsync", bridge.Send) 120 121 consoleObj, _ := c.jsre.Get("console") 122 consoleObj.Object().Set("log", c.consoleOutput) 123 consoleObj.Object().Set("error", c.consoleOutput) 124 125 // Load all the internal utility JavaScript libraries 126 if err := c.jsre.Compile("bignumber.js", jsre.BigNumber_JS); err != nil { 127 return fmt.Errorf("bignumber.js: %v", err) 128 } 129 if err := c.jsre.Compile("web3.js", jsre.Web3_JS); err != nil { 130 return fmt.Errorf("web3.js: %v", err) 131 } 132 if _, err := c.jsre.Run("var Web3 = require('web3');"); err != nil { 133 return fmt.Errorf("web3 require: %v", err) 134 } 135 if _, err := c.jsre.Run("var web3 = new Web3(jeth);"); err != nil { 136 return fmt.Errorf("web3 provider: %v", err) 137 } 138 // Load the supported APIs into the JavaScript runtime environment 139 apis, err := c.client.SupportedModules() 140 if err != nil { 141 return fmt.Errorf("api modules: %v", err) 142 } 143 flatten := "var klay = web3.klay; var personal = web3.personal; var eth = web3.eth; " 144 for api := range apis { 145 if api == "web3" { 146 continue // manually mapped or ignore 147 } 148 if file, ok := web3ext.Modules[api]; ok { 149 // Load our extension for the module. 150 if err = c.jsre.Compile(fmt.Sprintf("%s.js", api), file); err != nil { 151 return fmt.Errorf("%s.js: %v", api, err) 152 } 153 flatten += fmt.Sprintf("var %s = web3.%s; ", api, api) 154 } else if obj, err := c.jsre.Run("web3." + api); err == nil && obj.IsObject() { 155 // Enable web3.js built-in extension if available. 156 flatten += fmt.Sprintf("var %s = web3.%s; ", api, api) 157 } 158 } 159 if _, err = c.jsre.Run(flatten); err != nil { 160 return fmt.Errorf("namespace flattening: %v", err) 161 } 162 // Initialize the global name register (disabled for now) 163 // c.jsre.Run(`var GlobalRegistrar = klay.contract(` + registrar.GlobalRegistrarAbi + `); registrar = GlobalRegistrar.at("` + registrar.GlobalRegistrarAddr + `");`) 164 165 // If the console is in interactive mode, instrument password related methods to query the user 166 if c.prompter != nil { 167 // Retrieve the account management object to instrument 168 personal, err := c.jsre.Get("personal") 169 if err != nil { 170 return err 171 } 172 // Override the openWallet, unlockAccount, newAccount and sign methods since 173 // these require user interaction. Assign these method in the Console the 174 // original web3 callbacks. These will be called by the jeth.* methods after 175 // they got the password from the user and send the original web3 request to 176 // the backend. 177 if obj := personal.Object(); obj != nil { // make sure the personal api is enabled over the interface 178 if _, err = c.jsre.Run(`jeth.openWallet = personal.openWallet;`); err != nil { 179 return fmt.Errorf("personal.openWallet: %v", err) 180 } 181 if _, err = c.jsre.Run(`jeth.unlockAccount = personal.unlockAccount;`); err != nil { 182 return fmt.Errorf("personal.unlockAccount: %v", err) 183 } 184 if _, err = c.jsre.Run(`jeth.newAccount = personal.newAccount;`); err != nil { 185 return fmt.Errorf("personal.newAccount: %v", err) 186 } 187 if _, err = c.jsre.Run(`jeth.sign = personal.sign;`); err != nil { 188 return fmt.Errorf("personal.sign: %v", err) 189 } 190 obj.Set("openWallet", bridge.OpenWallet) 191 obj.Set("unlockAccount", bridge.UnlockAccount) 192 obj.Set("newAccount", bridge.NewAccount) 193 obj.Set("sign", bridge.Sign) 194 } 195 196 // Retrieve the account management object to instrument 197 subBridge, err := c.jsre.Get("subbridge") 198 if err != nil { 199 return err 200 } 201 // Override the unlockParentOperator, and unlockChildOperator methods since 202 // these require user interaction. Assign these method in the Console the 203 // original web3 callbacks. These will be called by the jeth.* methods after 204 // they got the password from the user and send the original web3 request to 205 // the backend. 206 if obj := subBridge.Object(); obj != nil { // make sure the subBridge api is enabled over the interface 207 if _, err = c.jsre.Run(`jeth.unlockParentOperator = subbridge.unlockParentOperator;`); err != nil { 208 return fmt.Errorf("subBridge.unlockParentOperator: %v", err) 209 } 210 if _, err = c.jsre.Run(`jeth.unlockChildOperator = subbridge.unlockChildOperator;`); err != nil { 211 return fmt.Errorf("subBridge.unlockChildOperator: %v", err) 212 } 213 obj.Set("unlockParentOperator", bridge.UnlockParentOperator) 214 obj.Set("unlockChildOperator", bridge.UnlockChildOperator) 215 } 216 } 217 // The admin.sleep and admin.sleepBlocks are offered by the console and not by the RPC layer. 218 admin, err := c.jsre.Get("admin") 219 if err != nil { 220 return err 221 } 222 if obj := admin.Object(); obj != nil { // make sure the admin api is enabled over the interface 223 obj.Set("sleepBlocks", bridge.SleepBlocks) 224 obj.Set("sleep", bridge.Sleep) 225 obj.Set("clearHistory", c.clearHistory) 226 } 227 // Preload any JavaScript files before starting the console 228 for _, path := range preload { 229 if err := c.jsre.Exec(path); err != nil { 230 failure := err.Error() 231 if ottoErr, ok := err.(*otto.Error); ok { 232 failure = ottoErr.String() 233 } 234 return fmt.Errorf("%s: %v", path, failure) 235 } 236 } 237 // Configure the console's input prompter for scrollback and tab completion 238 if c.prompter != nil { 239 if content, err := ioutil.ReadFile(c.histPath); err != nil { 240 c.prompter.SetHistory(nil) 241 } else { 242 c.history = strings.Split(string(content), "\n") 243 c.prompter.SetHistory(c.history) 244 } 245 c.prompter.SetWordCompleter(c.AutoCompleteInput) 246 } 247 return nil 248 } 249 250 func (c *Console) clearHistory() { 251 c.history = nil 252 c.prompter.ClearHistory() 253 if err := os.Remove(c.histPath); err != nil { 254 fmt.Fprintln(c.printer, "can't delete history file:", err) 255 } else { 256 fmt.Fprintln(c.printer, "history file deleted") 257 } 258 } 259 260 // consoleOutput is an override for the console.log and console.error methods to 261 // stream the output into the configured output stream instead of stdout. 262 func (c *Console) consoleOutput(call otto.FunctionCall) otto.Value { 263 output := []string{} 264 for _, argument := range call.ArgumentList { 265 output = append(output, fmt.Sprintf("%v", argument)) 266 } 267 fmt.Fprintln(c.printer, strings.Join(output, " ")) 268 return otto.Value{} 269 } 270 271 // AutoCompleteInput is a pre-assembled word completer to be used by the user 272 // input prompter to provide hints to the user about the methods available. 273 func (c *Console) AutoCompleteInput(line string, pos int) (string, []string, string) { 274 // No completions can be provided for empty inputs 275 if len(line) == 0 || pos == 0 { 276 return "", nil, "" 277 } 278 // Chunk data to relevant part for autocompletion 279 // E.g. in case of nested lines klay.getBalance(klay.coinb<tab><tab> 280 start := pos - 1 281 for ; start > 0; start-- { 282 // Skip all methods and namespaces (i.e. including the dot) 283 if line[start] == '.' || (line[start] >= 'a' && line[start] <= 'z') || (line[start] >= 'A' && line[start] <= 'Z') { 284 continue 285 } 286 // Handle web3 in a special way (i.e. other numbers aren't auto completed) 287 if start >= 3 && line[start-3:start] == "web3" { 288 start -= 3 289 continue 290 } 291 // We've hit an unexpected character, autocomplete form here 292 start++ 293 break 294 } 295 return line[:start], c.jsre.CompleteKeywords(line[start:pos]), line[pos:] 296 } 297 298 // Welcome show summary of current node instance and some metadata about the 299 // console's available modules. 300 func (c *Console) Welcome() { 301 // Print some generic Klaytn metadata 302 fmt.Fprintf(c.printer, "Welcome to the Klaytn JavaScript console!\n\n") 303 c.jsre.Run(` 304 console.log("instance: " + web3.version.node); 305 console.log(" datadir: " + admin.datadir); 306 `) 307 // List all the supported modules for the user to call 308 if apis, err := c.client.SupportedModules(); err == nil { 309 modules := make([]string, 0, len(apis)) 310 for api, version := range apis { 311 modules = append(modules, fmt.Sprintf("%s:%s", api, version)) 312 } 313 sort.Strings(modules) 314 fmt.Fprintln(c.printer, " modules:", strings.Join(modules, " ")) 315 } 316 fmt.Fprintln(c.printer) 317 } 318 319 // Evaluate executes code and pretty prints the result to the specified output 320 // stream. 321 func (c *Console) Evaluate(statement string) error { 322 defer func() { 323 if r := recover(); r != nil { 324 fmt.Fprintf(c.printer, "[native] error: %v\n", r) 325 } 326 }() 327 return c.jsre.Evaluate(statement, c.printer) 328 } 329 330 // Interactive starts an interactive user session, where input is propted from 331 // the configured user prompter. 332 func (c *Console) Interactive() { 333 var ( 334 prompt = c.prompt // Current prompt line (used for multi-line inputs) 335 indents = 0 // Current number of input indents (used for multi-line inputs) 336 input = "" // Current user input 337 scheduler = make(chan string) // Channel to send the next prompt on and receive the input 338 ) 339 // Start a goroutine to listen for promt requests and send back inputs 340 go func() { 341 for { 342 // Read the next user input 343 line, err := c.prompter.PromptInput(<-scheduler) 344 if err != nil { 345 // In case of an error, either clear the prompt or fail 346 if err == liner.ErrPromptAborted { // ctrl-C 347 prompt, indents, input = c.prompt, 0, "" 348 scheduler <- "" 349 continue 350 } 351 close(scheduler) 352 return 353 } 354 // User input retrieved, send for interpretation and loop 355 scheduler <- line 356 } 357 }() 358 // Monitor Ctrl-C too in case the input is empty and we need to bail 359 abort := make(chan os.Signal, 1) 360 signal.Notify(abort, syscall.SIGINT, syscall.SIGTERM) 361 362 // Start sending prompts to the user and reading back inputs 363 for { 364 // Send the next prompt, triggering an input read and process the result 365 scheduler <- prompt 366 select { 367 case <-abort: 368 // User forcefully quite the console 369 fmt.Fprintln(c.printer, "caught interrupt, exiting") 370 return 371 372 case line, ok := <-scheduler: 373 // User input was returned by the prompter, handle special cases 374 if !ok || (indents <= 0 && exit.MatchString(line)) { 375 return 376 } 377 if onlyWhitespace.MatchString(line) { 378 continue 379 } 380 // Append the line to the input and check for multi-line interpretation 381 input += line + "\n" 382 383 indents = countIndents(input) 384 if indents <= 0 { 385 prompt = c.prompt 386 } else { 387 prompt = strings.Repeat(".", indents*3) + " " 388 } 389 // If all the needed lines are present, save the command and run 390 if indents <= 0 { 391 if len(input) > 0 && input[0] != ' ' && !passwordRegexp.MatchString(input) { 392 if command := strings.TrimSpace(input); len(c.history) == 0 || command != c.history[len(c.history)-1] { 393 c.history = append(c.history, command) 394 if c.prompter != nil { 395 c.prompter.AppendHistory(command) 396 } 397 } 398 } 399 c.Evaluate(input) 400 input = "" 401 } 402 } 403 } 404 } 405 406 // countIndents returns the number of identations for the given input. 407 // In case of invalid input such as var a = } the result can be negative. 408 func countIndents(input string) int { 409 var ( 410 indents = 0 411 inString = false 412 strOpenChar = ' ' // keep track of the string open char to allow var str = "I'm ...."; 413 charEscaped = false // keep track if the previous char was the '\' char, allow var str = "abc\"def"; 414 ) 415 416 for _, c := range input { 417 switch c { 418 case '\\': 419 // indicate next char as escaped when in string and previous char isn't escaping this backslash 420 if !charEscaped && inString { 421 charEscaped = true 422 } 423 case '\'', '"': 424 if inString && !charEscaped && strOpenChar == c { // end string 425 inString = false 426 } else if !inString && !charEscaped { // begin string 427 inString = true 428 strOpenChar = c 429 } 430 charEscaped = false 431 case '{', '(': 432 if !inString { // ignore brackets when in string, allow var str = "a{"; without indenting 433 indents++ 434 } 435 charEscaped = false 436 case '}', ')': 437 if !inString { 438 indents-- 439 } 440 charEscaped = false 441 default: 442 charEscaped = false 443 } 444 } 445 446 return indents 447 } 448 449 // Execute runs the JavaScript file specified as the argument. 450 func (c *Console) Execute(path string) error { 451 return c.jsre.Exec(path) 452 } 453 454 // Stop cleans up the console and terminates the runtime environment. 455 func (c *Console) Stop(graceful bool) error { 456 if err := ioutil.WriteFile(c.histPath, []byte(strings.Join(c.history, "\n")), 0o600); err != nil { 457 return err 458 } 459 if err := os.Chmod(c.histPath, 0o600); err != nil { // Force 0600, even if it was different previously 460 return err 461 } 462 c.jsre.Stop(graceful) 463 return nil 464 }