From 2b43c1f34ee040e9650454ec139f985a77557211 Mon Sep 17 00:00:00 2001 From: allnil Date: Thu, 14 Nov 2024 15:03:03 +0000 Subject: [PATCH] chore: refactor --- store/precomputed_key/wvm/types.go | 79 +++++++++++++++++++++ store/precomputed_key/wvm/wvm_rpc_client.go | 10 ++- 2 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 store/precomputed_key/wvm/types.go diff --git a/store/precomputed_key/wvm/types.go b/store/precomputed_key/wvm/types.go new file mode 100644 index 00000000..030c3361 --- /dev/null +++ b/store/precomputed_key/wvm/types.go @@ -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"` +} diff --git a/store/precomputed_key/wvm/wvm_rpc_client.go b/store/precomputed_key/wvm/wvm_rpc_client.go index e18a61df..d39a62c3 100644 --- a/store/precomputed_key/wvm/wvm_rpc_client.go +++ b/store/precomputed_key/wvm/wvm_rpc_client.go @@ -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 @@ -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