Example #1
0
func newLightweightJSRE(libPath string, client comms.EthereumClient, interactive bool, f xeth.Frontend) *jsre {
	js := &jsre{ps1: "> "}
	js.wait = make(chan *big.Int)
	js.client = client
	js.ds = docserver.New("/")

	if f == nil {
		f = js
	}

	// update state in separare forever blocks
	js.re = re.New(libPath)
	if err := js.apiBindings(f); err != nil {
		utils.Fatalf("Unable to initialize console - %v", err)
	}

	if !liner.TerminalSupported() || !interactive {
		js.prompter = dumbterm{bufio.NewReader(os.Stdin)}
	} else {
		lr := liner.NewLiner()
		js.withHistory(func(hist *os.File) { lr.ReadHistory(hist) })
		lr.SetCtrlCAborts(true)
		js.loadAutoCompletion()
		lr.SetWordCompleter(apiWordCompleter)
		lr.SetTabCompletionStyle(liner.TabPrints)
		js.prompter = lr
		js.atexit = func() {
			js.withHistory(func(hist *os.File) { hist.Truncate(0); lr.WriteHistory(hist) })
			lr.Close()
			close(js.wait)
		}
	}
	return js
}
Example #2
0
func newLightweightJSRE(docRoot string, client rpc.Client, datadir string, interactive bool) *jsre {
	js := &jsre{ps1: "> "}
	js.wait = make(chan *big.Int)
	js.client = client
	js.re = re.New(docRoot)
	if err := js.apiBindings(); err != nil {
		utils.Fatalf("Unable to initialize console - %v", err)
	}
	js.setupInput(datadir)
	return js
}
Example #3
0
func newJSRE(stack *node.Node, docRoot, corsDomain string, client rpc.Client, interactive bool) *jsre {
	js := &jsre{stack: stack, ps1: "> "}
	// set default cors domain used by startRpc from CLI flag
	js.corsDomain = corsDomain
	js.wait = make(chan *big.Int)
	js.client = client
	js.re = re.New(docRoot)
	if err := js.apiBindings(); err != nil {
		utils.Fatalf("Unable to connect - %v", err)
	}
	js.setupInput(stack.DataDir())
	return js
}
Example #4
0
func newJSRE(ethereum *eth.Ethereum, libPath, corsDomain string, client comms.EthereumClient, interactive bool, f xeth.Frontend) *jsre {
	js := &jsre{ethereum: ethereum, ps1: "> "}
	// set default cors domain used by startRpc from CLI flag
	js.corsDomain = corsDomain
	if f == nil {
		f = js
	}
	js.ds = docserver.New("/")
	js.xeth = xeth.New(ethereum, f)
	js.wait = js.xeth.UpdateState()
	js.client = client
	if clt, ok := js.client.(*comms.InProcClient); ok {
		if offeredApis, err := api.ParseApiString(shared.AllApis, codec.JSON, js.xeth, ethereum); err == nil {
			clt.Initialize(api.Merge(offeredApis...))
		}
	}

	// update state in separare forever blocks
	js.re = re.New(libPath)
	if err := js.apiBindings(f); err != nil {
		utils.Fatalf("Unable to connect - %v", err)
	}

	if !liner.TerminalSupported() || !interactive {
		js.prompter = dumbterm{bufio.NewReader(os.Stdin)}
	} else {
		lr := liner.NewLiner()
		js.withHistory(func(hist *os.File) { lr.ReadHistory(hist) })
		lr.SetCtrlCAborts(true)
		js.loadAutoCompletion()
		lr.SetWordCompleter(apiWordCompleter)
		lr.SetTabCompletionStyle(liner.TabPrints)
		js.prompter = lr
		js.atexit = func() {
			js.withHistory(func(hist *os.File) { hist.Truncate(0); lr.WriteHistory(hist) })
			lr.Close()
			close(js.wait)
		}
	}
	return js
}
Example #5
0
func newJSRE(ethereum *eth.Ethereum, libPath string, interactive bool, corsDomain string) *jsre {
	js := &jsre{ethereum: ethereum, ps1: "> "}
	// set default cors domain used by startRpc from CLI flag
	js.corsDomain = corsDomain
	js.xeth = xeth.New(ethereum, js)
	js.re = re.New(libPath)
	js.apiBindings()
	js.adminBindings()

	if !liner.TerminalSupported() || !interactive {
		js.prompter = dumbterm{bufio.NewReader(os.Stdin)}
	} else {
		lr := liner.NewLiner()
		js.withHistory(func(hist *os.File) { lr.ReadHistory(hist) })
		lr.SetCtrlCAborts(true)
		js.prompter = lr
		js.atexit = func() {
			js.withHistory(func(hist *os.File) { hist.Truncate(0); lr.WriteHistory(hist) })
			lr.Close()
		}
	}
	return js
}
Example #6
0
// newREPLConsole creates a new JavaScript interpreter console that can be used
// by the API to serve fronted console requests.
func newREPLConsole(eapis *etherapis.EtherAPIs) *jsre.JSRE {
	// Create a JavaScript interpreter with web3 injected
	console := jsre.New("")

	client, _ := eapis.Geth().Stack().Attach()

	jeth := utils.NewJeth(console, client)
	console.Set("jeth", struct{}{})
	t, _ := console.Get("jeth")
	jethObj := t.Object()

	jethObj.Set("send", jeth.Send)
	jethObj.Set("sendAsync", jeth.Send)

	console.Compile("bignumber.js", jsre.BigNumber_JS)
	console.Compile("web3.js", jsre.Web3_JS)
	console.Run("var Web3 = require('web3');")
	console.Run("var web3 = new Web3(jeth);")

	// Inject all of the APIs exposed by the inproc RPC client
	shortcut := "var eth = web3.eth; var personal = web3.personal; "
	apis, _ := client.SupportedModules()
	for api, _ := range apis {
		if api == "web3" || api == "rpc" {
			continue
		}
		if jsFile, ok := web3ext.Modules[api]; ok {
			console.Compile(fmt.Sprintf("%s.js", api), jsFile)
			shortcut += fmt.Sprintf("var %s = web3.%s; ", api, api)
		}
	}
	console.Run(shortcut)

	// Finally return the ready console
	return console
}