Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
allnil committed Nov 14, 2024
1 parent a8f50da commit 2b43c1f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
79 changes: 79 additions & 0 deletions store/precomputed_key/wvm/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package wvm

import (
"encoding/json"
"fmt"
"math/big"
"strings"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
)

type Web3SignerTransactionData struct {
bytesData []byte
chainID int64
fromAddress common.Address
toAddress common.Address
gasLimit uint64
gasFeeCap *big.Int
nonce uint64
}

func NewWeb3SignerTransactionData(fromAddress, toAddress common.Address,
gasLimit uint64, gasFeeCap *big.Int, data string, nonce uint64, chainID int64,
) (*Web3SignerTransactionData, error) {
// Prepare data payload.
var hexData string
if strings.HasPrefix(data, "0x") {
hexData = data
} else {
hexData = hexutil.Encode([]byte(data))
}
bytesData, err := hexutil.Decode(hexData)
if err != nil {
return nil, err
}

return &Web3SignerTransactionData{
bytesData: bytesData,
chainID: chainID,
fromAddress: fromAddress,
toAddress: toAddress,
gasLimit: gasLimit,
gasFeeCap: gasFeeCap,
nonce: nonce,
}, nil
}

func (data *Web3SignerTransactionData) ToRequestMap() map[string]interface{} {
return map[string]interface{}{
"from": data.fromAddress.String(),
"to": data.toAddress.String(),
"gas": fmt.Sprintf("0x%x", data.gasLimit),
"maxFeePerGas": fmt.Sprintf("0x%x", data.gasFeeCap),
"maxPriorityFeePerGas": "0x0",
"value": "0x0",
"data": data.bytesData,
"nonce": fmt.Sprintf("0x%x", data.nonce),
"chainId": fmt.Sprintf("0x%x", data.chainID),
}
}

type SignTransactionResponse string

func (r *SignTransactionResponse) UnmarshalJSON(data []byte) error {
var signedTx string
if err := json.Unmarshal(data, &signedTx); err != nil {
return err
}
*r = SignTransactionResponse(signedTx)
return nil
}

type RetrieverResponse struct {
ArweaveBlockHash string `json:"arweave_block_hash"`
Calldata string `json:"calldata"`
WarDecodedCalldata string `json:"war_decoded_calldata"`
WvmBlockHash string `json:"wvm_block_hash"`
}
10 changes: 7 additions & 3 deletions store/precomputed_key/wvm/wvm_rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ func NewEthRPCClient(log log.Logger, cfg *Config) (*EthRPCClient, error) {
chainID: cfg.ChainID,
}

privKey := os.Getenv("WVM_PRIV_KEY")
if privKey == "" {
if cfg.Web3SignerEndpoint != "" {
ethRPCClient.web3signer = NewWeb3SignerClient(cfg, log)
}

if os.Getenv("WVM_PRIV_KEY") == "" {
if cfg.Web3SignerEndpoint == "" {
return nil, fmt.Errorf("wvm archiver private key is empty and wvm web3 signer is empty")
}
ethRPCClient.web3signer = NewWeb3SignerClient(cfg, log)
}

return ethRPCClient, nil
Expand Down Expand Up @@ -185,6 +187,8 @@ func getAddressFromPrivateKey() (common.Address, *ecdsa.PrivateKey, error) {
}

func (rpc *EthRPCClient) signTxWithPrivateKey(ctx context.Context, to string, data string, gasFeeCap *big.Int, gasLimit uint64) (string, error) {
rpc.log.Info("sign transaction using private key")

fromAddress, ecdsaPrivateKey, err := getAddressFromPrivateKey()
if err != nil {
return "", err
Expand Down

0 comments on commit 2b43c1f

Please sign in to comment.