| 
									
										
										
										
											2016-04-14 18:18:24 +02:00
										 |  |  | // Copyright 2015 The go-ethereum Authors | 
					
						
							| 
									
										
										
										
											2015-07-07 02:54:22 +02:00
										 |  |  | // This file is part of go-ethereum. | 
					
						
							| 
									
										
										
										
											2015-03-04 12:18:26 +01:00
										 |  |  | // | 
					
						
							| 
									
										
										
										
											2015-07-07 02:54:22 +02:00
										 |  |  | // go-ethereum 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 3 of the License, or | 
					
						
							|  |  |  | // (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2015-03-04 12:18:26 +01:00
										 |  |  | // | 
					
						
							| 
									
										
										
										
											2015-07-07 02:54:22 +02:00
										 |  |  | // go-ethereum is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2015-03-04 12:18:26 +01:00
										 |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							| 
									
										
										
										
											2015-07-22 18:48:40 +02:00
										 |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
					
						
							| 
									
										
										
										
											2015-07-07 02:54:22 +02:00
										 |  |  | // GNU General Public License for more details. | 
					
						
							| 
									
										
										
										
											2015-03-04 12:18:26 +01:00
										 |  |  | // | 
					
						
							|  |  |  | // You should have received a copy of the GNU General Public License | 
					
						
							| 
									
										
										
										
											2015-07-22 18:48:40 +02:00
										 |  |  | // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2014-10-23 15:48:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-23 12:20:59 +01:00
										 |  |  | package main | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2015-04-22 23:11:11 +01:00
										 |  |  | 	"math/big" | 
					
						
							| 
									
										
										
										
											2014-08-11 16:24:35 +02:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2015-05-27 00:52:02 +02:00
										 |  |  | 	"os/signal" | 
					
						
							| 
									
										
										
										
											2015-05-12 14:24:11 +02:00
										 |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2015-07-31 11:51:18 +02:00
										 |  |  | 	"regexp" | 
					
						
							| 
									
										
										
										
											2015-06-18 18:23:13 +02:00
										 |  |  | 	"sort" | 
					
						
							| 
									
										
										
										
											2015-12-16 10:58:01 +01:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2015-06-18 18:23:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-07 13:48:24 +02:00
										 |  |  | 	"github.com/codegangsta/cli" | 
					
						
							| 
									
										
										
										
											2016-03-03 01:09:16 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/accounts" | 
					
						
							| 
									
										
										
										
											2015-03-15 13:31:40 +07:00
										 |  |  | 	"github.com/ethereum/go-ethereum/cmd/utils" | 
					
						
							| 
									
										
										
										
											2015-04-02 21:14:25 +02:00
										 |  |  | 	"github.com/ethereum/go-ethereum/common" | 
					
						
							| 
									
										
										
										
											2015-06-23 15:48:33 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/common/registrar" | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2015-11-17 18:33:25 +02:00
										 |  |  | 	"github.com/ethereum/go-ethereum/node" | 
					
						
							| 
									
										
										
										
											2015-03-15 13:31:40 +07:00
										 |  |  | 	"github.com/ethereum/go-ethereum/rpc" | 
					
						
							| 
									
										
										
										
											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-09-22 10:34:58 +02:00
										 |  |  | var ( | 
					
						
							|  |  |  | 	passwordRegexp = regexp.MustCompile("personal.[nu]") | 
					
						
							|  |  |  | 	leadingSpace   = regexp.MustCompile("^ ") | 
					
						
							|  |  |  | 	onlyws         = regexp.MustCompile("^\\s*$") | 
					
						
							|  |  |  | 	exit           = regexp.MustCompile("^\\s*exit\\s*;*\\s*$") | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2015-07-31 11:51:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | type jsre struct { | 
					
						
							| 
									
										
										
										
											2015-04-22 14:55:01 +01:00
										 |  |  | 	re         *re.JSRE | 
					
						
							| 
									
										
										
										
											2015-11-17 18:33:25 +02:00
										 |  |  | 	stack      *node.Node | 
					
						
							| 
									
										
										
										
											2015-04-22 23:11:11 +01:00
										 |  |  | 	wait       chan *big.Int | 
					
						
							| 
									
										
										
										
											2015-04-22 14:55:01 +01:00
										 |  |  | 	ps1        string | 
					
						
							|  |  |  | 	atexit     func() | 
					
						
							|  |  |  | 	corsDomain string | 
					
						
							| 
									
										
										
										
											2015-12-16 10:58:01 +01:00
										 |  |  | 	client     rpc.Client | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-15 14:56:26 +01:00
										 |  |  | func makeCompleter(re *jsre) liner.WordCompleter { | 
					
						
							|  |  |  | 	return func(line string, pos int) (head string, completions []string, tail string) { | 
					
						
							| 
									
										
										
										
											2015-12-16 10:58:01 +01:00
										 |  |  | 		if len(line) == 0 || pos == 0 { | 
					
						
							|  |  |  | 			return "", nil, "" | 
					
						
							| 
									
										
										
										
											2015-06-17 16:22:35 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-12-16 10:58:01 +01:00
										 |  |  | 		// chuck data to relevant part for autocompletion, e.g. in case of nested lines eth.getBalance(eth.coinb<tab><tab> | 
					
						
							|  |  |  | 		i := 0 | 
					
						
							|  |  |  | 		for i = pos - 1; i > 0; i-- { | 
					
						
							|  |  |  | 			if line[i] == '.' || (line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z') { | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-02-12 02:20:18 +01:00
										 |  |  | 			if i >= 3 && line[i] == '3' && line[i-3] == 'w' && line[i-2] == 'e' && line[i-1] == 'b' { | 
					
						
							| 
									
										
										
										
											2015-12-16 10:58:01 +01:00
										 |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			i += 1 | 
					
						
							|  |  |  | 			break | 
					
						
							| 
									
										
										
										
											2015-06-17 16:22:35 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-02-15 14:56:26 +01:00
										 |  |  | 		return line[:i], re.re.CompleteKeywords(line[i:pos]), line[pos:] | 
					
						
							| 
									
										
										
										
											2015-12-16 10:58:01 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-06-17 16:22:35 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 10:58:01 +01:00
										 |  |  | func newLightweightJSRE(docRoot string, client rpc.Client, datadir string, interactive bool) *jsre { | 
					
						
							| 
									
										
										
										
											2015-06-18 18:23:13 +02:00
										 |  |  | 	js := &jsre{ps1: "> "} | 
					
						
							|  |  |  | 	js.wait = make(chan *big.Int) | 
					
						
							|  |  |  | 	js.client = client | 
					
						
							| 
									
										
										
										
											2015-09-22 10:34:58 +02:00
										 |  |  | 	js.re = re.New(docRoot) | 
					
						
							| 
									
										
										
										
											2015-12-16 10:58:01 +01:00
										 |  |  | 	if err := js.apiBindings(); err != nil { | 
					
						
							| 
									
										
										
										
											2015-06-18 18:23:13 +02:00
										 |  |  | 		utils.Fatalf("Unable to initialize console - %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-03-21 14:05:22 +01:00
										 |  |  | 	js.setupInput(datadir) | 
					
						
							| 
									
										
										
										
											2015-06-18 18:23:13 +02:00
										 |  |  | 	return js | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 10:58:01 +01:00
										 |  |  | func newJSRE(stack *node.Node, docRoot, corsDomain string, client rpc.Client, interactive bool) *jsre { | 
					
						
							| 
									
										
										
										
											2015-11-17 18:33:25 +02:00
										 |  |  | 	js := &jsre{stack: stack, ps1: "> "} | 
					
						
							| 
									
										
										
										
											2015-04-22 14:55:01 +01:00
										 |  |  | 	// set default cors domain used by startRpc from CLI flag | 
					
						
							|  |  |  | 	js.corsDomain = corsDomain | 
					
						
							| 
									
										
										
										
											2015-12-16 10:58:01 +01:00
										 |  |  | 	js.wait = make(chan *big.Int) | 
					
						
							| 
									
										
										
										
											2015-06-17 16:22:35 +02:00
										 |  |  | 	js.client = client | 
					
						
							| 
									
										
										
										
											2015-09-22 10:34:58 +02:00
										 |  |  | 	js.re = re.New(docRoot) | 
					
						
							| 
									
										
										
										
											2015-12-16 10:58:01 +01:00
										 |  |  | 	if err := js.apiBindings(); err != nil { | 
					
						
							| 
									
										
										
										
											2015-06-17 16:22:35 +02:00
										 |  |  | 		utils.Fatalf("Unable to connect - %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-03-21 14:05:22 +01:00
										 |  |  | 	js.setupInput(stack.DataDir()) | 
					
						
							|  |  |  | 	return js | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-21 14:05:22 +01:00
										 |  |  | func (self *jsre) setupInput(datadir string) { | 
					
						
							|  |  |  | 	self.withHistory(datadir, func(hist *os.File) { utils.Stdin.ReadHistory(hist) }) | 
					
						
							|  |  |  | 	utils.Stdin.SetCtrlCAborts(true) | 
					
						
							|  |  |  | 	utils.Stdin.SetWordCompleter(makeCompleter(self)) | 
					
						
							|  |  |  | 	utils.Stdin.SetTabCompletionStyle(liner.TabPrints) | 
					
						
							|  |  |  | 	self.atexit = func() { | 
					
						
							|  |  |  | 		self.withHistory(datadir, func(hist *os.File) { | 
					
						
							|  |  |  | 			hist.Truncate(0) | 
					
						
							|  |  |  | 			utils.Stdin.WriteHistory(hist) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		utils.Stdin.Close() | 
					
						
							|  |  |  | 		close(self.wait) | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 14:04:18 +02:00
										 |  |  | func (self *jsre) batch(statement string) { | 
					
						
							| 
									
										
										
										
											2015-08-14 13:06:34 +02:00
										 |  |  | 	err := self.re.EvalAndPrettyPrint(statement) | 
					
						
							| 
									
										
										
										
											2015-06-19 14:04:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		fmt.Printf("error: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if self.atexit != nil { | 
					
						
							|  |  |  | 		self.atexit() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	self.re.Stop(false) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-18 18:23:13 +02:00
										 |  |  | // show summary of current geth instance | 
					
						
							|  |  |  | func (self *jsre) welcome() { | 
					
						
							| 
									
										
										
										
											2015-08-11 17:14:46 +01:00
										 |  |  | 	self.re.Run(` | 
					
						
							| 
									
										
										
										
											2015-10-26 22:24:09 +01:00
										 |  |  |     (function () { | 
					
						
							| 
									
										
										
										
											2015-11-30 09:49:10 +01:00
										 |  |  |       console.log('instance: ' + web3.version.node); | 
					
						
							| 
									
										
										
										
											2015-10-26 22:24:09 +01:00
										 |  |  |       console.log("coinbase: " + eth.coinbase); | 
					
						
							|  |  |  |       var ts = 1000 * eth.getBlock(eth.blockNumber).timestamp; | 
					
						
							|  |  |  |       console.log("at block: " + eth.blockNumber + " (" + new Date(ts) + ")"); | 
					
						
							| 
									
										
										
										
											2015-10-15 16:07:19 +02:00
										 |  |  |       console.log(' datadir: ' + admin.datadir); | 
					
						
							| 
									
										
										
										
											2015-10-26 22:24:09 +01:00
										 |  |  |     })(); | 
					
						
							|  |  |  |   `) | 
					
						
							| 
									
										
										
										
											2015-06-18 18:23:13 +02:00
										 |  |  | 	if modules, err := self.supportedApis(); err == nil { | 
					
						
							|  |  |  | 		loadedModules := make([]string, 0) | 
					
						
							|  |  |  | 		for api, version := range modules { | 
					
						
							|  |  |  | 			loadedModules = append(loadedModules, fmt.Sprintf("%s:%s", api, version)) | 
					
						
							| 
									
										
										
										
											2015-06-17 16:22:35 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-06-18 18:23:13 +02:00
										 |  |  | 		sort.Strings(loadedModules) | 
					
						
							| 
									
										
										
										
											2015-06-17 16:22:35 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-06-18 18:23:13 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-06-17 16:22:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-18 18:23:13 +02:00
										 |  |  | func (self *jsre) supportedApis() (map[string]string, error) { | 
					
						
							|  |  |  | 	return self.client.SupportedModules() | 
					
						
							| 
									
										
										
										
											2015-06-17 16:22:35 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 10:58:01 +01:00
										 |  |  | func (js *jsre) apiBindings() error { | 
					
						
							| 
									
										
										
										
											2015-06-18 18:23:13 +02:00
										 |  |  | 	apis, err := js.supportedApis() | 
					
						
							| 
									
										
										
										
											2015-06-17 16:22:35 +02:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	apiNames := make([]string, 0, len(apis)) | 
					
						
							|  |  |  | 	for a, _ := range apis { | 
					
						
							|  |  |  | 		apiNames = append(apiNames, a) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 10:58:01 +01:00
										 |  |  | 	jeth := utils.NewJeth(js.re, js.client) | 
					
						
							| 
									
										
										
										
											2015-06-17 16:22:35 +02:00
										 |  |  | 	js.re.Set("jeth", struct{}{}) | 
					
						
							|  |  |  | 	t, _ := js.re.Get("jeth") | 
					
						
							|  |  |  | 	jethObj := t.Object() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	jethObj.Set("send", jeth.Send) | 
					
						
							|  |  |  | 	jethObj.Set("sendAsync", jeth.Send) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err = js.re.Compile("bignumber.js", re.BigNumber_JS) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		utils.Fatalf("Error loading bignumber.js: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-12 02:20:18 +01:00
										 |  |  | 	err = js.re.Compile("web3.js", re.Web3_JS) | 
					
						
							| 
									
										
										
										
											2015-06-17 16:22:35 +02:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		utils.Fatalf("Error loading web3.js: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-24 09:37:29 +01:00
										 |  |  | 	_, err = js.re.Run("var Web3 = require('web3');") | 
					
						
							| 
									
										
										
										
											2015-06-17 16:22:35 +02:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		utils.Fatalf("Error requiring web3: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-24 09:37:29 +01:00
										 |  |  | 	_, err = js.re.Run("var web3 = new Web3(jeth);") | 
					
						
							| 
									
										
										
										
											2015-06-17 16:22:35 +02:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		utils.Fatalf("Error setting web3 provider: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// load only supported API's in javascript runtime | 
					
						
							| 
									
										
										
										
											2016-03-14 09:38:54 +01:00
										 |  |  | 	shortcuts := "var eth = web3.eth; var personal = web3.personal; " | 
					
						
							| 
									
										
										
										
											2015-06-17 16:22:35 +02:00
										 |  |  | 	for _, apiName := range apiNames { | 
					
						
							| 
									
										
										
										
											2015-12-16 10:58:01 +01:00
										 |  |  | 		if apiName == "web3" || apiName == "rpc" { | 
					
						
							|  |  |  | 			continue // manually mapped or ignore | 
					
						
							| 
									
										
										
										
											2015-06-17 16:22:35 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 10:58:01 +01:00
										 |  |  | 		if jsFile, ok := rpc.WEB3Extensions[apiName]; ok { | 
					
						
							|  |  |  | 			if err = js.re.Compile(fmt.Sprintf("%s.js", apiName), jsFile); err == nil { | 
					
						
							|  |  |  | 				shortcuts += fmt.Sprintf("var %s = web3.%s; ", apiName, apiName) | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				utils.Fatalf("Error loading %s.js: %v", apiName, err) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-06-17 16:22:35 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 17:14:46 +01:00
										 |  |  | 	_, err = js.re.Run(shortcuts) | 
					
						
							| 
									
										
										
										
											2015-06-17 16:22:35 +02:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		utils.Fatalf("Error setting namespaces: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-26 22:24:09 +01:00
										 |  |  | 	js.re.Run(`var GlobalRegistrar = eth.contract(` + registrar.GlobalRegistrarAbi + `);   registrar = GlobalRegistrar.at("` + registrar.GlobalRegistrarAddr + `");`) | 
					
						
							| 
									
										
										
										
											2015-10-15 16:07:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// overrule some of the methods that require password as input and ask for it interactively | 
					
						
							|  |  |  | 	p, err := js.re.Get("personal") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		fmt.Println("Unable to overrule sensitive methods in personal module") | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Override the unlockAccount and newAccount methods on the personal object since these require user interaction. | 
					
						
							|  |  |  | 	// Assign the jeth.unlockAccount and jeth.newAccount in the jsre the original web3 callbacks. These will be called | 
					
						
							|  |  |  | 	// by the jeth.* methods after they got the password from the user and send the original web3 request to the backend. | 
					
						
							| 
									
										
										
										
											2015-12-18 15:23:43 +01:00
										 |  |  | 	if persObj := p.Object(); persObj != nil { // make sure the personal api is enabled over the interface | 
					
						
							|  |  |  | 		js.re.Run(`jeth.unlockAccount = personal.unlockAccount;`) | 
					
						
							|  |  |  | 		persObj.Set("unlockAccount", jeth.UnlockAccount) | 
					
						
							|  |  |  | 		js.re.Run(`jeth.newAccount = personal.newAccount;`) | 
					
						
							|  |  |  | 		persObj.Set("newAccount", jeth.NewAccount) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-10-15 16:07:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 09:26:17 +01:00
										 |  |  | 	// The admin.sleep and admin.sleepBlocks are offered by the console and not by the RPC layer. | 
					
						
							|  |  |  | 	// Bind these if the admin module is available. | 
					
						
							|  |  |  | 	if a, err := js.re.Get("admin"); err == nil { | 
					
						
							|  |  |  | 		if adminObj := a.Object(); adminObj != nil { | 
					
						
							|  |  |  | 			adminObj.Set("sleepBlocks", jeth.SleepBlocks) | 
					
						
							|  |  |  | 			adminObj.Set("sleep", jeth.Sleep) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-17 16:22:35 +02:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-22 10:34:58 +02:00
										 |  |  | func (self *jsre) AskPassword() (string, bool) { | 
					
						
							| 
									
										
										
										
											2016-03-21 14:05:22 +01:00
										 |  |  | 	pass, err := utils.Stdin.PasswordPrompt("Passphrase: ") | 
					
						
							| 
									
										
										
										
											2015-09-22 10:34:58 +02:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return "", false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return pass, true | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-31 08:28:12 +02:00
										 |  |  | func (self *jsre) ConfirmTransaction(tx string) bool { | 
					
						
							| 
									
										
										
										
											2015-11-17 18:33:25 +02:00
										 |  |  | 	// Retrieve the Ethereum instance from the node | 
					
						
							|  |  |  | 	var ethereum *eth.Ethereum | 
					
						
							| 
									
										
										
										
											2015-11-26 18:35:44 +02:00
										 |  |  | 	if err := self.stack.Service(ðereum); err != nil { | 
					
						
							| 
									
										
										
										
											2015-11-17 18:33:25 +02:00
										 |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// If natspec is enabled, ask for permission | 
					
						
							| 
									
										
										
										
											2015-12-16 10:58:01 +01:00
										 |  |  | 	if ethereum.NatSpec && false /* disabled for now */ { | 
					
						
							|  |  |  | 		//		notice := natspec.GetNotice(self.xeth, tx, ethereum.HTTPClient()) | 
					
						
							|  |  |  | 		//		fmt.Println(notice) | 
					
						
							|  |  |  | 		//		answer, _ := self.Prompt("Confirm Transaction [y/n]") | 
					
						
							|  |  |  | 		//		return strings.HasPrefix(strings.Trim(answer, " "), "y") | 
					
						
							| 
									
										
										
										
											2015-04-08 13:22:31 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-12-16 10:58:01 +01:00
										 |  |  | 	return true | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							| 
									
										
										
										
											2016-03-21 14:05:22 +01:00
										 |  |  | 	pass, err := utils.Stdin.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 | 
					
						
							| 
									
										
										
										
											2015-11-17 18:33:25 +02:00
										 |  |  | 	var ethereum *eth.Ethereum | 
					
						
							| 
									
										
										
										
											2015-11-26 18:35:44 +02:00
										 |  |  | 	if err := self.stack.Service(ðereum); err != nil { | 
					
						
							| 
									
										
										
										
											2015-11-17 18:33:25 +02:00
										 |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-03-03 01:09:16 +01:00
										 |  |  | 	a := accounts.Account{Address: common.BytesToAddress(addr)} | 
					
						
							|  |  |  | 	if err := ethereum.AccountManager().Unlock(a, pass); err != nil { | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | 		return false | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		fmt.Println("Account is now unlocked for this session.") | 
					
						
							|  |  |  | 		return true | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-07 13:48:24 +02:00
										 |  |  | // preloadJSFiles loads JS files that the user has specified with ctx.PreLoadJSFlag into | 
					
						
							|  |  |  | // the JSRE. If not all files could be loaded it will return an error describing the error. | 
					
						
							|  |  |  | func (self *jsre) preloadJSFiles(ctx *cli.Context) error { | 
					
						
							|  |  |  | 	if ctx.GlobalString(utils.PreLoadJSFlag.Name) != "" { | 
					
						
							|  |  |  | 		assetPath := ctx.GlobalString(utils.JSpathFlag.Name) | 
					
						
							|  |  |  | 		jsFiles := strings.Split(ctx.GlobalString(utils.PreLoadJSFlag.Name), ",") | 
					
						
							|  |  |  | 		for _, file := range jsFiles { | 
					
						
							|  |  |  | 			filename := common.AbsolutePath(assetPath, strings.TrimSpace(file)) | 
					
						
							|  |  |  | 			if err := self.re.Exec(filename); err != nil { | 
					
						
							|  |  |  | 				return fmt.Errorf("%s: %v", file, err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // exec executes the JS file with the given filename and stops the JSRE | 
					
						
							| 
									
										
										
										
											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-04-22 02:31:59 +02:00
										 |  |  | 		self.re.Stop(false) | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | 		return fmt.Errorf("Javascript Error: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-04-22 02:31:59 +02:00
										 |  |  | 	self.re.Stop(true) | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | func (self *jsre) interactive() { | 
					
						
							| 
									
										
										
										
											2015-05-27 00:52:02 +02:00
										 |  |  | 	// Read input lines. | 
					
						
							|  |  |  | 	prompt := make(chan string) | 
					
						
							|  |  |  | 	inputln := make(chan string) | 
					
						
							|  |  |  | 	go func() { | 
					
						
							|  |  |  | 		defer close(inputln) | 
					
						
							|  |  |  | 		for { | 
					
						
							| 
									
										
										
										
											2016-03-21 14:05:22 +01:00
										 |  |  | 			line, err := utils.Stdin.Prompt(<-prompt) | 
					
						
							| 
									
										
										
										
											2015-05-27 00:52:02 +02:00
										 |  |  | 			if err != nil { | 
					
						
							| 
									
										
										
										
											2015-08-14 11:31:29 +02:00
										 |  |  | 				if err == liner.ErrPromptAborted { // ctrl-C | 
					
						
							|  |  |  | 					self.resetPrompt() | 
					
						
							|  |  |  | 					inputln <- "" | 
					
						
							|  |  |  | 					continue | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-05-27 00:52:02 +02:00
										 |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			inputln <- line | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-05-27 00:52:02 +02:00
										 |  |  | 	}() | 
					
						
							|  |  |  | 	// Wait for Ctrl-C, too. | 
					
						
							|  |  |  | 	sig := make(chan os.Signal, 1) | 
					
						
							|  |  |  | 	signal.Notify(sig, os.Interrupt) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	defer func() { | 
					
						
							|  |  |  | 		if self.atexit != nil { | 
					
						
							|  |  |  | 			self.atexit() | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-05-27 00:52:02 +02:00
										 |  |  | 		self.re.Stop(false) | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | 	for { | 
					
						
							|  |  |  | 		prompt <- self.ps1 | 
					
						
							|  |  |  | 		select { | 
					
						
							|  |  |  | 		case <-sig: | 
					
						
							|  |  |  | 			fmt.Println("caught interrupt, exiting") | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		case input, ok := <-inputln: | 
					
						
							| 
									
										
										
										
											2015-09-22 10:34:58 +02:00
										 |  |  | 			if !ok || indentCount <= 0 && exit.MatchString(input) { | 
					
						
							| 
									
										
										
										
											2015-05-27 00:52:02 +02:00
										 |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-09-22 10:34:58 +02:00
										 |  |  | 			if onlyws.MatchString(input) { | 
					
						
							| 
									
										
										
										
											2015-05-27 00:52:02 +02:00
										 |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			str += input + "\n" | 
					
						
							|  |  |  | 			self.setIndent() | 
					
						
							|  |  |  | 			if indentCount <= 0 { | 
					
						
							| 
									
										
										
										
											2015-09-22 10:34:58 +02:00
										 |  |  | 				if mustLogInHistory(str) { | 
					
						
							| 
									
										
										
										
											2016-03-21 14:05:22 +01:00
										 |  |  | 					utils.Stdin.AppendHistory(str[:len(str)-1]) | 
					
						
							| 
									
										
										
										
											2015-07-31 11:51:18 +02:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-05-27 00:52:02 +02:00
										 |  |  | 				self.parseInput(str) | 
					
						
							|  |  |  | 				str = "" | 
					
						
							| 
									
										
										
										
											2015-03-06 03:38:19 +01:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-22 10:34:58 +02:00
										 |  |  | func mustLogInHistory(input string) bool { | 
					
						
							|  |  |  | 	return len(input) == 0 || | 
					
						
							|  |  |  | 		passwordRegexp.MatchString(input) || | 
					
						
							| 
									
										
										
										
											2015-10-23 20:37:12 +02:00
										 |  |  | 		!leadingSpace.MatchString(input) | 
					
						
							| 
									
										
										
										
											2015-07-31 11:51:18 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 13:08:48 +02:00
										 |  |  | func (self *jsre) withHistory(datadir string, op func(*os.File)) { | 
					
						
							| 
									
										
										
										
											2015-06-18 18:23:13 +02:00
										 |  |  | 	hist, err := os.OpenFile(filepath.Join(datadir, "history"), os.O_RDWR|os.O_CREATE, os.ModePerm) | 
					
						
							| 
									
										
										
										
											2015-03-10 02:00:57 +01:00
										 |  |  | 	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-08-11 17:14:46 +01:00
										 |  |  | 	if err := self.re.EvalAndPrettyPrint(code); 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 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											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-08-14 11:31:29 +02:00
										 |  |  | func (self *jsre) resetPrompt() { | 
					
						
							|  |  |  | 	indentCount = 0 | 
					
						
							|  |  |  | 	str = "" | 
					
						
							|  |  |  | 	self.ps1 = "> " | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	} | 
					
						
							|  |  |  | } |