2015-07-07 00:54:22 +00:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2016-04-14 16:18:24 +00:00
|
|
|
// This file is part of go-ethereum.
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2016-04-14 16:18:24 +00: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
|
2015-07-07 00:54:22 +00:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2016-04-14 16:18:24 +00:00
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
2015-07-07 00:54:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 16:48:40 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2016-04-14 16:18:24 +00:00
|
|
|
// GNU General Public License for more details.
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2016-04-14 16:18:24 +00:00
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 00:54:22 +00:00
|
|
|
|
2015-12-16 09:58:01 +00:00
|
|
|
package utils
|
2015-03-15 06:21:54 +00:00
|
|
|
|
|
|
|
import (
|
2015-06-17 14:33:34 +00:00
|
|
|
"encoding/json"
|
2015-07-03 10:22:20 +00:00
|
|
|
"fmt"
|
2016-01-19 08:26:17 +00:00
|
|
|
"time"
|
2015-07-03 10:22:20 +00:00
|
|
|
|
2015-03-20 12:22:01 +00:00
|
|
|
"github.com/ethereum/go-ethereum/jsre"
|
2015-12-16 09:58:01 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2015-08-07 07:56:49 +00:00
|
|
|
|
2015-06-09 07:48:18 +00:00
|
|
|
"github.com/robertkrimen/otto"
|
2015-03-15 06:21:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Jeth struct {
|
2015-06-17 14:22:35 +00:00
|
|
|
re *jsre.JSRE
|
2015-12-16 09:58:01 +00:00
|
|
|
client rpc.Client
|
2015-03-15 06:21:54 +00:00
|
|
|
}
|
|
|
|
|
2015-12-16 09:58:01 +00:00
|
|
|
// NewJeth create a new backend for the JSRE console
|
|
|
|
func NewJeth(re *jsre.JSRE, client rpc.Client) *Jeth {
|
|
|
|
return &Jeth{re, client}
|
2015-03-15 06:21:54 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 08:38:54 +00:00
|
|
|
// err returns an error object for the given error code and message.
|
|
|
|
func (self *Jeth) err(call otto.FunctionCall, code int, msg string, id interface{}) (response otto.Value) {
|
2015-12-16 09:58:01 +00:00
|
|
|
m := rpc.JSONErrResponse{
|
|
|
|
Version: "2.0",
|
|
|
|
Id: id,
|
|
|
|
Error: rpc.JSONError{
|
|
|
|
Code: code,
|
|
|
|
Message: msg,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-08-26 09:09:09 +00:00
|
|
|
errObj, _ := json.Marshal(m.Error)
|
|
|
|
errRes, _ := json.Marshal(m)
|
2015-07-03 10:22:20 +00:00
|
|
|
|
2015-08-26 09:09:09 +00:00
|
|
|
call.Otto.Run("ret_error = " + string(errObj))
|
|
|
|
res, _ := call.Otto.Run("ret_response = " + string(errRes))
|
2015-07-03 10:22:20 +00:00
|
|
|
|
|
|
|
return res
|
2015-03-15 06:21:54 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 08:38:54 +00:00
|
|
|
// UnlockAccount asks the user for the password and than executes the jeth.UnlockAccount callback in the jsre.
|
|
|
|
// It will need the public address for the account to unlock as first argument.
|
|
|
|
// The second argument is an optional string with the password. If not given the user is prompted for the password.
|
|
|
|
// The third argument is an optional integer which specifies for how long the account will be unlocked (in seconds).
|
2015-10-15 14:07:19 +00:00
|
|
|
func (self *Jeth) UnlockAccount(call otto.FunctionCall) (response otto.Value) {
|
2016-03-14 08:38:54 +00:00
|
|
|
var account, passwd otto.Value
|
|
|
|
duration := otto.NullValue()
|
2015-10-15 14:07:19 +00:00
|
|
|
|
2016-03-14 08:38:54 +00:00
|
|
|
if !call.Argument(0).IsString() {
|
|
|
|
fmt.Println("first argument must be the account to unlock")
|
2015-10-15 14:07:19 +00:00
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
|
|
|
|
2016-03-14 08:38:54 +00:00
|
|
|
account = call.Argument(0)
|
|
|
|
|
|
|
|
// if password is not given or as null value -> ask user for password
|
|
|
|
if call.Argument(1).IsUndefined() || call.Argument(1).IsNull() {
|
|
|
|
fmt.Printf("Unlock account %s\n", account)
|
2016-03-21 13:05:22 +00:00
|
|
|
if input, err := Stdin.PasswordPrompt("Passphrase: "); err != nil {
|
2016-03-14 08:38:54 +00:00
|
|
|
throwJSExeception(err.Error())
|
2016-04-12 15:01:37 +00:00
|
|
|
} else {
|
|
|
|
passwd, _ = otto.ToValue(input)
|
2015-10-15 14:07:19 +00:00
|
|
|
}
|
2016-03-14 08:38:54 +00:00
|
|
|
} else {
|
|
|
|
if !call.Argument(1).IsString() {
|
|
|
|
throwJSExeception("password must be a string")
|
2015-10-15 14:07:19 +00:00
|
|
|
}
|
2016-03-14 08:38:54 +00:00
|
|
|
passwd = call.Argument(1)
|
2015-10-15 14:07:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 08:38:54 +00:00
|
|
|
// third argument is the duration how long the account must be unlocked.
|
|
|
|
// verify that its a number.
|
|
|
|
if call.Argument(2).IsDefined() && !call.Argument(2).IsNull() {
|
|
|
|
if !call.Argument(2).IsNumber() {
|
|
|
|
throwJSExeception("unlock duration must be a number")
|
2015-10-15 14:07:19 +00:00
|
|
|
}
|
2016-03-14 08:38:54 +00:00
|
|
|
duration = call.Argument(2)
|
2015-10-15 14:07:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 08:38:54 +00:00
|
|
|
// jeth.unlockAccount will send the request to the backend.
|
|
|
|
if val, err := call.Otto.Call("jeth.unlockAccount", nil, account, passwd, duration); err == nil {
|
2015-10-15 14:07:19 +00:00
|
|
|
return val
|
2016-03-14 08:38:54 +00:00
|
|
|
} else {
|
|
|
|
throwJSExeception(err.Error())
|
2015-10-15 14:07:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAccount asks the user for the password and than executes the jeth.newAccount callback in the jsre
|
|
|
|
func (self *Jeth) NewAccount(call otto.FunctionCall) (response otto.Value) {
|
2016-03-08 13:50:07 +00:00
|
|
|
var passwd string
|
2015-10-15 14:07:19 +00:00
|
|
|
if len(call.ArgumentList) == 0 {
|
2016-03-08 13:50:07 +00:00
|
|
|
var err error
|
2016-03-21 13:05:22 +00:00
|
|
|
passwd, err = Stdin.PasswordPrompt("Passphrase: ")
|
2015-10-15 14:07:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
2016-03-21 13:05:22 +00:00
|
|
|
passwd2, err := Stdin.PasswordPrompt("Repeat passphrase: ")
|
2015-10-15 14:07:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
if passwd != passwd2 {
|
|
|
|
fmt.Println("Passphrases don't match")
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
2016-03-08 13:50:07 +00:00
|
|
|
} else if len(call.ArgumentList) == 1 && call.Argument(0).IsString() {
|
|
|
|
passwd, _ = call.Argument(0).ToString()
|
|
|
|
} else {
|
|
|
|
fmt.Println("expected 0 or 1 string argument")
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
2015-10-15 14:07:19 +00:00
|
|
|
|
2016-04-15 09:06:57 +00:00
|
|
|
ret, err := call.Otto.Call("jeth.newAccount", nil, passwd)
|
|
|
|
if err == nil {
|
2016-03-08 13:50:07 +00:00
|
|
|
return ret
|
2015-10-15 14:07:19 +00:00
|
|
|
}
|
2016-04-15 09:06:57 +00:00
|
|
|
fmt.Println(err)
|
2015-10-15 14:07:19 +00:00
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
|
|
|
|
2016-03-14 08:38:54 +00:00
|
|
|
// Send will serialize the first argument, send it to the node and returns the response.
|
2015-03-15 06:21:54 +00:00
|
|
|
func (self *Jeth) Send(call otto.FunctionCall) (response otto.Value) {
|
2016-03-14 08:38:54 +00:00
|
|
|
// verify we got a batch request (array) or a single request (object)
|
|
|
|
ro := call.Argument(0).Object()
|
|
|
|
if ro == nil || (ro.Class() != "Array" && ro.Class() != "Object") {
|
|
|
|
throwJSExeception("Internal Error: request must be an object or array")
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert otto vm arguments to go values by JSON serialising and parsing.
|
|
|
|
data, err := call.Otto.Call("JSON.stringify", nil, ro)
|
2015-03-15 06:21:54 +00:00
|
|
|
if err != nil {
|
2016-03-14 08:38:54 +00:00
|
|
|
throwJSExeception(err.Error())
|
2015-03-15 06:21:54 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 08:38:54 +00:00
|
|
|
jsonreq, _ := data.ToString()
|
|
|
|
|
|
|
|
// parse arguments to JSON rpc requests, either to an array (batch) or to a single request.
|
2015-12-16 09:58:01 +00:00
|
|
|
var reqs []rpc.JSONRequest
|
2015-06-08 11:21:24 +00:00
|
|
|
batch := true
|
2016-03-14 08:38:54 +00:00
|
|
|
if err = json.Unmarshal([]byte(jsonreq), &reqs); err != nil {
|
|
|
|
// single request?
|
2015-12-16 09:58:01 +00:00
|
|
|
reqs = make([]rpc.JSONRequest, 1)
|
2016-03-14 08:38:54 +00:00
|
|
|
if err = json.Unmarshal([]byte(jsonreq), &reqs[0]); err != nil {
|
|
|
|
throwJSExeception("invalid request")
|
|
|
|
}
|
2015-06-08 11:21:24 +00:00
|
|
|
batch = false
|
|
|
|
}
|
|
|
|
|
|
|
|
call.Otto.Set("response_len", len(reqs))
|
|
|
|
call.Otto.Run("var ret_response = new Array(response_len);")
|
|
|
|
|
|
|
|
for i, req := range reqs {
|
2016-03-14 08:38:54 +00:00
|
|
|
if err := self.client.Send(&req); err != nil {
|
2015-06-17 14:22:35 +00:00
|
|
|
return self.err(call, -32603, err.Error(), req.Id)
|
|
|
|
}
|
2015-07-03 10:22:20 +00:00
|
|
|
|
2015-12-16 09:58:01 +00:00
|
|
|
result := make(map[string]interface{})
|
2016-03-14 08:38:54 +00:00
|
|
|
if err = self.client.Recv(&result); err != nil {
|
2015-06-08 11:21:24 +00:00
|
|
|
return self.err(call, -32603, err.Error(), req.Id)
|
|
|
|
}
|
2015-06-17 14:22:35 +00:00
|
|
|
|
2015-12-16 09:58:01 +00:00
|
|
|
id, _ := result["id"]
|
|
|
|
jsonver, _ := result["jsonrpc"]
|
2016-03-14 08:38:54 +00:00
|
|
|
|
|
|
|
call.Otto.Set("ret_id", id)
|
2015-12-16 09:58:01 +00:00
|
|
|
call.Otto.Set("ret_jsonrpc", jsonver)
|
2016-03-14 08:38:54 +00:00
|
|
|
call.Otto.Set("response_idx", i)
|
2015-12-16 09:58:01 +00:00
|
|
|
|
2016-03-14 08:38:54 +00:00
|
|
|
// call was successful
|
|
|
|
if res, ok := result["result"]; ok {
|
|
|
|
payload, _ := json.Marshal(res)
|
|
|
|
call.Otto.Set("ret_result", string(payload))
|
|
|
|
response, err = call.Otto.Run(`
|
|
|
|
ret_response[response_idx] = { jsonrpc: ret_jsonrpc, id: ret_id, result: JSON.parse(ret_result) };
|
|
|
|
`)
|
|
|
|
continue
|
2015-08-07 07:56:49 +00:00
|
|
|
}
|
2015-12-16 09:58:01 +00:00
|
|
|
|
2016-03-14 08:38:54 +00:00
|
|
|
// request returned an error
|
|
|
|
if res, ok := result["error"]; ok {
|
|
|
|
payload, _ := json.Marshal(res)
|
|
|
|
call.Otto.Set("ret_result", string(payload))
|
|
|
|
response, err = call.Otto.Run(`
|
|
|
|
ret_response[response_idx] = { jsonrpc: ret_jsonrpc, id: ret_id, error: JSON.parse(ret_result) };
|
|
|
|
`)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.err(call, -32603, fmt.Sprintf("Invalid response"), new(int64))
|
2015-06-08 11:21:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !batch {
|
|
|
|
call.Otto.Run("ret_response = ret_response[0];")
|
|
|
|
}
|
|
|
|
|
2016-03-14 08:38:54 +00:00
|
|
|
// if a callback was given execute it.
|
2015-06-08 11:21:24 +00:00
|
|
|
if call.Argument(1).IsObject() {
|
|
|
|
call.Otto.Set("callback", call.Argument(1))
|
|
|
|
call.Otto.Run(`
|
2015-12-16 09:58:01 +00:00
|
|
|
if (Object.prototype.toString.call(callback) == '[object Function]') {
|
2015-06-08 11:21:24 +00:00
|
|
|
callback(null, ret_response);
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2015-08-07 07:56:49 +00:00
|
|
|
|
2016-01-19 08:26:17 +00:00
|
|
|
// throwJSExeception panics on an otto value, the Otto VM will then throw msg as a javascript error.
|
|
|
|
func throwJSExeception(msg interface{}) otto.Value {
|
|
|
|
p, _ := otto.ToValue(msg)
|
|
|
|
panic(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sleep will halt the console for arg[0] seconds.
|
|
|
|
func (self *Jeth) Sleep(call otto.FunctionCall) (response otto.Value) {
|
|
|
|
if len(call.ArgumentList) >= 1 {
|
|
|
|
if call.Argument(0).IsNumber() {
|
|
|
|
sleep, _ := call.Argument(0).ToInteger()
|
|
|
|
time.Sleep(time.Duration(sleep) * time.Second)
|
|
|
|
return otto.TrueValue()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return throwJSExeception("usage: sleep(<sleep in seconds>)")
|
|
|
|
}
|
|
|
|
|
|
|
|
// SleepBlocks will wait for a specified number of new blocks or max for a
|
|
|
|
// given of seconds. sleepBlocks(nBlocks[, maxSleep]).
|
|
|
|
func (self *Jeth) SleepBlocks(call otto.FunctionCall) (response otto.Value) {
|
|
|
|
nBlocks := int64(0)
|
|
|
|
maxSleep := int64(9999999999999999) // indefinitely
|
|
|
|
|
|
|
|
nArgs := len(call.ArgumentList)
|
|
|
|
|
|
|
|
if nArgs == 0 {
|
|
|
|
throwJSExeception("usage: sleepBlocks(<n blocks>[, max sleep in seconds])")
|
|
|
|
}
|
|
|
|
|
|
|
|
if nArgs >= 1 {
|
|
|
|
if call.Argument(0).IsNumber() {
|
|
|
|
nBlocks, _ = call.Argument(0).ToInteger()
|
|
|
|
} else {
|
|
|
|
throwJSExeception("expected number as first argument")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if nArgs >= 2 {
|
|
|
|
if call.Argument(1).IsNumber() {
|
|
|
|
maxSleep, _ = call.Argument(1).ToInteger()
|
|
|
|
} else {
|
|
|
|
throwJSExeception("expected number as second argument")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// go through the console, this will allow web3 to call the appropriate
|
|
|
|
// callbacks if a delayed response or notification is received.
|
|
|
|
currentBlockNr := func() int64 {
|
|
|
|
result, err := call.Otto.Run("eth.blockNumber")
|
|
|
|
if err != nil {
|
|
|
|
throwJSExeception(err.Error())
|
|
|
|
}
|
|
|
|
blockNr, err := result.ToInteger()
|
|
|
|
if err != nil {
|
|
|
|
throwJSExeception(err.Error())
|
|
|
|
}
|
|
|
|
return blockNr
|
|
|
|
}
|
|
|
|
|
|
|
|
targetBlockNr := currentBlockNr() + nBlocks
|
|
|
|
deadline := time.Now().Add(time.Duration(maxSleep) * time.Second)
|
|
|
|
|
|
|
|
for time.Now().Before(deadline) {
|
|
|
|
if currentBlockNr() >= targetBlockNr {
|
|
|
|
return otto.TrueValue()
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|