Skip to content
This repository was archived by the owner on Nov 30, 2021. It is now read-only.

Commit 517de55

Browse files
nootaraskachoi
andauthored
fix account sending from new accounts (#517)
* fix account sending from new accounts * cleanup * more cleanup * fix rpc tests Co-authored-by: araskachoi <[email protected]>
1 parent 73d6c41 commit 517de55

File tree

6 files changed

+118
-8
lines changed

6 files changed

+118
-8
lines changed

app/ante/ante.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func NewAnteHandler(ak auth.AccountKeeper, evmKeeper EVMKeeper, sk types.SupplyK
3737
case auth.StdTx:
3838
anteHandler = sdk.ChainAnteDecorators(
3939
authante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
40+
NewAccountSetupDecorator(ak),
4041
authante.NewMempoolFeeDecorator(),
4142
authante.NewValidateBasicDecorator(),
4243
authante.NewValidateMemoDecorator(ak),
@@ -126,3 +127,33 @@ func (isd IncrementSequenceDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, sim
126127

127128
return next(ctx, tx, simulate)
128129
}
130+
131+
type AccountSetupDecorator struct {
132+
ak auth.AccountKeeper
133+
}
134+
135+
func NewAccountSetupDecorator(ak auth.AccountKeeper) AccountSetupDecorator {
136+
return AccountSetupDecorator{
137+
ak: ak,
138+
}
139+
}
140+
141+
func (asd AccountSetupDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
142+
msgs := tx.GetMsgs()
143+
if len(msgs) == 0 {
144+
return ctx, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "no messages included in transaction")
145+
}
146+
147+
msg, ok := msgs[0].(evmtypes.MsgEthermint)
148+
if !ok {
149+
return next(ctx, tx, simulate)
150+
}
151+
152+
acc := asd.ak.GetAccount(ctx, msg.From)
153+
if acc == nil {
154+
info := asd.ak.NewAccountWithAddress(ctx, msg.From)
155+
asd.ak.SetAccount(ctx, info)
156+
}
157+
158+
return next(ctx, tx, simulate)
159+
}

app/ante/eth.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ func (avd AccountVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, s
191191

192192
acc := avd.ak.GetAccount(ctx, address)
193193
if acc == nil {
194-
return ctx, fmt.Errorf("account %s (%s) is nil", common.BytesToAddress(address.Bytes()), address)
194+
acc = avd.ak.NewAccountWithAddress(ctx, address)
195+
avd.ak.SetAccount(ctx, acc)
195196
}
196197

197198
// on InitChain make sure account number == 0

rpc/eth_api.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,41 @@ type PublicEthAPI struct {
5454
func NewPublicEthAPI(cliCtx context.CLIContext, backend Backend, nonceLock *AddrLocker,
5555
key []crypto.PrivKeySecp256k1) *PublicEthAPI {
5656

57-
return &PublicEthAPI{
57+
api := &PublicEthAPI{
5858
cliCtx: cliCtx,
5959
logger: log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "json-rpc"),
6060
backend: backend,
6161
keys: key,
6262
nonceLock: nonceLock,
6363
}
64+
err := api.getKeybaseInfo()
65+
if err != nil {
66+
api.logger.Error("failed to get keybase info", "error", err)
67+
}
68+
69+
return api
70+
}
71+
72+
func (e *PublicEthAPI) getKeybaseInfo() error {
73+
e.keybaseLock.Lock()
74+
defer e.keybaseLock.Unlock()
75+
76+
if e.cliCtx.Keybase == nil {
77+
keybase, err := keys.NewKeyring(
78+
sdk.KeyringServiceName(),
79+
viper.GetString(flags.FlagKeyringBackend),
80+
viper.GetString(flags.FlagHome),
81+
e.cliCtx.Input,
82+
crypto.EthSecp256k1Options()...,
83+
)
84+
if err != nil {
85+
return err
86+
}
87+
88+
e.cliCtx.Keybase = keybase
89+
}
90+
91+
return nil
6492
}
6593

6694
// ProtocolVersion returns the supported Ethereum protocol version.
@@ -349,8 +377,13 @@ func (e *PublicEthAPI) SendTransaction(args params.SendTxArgs) (common.Hash, err
349377
e.logger.Debug("eth_sendTransaction", "args", args)
350378
// TODO: Change this functionality to find an unlocked account by address
351379

380+
for _, key := range e.keys {
381+
e.logger.Debug("eth_sendTransaction", "key", fmt.Sprintf("0x%x", key.PubKey().Address().Bytes()))
382+
}
383+
352384
key, exist := checkKeyInKeyring(e.keys, args.From)
353385
if !exist {
386+
e.logger.Debug("failed to find key in keyring", "key", args.From)
354387
return common.Hash{}, keystore.ErrLocked
355388
}
356389

@@ -363,6 +396,7 @@ func (e *PublicEthAPI) SendTransaction(args params.SendTxArgs) (common.Hash, err
363396
// Assemble transaction from fields
364397
tx, err := e.generateFromArgs(args)
365398
if err != nil {
399+
e.logger.Debug("failed to generate tx", "error", err)
366400
return common.Hash{}, err
367401
}
368402

@@ -376,6 +410,7 @@ func (e *PublicEthAPI) SendTransaction(args params.SendTxArgs) (common.Hash, err
376410

377411
// Sign transaction
378412
if err := tx.Sign(intChainID, key.ToECDSA()); err != nil {
413+
e.logger.Debug("failed to sign tx", "error", err)
379414
return common.Hash{}, err
380415
}
381416

@@ -967,6 +1002,10 @@ func (e *PublicEthAPI) generateFromArgs(args params.SendTxArgs) (*evmtypes.MsgEt
9671002
from := sdk.AccAddress(args.From.Bytes())
9681003
accRet := authtypes.NewAccountRetriever(e.cliCtx)
9691004

1005+
if e.cliCtx.Keybase == nil {
1006+
return nil, fmt.Errorf("cliCtx.Keybase is nil")
1007+
}
1008+
9701009
err = accRet.EnsureExists(from)
9711010
if err != nil {
9721011
// account doesn't exist

rpc/personal_api.go

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7-
"log"
87
"os"
98
"sync"
109
"time"
@@ -16,6 +15,7 @@ import (
1615
emintcrypto "github.com/cosmos/ethermint/crypto"
1716
params "github.com/cosmos/ethermint/rpc/args"
1817
"github.com/spf13/viper"
18+
"github.com/tendermint/tendermint/libs/log"
1919

2020
"github.com/ethereum/go-ethereum/accounts"
2121
"github.com/ethereum/go-ethereum/common"
@@ -25,6 +25,7 @@ import (
2525

2626
// PersonalEthAPI is the eth_ prefixed set of APIs in the Web3 JSON-RPC spec.
2727
type PersonalEthAPI struct {
28+
logger log.Logger
2829
cliCtx sdkcontext.CLIContext
2930
ethAPI *PublicEthAPI
3031
nonceLock *AddrLocker
@@ -36,6 +37,7 @@ type PersonalEthAPI struct {
3637
// NewPersonalEthAPI creates an instance of the public ETH Web3 API.
3738
func NewPersonalEthAPI(cliCtx sdkcontext.CLIContext, ethAPI *PublicEthAPI, nonceLock *AddrLocker, keys []emintcrypto.PrivKeySecp256k1) *PersonalEthAPI {
3839
api := &PersonalEthAPI{
40+
logger: log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "json-rpc"),
3941
cliCtx: cliCtx,
4042
ethAPI: ethAPI,
4143
nonceLock: nonceLock,
@@ -77,6 +79,7 @@ func (e *PersonalEthAPI) getKeybaseInfo() ([]keys.Info, error) {
7779
// encrypting it with the passphrase.
7880
// Currently, this is not implemented since the feature is not supported by the keys.
7981
func (e *PersonalEthAPI) ImportRawKey(privkey, password string) (common.Address, error) {
82+
e.logger.Debug("personal_importRawKey", "error", "not implemented")
8083
_, err := crypto.HexToECDSA(privkey)
8184
if err != nil {
8285
return common.Address{}, err
@@ -87,6 +90,7 @@ func (e *PersonalEthAPI) ImportRawKey(privkey, password string) (common.Address,
8790

8891
// ListAccounts will return a list of addresses for accounts this node manages.
8992
func (e *PersonalEthAPI) ListAccounts() ([]common.Address, error) {
93+
e.logger.Debug("personal_listAccounts")
9094
addrs := []common.Address{}
9195
for _, info := range e.keyInfos {
9296
addressBytes := info.GetPubKey().Address().Bytes()
@@ -99,6 +103,7 @@ func (e *PersonalEthAPI) ListAccounts() ([]common.Address, error) {
99103
// LockAccount will lock the account associated with the given address when it's unlocked.
100104
// It removes the key corresponding to the given address from the API's local keys.
101105
func (e *PersonalEthAPI) LockAccount(address common.Address) bool {
106+
e.logger.Debug("personal_lockAccount", "address", address)
102107
for i, key := range e.keys {
103108
if !bytes.Equal(key.PubKey().Address().Bytes(), address.Bytes()) {
104109
continue
@@ -111,11 +116,24 @@ func (e *PersonalEthAPI) LockAccount(address common.Address) bool {
111116
return true
112117
}
113118

119+
for i, key := range e.ethAPI.keys {
120+
if !bytes.Equal(key.PubKey().Address().Bytes(), address.Bytes()) {
121+
continue
122+
}
123+
124+
tmp := make([]emintcrypto.PrivKeySecp256k1, len(e.ethAPI.keys)-1)
125+
copy(tmp[:i], e.ethAPI.keys[:i])
126+
copy(tmp[i:], e.ethAPI.keys[i+1:])
127+
e.ethAPI.keys = tmp
128+
return true
129+
}
130+
114131
return false
115132
}
116133

117134
// NewAccount will create a new account and returns the address for the new account.
118135
func (e *PersonalEthAPI) NewAccount(password string) (common.Address, error) {
136+
e.logger.Debug("personal_newAccount")
119137
_, err := e.getKeybaseInfo()
120138
if err != nil {
121139
return common.Address{}, err
@@ -129,10 +147,23 @@ func (e *PersonalEthAPI) NewAccount(password string) (common.Address, error) {
129147

130148
e.keyInfos = append(e.keyInfos, info)
131149

150+
// update ethAPI
151+
privKey, err := e.cliCtx.Keybase.ExportPrivateKeyObject(name, password)
152+
if err != nil {
153+
return common.Address{}, err
154+
}
155+
156+
emintKey, ok := privKey.(emintcrypto.PrivKeySecp256k1)
157+
if !ok {
158+
return common.Address{}, fmt.Errorf("invalid private key type: %T", privKey)
159+
}
160+
e.ethAPI.keys = append(e.ethAPI.keys, emintKey)
161+
e.logger.Debug("personal_newAccount", "address", fmt.Sprintf("0x%x", emintKey.PubKey().Address().Bytes()))
162+
132163
addr := common.BytesToAddress(info.GetPubKey().Address().Bytes())
133-
log.Printf("Your new key was generated\t\taddress=0x%x", addr)
134-
log.Printf("Please backup your key file!\tpath=%s", os.Getenv("HOME")+"/.ethermintcli/"+name)
135-
log.Println("Please remember your password!")
164+
e.logger.Info("Your new key was generated", "address", addr)
165+
e.logger.Info("Please backup your key file!", "path", os.Getenv("HOME")+"/.ethermintcli/"+name)
166+
e.logger.Info("Please remember your password!")
136167
return addr, nil
137168
}
138169

@@ -141,6 +172,7 @@ func (e *PersonalEthAPI) NewAccount(password string) (common.Address, error) {
141172
// default of 300 seconds. It returns an indication if the account was unlocked.
142173
// It exports the private key corresponding to the given address from the keyring and stores it in the API's local keys.
143174
func (e *PersonalEthAPI) UnlockAccount(ctx context.Context, addr common.Address, password string, _ *uint64) (bool, error) {
175+
e.logger.Debug("personal_unlockAccount", "address", addr)
144176
// TODO: use duration
145177

146178
name := ""
@@ -167,6 +199,9 @@ func (e *PersonalEthAPI) UnlockAccount(ctx context.Context, addr common.Address,
167199
}
168200

169201
e.keys = append(e.keys, emintKey)
202+
e.ethAPI.keys = append(e.ethAPI.keys, emintKey)
203+
e.logger.Debug("personal_unlockAccount", "address", fmt.Sprintf("0x%x", emintKey.PubKey().Address().Bytes()))
204+
170205
return true, nil
171206
}
172207

@@ -187,6 +222,8 @@ func (e *PersonalEthAPI) SendTransaction(ctx context.Context, args params.SendTx
187222
//
188223
// https://github.com/ethereum/go-ethereum/wiki/Management-APIs#personal_sign
189224
func (e *PersonalEthAPI) Sign(ctx context.Context, data hexutil.Bytes, addr common.Address, passwd string) (hexutil.Bytes, error) {
225+
e.logger.Debug("personal_sign", "data", data, "address", addr)
226+
190227
key, ok := checkKeyInKeyring(e.keys, addr)
191228
if !ok {
192229
return nil, fmt.Errorf("cannot find key with given address")
@@ -212,6 +249,8 @@ func (e *PersonalEthAPI) Sign(ctx context.Context, data hexutil.Bytes, addr comm
212249
//
213250
// https://github.com/ethereum/go-ethereum/wiki/Management-APIs#personal_ecRecove
214251
func (e *PersonalEthAPI) EcRecover(ctx context.Context, data, sig hexutil.Bytes) (common.Address, error) {
252+
e.logger.Debug("personal_ecRecover", "data", data, "sig", sig)
253+
215254
if len(sig) != crypto.SignatureLength {
216255
return common.Address{}, fmt.Errorf("signature must be %d bytes long", crypto.SignatureLength)
217256
}

tests/personal_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestPersonal_ListAccounts(t *testing.T) {
2020
}
2121

2222
func TestPersonal_NewAccount(t *testing.T) {
23-
rpcRes := call(t, "personal_newAccount", []string{""})
23+
rpcRes := call(t, "personal_newAccount", []string{"password"})
2424
var addr common.Address
2525
err := json.Unmarshal(rpcRes.Result, &addr)
2626
require.NoError(t, err)

tests/rpc_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ func TestEth_EstimateGas(t *testing.T) {
758758
err := json.Unmarshal(rpcRes.Result, &gas)
759759
require.NoError(t, err, string(rpcRes.Result))
760760

761-
require.Equal(t, "0xef7e", gas)
761+
require.Equal(t, "0xf552", gas)
762762
}
763763

764764
func TestEth_EstimateGas_ContractDeployment(t *testing.T) {

0 commit comments

Comments
 (0)