Skip to content

Commit

Permalink
Add cosmos type for web3 tx
Browse files Browse the repository at this point in the history
  • Loading branch information
BoThe1K committed Feb 3, 2023
1 parent 8227831 commit e1cdc14
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (web3 *Web3Server) registerAPIs(server *rpc.Server, apis []rpc.API) error {

func (web3 *Web3Server) StartHTTP(apis []rpc.API) error {
rpcSrv := rpc.NewServer()
handler := node.NewHTTPHandlerStack(rpcSrv, []string{"*"}, []string{"localhost"}, []byte{}) // TODO: Replace cors and vshosts from config
handler := node.NewHTTPHandlerStack(rpcSrv, []string{"*"}, []string{"localhost", "host.docker.internal"}, []byte{}) // TODO: Replace cors and vshosts from config
if err := web3.registerAPIs(rpcSrv, apis); err != nil {
return err
}
Expand Down
22 changes: 20 additions & 2 deletions rpc/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"bytes"
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
Expand Down Expand Up @@ -33,6 +34,21 @@ import (
var bAttributeKeyEthereumBloom = []byte(evmtypes.AttributeKeyEthereumBloom)
var MempoolCapacity = 100

// HashToUint64 used to convert string or hash string to uint64
func HashToUint64(s string) uint64 {
h := sha1.New()
h.Write([]byte(s))

hash := h.Sum(nil)[:2]
result := uint64(0)
for i := 0; i < len(hash); i++ {
result = result << 8
result += uint64(hash[i])

}
return result
}

// RawTxToEthTx returns a evm MsgEthereum transaction from raw tx bytes.
func RawTxToEthTx(clientCtx client.Context, txBz tmtypes.Tx) ([]*evmtypes.MsgEthereumTx, error) {
tx, err := clientCtx.TxConfig.TxDecoder()(txBz)
Expand Down Expand Up @@ -374,6 +390,7 @@ func TmTxToEthTx(
addr := msg.GetSigners()[0]
from := common.BytesToAddress(addr.Bytes())

nonce := uint64(0)
v := new(big.Int).SetInt64(0)
r := new(big.Int).SetInt64(0)
s := new(big.Int).SetInt64(0)
Expand All @@ -384,6 +401,7 @@ func TmTxToEthTx(
sig := sigs[0]
sigProto := signing.SignatureDataToProto(sig.Data)
v, r, s = GetNonEVMSignatures(sigProto.GetSingle().GetSignature())
nonce = sig.Sequence
}
} else {
fmt.Printf("failed to get signature for %s\n", tmTx.Hash())
Expand All @@ -407,13 +425,13 @@ func TmTxToEthTx(
return &Transaction{
BlockHash: blockHash,
BlockNumber: (*hexutil.Big)(bNumber),
Type: hexutil.Uint64(0),
Type: hexutil.Uint64(HashToUint64(sdk.MsgTypeURL(msg))),
From: from,
Gas: hexutil.Uint64(gas),
GasPrice: (*hexutil.Big)(gasPrice),
Hash: common.BytesToHash(tmTx.Hash()),
Input: make(hexutil.Bytes, 0),
Nonce: hexutil.Uint64(0),
Nonce: hexutil.Uint64(nonce),
To: new(common.Address),
TransactionIndex: (*hexutil.Uint64)(index),
Value: (*hexutil.Big)(new(big.Int).SetInt64(0)), // NOTE: How to get value in generic way?
Expand Down

0 comments on commit e1cdc14

Please sign in to comment.