2019-07-15 14:13:59 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2019-12-13 19:50:19 +00:00
|
|
|
"bufio"
|
2019-09-18 18:45:21 +00:00
|
|
|
"fmt"
|
2019-12-13 19:50:19 +00:00
|
|
|
"os"
|
2019-09-18 18:45:21 +00:00
|
|
|
|
2019-12-20 09:47:02 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2019-09-18 20:14:39 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2019-12-13 19:50:19 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/input"
|
2019-07-15 14:13:59 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/lcd"
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2020-06-08 16:19:26 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto"
|
2020-04-22 19:26:01 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-12-20 09:47:02 +00:00
|
|
|
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
|
2020-04-22 19:26:01 +00:00
|
|
|
|
2019-12-20 09:47:02 +00:00
|
|
|
"github.com/cosmos/ethermint/app"
|
2019-09-18 18:45:21 +00:00
|
|
|
emintcrypto "github.com/cosmos/ethermint/crypto"
|
2019-07-15 14:13:59 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2019-09-18 18:45:21 +00:00
|
|
|
|
2019-07-15 14:13:59 +00:00
|
|
|
"github.com/spf13/cobra"
|
2019-09-18 18:45:21 +00:00
|
|
|
"github.com/spf13/viper"
|
2019-07-15 14:13:59 +00:00
|
|
|
)
|
|
|
|
|
2019-09-18 18:45:21 +00:00
|
|
|
const (
|
|
|
|
flagUnlockKey = "unlock-key"
|
|
|
|
)
|
2019-07-15 14:13:59 +00:00
|
|
|
|
|
|
|
// Config contains configuration fields that determine the behavior of the RPC HTTP server.
|
|
|
|
// TODO: These may become irrelevant if HTTP config is handled by the SDK
|
|
|
|
type Config struct {
|
|
|
|
// EnableRPC defines whether or not to enable the RPC server
|
|
|
|
EnableRPC bool
|
|
|
|
// RPCAddr defines the IP address to listen on
|
|
|
|
RPCAddr string
|
|
|
|
// RPCPort defines the port to listen on
|
|
|
|
RPCPort int
|
|
|
|
// RPCCORSDomains defines list of domains to enable CORS headers for (used by browsers)
|
|
|
|
RPCCORSDomains []string
|
|
|
|
// RPCVhosts defines list of domains to listen on (useful if Tendermint is addressable via DNS)
|
|
|
|
RPCVHosts []string
|
|
|
|
}
|
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
// EmintServeCmd creates a CLI command to start Cosmos REST server with web3 RPC API and
|
2019-12-20 09:47:02 +00:00
|
|
|
// Cosmos rest-server endpoints
|
|
|
|
func EmintServeCmd(cdc *codec.Codec) *cobra.Command {
|
2019-09-18 18:45:21 +00:00
|
|
|
cmd := lcd.ServeCommand(cdc, registerRoutes)
|
|
|
|
cmd.Flags().String(flagUnlockKey, "", "Select a key to unlock on the RPC server")
|
2019-09-18 20:14:39 +00:00
|
|
|
cmd.Flags().StringP(flags.FlagBroadcastMode, "b", flags.BroadcastSync, "Transaction broadcasting mode (sync|async|block)")
|
2019-09-18 18:45:21 +00:00
|
|
|
return cmd
|
2019-07-15 14:13:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// registerRoutes creates a new server and registers the `/rpc` endpoint.
|
|
|
|
// Rpc calls are enabled based on their associated module (eg. "eth").
|
|
|
|
func registerRoutes(rs *lcd.RestServer) {
|
|
|
|
s := rpc.NewServer()
|
2019-09-18 18:45:21 +00:00
|
|
|
accountName := viper.GetString(flagUnlockKey)
|
|
|
|
|
|
|
|
var emintKey emintcrypto.PrivKeySecp256k1
|
|
|
|
if len(accountName) > 0 {
|
2019-12-13 19:50:19 +00:00
|
|
|
var err error
|
2020-04-22 19:26:01 +00:00
|
|
|
inBuf := bufio.NewReader(os.Stdin)
|
|
|
|
|
2019-12-13 19:50:19 +00:00
|
|
|
keyringBackend := viper.GetString(flags.FlagKeyringBackend)
|
|
|
|
passphrase := ""
|
|
|
|
switch keyringBackend {
|
2020-04-22 19:26:01 +00:00
|
|
|
case keyring.BackendOS:
|
2019-12-13 19:50:19 +00:00
|
|
|
break
|
2020-04-22 19:26:01 +00:00
|
|
|
case keyring.BackendFile:
|
|
|
|
passphrase, err = input.GetPassword(
|
|
|
|
"Enter password to unlock key for RPC API: ",
|
|
|
|
inBuf)
|
2019-12-13 19:50:19 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-09-18 18:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
emintKey, err = unlockKeyFromNameAndPassphrase(accountName, passphrase)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
apis := GetRPCAPIs(rs.CliCtx, emintKey)
|
2019-07-15 14:13:59 +00:00
|
|
|
|
|
|
|
// TODO: Allow cli to configure modules https://github.com/ChainSafe/ethermint/issues/74
|
|
|
|
whitelist := make(map[string]bool)
|
|
|
|
|
|
|
|
// Register all the APIs exposed by the services
|
|
|
|
for _, api := range apis {
|
|
|
|
if whitelist[api.Namespace] || (len(whitelist) == 0 && api.Public) {
|
|
|
|
if err := s.RegisterName(api.Namespace, api.Service); err != nil {
|
2019-09-18 18:45:21 +00:00
|
|
|
panic(err)
|
2019-07-15 14:13:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 09:47:02 +00:00
|
|
|
// Web3 RPC API route
|
2019-10-31 15:09:40 +00:00
|
|
|
rs.Mux.HandleFunc("/", s.ServeHTTP).Methods("POST", "OPTIONS")
|
2019-12-20 09:47:02 +00:00
|
|
|
|
|
|
|
// Register all other Cosmos routes
|
|
|
|
client.RegisterRoutes(rs.CliCtx, rs.Mux)
|
|
|
|
authrest.RegisterTxRoutes(rs.CliCtx, rs.Mux)
|
|
|
|
app.ModuleBasics.RegisterRESTRoutes(rs.CliCtx, rs.Mux)
|
2019-07-15 14:13:59 +00:00
|
|
|
}
|
2019-09-18 18:45:21 +00:00
|
|
|
|
|
|
|
func unlockKeyFromNameAndPassphrase(accountName, passphrase string) (emintKey emintcrypto.PrivKeySecp256k1, err error) {
|
2020-06-08 16:19:26 +00:00
|
|
|
keystore, err := keyring.New(
|
2020-04-22 19:26:01 +00:00
|
|
|
sdk.KeyringServiceName(),
|
|
|
|
viper.GetString(flags.FlagKeyringBackend),
|
|
|
|
viper.GetString(flags.FlagHome),
|
|
|
|
os.Stdin,
|
|
|
|
)
|
2019-09-18 18:45:21 +00:00
|
|
|
if err != nil {
|
2020-06-08 16:19:26 +00:00
|
|
|
return nil, err
|
2019-09-18 18:45:21 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 16:19:26 +00:00
|
|
|
// With keyring key store, password is not required as it is pulled from the OS prompt
|
|
|
|
// Exports private key from keyring using password
|
|
|
|
armored, err := keystore.ExportPrivKeyArmor(accountName, passphrase)
|
2019-09-18 18:45:21 +00:00
|
|
|
if err != nil {
|
2020-06-08 16:19:26 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
privKey, _, err := crypto.UnarmorDecryptPrivKey(armored, passphrase)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-09-18 18:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
emintKey, ok = privKey.(emintcrypto.PrivKeySecp256k1)
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("invalid private key type: %T", privKey))
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|