2021-06-23 06:38:05 +00:00
|
|
|
package personal
|
2021-05-12 16:50:26 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2021-10-01 14:49:22 +00:00
|
|
|
"github.com/tharsis/ethermint/rpc/ethereum/backend"
|
2021-08-10 10:04:16 +00:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
|
2021-06-22 10:49:18 +00:00
|
|
|
"github.com/tharsis/ethermint/crypto/hd"
|
|
|
|
ethermint "github.com/tharsis/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"
|
|
|
|
|
2021-06-22 10:49:18 +00:00
|
|
|
"github.com/tharsis/ethermint/crypto/ethsecp256k1"
|
2021-10-05 15:38:20 +00:00
|
|
|
evmtypes "github.com/tharsis/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
|
|
|
|
backend backend.Backend
|
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.
|
2021-08-10 10:04:16 +00:00
|
|
|
func NewAPI(logger log.Logger, clientCtx client.Context, backend backend.Backend) *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
|
|
|
|
}
|