rpc: migrated the RPC insterface to a new reflection based RPC layer
This commit is contained in:
committed by
Jeffrey Wilcke
parent
f2ab351e8d
commit
19b2640e89
@ -1,74 +0,0 @@
|
||||
// Copyright 2015 The go-ethereum Authors
|
||||
// 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 utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/node"
|
||||
"github.com/ethereum/go-ethereum/p2p"
|
||||
rpc "github.com/ethereum/go-ethereum/rpc/v2"
|
||||
)
|
||||
|
||||
// PublicWeb3API offers helper utils
|
||||
type PublicWeb3API struct {
|
||||
stack *node.Node
|
||||
}
|
||||
|
||||
// NewPublicWeb3API creates a new Web3Service instance
|
||||
func NewPublicWeb3API(stack *node.Node) *PublicWeb3API {
|
||||
return &PublicWeb3API{stack}
|
||||
}
|
||||
|
||||
// ClientVersion returns the node name
|
||||
func (s *PublicWeb3API) ClientVersion() string {
|
||||
return s.stack.Server().Name
|
||||
}
|
||||
|
||||
// Sha3 applies the ethereum sha3 implementation on the input.
|
||||
// It assumes the input is hex encoded.
|
||||
func (s *PublicWeb3API) Sha3(input string) string {
|
||||
return common.ToHex(crypto.Sha3(common.FromHex(input)))
|
||||
}
|
||||
|
||||
// PublicNetAPI offers network related RPC methods
|
||||
type PublicNetAPI struct {
|
||||
net *p2p.Server
|
||||
networkVersion int
|
||||
}
|
||||
|
||||
// NewPublicNetAPI creates a new net api instance.
|
||||
func NewPublicNetAPI(net *p2p.Server, networkVersion int) *PublicNetAPI {
|
||||
return &PublicNetAPI{net, networkVersion}
|
||||
}
|
||||
|
||||
// Listening returns an indication if the node is listening for network connections.
|
||||
func (s *PublicNetAPI) Listening() bool {
|
||||
return true // always listening
|
||||
}
|
||||
|
||||
// Peercount returns the number of connected peers
|
||||
func (s *PublicNetAPI) PeerCount() *rpc.HexNumber {
|
||||
return rpc.NewHexNumber(s.net.PeerCount())
|
||||
}
|
||||
|
||||
// ProtocolVersion returns the current ethereum protocol version.
|
||||
func (s *PublicNetAPI) Version() string {
|
||||
return fmt.Sprintf("%d", s.networkVersion)
|
||||
}
|
176
cmd/utils/client.go
Normal file
176
cmd/utils/client.go
Normal file
@ -0,0 +1,176 @@
|
||||
// Copyright 2015 The go-ethereum Authors
|
||||
// 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 utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"strings"
|
||||
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/ethereum/go-ethereum/eth"
|
||||
"github.com/ethereum/go-ethereum/logger"
|
||||
"github.com/ethereum/go-ethereum/logger/glog"
|
||||
"github.com/ethereum/go-ethereum/node"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
)
|
||||
|
||||
// NewInProcRPCClient will start a new RPC server for the given node and returns a client to interact with it.
|
||||
func NewInProcRPCClient(stack *node.Node) *inProcClient {
|
||||
server := rpc.NewServer()
|
||||
|
||||
offered := stack.APIs()
|
||||
for _, api := range offered {
|
||||
server.RegisterName(api.Namespace, api.Service)
|
||||
}
|
||||
|
||||
web3 := node.NewPublicWeb3API(stack)
|
||||
server.RegisterName("web3", web3)
|
||||
|
||||
var ethereum *eth.Ethereum
|
||||
if err := stack.Service(ðereum); err == nil {
|
||||
net := eth.NewPublicNetAPI(stack.Server(), ethereum.NetVersion())
|
||||
server.RegisterName("net", net)
|
||||
} else {
|
||||
glog.V(logger.Warn).Infof("%v\n", err)
|
||||
}
|
||||
|
||||
buf := &buf{
|
||||
requests: make(chan []byte),
|
||||
responses: make(chan []byte),
|
||||
}
|
||||
client := &inProcClient{
|
||||
server: server,
|
||||
buf: buf,
|
||||
}
|
||||
|
||||
go func() {
|
||||
server.ServeCodec(rpc.NewJSONCodec(client.buf))
|
||||
}()
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
// buf represents the connection between the RPC server and console
|
||||
type buf struct {
|
||||
readBuf []byte // store remaining request bytes after a partial read
|
||||
requests chan []byte // list with raw serialized requests
|
||||
responses chan []byte // list with raw serialized responses
|
||||
}
|
||||
|
||||
// will read the next request in json format
|
||||
func (b *buf) Read(p []byte) (int, error) {
|
||||
// last read didn't read entire request, return remaining bytes
|
||||
if len(b.readBuf) > 0 {
|
||||
n := copy(p, b.readBuf)
|
||||
if n < len(b.readBuf) {
|
||||
b.readBuf = b.readBuf[:n]
|
||||
} else {
|
||||
b.readBuf = b.readBuf[:0]
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// read next request
|
||||
req := <-b.requests
|
||||
n := copy(p, req)
|
||||
if n < len(req) {
|
||||
// buf too small, store remaining chunk for next read
|
||||
b.readBuf = req[n:]
|
||||
}
|
||||
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// Write send the given buffer to the backend
|
||||
func (b *buf) Write(p []byte) (n int, err error) {
|
||||
b.responses <- p
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
// Close cleans up obtained resources.
|
||||
func (b *buf) Close() error {
|
||||
close(b.requests)
|
||||
close(b.responses)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// inProcClient starts a RPC server and uses buf to communicate with it.
|
||||
type inProcClient struct {
|
||||
server *rpc.Server
|
||||
buf *buf
|
||||
}
|
||||
|
||||
// Close will stop the RPC server
|
||||
func (c *inProcClient) Close() {
|
||||
c.server.Stop()
|
||||
}
|
||||
|
||||
// Send a msg to the endpoint
|
||||
func (c *inProcClient) Send(msg interface{}) error {
|
||||
d, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.buf.requests <- d
|
||||
return nil
|
||||
}
|
||||
|
||||
// Recv reads a message and tries to parse it into the given msg
|
||||
func (c *inProcClient) Recv(msg interface{}) error {
|
||||
data := <-c.buf.responses
|
||||
return json.Unmarshal(data, &msg)
|
||||
}
|
||||
|
||||
// Returns the collection of modules the RPC server offers.
|
||||
func (c *inProcClient) SupportedModules() (map[string]string, error) {
|
||||
return rpc.SupportedModules(c)
|
||||
}
|
||||
|
||||
// NewRemoteRPCClient returns a RPC client which connects to a running geth instance.
|
||||
// Depending on the given context this can either be a IPC or a HTTP client.
|
||||
func NewRemoteRPCClient(ctx *cli.Context) (rpc.Client, error) {
|
||||
if ctx.Args().Present() {
|
||||
endpoint := ctx.Args().First()
|
||||
return NewRemoteRPCClientFromString(endpoint)
|
||||
}
|
||||
|
||||
// use IPC by default
|
||||
endpoint := IPCSocketPath(ctx)
|
||||
return rpc.NewIPCClient(endpoint)
|
||||
}
|
||||
|
||||
// NewRemoteRPCClientFromString returns a RPC client which connects to the given
|
||||
// endpoint. It must start with either `ipc:` or `rpc:` (HTTP).
|
||||
func NewRemoteRPCClientFromString(endpoint string) (rpc.Client, error) {
|
||||
if strings.HasPrefix(endpoint, "ipc:") {
|
||||
return rpc.NewIPCClient(endpoint[4:])
|
||||
}
|
||||
if strings.HasPrefix(endpoint, "rpc:") {
|
||||
return rpc.NewHTTPClient(endpoint[4:])
|
||||
}
|
||||
if strings.HasPrefix(endpoint, "http://") {
|
||||
return rpc.NewHTTPClient(endpoint)
|
||||
}
|
||||
if strings.HasPrefix(endpoint, "ws:") {
|
||||
return rpc.NewWSClient(endpoint)
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("invalid endpoint")
|
||||
}
|
@ -23,7 +23,6 @@ import (
|
||||
"log"
|
||||
"math"
|
||||
"math/big"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@ -31,6 +30,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"errors"
|
||||
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/ethereum/ethash"
|
||||
"github.com/ethereum/go-ethereum/accounts"
|
||||
@ -49,14 +50,8 @@ import (
|
||||
"github.com/ethereum/go-ethereum/p2p/discover"
|
||||
"github.com/ethereum/go-ethereum/p2p/nat"
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
"github.com/ethereum/go-ethereum/rpc/api"
|
||||
"github.com/ethereum/go-ethereum/rpc/codec"
|
||||
"github.com/ethereum/go-ethereum/rpc/comms"
|
||||
"github.com/ethereum/go-ethereum/rpc/shared"
|
||||
"github.com/ethereum/go-ethereum/rpc/useragent"
|
||||
rpc "github.com/ethereum/go-ethereum/rpc/v2"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
"github.com/ethereum/go-ethereum/whisper"
|
||||
"github.com/ethereum/go-ethereum/xeth"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -282,10 +277,10 @@ var (
|
||||
Usage: "Domains from which to accept cross origin requests (browser enforced)",
|
||||
Value: "",
|
||||
}
|
||||
RpcApiFlag = cli.StringFlag{
|
||||
RPCApiFlag = cli.StringFlag{
|
||||
Name: "rpcapi",
|
||||
Usage: "API's offered over the HTTP-RPC interface",
|
||||
Value: comms.DefaultHttpRpcApis,
|
||||
Value: rpc.DefaultHttpRpcApis,
|
||||
}
|
||||
IPCDisabledFlag = cli.BoolFlag{
|
||||
Name: "ipcdisable",
|
||||
@ -294,16 +289,36 @@ var (
|
||||
IPCApiFlag = cli.StringFlag{
|
||||
Name: "ipcapi",
|
||||
Usage: "API's offered over the IPC-RPC interface",
|
||||
Value: comms.DefaultIpcApis,
|
||||
Value: rpc.DefaultIpcApis,
|
||||
}
|
||||
IPCPathFlag = DirectoryFlag{
|
||||
Name: "ipcpath",
|
||||
Usage: "Filename for IPC socket/pipe",
|
||||
Value: DirectoryString{common.DefaultIpcPath()},
|
||||
}
|
||||
IPCExperimental = cli.BoolFlag{
|
||||
Name: "ipcexp",
|
||||
Usage: "Enable the new RPC implementation",
|
||||
WSEnabledFlag = cli.BoolFlag{
|
||||
Name: "ws",
|
||||
Usage: "Enable the WS-RPC server",
|
||||
}
|
||||
WSListenAddrFlag = cli.StringFlag{
|
||||
Name: "wsaddr",
|
||||
Usage: "WS-RPC server listening interface",
|
||||
Value: "127.0.0.1",
|
||||
}
|
||||
WSPortFlag = cli.IntFlag{
|
||||
Name: "wsport",
|
||||
Usage: "WS-RPC server listening port",
|
||||
Value: 8546,
|
||||
}
|
||||
WSApiFlag = cli.StringFlag{
|
||||
Name: "wsapi",
|
||||
Usage: "API's offered over the WS-RPC interface",
|
||||
Value: rpc.DefaultHttpRpcApis,
|
||||
}
|
||||
WSAllowedDomainsFlag = cli.StringFlag{
|
||||
Name: "wsdomains",
|
||||
Usage: "Domains from which to accept websockets requests",
|
||||
Value: "",
|
||||
}
|
||||
ExecFlag = cli.StringFlag{
|
||||
Name: "exec",
|
||||
@ -760,7 +775,7 @@ func MakeChain(ctx *cli.Context) (chain *core.BlockChain, chainDb ethdb.Database
|
||||
return chain, chainDb
|
||||
}
|
||||
|
||||
func IpcSocketPath(ctx *cli.Context) (ipcpath string) {
|
||||
func IPCSocketPath(ctx *cli.Context) (ipcpath string) {
|
||||
if runtime.GOOS == "windows" {
|
||||
ipcpath = common.DefaultIpcPath()
|
||||
if ctx.GlobalIsSet(IPCPathFlag.Name) {
|
||||
@ -780,79 +795,83 @@ func IpcSocketPath(ctx *cli.Context) (ipcpath string) {
|
||||
}
|
||||
|
||||
func StartIPC(stack *node.Node, ctx *cli.Context) error {
|
||||
config := comms.IpcConfig{
|
||||
Endpoint: IpcSocketPath(ctx),
|
||||
}
|
||||
|
||||
var ethereum *eth.Ethereum
|
||||
if err := stack.Service(ðereum); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if ctx.GlobalIsSet(IPCExperimental.Name) {
|
||||
listener, err := comms.CreateListener(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
endpoint := IPCSocketPath(ctx)
|
||||
listener, err := rpc.CreateIPCListener(endpoint)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
server := rpc.NewServer()
|
||||
server := rpc.NewServer()
|
||||
|
||||
// register package API's this node provides
|
||||
offered := stack.APIs()
|
||||
for _, api := range offered {
|
||||
server.RegisterName(api.Namespace, api.Service)
|
||||
glog.V(logger.Debug).Infof("Register %T under namespace '%s' for IPC service\n", api.Service, api.Namespace)
|
||||
}
|
||||
// register package API's this node provides
|
||||
offered := stack.APIs()
|
||||
for _, api := range offered {
|
||||
server.RegisterName(api.Namespace, api.Service)
|
||||
glog.V(logger.Debug).Infof("Register %T under namespace '%s' for IPC service\n", api.Service, api.Namespace)
|
||||
}
|
||||
|
||||
web3 := NewPublicWeb3API(stack)
|
||||
server.RegisterName("web3", web3)
|
||||
net := NewPublicNetAPI(stack.Server(), ethereum.NetVersion())
|
||||
server.RegisterName("net", net)
|
||||
|
||||
go func() {
|
||||
glog.V(logger.Info).Infof("Start IPC server on %s\n", config.Endpoint)
|
||||
for {
|
||||
conn, err := listener.Accept()
|
||||
if err != nil {
|
||||
glog.V(logger.Error).Infof("Unable to accept connection - %v\n", err)
|
||||
}
|
||||
|
||||
codec := rpc.NewJSONCodec(conn)
|
||||
go server.ServeCodec(codec)
|
||||
go func() {
|
||||
glog.V(logger.Info).Infof("Start IPC server on %s\n", endpoint)
|
||||
for {
|
||||
conn, err := listener.Accept()
|
||||
if err != nil {
|
||||
glog.V(logger.Error).Infof("Unable to accept connection - %v\n", err)
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
initializer := func(conn net.Conn) (comms.Stopper, shared.EthereumApi, error) {
|
||||
fe := useragent.NewRemoteFrontend(conn, ethereum.AccountManager())
|
||||
xeth := xeth.New(stack, fe)
|
||||
apis, err := api.ParseApiString(ctx.GlobalString(IPCApiFlag.Name), codec.JSON, xeth, stack)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
codec := rpc.NewJSONCodec(conn)
|
||||
go server.ServeCodec(codec)
|
||||
}
|
||||
return xeth, api.Merge(apis...), nil
|
||||
}
|
||||
return comms.StartIpc(config, codec.JSON, initializer)
|
||||
}()
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
// StartRPC starts a HTTP JSON-RPC API server.
|
||||
func StartRPC(stack *node.Node, ctx *cli.Context) error {
|
||||
config := comms.HttpConfig{
|
||||
ListenAddress: ctx.GlobalString(RPCListenAddrFlag.Name),
|
||||
ListenPort: uint(ctx.GlobalInt(RPCPortFlag.Name)),
|
||||
CorsDomain: ctx.GlobalString(RPCCORSDomainFlag.Name),
|
||||
for _, api := range stack.APIs() {
|
||||
if adminApi, ok := api.Service.(*node.PrivateAdminAPI); ok {
|
||||
address := ctx.GlobalString(RPCListenAddrFlag.Name)
|
||||
port := ctx.GlobalInt(RPCPortFlag.Name)
|
||||
cors := ctx.GlobalString(RPCCORSDomainFlag.Name)
|
||||
apiStr := ""
|
||||
if ctx.GlobalIsSet(RPCApiFlag.Name) {
|
||||
apiStr = ctx.GlobalString(RPCApiFlag.Name)
|
||||
}
|
||||
|
||||
_, err := adminApi.StartRPC(address, port, cors, apiStr)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
xeth := xeth.New(stack, nil)
|
||||
codec := codec.JSON
|
||||
glog.V(logger.Error).Infof("Unable to start RPC-HTTP interface, could not find admin API")
|
||||
return errors.New("Unable to start RPC-HTTP interface")
|
||||
}
|
||||
|
||||
apis, err := api.ParseApiString(ctx.GlobalString(RpcApiFlag.Name), codec, xeth, stack)
|
||||
if err != nil {
|
||||
return err
|
||||
// StartWS starts a websocket JSON-RPC API server.
|
||||
func StartWS(stack *node.Node, ctx *cli.Context) error {
|
||||
for _, api := range stack.APIs() {
|
||||
if adminApi, ok := api.Service.(*node.PrivateAdminAPI); ok {
|
||||
address := ctx.GlobalString(WSListenAddrFlag.Name)
|
||||
port := ctx.GlobalInt(WSAllowedDomainsFlag.Name)
|
||||
allowedDomains := ctx.GlobalString(WSAllowedDomainsFlag.Name)
|
||||
apiStr := ""
|
||||
if ctx.GlobalIsSet(WSApiFlag.Name) {
|
||||
apiStr = ctx.GlobalString(WSApiFlag.Name)
|
||||
}
|
||||
|
||||
_, err := adminApi.StartWS(address, port, allowedDomains, apiStr)
|
||||
return err
|
||||
}
|
||||
}
|
||||
return comms.StartHttp(config, codec, api.Merge(apis...))
|
||||
|
||||
glog.V(logger.Error).Infof("Unable to start RPC-WS interface, could not find admin API")
|
||||
return errors.New("Unable to start RPC-WS interface")
|
||||
}
|
||||
|
||||
func StartPProf(ctx *cli.Context) {
|
||||
|
323
cmd/utils/jeth.go
Normal file
323
cmd/utils/jeth.go
Normal file
@ -0,0 +1,323 @@
|
||||
// Copyright 2015 The go-ethereum Authors
|
||||
// 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 utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/jsre"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
|
||||
"github.com/robertkrimen/otto"
|
||||
)
|
||||
|
||||
type Jeth struct {
|
||||
re *jsre.JSRE
|
||||
client rpc.Client
|
||||
}
|
||||
|
||||
// NewJeth create a new backend for the JSRE console
|
||||
func NewJeth(re *jsre.JSRE, client rpc.Client) *Jeth {
|
||||
return &Jeth{re, client}
|
||||
}
|
||||
|
||||
func (self *Jeth) err(call otto.FunctionCall, code int, msg string, id *int64) (response otto.Value) {
|
||||
m := rpc.JSONErrResponse{
|
||||
Version: "2.0",
|
||||
Id: id,
|
||||
Error: rpc.JSONError{
|
||||
Code: code,
|
||||
Message: msg,
|
||||
},
|
||||
}
|
||||
|
||||
errObj, _ := json.Marshal(m.Error)
|
||||
errRes, _ := json.Marshal(m)
|
||||
|
||||
call.Otto.Run("ret_error = " + string(errObj))
|
||||
res, _ := call.Otto.Run("ret_response = " + string(errRes))
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
// UnlockAccount asks the user for the password and than executes the jeth.UnlockAccount callback in the jsre
|
||||
func (self *Jeth) UnlockAccount(call otto.FunctionCall) (response otto.Value) {
|
||||
var cmd, account, passwd string
|
||||
timeout := int64(300)
|
||||
var ok bool
|
||||
|
||||
if len(call.ArgumentList) == 0 {
|
||||
fmt.Println("expected address of account to unlock")
|
||||
return otto.FalseValue()
|
||||
}
|
||||
|
||||
if len(call.ArgumentList) >= 1 {
|
||||
if accountExport, err := call.Argument(0).Export(); err == nil {
|
||||
if account, ok = accountExport.(string); ok {
|
||||
if len(call.ArgumentList) == 1 {
|
||||
fmt.Printf("Unlock account %s\n", account)
|
||||
passwd, err = PromptPassword("Passphrase: ", true)
|
||||
if err != nil {
|
||||
return otto.FalseValue()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(call.ArgumentList) >= 2 {
|
||||
if passwdExport, err := call.Argument(1).Export(); err == nil {
|
||||
passwd, _ = passwdExport.(string)
|
||||
}
|
||||
}
|
||||
|
||||
if len(call.ArgumentList) >= 3 {
|
||||
if timeoutExport, err := call.Argument(2).Export(); err == nil {
|
||||
timeout, _ = timeoutExport.(int64)
|
||||
}
|
||||
}
|
||||
|
||||
cmd = fmt.Sprintf("jeth.unlockAccount('%s', '%s', %d)", account, passwd, timeout)
|
||||
if val, err := call.Otto.Run(cmd); err == nil {
|
||||
return val
|
||||
}
|
||||
|
||||
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) {
|
||||
if len(call.ArgumentList) == 0 {
|
||||
passwd, err := PromptPassword("Passphrase: ", true)
|
||||
if err != nil {
|
||||
return otto.FalseValue()
|
||||
}
|
||||
passwd2, err := PromptPassword("Repeat passphrase: ", true)
|
||||
if err != nil {
|
||||
return otto.FalseValue()
|
||||
}
|
||||
|
||||
if passwd != passwd2 {
|
||||
fmt.Println("Passphrases don't match")
|
||||
return otto.FalseValue()
|
||||
}
|
||||
|
||||
cmd := fmt.Sprintf("jeth.newAccount('%s')", passwd)
|
||||
if val, err := call.Otto.Run(cmd); err == nil {
|
||||
return val
|
||||
}
|
||||
} else {
|
||||
fmt.Println("New account doesn't expect argument(s), you will be prompted for a password")
|
||||
}
|
||||
|
||||
return otto.FalseValue()
|
||||
}
|
||||
|
||||
func (self *Jeth) Send(call otto.FunctionCall) (response otto.Value) {
|
||||
reqif, err := call.Argument(0).Export()
|
||||
if err != nil {
|
||||
return self.err(call, -32700, err.Error(), nil)
|
||||
}
|
||||
|
||||
jsonreq, err := json.Marshal(reqif)
|
||||
var reqs []rpc.JSONRequest
|
||||
batch := true
|
||||
err = json.Unmarshal(jsonreq, &reqs)
|
||||
if err != nil {
|
||||
reqs = make([]rpc.JSONRequest, 1)
|
||||
err = json.Unmarshal(jsonreq, &reqs[0])
|
||||
batch = false
|
||||
}
|
||||
|
||||
call.Otto.Set("response_len", len(reqs))
|
||||
call.Otto.Run("var ret_response = new Array(response_len);")
|
||||
|
||||
for i, req := range reqs {
|
||||
err := self.client.Send(&req)
|
||||
if err != nil {
|
||||
return self.err(call, -32603, err.Error(), req.Id)
|
||||
}
|
||||
|
||||
result := make(map[string]interface{})
|
||||
err = self.client.Recv(&result)
|
||||
if err != nil {
|
||||
return self.err(call, -32603, err.Error(), req.Id)
|
||||
}
|
||||
|
||||
_, isSuccessResponse := result["result"]
|
||||
_, isErrorResponse := result["error"]
|
||||
if !isSuccessResponse && !isErrorResponse {
|
||||
return self.err(call, -32603, fmt.Sprintf("Invalid response"), new(int64))
|
||||
}
|
||||
|
||||
id, _ := result["id"]
|
||||
call.Otto.Set("ret_id", id)
|
||||
|
||||
jsonver, _ := result["jsonrpc"]
|
||||
call.Otto.Set("ret_jsonrpc", jsonver)
|
||||
|
||||
var payload []byte
|
||||
if isSuccessResponse {
|
||||
payload, _ = json.Marshal(result["result"])
|
||||
} else if isErrorResponse {
|
||||
payload, _ = json.Marshal(result["error"])
|
||||
}
|
||||
call.Otto.Set("ret_result", string(payload))
|
||||
call.Otto.Set("response_idx", i)
|
||||
|
||||
response, err = call.Otto.Run(`
|
||||
ret_response[response_idx] = { jsonrpc: ret_jsonrpc, id: ret_id, result: JSON.parse(ret_result) };
|
||||
`)
|
||||
}
|
||||
|
||||
if !batch {
|
||||
call.Otto.Run("ret_response = ret_response[0];")
|
||||
}
|
||||
|
||||
if call.Argument(1).IsObject() {
|
||||
call.Otto.Set("callback", call.Argument(1))
|
||||
call.Otto.Run(`
|
||||
if (Object.prototype.toString.call(callback) == '[object Function]') {
|
||||
callback(null, ret_response);
|
||||
}
|
||||
`)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
// handleRequest will handle user agent requests by interacting with the user and sending
|
||||
// the user response back to the geth service
|
||||
func (self *Jeth) handleRequest(req *shared.Request) bool {
|
||||
var err error
|
||||
var args []interface{}
|
||||
if err = json.Unmarshal(req.Params, &args); err != nil {
|
||||
glog.V(logger.Info).Infof("Unable to parse agent request - %v\n", err)
|
||||
return false
|
||||
}
|
||||
|
||||
switch req.Method {
|
||||
case useragent.AskPasswordMethod:
|
||||
return self.askPassword(req.Id, req.Jsonrpc, args)
|
||||
case useragent.ConfirmTransactionMethod:
|
||||
return self.confirmTransaction(req.Id, req.Jsonrpc, args)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// askPassword will ask the user to supply the password for a given account
|
||||
func (self *Jeth) askPassword(id interface{}, jsonrpc string, args []interface{}) bool {
|
||||
var err error
|
||||
var passwd string
|
||||
if len(args) >= 1 {
|
||||
if account, ok := args[0].(string); ok {
|
||||
fmt.Printf("Unlock account %s\n", account)
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
passwd, err = PromptPassword("Passphrase: ", true)
|
||||
|
||||
if err = self.client.Send(shared.NewRpcResponse(id, jsonrpc, passwd, err)); err != nil {
|
||||
glog.V(logger.Info).Infof("Unable to send user agent ask password response - %v\n", err)
|
||||
}
|
||||
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func (self *Jeth) confirmTransaction(id interface{}, jsonrpc string, args []interface{}) bool {
|
||||
// Accept all tx which are send from this console
|
||||
return self.client.Send(shared.NewRpcResponse(id, jsonrpc, true, nil)) == nil
|
||||
}
|
||||
*/
|
||||
|
||||
// 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)
|
||||
return 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()
|
||||
}
|
Reference in New Issue
Block a user