2020-10-22 20:39:51 +00:00
|
|
|
package personal
|
2019-09-18 18:45:21 +00:00
|
|
|
|
|
|
|
import (
|
2020-08-13 17:14:48 +00:00
|
|
|
"bytes"
|
2019-09-18 18:45:21 +00:00
|
|
|
"context"
|
2020-08-13 17:14:48 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
2019-09-18 18:45:21 +00:00
|
|
|
|
2020-10-22 20:39:51 +00:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2020-09-30 00:34:01 +00:00
|
|
|
|
2020-08-23 21:41:54 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys"
|
2020-09-30 00:34:01 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/mintkey"
|
2020-07-02 15:19:48 +00:00
|
|
|
|
2020-08-13 17:14:48 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts"
|
2019-09-18 18:45:21 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2020-08-13 17:14:48 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2020-09-30 00:34:01 +00:00
|
|
|
|
2020-10-06 18:57:55 +00:00
|
|
|
"github.com/cosmos/ethermint/crypto/ethsecp256k1"
|
|
|
|
"github.com/cosmos/ethermint/crypto/hd"
|
2020-10-22 20:39:51 +00:00
|
|
|
"github.com/cosmos/ethermint/rpc/namespaces/eth"
|
|
|
|
rpctypes "github.com/cosmos/ethermint/rpc/types"
|
2019-09-18 18:45:21 +00:00
|
|
|
)
|
|
|
|
|
2020-10-22 20:39:51 +00:00
|
|
|
// PrivateAccountAPI is the personal_ prefixed set of APIs in the Web3 JSON-RPC spec.
|
|
|
|
type PrivateAccountAPI struct {
|
|
|
|
ethAPI *eth.PublicEthereumAPI
|
|
|
|
logger log.Logger
|
2020-09-30 00:34:01 +00:00
|
|
|
keyInfos []keys.Info // all keys, both locked and unlocked. unlocked keys are stored in ethAPI.keys
|
2019-09-18 18:45:21 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 20:39:51 +00:00
|
|
|
// NewAPI creates an instance of the public Personal Eth API.
|
|
|
|
func NewAPI(ethAPI *eth.PublicEthereumAPI) *PrivateAccountAPI {
|
|
|
|
api := &PrivateAccountAPI{
|
2020-09-30 00:34:01 +00:00
|
|
|
ethAPI: ethAPI,
|
2020-10-22 20:39:51 +00:00
|
|
|
logger: log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "json-rpc", "namespace", "personal"),
|
2019-09-18 18:45:21 +00:00
|
|
|
}
|
2020-08-13 17:14:48 +00:00
|
|
|
|
2020-10-22 20:39:51 +00:00
|
|
|
err := api.ethAPI.GetKeyringInfo()
|
2020-08-13 17:14:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return api
|
|
|
|
}
|
|
|
|
|
2020-10-22 20:39:51 +00:00
|
|
|
api.keyInfos, err = api.ethAPI.ClientCtx().Keybase.List()
|
|
|
|
if err != nil {
|
|
|
|
return api
|
2020-08-13 17:14:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 20:39:51 +00:00
|
|
|
return api
|
2020-08-13 17:14:48 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 00:34:01 +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.
|
2020-10-22 20:39:51 +00:00
|
|
|
func (api *PrivateAccountAPI) ImportRawKey(privkey, password string) (common.Address, error) {
|
|
|
|
api.logger.Debug("personal_importRawKey")
|
2020-09-30 00:34:01 +00:00
|
|
|
priv, err := crypto.HexToECDSA(privkey)
|
2020-08-13 17:14:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return common.Address{}, err
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:57:55 +00:00
|
|
|
privKey := ethsecp256k1.PrivKey(crypto.FromECDSA(priv))
|
2020-09-30 00:34:01 +00:00
|
|
|
|
2020-10-06 18:57:55 +00:00
|
|
|
armor := mintkey.EncryptArmorPrivKey(privKey, password, ethsecp256k1.KeyType)
|
2020-09-30 00:34:01 +00:00
|
|
|
|
|
|
|
// ignore error as we only care about the length of the list
|
2020-10-22 20:39:51 +00:00
|
|
|
list, _ := api.ethAPI.ClientCtx().Keybase.List()
|
2020-09-30 00:34:01 +00:00
|
|
|
privKeyName := fmt.Sprintf("personal_%d", len(list))
|
|
|
|
|
2020-10-22 20:39:51 +00:00
|
|
|
if err := api.ethAPI.ClientCtx().Keybase.ImportPrivKey(privKeyName, armor, password); err != nil {
|
2020-09-30 00:34:01 +00:00
|
|
|
return common.Address{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := common.BytesToAddress(privKey.PubKey().Address().Bytes())
|
|
|
|
|
2020-10-22 20:39:51 +00:00
|
|
|
info, err := api.ethAPI.ClientCtx().Keybase.Get(privKeyName)
|
2020-09-30 00:34:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return common.Address{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// append key and info to be able to lock and list the account
|
2020-10-22 20:39:51 +00:00
|
|
|
//api.ethAPI.keys = append(api.ethAPI.keys, privKey)
|
|
|
|
api.keyInfos = append(api.keyInfos, info)
|
|
|
|
api.logger.Info("key successfully imported", "name", privKeyName, "address", addr.String())
|
2020-09-30 00:34:01 +00:00
|
|
|
|
|
|
|
return addr, nil
|
2020-08-13 17:14:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListAccounts will return a list of addresses for accounts this node manages.
|
2020-10-22 20:39:51 +00:00
|
|
|
func (api *PrivateAccountAPI) ListAccounts() ([]common.Address, error) {
|
|
|
|
api.logger.Debug("personal_listAccounts")
|
2020-08-13 17:14:48 +00:00
|
|
|
addrs := []common.Address{}
|
2020-10-22 20:39:51 +00:00
|
|
|
for _, info := range api.keyInfos {
|
2020-08-13 17:14:48 +00:00
|
|
|
addressBytes := info.GetPubKey().Address().Bytes()
|
|
|
|
addrs = append(addrs, common.BytesToAddress(addressBytes))
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2020-10-22 20:39:51 +00:00
|
|
|
func (api *PrivateAccountAPI) LockAccount(address common.Address) bool {
|
|
|
|
api.logger.Debug("personal_lockAccount", "address", address.String())
|
2020-08-13 17:14:48 +00:00
|
|
|
|
2020-10-22 20:39:51 +00:00
|
|
|
keys := api.ethAPI.GetKeys()
|
|
|
|
for i, key := range keys {
|
2020-09-17 22:04:36 +00:00
|
|
|
if !bytes.Equal(key.PubKey().Address().Bytes(), address.Bytes()) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-10-22 20:39:51 +00:00
|
|
|
tmp := make([]ethsecp256k1.PrivKey, len(keys)-1)
|
|
|
|
copy(tmp[:i], keys[:i])
|
|
|
|
copy(tmp[i:], keys[i+1:])
|
|
|
|
api.ethAPI.SetKeys(tmp)
|
2020-09-30 00:34:01 +00:00
|
|
|
|
2020-10-22 20:39:51 +00:00
|
|
|
api.logger.Debug("account unlocked", "address", address.String())
|
2020-09-17 22:04:36 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-08-13 17:14:48 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAccount will create a new account and returns the address for the new account.
|
2020-10-22 20:39:51 +00:00
|
|
|
func (api *PrivateAccountAPI) NewAccount(password string) (common.Address, error) {
|
|
|
|
api.logger.Debug("personal_newAccount")
|
2020-08-13 17:14:48 +00:00
|
|
|
|
|
|
|
name := "key_" + time.Now().UTC().Format(time.RFC3339)
|
2020-10-22 20:39:51 +00:00
|
|
|
info, _, err := api.ethAPI.ClientCtx().Keybase.CreateMnemonic(name, keys.English, password, hd.EthSecp256k1)
|
2020-08-13 17:14:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return common.Address{}, err
|
|
|
|
}
|
|
|
|
|
2020-10-22 20:39:51 +00:00
|
|
|
api.keyInfos = append(api.keyInfos, info)
|
2020-08-13 17:14:48 +00:00
|
|
|
|
|
|
|
addr := common.BytesToAddress(info.GetPubKey().Address().Bytes())
|
2020-10-22 20:39:51 +00:00
|
|
|
api.logger.Info("Your new key was generated", "address", addr.String())
|
|
|
|
api.logger.Info("Please backup your key file!", "path", os.Getenv("HOME")+"/.ethermintd/"+name)
|
|
|
|
api.logger.Info("Please remember your password!")
|
2020-08-13 17:14:48 +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.
|
|
|
|
// It exports the private key corresponding to the given address from the keyring and stores it in the API's local keys.
|
2020-10-22 20:39:51 +00:00
|
|
|
func (api *PrivateAccountAPI) UnlockAccount(_ context.Context, addr common.Address, password string, _ *uint64) (bool, error) { // nolint: interfacer
|
|
|
|
api.logger.Debug("personal_unlockAccount", "address", addr.String())
|
2020-08-13 17:14:48 +00:00
|
|
|
// TODO: use duration
|
|
|
|
|
2020-09-30 00:34:01 +00:00
|
|
|
var keyInfo keys.Info
|
|
|
|
|
2020-10-22 20:39:51 +00:00
|
|
|
for _, info := range api.keyInfos {
|
2020-08-13 17:14:48 +00:00
|
|
|
addressBytes := info.GetPubKey().Address().Bytes()
|
|
|
|
if bytes.Equal(addressBytes, addr[:]) {
|
2020-09-30 00:34:01 +00:00
|
|
|
keyInfo = info
|
|
|
|
break
|
2020-08-13 17:14:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 00:34:01 +00:00
|
|
|
if keyInfo == nil {
|
|
|
|
return false, fmt.Errorf("cannot find key with given address %s", addr.String())
|
2020-08-13 17:14:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 20:39:51 +00:00
|
|
|
privKey, err := api.ethAPI.ClientCtx().Keybase.ExportPrivateKeyObject(keyInfo.GetName(), password)
|
2020-08-13 17:14:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2020-10-22 20:39:51 +00:00
|
|
|
ethermintPrivKey, ok := privKey.(ethsecp256k1.PrivKey)
|
2020-08-13 17:14:48 +00:00
|
|
|
if !ok {
|
2020-10-22 20:39:51 +00:00
|
|
|
return false, fmt.Errorf("invalid private key type %T, expected %T", privKey, ðsecp256k1.PrivKey{})
|
2020-08-13 17:14:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 20:39:51 +00:00
|
|
|
api.ethAPI.SetKeys(append(api.ethAPI.GetKeys(), ethermintPrivKey))
|
|
|
|
api.logger.Debug("account unlocked", "address", addr.String())
|
2020-08-13 17:14:48 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2020-10-22 20:39:51 +00:00
|
|
|
func (api *PrivateAccountAPI) SendTransaction(_ context.Context, args rpctypes.SendTxArgs, _ string) (common.Hash, error) {
|
|
|
|
return api.ethAPI.SendTransaction(args)
|
2019-09-18 18:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sign calculates an Ethereum ECDSA signature for:
|
2020-08-13 17:14:48 +00:00
|
|
|
// keccak256("\x19Ethereum Signed Message:\n" + len(message) + message))
|
2019-09-18 18:45:21 +00:00
|
|
|
//
|
|
|
|
// 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
|
2020-10-22 20:39:51 +00:00
|
|
|
func (api *PrivateAccountAPI) Sign(_ context.Context, data hexutil.Bytes, addr common.Address, _ string) (hexutil.Bytes, error) {
|
|
|
|
api.logger.Debug("personal_sign", "data", data, "address", addr.String())
|
2020-09-17 22:04:36 +00:00
|
|
|
|
2020-10-22 20:39:51 +00:00
|
|
|
key, ok := rpctypes.GetKeyByAddress(api.ethAPI.GetKeys(), addr)
|
2020-08-13 17:14:48 +00:00
|
|
|
if !ok {
|
2020-09-30 00:34:01 +00:00
|
|
|
return nil, fmt.Errorf("cannot find key with address %s", addr.String())
|
2020-08-13 17:14:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sig, err := crypto.Sign(accounts.TextHash(data), key.ToECDSA())
|
|
|
|
if err != nil {
|
|
|
|
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
|
2020-10-22 20:39:51 +00:00
|
|
|
func (api *PrivateAccountAPI) EcRecover(_ context.Context, data, sig hexutil.Bytes) (common.Address, error) {
|
|
|
|
api.logger.Debug("personal_ecRecover", "data", data, "sig", sig)
|
2020-09-17 22:04:36 +00:00
|
|
|
|
2020-08-13 17:14:48 +00:00
|
|
|
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
|
|
|
|
|
2020-09-30 00:34:01 +00:00
|
|
|
pubkey, err := crypto.SigToPub(accounts.TextHash(data), sig)
|
2020-08-13 17:14:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return common.Address{}, err
|
|
|
|
}
|
2020-09-30 00:34:01 +00:00
|
|
|
return crypto.PubkeyToAddress(*pubkey), nil
|
2019-09-18 18:45:21 +00:00
|
|
|
}
|