github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/console/console.go (about)

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