github.com/gochain-io/gochain/v3@v3.2.8/console/console.go (about)

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