2016-11-09 04:01:56 +03:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
2016-05-06 12:40:23 +03:00
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum 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 Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package console
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-05-02 14:42:55 +03:00
|
|
|
"strings"
|
2016-05-06 12:40:23 +03:00
|
|
|
"time"
|
|
|
|
|
2017-08-01 18:45:17 +03:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/usbwallet"
|
2017-02-22 15:10:07 +03:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2016-05-06 12:40:23 +03:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
"github.com/robertkrimen/otto"
|
|
|
|
)
|
|
|
|
|
|
|
|
// bridge is a collection of JavaScript utility methods to bride the .js runtime
|
|
|
|
// environment and the Go RPC connection backing the remote method calls.
|
|
|
|
type bridge struct {
|
2016-07-12 18:47:15 +03:00
|
|
|
client *rpc.Client // RPC client to execute Ethereum requests through
|
2016-05-06 12:40:23 +03:00
|
|
|
prompter UserPrompter // Input prompter to allow interactive user feedback
|
|
|
|
printer io.Writer // Output writer to serialize any display strings to
|
|
|
|
}
|
|
|
|
|
|
|
|
// newBridge creates a new JavaScript wrapper around an RPC client.
|
2016-07-12 18:47:15 +03:00
|
|
|
func newBridge(client *rpc.Client, prompter UserPrompter, printer io.Writer) *bridge {
|
2016-05-06 12:40:23 +03:00
|
|
|
return &bridge{
|
|
|
|
client: client,
|
|
|
|
prompter: prompter,
|
|
|
|
printer: printer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAccount is a wrapper around the personal.newAccount RPC method that uses a
|
2017-01-06 20:44:35 +03:00
|
|
|
// non-echoing password prompt to acquire the passphrase and executes the original
|
2016-05-06 12:40:23 +03:00
|
|
|
// RPC method (saved in jeth.newAccount) with it to actually execute the RPC call.
|
|
|
|
func (b *bridge) NewAccount(call otto.FunctionCall) (response otto.Value) {
|
|
|
|
var (
|
|
|
|
password string
|
|
|
|
confirm string
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
switch {
|
|
|
|
// No password was specified, prompt the user for it
|
|
|
|
case len(call.ArgumentList) == 0:
|
|
|
|
if password, err = b.prompter.PromptPassword("Passphrase: "); err != nil {
|
|
|
|
throwJSException(err.Error())
|
|
|
|
}
|
|
|
|
if confirm, err = b.prompter.PromptPassword("Repeat passphrase: "); err != nil {
|
|
|
|
throwJSException(err.Error())
|
|
|
|
}
|
|
|
|
if password != confirm {
|
|
|
|
throwJSException("passphrases don't match!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// A single string password was specified, use that
|
|
|
|
case len(call.ArgumentList) == 1 && call.Argument(0).IsString():
|
|
|
|
password, _ = call.Argument(0).ToString()
|
|
|
|
|
|
|
|
// Otherwise fail with some error
|
|
|
|
default:
|
|
|
|
throwJSException("expected 0 or 1 string argument")
|
|
|
|
}
|
2017-01-06 20:44:35 +03:00
|
|
|
// Password acquired, execute the call and return
|
2016-05-06 12:40:23 +03:00
|
|
|
ret, err := call.Otto.Call("jeth.newAccount", nil, password)
|
|
|
|
if err != nil {
|
|
|
|
throwJSException(err.Error())
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2017-08-01 18:45:17 +03:00
|
|
|
// OpenWallet is a wrapper around personal.openWallet which can interpret and
|
|
|
|
// react to certain error messages, such as the Trezor PIN matrix request.
|
|
|
|
func (b *bridge) OpenWallet(call otto.FunctionCall) (response otto.Value) {
|
2018-04-19 16:32:02 +03:00
|
|
|
// Make sure we have a wallet specified to open
|
2017-08-01 18:45:17 +03:00
|
|
|
if !call.Argument(0).IsString() {
|
|
|
|
throwJSException("first argument must be the wallet URL to open")
|
|
|
|
}
|
|
|
|
wallet := call.Argument(0)
|
|
|
|
|
|
|
|
var passwd otto.Value
|
|
|
|
if call.Argument(1).IsUndefined() || call.Argument(1).IsNull() {
|
|
|
|
passwd, _ = otto.ToValue("")
|
|
|
|
} else {
|
|
|
|
passwd = call.Argument(1)
|
|
|
|
}
|
|
|
|
// Open the wallet and return if successful in itself
|
|
|
|
val, err := call.Otto.Call("jeth.openWallet", nil, wallet, passwd)
|
|
|
|
if err == nil {
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
// Wallet open failed, report error unless it's a PIN entry
|
|
|
|
if !strings.HasSuffix(err.Error(), usbwallet.ErrTrezorPINNeeded.Error()) {
|
|
|
|
throwJSException(err.Error())
|
|
|
|
}
|
|
|
|
// Trezor PIN matrix input requested, display the matrix to the user and fetch the data
|
|
|
|
fmt.Fprintf(b.printer, "Look at the device for number positions\n\n")
|
2017-08-14 12:21:40 +03:00
|
|
|
fmt.Fprintf(b.printer, "7 | 8 | 9\n")
|
2017-08-01 18:45:17 +03:00
|
|
|
fmt.Fprintf(b.printer, "--+---+--\n")
|
2017-08-14 12:21:40 +03:00
|
|
|
fmt.Fprintf(b.printer, "4 | 5 | 6\n")
|
2017-08-01 18:45:17 +03:00
|
|
|
fmt.Fprintf(b.printer, "--+---+--\n")
|
2017-08-14 12:21:40 +03:00
|
|
|
fmt.Fprintf(b.printer, "1 | 2 | 3\n\n")
|
2017-08-01 18:45:17 +03:00
|
|
|
|
|
|
|
if input, err := b.prompter.PromptPassword("Please enter current PIN: "); err != nil {
|
|
|
|
throwJSException(err.Error())
|
|
|
|
} else {
|
|
|
|
passwd, _ = otto.ToValue(input)
|
|
|
|
}
|
|
|
|
if val, err = call.Otto.Call("jeth.openWallet", nil, wallet, passwd); err != nil {
|
|
|
|
throwJSException(err.Error())
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2016-05-06 12:40:23 +03:00
|
|
|
// UnlockAccount is a wrapper around the personal.unlockAccount RPC method that
|
2017-01-06 20:44:35 +03:00
|
|
|
// uses a non-echoing password prompt to acquire the passphrase and executes the
|
2016-05-06 12:40:23 +03:00
|
|
|
// original RPC method (saved in jeth.unlockAccount) with it to actually execute
|
|
|
|
// the RPC call.
|
|
|
|
func (b *bridge) UnlockAccount(call otto.FunctionCall) (response otto.Value) {
|
|
|
|
// Make sure we have an account specified to unlock
|
|
|
|
if !call.Argument(0).IsString() {
|
|
|
|
throwJSException("first argument must be the account to unlock")
|
|
|
|
}
|
|
|
|
account := call.Argument(0)
|
|
|
|
|
|
|
|
// If password is not given or is the null value, prompt the user for it
|
|
|
|
var passwd otto.Value
|
|
|
|
|
|
|
|
if call.Argument(1).IsUndefined() || call.Argument(1).IsNull() {
|
|
|
|
fmt.Fprintf(b.printer, "Unlock account %s\n", account)
|
|
|
|
if input, err := b.prompter.PromptPassword("Passphrase: "); err != nil {
|
|
|
|
throwJSException(err.Error())
|
|
|
|
} else {
|
|
|
|
passwd, _ = otto.ToValue(input)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !call.Argument(1).IsString() {
|
|
|
|
throwJSException("password must be a string")
|
|
|
|
}
|
|
|
|
passwd = call.Argument(1)
|
|
|
|
}
|
|
|
|
// Third argument is the duration how long the account must be unlocked.
|
|
|
|
duration := otto.NullValue()
|
|
|
|
if call.Argument(2).IsDefined() && !call.Argument(2).IsNull() {
|
|
|
|
if !call.Argument(2).IsNumber() {
|
|
|
|
throwJSException("unlock duration must be a number")
|
|
|
|
}
|
|
|
|
duration = call.Argument(2)
|
|
|
|
}
|
|
|
|
// Send the request to the backend and return
|
|
|
|
val, err := call.Otto.Call("jeth.unlockAccount", nil, account, passwd, duration)
|
|
|
|
if err != nil {
|
|
|
|
throwJSException(err.Error())
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2016-10-28 22:25:49 +03:00
|
|
|
// Sign is a wrapper around the personal.sign RPC method that uses a non-echoing password
|
2017-01-06 20:44:35 +03:00
|
|
|
// prompt to acquire the passphrase and executes the original RPC method (saved in
|
2016-10-28 22:25:49 +03:00
|
|
|
// jeth.sign) with it to actually execute the RPC call.
|
|
|
|
func (b *bridge) Sign(call otto.FunctionCall) (response otto.Value) {
|
|
|
|
var (
|
|
|
|
message = call.Argument(0)
|
|
|
|
account = call.Argument(1)
|
|
|
|
passwd = call.Argument(2)
|
|
|
|
)
|
|
|
|
|
|
|
|
if !message.IsString() {
|
|
|
|
throwJSException("first argument must be the message to sign")
|
|
|
|
}
|
|
|
|
if !account.IsString() {
|
|
|
|
throwJSException("second argument must be the account to sign with")
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the password is not given or null ask the user and ensure password is a string
|
|
|
|
if passwd.IsUndefined() || passwd.IsNull() {
|
|
|
|
fmt.Fprintf(b.printer, "Give password for account %s\n", account)
|
|
|
|
if input, err := b.prompter.PromptPassword("Passphrase: "); err != nil {
|
|
|
|
throwJSException(err.Error())
|
|
|
|
} else {
|
|
|
|
passwd, _ = otto.ToValue(input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !passwd.IsString() {
|
|
|
|
throwJSException("third argument must be the password to unlock the account")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the request to the backend and return
|
|
|
|
val, err := call.Otto.Call("jeth.sign", nil, message, account, passwd)
|
|
|
|
if err != nil {
|
|
|
|
throwJSException(err.Error())
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2016-05-06 12:40:23 +03:00
|
|
|
// Sleep will block the console for the specified number of seconds.
|
|
|
|
func (b *bridge) Sleep(call otto.FunctionCall) (response otto.Value) {
|
|
|
|
if call.Argument(0).IsNumber() {
|
|
|
|
sleep, _ := call.Argument(0).ToInteger()
|
|
|
|
time.Sleep(time.Duration(sleep) * time.Second)
|
|
|
|
return otto.TrueValue()
|
|
|
|
}
|
|
|
|
return throwJSException("usage: sleep(<number of seconds>)")
|
|
|
|
}
|
|
|
|
|
|
|
|
// SleepBlocks will block the console for a specified number of new blocks optionally
|
|
|
|
// until the given timeout is reached.
|
|
|
|
func (b *bridge) SleepBlocks(call otto.FunctionCall) (response otto.Value) {
|
|
|
|
var (
|
|
|
|
blocks = int64(0)
|
|
|
|
sleep = int64(9999999999999999) // indefinitely
|
|
|
|
)
|
|
|
|
// Parse the input parameters for the sleep
|
|
|
|
nArgs := len(call.ArgumentList)
|
|
|
|
if nArgs == 0 {
|
|
|
|
throwJSException("usage: sleepBlocks(<n blocks>[, max sleep in seconds])")
|
|
|
|
}
|
|
|
|
if nArgs >= 1 {
|
|
|
|
if call.Argument(0).IsNumber() {
|
|
|
|
blocks, _ = call.Argument(0).ToInteger()
|
|
|
|
} else {
|
|
|
|
throwJSException("expected number as first argument")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if nArgs >= 2 {
|
|
|
|
if call.Argument(1).IsNumber() {
|
|
|
|
sleep, _ = call.Argument(1).ToInteger()
|
|
|
|
} else {
|
|
|
|
throwJSException("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.
|
|
|
|
blockNumber := func() int64 {
|
|
|
|
result, err := call.Otto.Run("eth.blockNumber")
|
|
|
|
if err != nil {
|
|
|
|
throwJSException(err.Error())
|
|
|
|
}
|
|
|
|
block, err := result.ToInteger()
|
|
|
|
if err != nil {
|
|
|
|
throwJSException(err.Error())
|
|
|
|
}
|
|
|
|
return block
|
|
|
|
}
|
|
|
|
// Poll the current block number until either it ot a timeout is reached
|
|
|
|
targetBlockNr := blockNumber() + blocks
|
|
|
|
deadline := time.Now().Add(time.Duration(sleep) * time.Second)
|
|
|
|
|
|
|
|
for time.Now().Before(deadline) {
|
|
|
|
if blockNumber() >= targetBlockNr {
|
|
|
|
return otto.TrueValue()
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
|
|
|
|
2016-07-12 18:47:15 +03:00
|
|
|
type jsonrpcCall struct {
|
|
|
|
Id int64
|
|
|
|
Method string
|
|
|
|
Params []interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send implements the web3 provider "send" method.
|
2016-05-06 12:40:23 +03:00
|
|
|
func (b *bridge) Send(call otto.FunctionCall) (response otto.Value) {
|
2016-07-12 18:47:15 +03:00
|
|
|
// Remarshal the request into a Go value.
|
|
|
|
JSON, _ := call.Otto.Object("JSON")
|
|
|
|
reqVal, err := JSON.Call("stringify", call.Argument(0))
|
2016-05-06 12:40:23 +03:00
|
|
|
if err != nil {
|
|
|
|
throwJSException(err.Error())
|
|
|
|
}
|
|
|
|
var (
|
2017-05-02 14:42:55 +03:00
|
|
|
rawReq = reqVal.String()
|
|
|
|
dec = json.NewDecoder(strings.NewReader(rawReq))
|
2016-07-12 18:47:15 +03:00
|
|
|
reqs []jsonrpcCall
|
|
|
|
batch bool
|
2016-05-06 12:40:23 +03:00
|
|
|
)
|
2017-05-02 14:42:55 +03:00
|
|
|
dec.UseNumber() // avoid float64s
|
2016-07-12 18:47:15 +03:00
|
|
|
if rawReq[0] == '[' {
|
|
|
|
batch = true
|
2017-05-02 14:42:55 +03:00
|
|
|
dec.Decode(&reqs)
|
2016-07-12 18:47:15 +03:00
|
|
|
} else {
|
2016-05-06 12:40:23 +03:00
|
|
|
batch = false
|
2016-07-12 18:47:15 +03:00
|
|
|
reqs = make([]jsonrpcCall, 1)
|
2017-05-02 14:42:55 +03:00
|
|
|
dec.Decode(&reqs[0])
|
2016-05-06 12:40:23 +03:00
|
|
|
}
|
|
|
|
|
2016-07-12 18:47:15 +03:00
|
|
|
// Execute the requests.
|
|
|
|
resps, _ := call.Otto.Object("new Array()")
|
|
|
|
for _, req := range reqs {
|
|
|
|
resp, _ := call.Otto.Object(`({"jsonrpc":"2.0"})`)
|
|
|
|
resp.Set("id", req.Id)
|
|
|
|
var result json.RawMessage
|
|
|
|
err = b.client.Call(&result, req.Method, req.Params...)
|
|
|
|
switch err := err.(type) {
|
|
|
|
case nil:
|
|
|
|
if result == nil {
|
|
|
|
// Special case null because it is decoded as an empty
|
|
|
|
// raw message for some reason.
|
|
|
|
resp.Set("result", otto.NullValue())
|
|
|
|
} else {
|
|
|
|
resultVal, err := JSON.Call("parse", string(result))
|
|
|
|
if err != nil {
|
2017-01-08 02:47:47 +03:00
|
|
|
setError(resp, -32603, err.Error())
|
2016-07-12 18:47:15 +03:00
|
|
|
} else {
|
|
|
|
resp.Set("result", resultVal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case rpc.Error:
|
2017-01-08 02:47:47 +03:00
|
|
|
setError(resp, err.ErrorCode(), err.Error())
|
2016-07-12 18:47:15 +03:00
|
|
|
default:
|
2017-01-08 02:47:47 +03:00
|
|
|
setError(resp, -32603, err.Error())
|
2016-05-06 12:40:23 +03:00
|
|
|
}
|
2016-07-12 18:47:15 +03:00
|
|
|
resps.Call("push", resp)
|
|
|
|
}
|
2016-05-06 12:40:23 +03:00
|
|
|
|
2016-07-12 18:47:15 +03:00
|
|
|
// Return the responses either to the callback (if supplied)
|
|
|
|
// or directly as the return value.
|
|
|
|
if batch {
|
|
|
|
response = resps.Value()
|
|
|
|
} else {
|
|
|
|
response, _ = resps.Get("0")
|
2016-05-06 12:40:23 +03:00
|
|
|
}
|
2016-08-16 00:52:47 +03:00
|
|
|
if fn := call.Argument(1); fn.Class() == "Function" {
|
|
|
|
fn.Call(otto.NullValue(), otto.NullValue(), response)
|
2016-07-12 18:47:15 +03:00
|
|
|
return otto.UndefinedValue()
|
2016-05-06 12:40:23 +03:00
|
|
|
}
|
2016-07-12 18:47:15 +03:00
|
|
|
return response
|
|
|
|
}
|
|
|
|
|
2017-01-08 02:47:47 +03:00
|
|
|
func setError(resp *otto.Object, code int, msg string) {
|
|
|
|
resp.Set("error", map[string]interface{}{"code": code, "message": msg})
|
2016-05-06 12:40:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// throwJSException panics on an otto.Value. The Otto VM will recover from the
|
|
|
|
// Go panic and throw msg as a JavaScript error.
|
|
|
|
func throwJSException(msg interface{}) otto.Value {
|
|
|
|
val, err := otto.ToValue(msg)
|
|
|
|
if err != nil {
|
2017-02-28 14:35:17 +03:00
|
|
|
log.Error("Failed to serialize JavaScript exception", "exception", msg, "err", err)
|
2016-05-06 12:40:23 +03:00
|
|
|
}
|
|
|
|
panic(val)
|
|
|
|
}
|