2021-06-23 06:38:05 +00:00
|
|
|
package personal
|
2021-05-12 16:50:26 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2022-06-19 09:43:41 +00:00
|
|
|
"github.com/evmos/ethermint/rpc/backend"
|
2021-08-10 10:04:16 +00:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
|
2022-06-19 09:43:41 +00:00
|
|
|
"github.com/evmos/ethermint/crypto/hd"
|
|
|
|
ethermint "github.com/evmos/ethermint/types"
|
2021-05-12 16:50:26 +00:00
|
|
|
|
2021-07-12 18:39:35 +00:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2021-05-12 16:50:26 +00:00
|
|
|
|
|
|
|
sdkcrypto "github.com/cosmos/cosmos-sdk/crypto"
|
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/accounts"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
|
2022-06-19 09:43:41 +00:00
|
|
|
"github.com/evmos/ethermint/crypto/ethsecp256k1"
|
|
|
|
evmtypes "github.com/evmos/ethermint/x/evm/types"
|
2021-05-12 16:50:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PrivateAccountAPI is the personal_ prefixed set of APIs in the Web3 JSON-RPC spec.
|
|
|
|
type PrivateAccountAPI struct {
|
2021-08-10 10:04:16 +00:00
|
|
|
clientCtx client.Context
|
2022-05-02 06:26:24 +00:00
|
|
|
backend backend.EVMBackend
|
2021-07-28 09:47:29 +00:00
|
|
|
logger log.Logger
|
|
|
|
hdPathIter ethermint.HDPathIterator
|
2021-05-12 16:50:26 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 06:38:05 +00:00
|
|
|
// NewAPI creates an instance of the public Personal Eth API.
|
2022-05-02 06:26:24 +00:00
|
|
|
func NewAPI(
|
|
|
|
logger log.Logger,
|
|
|
|
clientCtx client.Context,
|
|
|
|
backend backend.EVMBackend,
|
|
|
|
) *PrivateAccountAPI {
|
2021-07-28 09:47:29 +00:00
|
|
|
cfg := sdk.GetConfig()
|
2021-08-26 15:45:45 +00:00
|
|
|
basePath := cfg.GetFullBIP44Path()
|
2021-07-28 09:47:29 +00:00
|
|
|
|
|
|
|
iterator, err := ethermint.NewHDPathIterator(basePath, true)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-05-12 16:50:26 +00:00
|
|
|
return &PrivateAccountAPI{
|
2021-08-10 10:04:16 +00:00
|
|
|
clientCtx: clientCtx,
|
2021-07-28 09:47:29 +00:00
|
|
|
logger: logger.With("api", "personal"),
|
|
|
|
hdPathIter: iterator,
|
2021-08-10 10:04:16 +00:00
|
|
|
backend: backend,
|
2021-05-12 16:50:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImportRawKey armors and encrypts a given raw hex encoded ECDSA key and stores it into the key directory.
|
|
|
|
// The name of the key will have the format "personal_<length-keys>", where <length-keys> is the total number of
|
|
|
|
// keys stored on the keyring.
|
|
|
|
//
|
|
|
|
// NOTE: The key will be both armored and encrypted using the same passphrase.
|
|
|
|
func (api *PrivateAccountAPI) ImportRawKey(privkey, password string) (common.Address, error) {
|
|
|
|
api.logger.Debug("personal_importRawKey")
|
|
|
|
priv, err := crypto.HexToECDSA(privkey)
|
|
|
|
if err != nil {
|
|
|
|
return common.Address{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
privKey := ðsecp256k1.PrivKey{Key: crypto.FromECDSA(priv)}
|
|
|
|
|
|
|
|
addr := sdk.AccAddress(privKey.PubKey().Address().Bytes())
|
|
|
|
ethereumAddr := common.BytesToAddress(addr)
|
|
|
|
|
|
|
|
// return if the key has already been imported
|
2021-08-10 10:04:16 +00:00
|
|
|
if _, err := api.clientCtx.Keyring.KeyByAddress(addr); err == nil {
|
2021-05-12 16:50:26 +00:00
|
|
|
return ethereumAddr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore error as we only care about the length of the list
|
2021-08-10 10:04:16 +00:00
|
|
|
list, _ := api.clientCtx.Keyring.List()
|
2021-05-12 16:50:26 +00:00
|
|
|
privKeyName := fmt.Sprintf("personal_%d", len(list))
|
|
|
|
|
|
|
|
armor := sdkcrypto.EncryptArmorPrivKey(privKey, password, ethsecp256k1.KeyType)
|
|
|
|
|
2021-08-10 10:04:16 +00:00
|
|
|
if err := api.clientCtx.Keyring.ImportPrivKey(privKeyName, armor, password); err != nil {
|
2021-05-12 16:50:26 +00:00
|
|
|
return common.Address{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
api.logger.Info("key successfully imported", "name", privKeyName, "address", ethereumAddr.String())
|
|
|
|
|
|
|
|
return ethereumAddr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListAccounts will return a list of addresses for accounts this node manages.
|
|
|
|
func (api *PrivateAccountAPI) ListAccounts() ([]common.Address, error) {
|
|
|
|
api.logger.Debug("personal_listAccounts")
|
|
|
|
addrs := []common.Address{}
|
|
|
|
|
2021-08-10 10:04:16 +00:00
|
|
|
list, err := api.clientCtx.Keyring.List()
|
2021-05-12 16:50:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, info := range list {
|
|
|
|
addrs = append(addrs, common.BytesToAddress(info.GetPubKey().Address()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return addrs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LockAccount will lock the account associated with the given address when it's unlocked.
|
|
|
|
// It removes the key corresponding to the given address from the API's local keys.
|
2021-09-05 11:03:06 +00:00
|
|
|
func (api *PrivateAccountAPI) LockAccount(address common.Address) bool {
|
2021-07-12 18:39:35 +00:00
|
|
|
api.logger.Debug("personal_lockAccount", "address", address.String())
|
2021-05-12 16:50:26 +00:00
|
|
|
api.logger.Info("personal_lockAccount not supported")
|
2021-06-15 07:18:58 +00:00
|
|
|
// TODO: Not supported. See underlying issue https://github.com/99designs/keyring/issues/85
|
2021-05-12 16:50:26 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAccount will create a new account and returns the address for the new account.
|
|
|
|
func (api *PrivateAccountAPI) NewAccount(password string) (common.Address, error) {
|
|
|
|
api.logger.Debug("personal_newAccount")
|
|
|
|
|
|
|
|
name := "key_" + time.Now().UTC().Format(time.RFC3339)
|
|
|
|
|
|
|
|
// create the mnemonic and save the account
|
2021-07-28 09:47:29 +00:00
|
|
|
hdPath := api.hdPathIter()
|
|
|
|
|
2021-08-10 10:04:16 +00:00
|
|
|
info, _, err := api.clientCtx.Keyring.NewMnemonic(name, keyring.English, hdPath.String(), password, hd.EthSecp256k1)
|
2021-05-12 16:50:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return common.Address{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := common.BytesToAddress(info.GetPubKey().Address().Bytes())
|
2021-07-12 18:39:35 +00:00
|
|
|
api.logger.Info("Your new key was generated", "address", addr.String())
|
2021-07-28 09:47:29 +00:00
|
|
|
api.logger.Info("Please backup your key file!", "path", os.Getenv("HOME")+"/.ethermint/"+name) // TODO: pass the correct binary
|
2021-07-12 18:39:35 +00:00
|
|
|
api.logger.Info("Please remember your password!")
|
2021-05-12 16:50:26 +00:00
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnlockAccount will unlock the account associated with the given address with
|
|
|
|
// the given password for duration seconds. If duration is nil it will use a
|
|
|
|
// default of 300 seconds. It returns an indication if the account was unlocked.
|
2021-09-05 11:03:06 +00:00
|
|
|
func (api *PrivateAccountAPI) UnlockAccount(_ context.Context, addr common.Address, _ string, _ *uint64) (bool, error) {
|
2021-07-12 18:39:35 +00:00
|
|
|
api.logger.Debug("personal_unlockAccount", "address", addr.String())
|
2021-06-15 07:18:58 +00:00
|
|
|
// TODO: Not supported. See underlying issue https://github.com/99designs/keyring/issues/85
|
|
|
|
return false, nil
|
2021-05-12 16:50:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendTransaction will create a transaction from the given arguments and
|
|
|
|
// tries to sign it with the key associated with args.To. If the given password isn't
|
|
|
|
// able to decrypt the key it fails.
|
2021-10-06 11:22:32 +00:00
|
|
|
func (api *PrivateAccountAPI) SendTransaction(_ context.Context, args evmtypes.TransactionArgs, _ string) (common.Hash, error) {
|
2021-08-10 10:04:16 +00:00
|
|
|
api.logger.Debug("personal_sendTransaction", "address", args.To.String())
|
2021-09-27 22:06:27 +00:00
|
|
|
|
2021-10-05 15:38:20 +00:00
|
|
|
if args.From == nil {
|
|
|
|
return common.Hash{}, fmt.Errorf("from address cannot be nil in send transaction")
|
|
|
|
}
|
|
|
|
|
2021-09-27 22:06:27 +00:00
|
|
|
addr := sdk.AccAddress(args.From.Bytes())
|
|
|
|
|
|
|
|
// check if the key is on the keyring
|
|
|
|
_, err := api.clientCtx.Keyring.KeyByAddress(addr)
|
|
|
|
if err != nil {
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:04:16 +00:00
|
|
|
return api.backend.SendTransaction(args)
|
2021-05-12 16:50:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sign calculates an Ethereum ECDSA signature for:
|
|
|
|
// keccak256("\x19Ethereum Signed Message:\n" + len(message) + message))
|
|
|
|
//
|
|
|
|
// Note, the produced signature conforms to the secp256k1 curve R, S and V values,
|
|
|
|
// where the V value will be 27 or 28 for legacy reasons.
|
|
|
|
//
|
|
|
|
// The key used to calculate the signature is decrypted with the given password.
|
|
|
|
//
|
|
|
|
// https://github.com/ethereum/go-ethereum/wiki/Management-APIs#personal_sign
|
2021-10-06 11:22:32 +00:00
|
|
|
func (api *PrivateAccountAPI) Sign(_ context.Context, data hexutil.Bytes, addr common.Address, _ string) (hexutil.Bytes, error) {
|
2021-05-12 16:50:26 +00:00
|
|
|
api.logger.Debug("personal_sign", "data", data, "address", addr.String())
|
|
|
|
|
|
|
|
cosmosAddr := sdk.AccAddress(addr.Bytes())
|
|
|
|
|
2021-08-10 10:04:16 +00:00
|
|
|
sig, _, err := api.clientCtx.Keyring.SignByAddress(cosmosAddr, accounts.TextHash(data))
|
2021-05-12 16:50:26 +00:00
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
api.logger.Error("failed to sign with key", "data", data, "address", addr.String(), "error", err.Error())
|
2021-05-12 16:50:26 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sig[crypto.RecoveryIDOffset] += 27 // transform V from 0/1 to 27/28
|
|
|
|
return sig, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EcRecover returns the address for the account that was used to create the signature.
|
|
|
|
// Note, this function is compatible with eth_sign and personal_sign. As such it recovers
|
|
|
|
// the address of:
|
|
|
|
// hash = keccak256("\x19Ethereum Signed Message:\n"${message length}${message})
|
|
|
|
// addr = ecrecover(hash, signature)
|
|
|
|
//
|
|
|
|
// Note, the signature must conform to the secp256k1 curve R, S and V values, where
|
|
|
|
// the V value must be 27 or 28 for legacy reasons.
|
|
|
|
//
|
|
|
|
// https://github.com/ethereum/go-ethereum/wiki/Management-APIs#personal_ecRecove
|
|
|
|
func (api *PrivateAccountAPI) EcRecover(_ context.Context, data, sig hexutil.Bytes) (common.Address, error) {
|
|
|
|
api.logger.Debug("personal_ecRecover", "data", data, "sig", sig)
|
|
|
|
|
|
|
|
if len(sig) != crypto.SignatureLength {
|
|
|
|
return common.Address{}, fmt.Errorf("signature must be %d bytes long", crypto.SignatureLength)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sig[crypto.RecoveryIDOffset] != 27 && sig[crypto.RecoveryIDOffset] != 28 {
|
|
|
|
return common.Address{}, fmt.Errorf("invalid Ethereum signature (V is not 27 or 28)")
|
|
|
|
}
|
|
|
|
|
|
|
|
sig[crypto.RecoveryIDOffset] -= 27 // Transform yellow paper V from 27/28 to 0/1
|
|
|
|
|
|
|
|
pubkey, err := crypto.SigToPub(accounts.TextHash(data), sig)
|
|
|
|
if err != nil {
|
|
|
|
return common.Address{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return crypto.PubkeyToAddress(*pubkey), nil
|
|
|
|
}
|
2021-11-10 09:09:53 +00:00
|
|
|
|
|
|
|
// Unpair deletes a pairing between wallet and ethermint.
|
|
|
|
func (api *PrivateAccountAPI) Unpair(_ context.Context, url, pin string) error {
|
|
|
|
api.logger.Debug("personal_unpair", "url", url, "pin", pin)
|
|
|
|
api.logger.Info("personal_unpair for smartcard wallet not supported")
|
|
|
|
// TODO: Smartcard wallet not supported yet, refer to: https://github.com/ethereum/go-ethereum/blob/master/accounts/scwallet/README.md
|
|
|
|
return fmt.Errorf("smartcard wallet not supported yet")
|
|
|
|
}
|
2021-11-11 15:12:14 +00:00
|
|
|
|
|
|
|
// InitializeWallet initializes a new wallet at the provided URL, by generating and returning a new private key.
|
|
|
|
func (api *PrivateAccountAPI) InitializeWallet(_ context.Context, url string) (string, error) {
|
|
|
|
api.logger.Debug("personal_initializeWallet", "url", url)
|
|
|
|
api.logger.Info("personal_initializeWallet for smartcard wallet not supported")
|
|
|
|
// TODO: Smartcard wallet not supported yet, refer to: https://github.com/ethereum/go-ethereum/blob/master/accounts/scwallet/README.md
|
|
|
|
return "", fmt.Errorf("smartcard wallet not supported yet")
|
|
|
|
}
|
2021-11-15 11:20:59 +00:00
|
|
|
|
|
|
|
// RawWallet is a JSON representation of an accounts.Wallet interface, with its
|
|
|
|
// data contents extracted into plain fields.
|
|
|
|
type RawWallet struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
Failure string `json:"failure,omitempty"`
|
|
|
|
Accounts []accounts.Account `json:"accounts,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListWallets will return a list of wallets this node manages.
|
|
|
|
func (api *PrivateAccountAPI) ListWallets() []RawWallet {
|
|
|
|
api.logger.Debug("personal_ListWallets")
|
|
|
|
api.logger.Info("currently wallet level that manages accounts is not supported")
|
|
|
|
return ([]RawWallet)(nil)
|
|
|
|
}
|