99 authutils "github.com/cosmos/cosmos-sdk/x/auth/client/utils"
1010 emintcrypto "github.com/cosmos/ethermint/crypto"
1111 emintkeys "github.com/cosmos/ethermint/keys"
12+ "github.com/cosmos/ethermint/rpc/args"
1213 "github.com/cosmos/ethermint/version"
1314 "github.com/cosmos/ethermint/x/evm/types"
1415
@@ -17,20 +18,26 @@ import (
1718 "github.com/ethereum/go-ethereum/common/hexutil"
1819 "github.com/ethereum/go-ethereum/rlp"
1920 "github.com/ethereum/go-ethereum/rpc"
20- "github.com/ethereum/go-ethereum/signer/core"
21+
22+ "github.com/cosmos/cosmos-sdk/client/flags"
23+ "github.com/spf13/viper"
2124)
2225
2326// PublicEthAPI is the eth_ prefixed set of APIs in the Web3 JSON-RPC spec.
2427type PublicEthAPI struct {
25- cliCtx context.CLIContext
26- key emintcrypto.PrivKeySecp256k1
28+ cliCtx context.CLIContext
29+ key emintcrypto.PrivKeySecp256k1
30+ nonceLock * AddrLocker
2731}
2832
2933// NewPublicEthAPI creates an instance of the public ETH Web3 API.
30- func NewPublicEthAPI (cliCtx context.CLIContext , key emintcrypto.PrivKeySecp256k1 ) * PublicEthAPI {
34+ func NewPublicEthAPI (cliCtx context.CLIContext , nonceLock * AddrLocker ,
35+ key emintcrypto.PrivKeySecp256k1 ) * PublicEthAPI {
36+
3137 return & PublicEthAPI {
32- cliCtx : cliCtx ,
33- key : key ,
38+ cliCtx : cliCtx ,
39+ key : key ,
40+ nonceLock : nonceLock ,
3441 }
3542}
3643
@@ -200,9 +207,54 @@ func (e *PublicEthAPI) Sign(address common.Address, data hexutil.Bytes) (hexutil
200207}
201208
202209// SendTransaction sends an Ethereum transaction.
203- func (e * PublicEthAPI ) SendTransaction (args core.SendTxArgs ) common.Hash {
204- var h common.Hash
205- return h
210+ func (e * PublicEthAPI ) SendTransaction (args args.SendTxArgs ) (common.Hash , error ) {
211+ // TODO: Change this functionality to find an unlocked account by address
212+ if e .key == nil || ! bytes .Equal (e .key .PubKey ().Address ().Bytes (), args .From .Bytes ()) {
213+ return common.Hash {}, keystore .ErrLocked
214+ }
215+
216+ // Mutex lock the address' nonce to avoid assigning it to multiple requests
217+ if args .Nonce == nil {
218+ e .nonceLock .LockAddr (args .From )
219+ defer e .nonceLock .UnlockAddr (args .From )
220+ }
221+
222+ // Assemble transaction from fields
223+ tx , err := types .GenerateFromArgs (args , e .cliCtx )
224+ if err != nil {
225+ return common.Hash {}, err
226+ }
227+
228+ // ChainID must be set as flag to send transaction
229+ chainID := viper .GetString (flags .FlagChainID )
230+ // parse the chainID from a string to a base-10 integer
231+ intChainID , ok := new (big.Int ).SetString (chainID , 10 )
232+ if ! ok {
233+ return common.Hash {}, fmt .Errorf (
234+ fmt .Sprintf ("Invalid chainID: %s, must be integer format" , chainID ))
235+ }
236+
237+ // Sign transaction
238+ tx .Sign (intChainID , e .key .ToECDSA ())
239+
240+ // Encode transaction by default Tx encoder
241+ txEncoder := authutils .GetTxEncoder (e .cliCtx .Codec )
242+ txBytes , err := txEncoder (tx )
243+ if err != nil {
244+ return common.Hash {}, err
245+ }
246+
247+ // Broadcast transaction
248+ res , err := e .cliCtx .BroadcastTx (txBytes )
249+ // If error is encountered on the node, the broadcast will not return an error
250+ // TODO: Remove res log
251+ fmt .Println (res .RawLog )
252+ if err != nil {
253+ return common.Hash {}, err
254+ }
255+
256+ // Return RLP encoded bytes
257+ return tx .Hash (), nil
206258}
207259
208260// SendRawTransaction send a raw Ethereum transaction.
@@ -225,12 +277,14 @@ func (e *PublicEthAPI) SendRawTransaction(data hexutil.Bytes) (common.Hash, erro
225277 // TODO: Possibly log the contract creation address (if recipient address is nil) or tx data
226278 res , err := e .cliCtx .BroadcastTx (txBytes )
227279 // If error is encountered on the node, the broadcast will not return an error
280+ // TODO: Remove res log
228281 fmt .Println (res .RawLog )
229282 if err != nil {
230283 return common.Hash {}, err
231284 }
232285
233- return common .HexToHash (res .TxHash ), nil
286+ // Return RLP encoded bytes
287+ return tx .Hash (), nil
234288}
235289
236290// CallArgs represents arguments to a smart contract call as provided by RPC clients.
0 commit comments