2016-11-09 01:01:56 +00:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
2016-05-06 09:40:23 +00: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"
|
2020-01-27 10:50:48 +00:00
|
|
|
"reflect"
|
2017-05-02 11:42:55 +00:00
|
|
|
"strings"
|
2016-05-06 09:40:23 +00:00
|
|
|
"time"
|
|
|
|
|
2020-01-27 10:50:48 +00:00
|
|
|
"github.com/dop251/goja"
|
2018-04-18 10:59:32 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/scwallet"
|
2017-08-01 15:45:17 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/usbwallet"
|
2020-01-27 10:50:48 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
"github.com/ethereum/go-ethereum/internal/jsre"
|
2016-05-06 09:40:23 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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 15:47:15 +00:00
|
|
|
client *rpc.Client // RPC client to execute Ethereum requests through
|
2016-05-06 09:40:23 +00: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 15:47:15 +00:00
|
|
|
func newBridge(client *rpc.Client, prompter UserPrompter, printer io.Writer) *bridge {
|
2016-05-06 09:40:23 +00:00
|
|
|
return &bridge{
|
|
|
|
client: client,
|
|
|
|
prompter: prompter,
|
|
|
|
printer: printer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-27 10:50:48 +00:00
|
|
|
func getJeth(vm *goja.Runtime) *goja.Object {
|
|
|
|
jeth := vm.Get("jeth")
|
|
|
|
if jeth == nil {
|
|
|
|
panic(vm.ToValue("jeth object does not exist"))
|
|
|
|
}
|
|
|
|
return jeth.ToObject(vm)
|
|
|
|
}
|
|
|
|
|
2016-05-06 09:40:23 +00:00
|
|
|
// NewAccount is a wrapper around the personal.newAccount RPC method that uses a
|
2017-01-06 17:44:35 +00:00
|
|
|
// non-echoing password prompt to acquire the passphrase and executes the original
|
2016-05-06 09:40:23 +00:00
|
|
|
// RPC method (saved in jeth.newAccount) with it to actually execute the RPC call.
|
2020-01-27 10:50:48 +00:00
|
|
|
func (b *bridge) NewAccount(call jsre.Call) (goja.Value, error) {
|
2016-05-06 09:40:23 +00:00
|
|
|
var (
|
|
|
|
password string
|
|
|
|
confirm string
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
switch {
|
|
|
|
// No password was specified, prompt the user for it
|
2020-01-27 10:50:48 +00:00
|
|
|
case len(call.Arguments) == 0:
|
|
|
|
if password, err = b.prompter.PromptPassword("Passphrase: "); err != nil {
|
|
|
|
return nil, err
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
if confirm, err = b.prompter.PromptPassword("Repeat passphrase: "); err != nil {
|
|
|
|
return nil, err
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
|
|
|
if password != confirm {
|
2020-01-27 10:50:48 +00:00
|
|
|
return nil, fmt.Errorf("passwords don't match!")
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
|
|
|
// A single string password was specified, use that
|
2020-01-27 10:50:48 +00:00
|
|
|
case len(call.Arguments) == 1 && call.Argument(0).ToString() != nil:
|
|
|
|
password = call.Argument(0).ToString().String()
|
2016-05-06 09:40:23 +00:00
|
|
|
default:
|
2020-01-27 10:50:48 +00:00
|
|
|
return nil, fmt.Errorf("expected 0 or 1 string argument")
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
2017-01-06 17:44:35 +00:00
|
|
|
// Password acquired, execute the call and return
|
2020-01-27 10:50:48 +00:00
|
|
|
newAccount, callable := goja.AssertFunction(getJeth(call.VM).Get("newAccount"))
|
|
|
|
if !callable {
|
|
|
|
return nil, fmt.Errorf("jeth.newAccount is not callable")
|
|
|
|
}
|
|
|
|
ret, err := newAccount(goja.Null(), call.VM.ToValue(password))
|
2016-05-06 09:40:23 +00:00
|
|
|
if err != nil {
|
2020-01-27 10:50:48 +00:00
|
|
|
return nil, err
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
return ret, nil
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 15:45:17 +00:00
|
|
|
// OpenWallet is a wrapper around personal.openWallet which can interpret and
|
|
|
|
// react to certain error messages, such as the Trezor PIN matrix request.
|
2020-01-27 10:50:48 +00:00
|
|
|
func (b *bridge) OpenWallet(call jsre.Call) (goja.Value, error) {
|
2018-04-19 13:32:02 +00:00
|
|
|
// Make sure we have a wallet specified to open
|
2020-01-27 10:50:48 +00:00
|
|
|
if call.Argument(0).ToObject(call.VM).ClassName() != "String" {
|
|
|
|
return nil, fmt.Errorf("first argument must be the wallet URL to open")
|
2017-08-01 15:45:17 +00:00
|
|
|
}
|
|
|
|
wallet := call.Argument(0)
|
|
|
|
|
2020-01-27 10:50:48 +00:00
|
|
|
var passwd goja.Value
|
|
|
|
if goja.IsUndefined(call.Argument(1)) || goja.IsNull(call.Argument(1)) {
|
|
|
|
passwd = call.VM.ToValue("")
|
2017-08-01 15:45:17 +00:00
|
|
|
} else {
|
|
|
|
passwd = call.Argument(1)
|
|
|
|
}
|
|
|
|
// Open the wallet and return if successful in itself
|
2020-01-27 10:50:48 +00:00
|
|
|
openWallet, callable := goja.AssertFunction(getJeth(call.VM).Get("openWallet"))
|
|
|
|
if !callable {
|
|
|
|
return nil, fmt.Errorf("jeth.openWallet is not callable")
|
|
|
|
}
|
|
|
|
val, err := openWallet(goja.Null(), wallet, passwd)
|
2017-08-01 15:45:17 +00:00
|
|
|
if err == nil {
|
2020-01-27 10:50:48 +00:00
|
|
|
return val, nil
|
2017-08-01 15:45:17 +00:00
|
|
|
}
|
2018-04-18 10:59:32 +00:00
|
|
|
|
|
|
|
// Wallet open failed, report error unless it's a PIN or PUK entry
|
|
|
|
switch {
|
|
|
|
case strings.HasSuffix(err.Error(), usbwallet.ErrTrezorPINNeeded.Error()):
|
2019-01-24 11:21:38 +00:00
|
|
|
val, err = b.readPinAndReopenWallet(call)
|
|
|
|
if err == nil {
|
2020-01-27 10:50:48 +00:00
|
|
|
return val, nil
|
2019-01-24 11:21:38 +00:00
|
|
|
}
|
2018-04-18 10:59:32 +00:00
|
|
|
val, err = b.readPassphraseAndReopenWallet(call)
|
|
|
|
if err != nil {
|
2020-01-27 10:50:48 +00:00
|
|
|
return nil, err
|
2018-04-18 10:59:32 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 22:03:13 +00:00
|
|
|
case strings.HasSuffix(err.Error(), scwallet.ErrPairingPasswordNeeded.Error()):
|
2018-04-18 10:59:32 +00:00
|
|
|
// PUK input requested, fetch from the user and call open again
|
2020-01-27 10:50:48 +00:00
|
|
|
input, err := b.prompter.PromptPassword("Please enter the pairing password: ")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-04-18 10:59:32 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
passwd = call.VM.ToValue(input)
|
|
|
|
if val, err = openWallet(goja.Null(), wallet, passwd); err != nil {
|
2018-04-18 10:59:32 +00:00
|
|
|
if !strings.HasSuffix(err.Error(), scwallet.ErrPINNeeded.Error()) {
|
2020-01-27 10:50:48 +00:00
|
|
|
return nil, err
|
2018-04-18 10:59:32 +00:00
|
|
|
} else {
|
|
|
|
// PIN input requested, fetch from the user and call open again
|
2020-01-27 10:50:48 +00:00
|
|
|
input, err := b.prompter.PromptPassword("Please enter current PIN: ")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-04-18 10:59:32 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
if val, err = openWallet(goja.Null(), wallet, call.VM.ToValue(input)); err != nil {
|
|
|
|
return nil, err
|
2018-04-18 10:59:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-20 07:37:45 +00:00
|
|
|
case strings.HasSuffix(err.Error(), scwallet.ErrPINUnblockNeeded.Error()):
|
|
|
|
// PIN unblock requested, fetch PUK and new PIN from the user
|
|
|
|
var pukpin string
|
2020-01-27 10:50:48 +00:00
|
|
|
input, err := b.prompter.PromptPassword("Please enter current PUK: ")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-04-20 07:37:45 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
pukpin = input
|
|
|
|
input, err = b.prompter.PromptPassword("Please enter new PIN: ")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-04-20 07:37:45 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
pukpin += input
|
|
|
|
|
|
|
|
if val, err = openWallet(goja.Null(), wallet, call.VM.ToValue(pukpin)); err != nil {
|
|
|
|
return nil, err
|
2018-04-20 07:37:45 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 10:59:32 +00:00
|
|
|
case strings.HasSuffix(err.Error(), scwallet.ErrPINNeeded.Error()):
|
|
|
|
// PIN input requested, fetch from the user and call open again
|
2020-01-27 10:50:48 +00:00
|
|
|
input, err := b.prompter.PromptPassword("Please enter current PIN: ")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-04-18 10:59:32 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
if val, err = openWallet(goja.Null(), wallet, call.VM.ToValue(input)); err != nil {
|
|
|
|
return nil, err
|
2018-04-18 10:59:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Unknown error occurred, drop to the user
|
2020-01-27 10:50:48 +00:00
|
|
|
return nil, err
|
2017-08-01 15:45:17 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
return val, nil
|
2019-01-24 11:21:38 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 10:50:48 +00:00
|
|
|
func (b *bridge) readPassphraseAndReopenWallet(call jsre.Call) (goja.Value, error) {
|
2019-01-24 11:21:38 +00:00
|
|
|
wallet := call.Argument(0)
|
2020-01-27 10:50:48 +00:00
|
|
|
input, err := b.prompter.PromptPassword("Please enter your passphrase: ")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
openWallet, callable := goja.AssertFunction(getJeth(call.VM).Get("openWallet"))
|
|
|
|
if !callable {
|
|
|
|
return nil, fmt.Errorf("jeth.openWallet is not callable")
|
2019-01-24 11:21:38 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
return openWallet(goja.Null(), wallet, call.VM.ToValue(input))
|
2019-01-24 11:21:38 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 10:50:48 +00:00
|
|
|
func (b *bridge) readPinAndReopenWallet(call jsre.Call) (goja.Value, error) {
|
2019-01-24 11:21:38 +00:00
|
|
|
wallet := call.Argument(0)
|
2017-08-01 15:45:17 +00:00
|
|
|
// 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 09:21:40 +00:00
|
|
|
fmt.Fprintf(b.printer, "7 | 8 | 9\n")
|
2017-08-01 15:45:17 +00:00
|
|
|
fmt.Fprintf(b.printer, "--+---+--\n")
|
2017-08-14 09:21:40 +00:00
|
|
|
fmt.Fprintf(b.printer, "4 | 5 | 6\n")
|
2017-08-01 15:45:17 +00:00
|
|
|
fmt.Fprintf(b.printer, "--+---+--\n")
|
2017-08-14 09:21:40 +00:00
|
|
|
fmt.Fprintf(b.printer, "1 | 2 | 3\n\n")
|
2017-08-01 15:45:17 +00:00
|
|
|
|
2020-01-27 10:50:48 +00:00
|
|
|
input, err := b.prompter.PromptPassword("Please enter current PIN: ")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
openWallet, callable := goja.AssertFunction(getJeth(call.VM).Get("openWallet"))
|
|
|
|
if !callable {
|
|
|
|
return nil, fmt.Errorf("jeth.openWallet is not callable")
|
2017-08-01 15:45:17 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
return openWallet(goja.Null(), wallet, call.VM.ToValue(input))
|
2017-08-01 15:45:17 +00:00
|
|
|
}
|
|
|
|
|
2016-05-06 09:40:23 +00:00
|
|
|
// UnlockAccount is a wrapper around the personal.unlockAccount RPC method that
|
2017-01-06 17:44:35 +00:00
|
|
|
// uses a non-echoing password prompt to acquire the passphrase and executes the
|
2016-05-06 09:40:23 +00:00
|
|
|
// original RPC method (saved in jeth.unlockAccount) with it to actually execute
|
|
|
|
// the RPC call.
|
2020-01-27 10:50:48 +00:00
|
|
|
func (b *bridge) UnlockAccount(call jsre.Call) (goja.Value, error) {
|
|
|
|
// Make sure we have an account specified to unlock.
|
|
|
|
if call.Argument(0).ExportType().Kind() != reflect.String {
|
|
|
|
return nil, fmt.Errorf("first argument must be the account to unlock")
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
|
|
|
account := call.Argument(0)
|
|
|
|
|
2020-01-27 10:50:48 +00:00
|
|
|
// If password is not given or is the null value, prompt the user for it.
|
|
|
|
var passwd goja.Value
|
|
|
|
if goja.IsUndefined(call.Argument(1)) || goja.IsNull(call.Argument(1)) {
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprintf(b.printer, "Unlock account %s\n", account)
|
2020-01-27 10:50:48 +00:00
|
|
|
input, err := b.prompter.PromptPassword("Passphrase: ")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
passwd = call.VM.ToValue(input)
|
2016-05-06 09:40:23 +00:00
|
|
|
} else {
|
2020-01-27 10:50:48 +00:00
|
|
|
if call.Argument(1).ExportType().Kind() != reflect.String {
|
|
|
|
return nil, fmt.Errorf("password must be a string")
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
|
|
|
passwd = call.Argument(1)
|
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
|
|
|
|
// Third argument is the duration how long the account should be unlocked.
|
|
|
|
duration := goja.Null()
|
|
|
|
if !goja.IsUndefined(call.Argument(2)) && !goja.IsNull(call.Argument(2)) {
|
|
|
|
if !isNumber(call.Argument(2)) {
|
|
|
|
return nil, fmt.Errorf("unlock duration must be a number")
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
|
|
|
duration = call.Argument(2)
|
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
|
|
|
|
// Send the request to the backend and return.
|
|
|
|
unlockAccount, callable := goja.AssertFunction(getJeth(call.VM).Get("unlockAccount"))
|
|
|
|
if !callable {
|
|
|
|
return nil, fmt.Errorf("jeth.unlockAccount is not callable")
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
return unlockAccount(goja.Null(), account, passwd, duration)
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
|
|
|
|
2016-10-28 19:25:49 +00:00
|
|
|
// Sign is a wrapper around the personal.sign RPC method that uses a non-echoing password
|
2017-01-06 17:44:35 +00:00
|
|
|
// prompt to acquire the passphrase and executes the original RPC method (saved in
|
2016-10-28 19:25:49 +00:00
|
|
|
// jeth.sign) with it to actually execute the RPC call.
|
2020-01-27 10:50:48 +00:00
|
|
|
func (b *bridge) Sign(call jsre.Call) (goja.Value, error) {
|
2016-10-28 19:25:49 +00:00
|
|
|
var (
|
|
|
|
message = call.Argument(0)
|
|
|
|
account = call.Argument(1)
|
|
|
|
passwd = call.Argument(2)
|
|
|
|
)
|
|
|
|
|
2020-01-27 10:50:48 +00:00
|
|
|
if message.ExportType().Kind() != reflect.String {
|
|
|
|
return nil, fmt.Errorf("first argument must be the message to sign")
|
2016-10-28 19:25:49 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
if account.ExportType().Kind() != reflect.String {
|
|
|
|
return nil, fmt.Errorf("second argument must be the account to sign with")
|
2016-10-28 19:25:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if the password is not given or null ask the user and ensure password is a string
|
2020-01-27 10:50:48 +00:00
|
|
|
if goja.IsUndefined(passwd) || goja.IsNull(passwd) {
|
2016-10-28 19:25:49 +00:00
|
|
|
fmt.Fprintf(b.printer, "Give password for account %s\n", account)
|
2020-01-27 10:50:48 +00:00
|
|
|
input, err := b.prompter.PromptPassword("Password: ")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-10-28 19:25:49 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
passwd = call.VM.ToValue(input)
|
|
|
|
} else if passwd.ExportType().Kind() != reflect.String {
|
|
|
|
return nil, fmt.Errorf("third argument must be the password to unlock the account")
|
2016-10-28 19:25:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send the request to the backend and return
|
2020-01-27 10:50:48 +00:00
|
|
|
sign, callable := goja.AssertFunction(getJeth(call.VM).Get("unlockAccount"))
|
|
|
|
if !callable {
|
|
|
|
return nil, fmt.Errorf("jeth.unlockAccount is not callable")
|
2016-10-28 19:25:49 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
return sign(goja.Null(), message, account, passwd)
|
2016-10-28 19:25:49 +00:00
|
|
|
}
|
|
|
|
|
2016-05-06 09:40:23 +00:00
|
|
|
// Sleep will block the console for the specified number of seconds.
|
2020-01-27 10:50:48 +00:00
|
|
|
func (b *bridge) Sleep(call jsre.Call) (goja.Value, error) {
|
|
|
|
if !isNumber(call.Argument(0)) {
|
|
|
|
return nil, fmt.Errorf("usage: sleep(<number of seconds>)")
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
sleep := call.Argument(0).ToFloat()
|
|
|
|
time.Sleep(time.Duration(sleep * float64(time.Second)))
|
|
|
|
return call.VM.ToValue(true), nil
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SleepBlocks will block the console for a specified number of new blocks optionally
|
|
|
|
// until the given timeout is reached.
|
2020-01-27 10:50:48 +00:00
|
|
|
func (b *bridge) SleepBlocks(call jsre.Call) (goja.Value, error) {
|
|
|
|
// Parse the input parameters for the sleep.
|
2016-05-06 09:40:23 +00:00
|
|
|
var (
|
|
|
|
blocks = int64(0)
|
|
|
|
sleep = int64(9999999999999999) // indefinitely
|
|
|
|
)
|
2020-01-27 10:50:48 +00:00
|
|
|
nArgs := len(call.Arguments)
|
2016-05-06 09:40:23 +00:00
|
|
|
if nArgs == 0 {
|
2020-01-27 10:50:48 +00:00
|
|
|
return nil, fmt.Errorf("usage: sleepBlocks(<n blocks>[, max sleep in seconds])")
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
|
|
|
if nArgs >= 1 {
|
2020-01-27 10:50:48 +00:00
|
|
|
if !isNumber(call.Argument(0)) {
|
|
|
|
return nil, fmt.Errorf("expected number as first argument")
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
blocks = call.Argument(0).ToInteger()
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
|
|
|
if nArgs >= 2 {
|
2020-01-27 10:50:48 +00:00
|
|
|
if isNumber(call.Argument(1)) {
|
|
|
|
return nil, fmt.Errorf("expected number as second argument")
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
sleep = call.Argument(1).ToInteger()
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
|
|
|
|
// Poll the current block number until either it or a timeout is reached.
|
|
|
|
var (
|
|
|
|
deadline = time.Now().Add(time.Duration(sleep) * time.Second)
|
|
|
|
lastNumber = ^hexutil.Uint64(0)
|
|
|
|
)
|
|
|
|
for time.Now().Before(deadline) {
|
|
|
|
var number hexutil.Uint64
|
|
|
|
err := b.client.Call(&number, "eth_blockNumber")
|
2016-05-06 09:40:23 +00:00
|
|
|
if err != nil {
|
2020-01-27 10:50:48 +00:00
|
|
|
return nil, err
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
if number != lastNumber {
|
|
|
|
lastNumber = number
|
|
|
|
blocks--
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
if blocks <= 0 {
|
|
|
|
break
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
return call.VM.ToValue(true), nil
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 15:47:15 +00:00
|
|
|
type jsonrpcCall struct {
|
2018-05-31 11:59:08 +00:00
|
|
|
ID int64
|
2016-07-12 15:47:15 +00:00
|
|
|
Method string
|
|
|
|
Params []interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send implements the web3 provider "send" method.
|
2020-01-27 10:50:48 +00:00
|
|
|
func (b *bridge) Send(call jsre.Call) (goja.Value, error) {
|
2016-07-12 15:47:15 +00:00
|
|
|
// Remarshal the request into a Go value.
|
2020-01-27 10:50:48 +00:00
|
|
|
reqVal, err := call.Argument(0).ToObject(call.VM).MarshalJSON()
|
2016-05-06 09:40:23 +00:00
|
|
|
if err != nil {
|
2020-01-27 10:50:48 +00:00
|
|
|
return nil, err
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
|
2016-05-06 09:40:23 +00:00
|
|
|
var (
|
2020-01-27 10:50:48 +00:00
|
|
|
rawReq = string(reqVal)
|
2017-05-02 11:42:55 +00:00
|
|
|
dec = json.NewDecoder(strings.NewReader(rawReq))
|
2016-07-12 15:47:15 +00:00
|
|
|
reqs []jsonrpcCall
|
|
|
|
batch bool
|
2016-05-06 09:40:23 +00:00
|
|
|
)
|
2017-05-02 11:42:55 +00:00
|
|
|
dec.UseNumber() // avoid float64s
|
2016-07-12 15:47:15 +00:00
|
|
|
if rawReq[0] == '[' {
|
|
|
|
batch = true
|
2017-05-02 11:42:55 +00:00
|
|
|
dec.Decode(&reqs)
|
2016-07-12 15:47:15 +00:00
|
|
|
} else {
|
2016-05-06 09:40:23 +00:00
|
|
|
batch = false
|
2016-07-12 15:47:15 +00:00
|
|
|
reqs = make([]jsonrpcCall, 1)
|
2017-05-02 11:42:55 +00:00
|
|
|
dec.Decode(&reqs[0])
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 15:47:15 +00:00
|
|
|
// Execute the requests.
|
2020-01-27 10:50:48 +00:00
|
|
|
var resps []*goja.Object
|
2016-07-12 15:47:15 +00:00
|
|
|
for _, req := range reqs {
|
2020-01-27 10:50:48 +00:00
|
|
|
resp := call.VM.NewObject()
|
|
|
|
resp.Set("jsonrpc", "2.0")
|
2018-05-31 11:59:08 +00:00
|
|
|
resp.Set("id", req.ID)
|
2020-01-27 10:50:48 +00:00
|
|
|
|
2016-07-12 15:47:15 +00:00
|
|
|
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.
|
2020-01-27 10:50:48 +00:00
|
|
|
resp.Set("result", goja.Null())
|
2016-07-12 15:47:15 +00:00
|
|
|
} else {
|
2020-01-27 10:50:48 +00:00
|
|
|
JSON := call.VM.Get("JSON").ToObject(call.VM)
|
|
|
|
parse, callable := goja.AssertFunction(JSON.Get("parse"))
|
|
|
|
if !callable {
|
|
|
|
return nil, fmt.Errorf("JSON.parse is not a function")
|
|
|
|
}
|
|
|
|
resultVal, err := parse(goja.Null(), call.VM.ToValue(string(result)))
|
2016-07-12 15:47:15 +00:00
|
|
|
if err != nil {
|
2017-01-07 23:47:47 +00:00
|
|
|
setError(resp, -32603, err.Error())
|
2016-07-12 15:47:15 +00:00
|
|
|
} else {
|
|
|
|
resp.Set("result", resultVal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case rpc.Error:
|
2017-01-07 23:47:47 +00:00
|
|
|
setError(resp, err.ErrorCode(), err.Error())
|
2016-07-12 15:47:15 +00:00
|
|
|
default:
|
2017-01-07 23:47:47 +00:00
|
|
|
setError(resp, -32603, err.Error())
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
resps = append(resps, resp)
|
2016-07-12 15:47:15 +00:00
|
|
|
}
|
2016-05-06 09:40:23 +00:00
|
|
|
|
2016-07-12 15:47:15 +00:00
|
|
|
// Return the responses either to the callback (if supplied)
|
|
|
|
// or directly as the return value.
|
2020-01-27 10:50:48 +00:00
|
|
|
var result goja.Value
|
2016-07-12 15:47:15 +00:00
|
|
|
if batch {
|
2020-01-27 10:50:48 +00:00
|
|
|
result = call.VM.ToValue(resps)
|
2016-07-12 15:47:15 +00:00
|
|
|
} else {
|
2020-01-27 10:50:48 +00:00
|
|
|
result = resps[0]
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
if fn, isFunc := goja.AssertFunction(call.Argument(1)); isFunc {
|
|
|
|
fn(goja.Null(), goja.Null(), result)
|
|
|
|
return goja.Undefined(), nil
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
return result, nil
|
2016-07-12 15:47:15 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 10:50:48 +00:00
|
|
|
func setError(resp *goja.Object, code int, msg string) {
|
2017-01-07 23:47:47 +00:00
|
|
|
resp.Set("error", map[string]interface{}{"code": code, "message": msg})
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 10:50:48 +00:00
|
|
|
// isNumber returns true if input value is a JS number.
|
|
|
|
func isNumber(v goja.Value) bool {
|
|
|
|
k := v.ExportType().Kind()
|
|
|
|
return k >= reflect.Int && k <= reflect.Float64
|
|
|
|
}
|
|
|
|
|
|
|
|
func getObject(vm *goja.Runtime, name string) *goja.Object {
|
|
|
|
v := vm.Get(name)
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
2020-01-27 10:50:48 +00:00
|
|
|
return v.ToObject(vm)
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|