| 
									
										
										
										
											2015-03-04 12:18:26 +01:00
										 |  |  | // Copyright (c) 2013-2014, Jeffrey Wilcke. All rights reserved. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This library is free software; you can redistribute it and/or | 
					
						
							|  |  |  | // modify it under the terms of the GNU General Public | 
					
						
							|  |  |  | // License as published by the Free Software Foundation; either | 
					
						
							|  |  |  | // version 2.1 of the License, or (at your option) any later version. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This library is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
					
						
							|  |  |  | // General Public License for more details. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | // along with this library; if not, write to the Free Software | 
					
						
							|  |  |  | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | 
					
						
							|  |  |  | // MA 02110-1301  USA | 
					
						
							| 
									
										
										
										
											2014-10-23 15:48:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-23 12:20:59 +01:00
										 |  |  | package main | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2015-03-06 12:18:44 +01:00
										 |  |  | 	"bufio" | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2014-08-11 16:24:35 +02:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | 	"path" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2014-08-11 16:24:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-15 13:31:40 +07:00
										 |  |  | 	"github.com/ethereum/go-ethereum/cmd/utils" | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/core/types" | 
					
						
							| 
									
										
										
										
											2014-12-14 18:23:48 +00:00
										 |  |  | 	"github.com/ethereum/go-ethereum/eth" | 
					
						
							| 
									
										
										
										
											2015-03-15 13:31:40 +07:00
										 |  |  | 	re "github.com/ethereum/go-ethereum/jsre" | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/rpc" | 
					
						
							| 
									
										
										
										
											2015-03-04 12:18:26 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/xeth" | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | 	"github.com/peterh/liner" | 
					
						
							| 
									
										
										
										
											2015-03-24 17:49:28 +01:00
										 |  |  | 	"github.com/robertkrimen/otto" | 
					
						
							| 
									
										
										
										
											2014-06-23 12:20:59 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | type prompter interface { | 
					
						
							|  |  |  | 	AppendHistory(string) | 
					
						
							|  |  |  | 	Prompt(p string) (string, error) | 
					
						
							|  |  |  | 	PasswordPrompt(p string) (string, error) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-10 02:33:59 +01:00
										 |  |  | type dumbterm struct{ r *bufio.Reader } | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-10 02:33:59 +01:00
										 |  |  | func (r dumbterm) Prompt(p string) (string, error) { | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | 	fmt.Print(p) | 
					
						
							|  |  |  | 	return r.r.ReadString('\n') | 
					
						
							| 
									
										
										
										
											2014-06-23 12:20:59 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-10 02:33:59 +01:00
										 |  |  | func (r dumbterm) PasswordPrompt(p string) (string, error) { | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | 	fmt.Println("!! Unsupported terminal, password will echo.") | 
					
						
							|  |  |  | 	fmt.Print(p) | 
					
						
							|  |  |  | 	input, err := bufio.NewReader(os.Stdin).ReadString('\n') | 
					
						
							|  |  |  | 	fmt.Println() | 
					
						
							|  |  |  | 	return input, err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-10 02:33:59 +01:00
										 |  |  | func (r dumbterm) AppendHistory(string) {} | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | type jsre struct { | 
					
						
							| 
									
										
										
										
											2015-03-15 13:31:40 +07:00
										 |  |  | 	re       *re.JSRE | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | 	ethereum *eth.Ethereum | 
					
						
							|  |  |  | 	xeth     *xeth.XEth | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | 	ps1      string | 
					
						
							| 
									
										
										
										
											2015-03-10 14:31:54 +01:00
										 |  |  | 	atexit   func() | 
					
						
							| 
									
										
										
										
											2015-03-10 02:33:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | 	prompter | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-15 13:31:40 +07:00
										 |  |  | func newJSRE(ethereum *eth.Ethereum, libPath string) *jsre { | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | 	js := &jsre{ethereum: ethereum, ps1: "> "} | 
					
						
							|  |  |  | 	js.xeth = xeth.New(ethereum, js) | 
					
						
							| 
									
										
										
										
											2015-03-15 13:31:40 +07:00
										 |  |  | 	js.re = re.New(libPath) | 
					
						
							|  |  |  | 	js.apiBindings() | 
					
						
							| 
									
										
										
										
											2015-03-15 13:43:48 +07:00
										 |  |  | 	js.adminBindings() | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:18:44 +01:00
										 |  |  | 	if !liner.TerminalSupported() { | 
					
						
							| 
									
										
										
										
											2015-03-10 02:33:59 +01:00
										 |  |  | 		js.prompter = dumbterm{bufio.NewReader(os.Stdin)} | 
					
						
							| 
									
										
										
										
											2015-03-06 12:18:44 +01:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		lr := liner.NewLiner() | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | 		js.withHistory(func(hist *os.File) { lr.ReadHistory(hist) }) | 
					
						
							| 
									
										
										
										
											2015-03-10 14:31:54 +01:00
										 |  |  | 		lr.SetCtrlCAborts(true) | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | 		js.prompter = lr | 
					
						
							| 
									
										
										
										
											2015-03-10 14:31:54 +01:00
										 |  |  | 		js.atexit = func() { | 
					
						
							|  |  |  | 			js.withHistory(func(hist *os.File) { hist.Truncate(0); lr.WriteHistory(hist) }) | 
					
						
							|  |  |  | 			lr.Close() | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | 	return js | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-15 13:31:40 +07:00
										 |  |  | func (js *jsre) apiBindings() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ethApi := rpc.NewEthereumApi(js.xeth, js.ethereum.DataDir) | 
					
						
							| 
									
										
										
										
											2015-03-23 17:41:50 +01:00
										 |  |  | 	ethApi.Close() | 
					
						
							| 
									
										
										
										
											2015-03-20 13:22:01 +01:00
										 |  |  | 	//js.re.Bind("jeth", rpc.NewJeth(ethApi, js.re.ToVal)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	jeth := rpc.NewJeth(ethApi, js.re.ToVal, js.re) | 
					
						
							|  |  |  | 	//js.re.Bind("jeth", jeth) | 
					
						
							|  |  |  | 	js.re.Set("jeth", struct{}{}) | 
					
						
							|  |  |  | 	t, _ := js.re.Get("jeth") | 
					
						
							|  |  |  | 	jethObj := t.Object() | 
					
						
							|  |  |  | 	jethObj.Set("send", jeth.Send) | 
					
						
							| 
									
										
										
										
											2015-03-15 13:31:40 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-24 17:49:28 +01:00
										 |  |  | 	err := js.re.Compile("bignum.js", re.BigNumber_JS) | 
					
						
							| 
									
										
										
										
											2015-03-15 13:31:40 +07:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		utils.Fatalf("Error loading bignumber.js: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// we need to declare a dummy setTimeout. Otto does not support it | 
					
						
							|  |  |  | 	_, err = js.re.Eval("setTimeout = function(cb, delay) {};") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		utils.Fatalf("Error defining setTimeout: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-24 17:49:28 +01:00
										 |  |  | 	err = js.re.Compile("ethereum.js", re.Ethereum_JS) | 
					
						
							| 
									
										
										
										
											2015-03-15 13:31:40 +07:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		utils.Fatalf("Error loading ethereum.js: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-24 17:49:28 +01:00
										 |  |  | 	_, err = js.re.Eval("var web3 = require('ethereum.js');") | 
					
						
							| 
									
										
										
										
											2015-03-15 13:31:40 +07:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		utils.Fatalf("Error requiring web3: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_, err = js.re.Eval("web3.setProvider(jeth)") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		utils.Fatalf("Error setting web3 provider: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	_, err = js.re.Eval(` | 
					
						
							| 
									
										
										
										
											2015-03-24 17:49:28 +01:00
										 |  |  | var eth = web3.eth; | 
					
						
							|  |  |  | var shh = web3.shh; | 
					
						
							|  |  |  | var db  = web3.db; | 
					
						
							|  |  |  | var net = web3.net; | 
					
						
							| 
									
										
										
										
											2015-03-15 13:31:40 +07:00
										 |  |  |   `) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		utils.Fatalf("Error setting namespaces: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | func (self *jsre) ConfirmTransaction(tx *types.Transaction) bool { | 
					
						
							|  |  |  | 	p := fmt.Sprintf("Confirm Transaction %v\n[y/n] ", tx) | 
					
						
							| 
									
										
										
										
											2015-03-10 02:33:59 +01:00
										 |  |  | 	answer, _ := self.Prompt(p) | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | 	return strings.HasPrefix(strings.Trim(answer, " "), "y") | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | func (self *jsre) UnlockAccount(addr []byte) bool { | 
					
						
							|  |  |  | 	fmt.Printf("Please unlock account %x.\n", addr) | 
					
						
							| 
									
										
										
										
											2015-03-10 02:33:59 +01:00
										 |  |  | 	pass, err := self.PasswordPrompt("Passphrase: ") | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// TODO: allow retry | 
					
						
							|  |  |  | 	if err := self.ethereum.AccountManager().Unlock(addr, pass); err != nil { | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		fmt.Println("Account is now unlocked for this session.") | 
					
						
							|  |  |  | 		return true | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | func (self *jsre) exec(filename string) error { | 
					
						
							| 
									
										
										
										
											2015-03-15 13:31:40 +07:00
										 |  |  | 	if err := self.re.Exec(filename); err != nil { | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | 		return fmt.Errorf("Javascript Error: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | func (self *jsre) interactive() { | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | 	for { | 
					
						
							| 
									
										
										
										
											2015-03-10 02:33:59 +01:00
										 |  |  | 		input, err := self.Prompt(self.ps1) | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2015-03-10 14:31:54 +01:00
										 |  |  | 			break | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if input == "" { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		str += input + "\n" | 
					
						
							|  |  |  | 		self.setIndent() | 
					
						
							|  |  |  | 		if indentCount <= 0 { | 
					
						
							|  |  |  | 			if input == "exit" { | 
					
						
							| 
									
										
										
										
											2015-03-10 14:31:54 +01:00
										 |  |  | 				break | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			hist := str[:len(str)-1] | 
					
						
							| 
									
										
										
										
											2015-03-10 02:33:59 +01:00
										 |  |  | 			self.AppendHistory(hist) | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | 			self.parseInput(str) | 
					
						
							|  |  |  | 			str = "" | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-03-10 14:31:54 +01:00
										 |  |  | 	if self.atexit != nil { | 
					
						
							|  |  |  | 		self.atexit() | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | func (self *jsre) withHistory(op func(*os.File)) { | 
					
						
							|  |  |  | 	hist, err := os.OpenFile(path.Join(self.ethereum.DataDir, "history"), os.O_RDWR|os.O_CREATE, os.ModePerm) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		fmt.Printf("unable to open history file: %v\n", err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	op(hist) | 
					
						
							|  |  |  | 	hist.Close() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (self *jsre) parseInput(code string) { | 
					
						
							|  |  |  | 	defer func() { | 
					
						
							|  |  |  | 		if r := recover(); r != nil { | 
					
						
							|  |  |  | 			fmt.Println("[native] error", r) | 
					
						
							| 
									
										
										
										
											2015-03-06 12:18:44 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}() | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | 	value, err := self.re.Run(code) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2015-03-24 17:49:28 +01:00
										 |  |  | 		if ottoErr, ok := err.(*otto.Error); ok { | 
					
						
							|  |  |  | 			fmt.Println(ottoErr.String()) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			fmt.Println(err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	self.printValue(value) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-03-06 12:18:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | var indentCount = 0 | 
					
						
							|  |  |  | var str = "" | 
					
						
							| 
									
										
										
										
											2015-03-06 12:18:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | func (self *jsre) setIndent() { | 
					
						
							|  |  |  | 	open := strings.Count(str, "{") | 
					
						
							|  |  |  | 	open += strings.Count(str, "(") | 
					
						
							|  |  |  | 	closed := strings.Count(str, "}") | 
					
						
							|  |  |  | 	closed += strings.Count(str, ")") | 
					
						
							|  |  |  | 	indentCount = open - closed | 
					
						
							|  |  |  | 	if indentCount <= 0 { | 
					
						
							|  |  |  | 		self.ps1 = "> " | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		self.ps1 = strings.Join(make([]string, indentCount*2), "..") | 
					
						
							|  |  |  | 		self.ps1 += " " | 
					
						
							| 
									
										
										
										
											2015-03-06 12:18:44 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | func (self *jsre) printValue(v interface{}) { | 
					
						
							| 
									
										
										
										
											2015-03-15 13:31:40 +07:00
										 |  |  | 	val, err := self.re.PrettyPrint(v) | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | 	if err == nil { | 
					
						
							| 
									
										
										
										
											2015-03-15 13:31:40 +07:00
										 |  |  | 		fmt.Printf("%v", val) | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |