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.
2727type 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.
3738func 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.
7981func (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.
8992func (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.
101105func (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.
118135func (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 \t address=0x%x " , addr )
134- log . Printf ("Please backup your key file!\t path=%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.
143174func (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
189224func (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
214251func (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 }
0 commit comments