Skip to content

Commit

Permalink
Introduce ECDSAKey and decouple from ethgo transactions signing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Feb 11, 2024
1 parent b459860 commit 4475fca
Show file tree
Hide file tree
Showing 56 changed files with 723 additions and 560 deletions.
29 changes: 15 additions & 14 deletions command/bridge/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
"github.com/0xPolygon/polygon-edge/consensus/polybft/validator"
"github.com/0xPolygon/polygon-edge/contracts"
"github.com/0xPolygon/polygon-edge/crypto"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"
)
Expand Down Expand Up @@ -111,12 +112,12 @@ var (
// initializersMap maps rootchain contract names to initializer function callbacks
initializersMap = map[string]func(command.OutputFormatter, txrelayer.TxRelayer,
[]*validator.GenesisValidator,
*polybft.RootchainConfig, ethgo.Key, int64) error{
*polybft.RootchainConfig, crypto.Key, int64) error{
getProxyNameForImpl(checkpointManagerName): func(fmt command.OutputFormatter,
relayer txrelayer.TxRelayer,
genesisValidators []*validator.GenesisValidator,
config *polybft.RootchainConfig,
key ethgo.Key,
key crypto.Key,
chainID int64) error {
if !consensusCfg.NativeTokenConfig.IsMintable {
// we can not initialize checkpoint manager at this moment if native token is not mintable
Expand Down Expand Up @@ -145,7 +146,7 @@ var (
relayer txrelayer.TxRelayer,
genesisValidators []*validator.GenesisValidator,
config *polybft.RootchainConfig,
key ethgo.Key,
key crypto.Key,
chainID int64) error {
inputParams := &contractsapi.InitializeExitHelperFn{
NewCheckpointManager: config.CheckpointManagerAddress,
Expand All @@ -157,7 +158,7 @@ var (
relayer txrelayer.TxRelayer,
genesisValidators []*validator.GenesisValidator,
config *polybft.RootchainConfig,
key ethgo.Key,
key crypto.Key,
chainID int64) error {
inputParams := &contractsapi.InitializeRootERC20PredicateFn{
NewStateSender: config.StateSenderAddress,
Expand All @@ -175,7 +176,7 @@ var (
relayer txrelayer.TxRelayer,
genesisValidators []*validator.GenesisValidator,
config *polybft.RootchainConfig,
key ethgo.Key,
key crypto.Key,
chainID int64) error {
initParams := &contractsapi.InitializeChildMintableERC20PredicateFn{
NewStateSender: config.StateSenderAddress,
Expand All @@ -191,7 +192,7 @@ var (
relayer txrelayer.TxRelayer,
genesisValidators []*validator.GenesisValidator,
config *polybft.RootchainConfig,
key ethgo.Key,
key crypto.Key,
chainID int64) error {
initParams := &contractsapi.InitializeRootERC721PredicateFn{
NewStateSender: config.StateSenderAddress,
Expand All @@ -207,7 +208,7 @@ var (
relayer txrelayer.TxRelayer,
genesisValidators []*validator.GenesisValidator,
config *polybft.RootchainConfig,
key ethgo.Key,
key crypto.Key,
chainID int64) error {
initParams := &contractsapi.InitializeChildMintableERC721PredicateFn{
NewStateSender: config.StateSenderAddress,
Expand All @@ -223,7 +224,7 @@ var (
relayer txrelayer.TxRelayer,
genesisValidators []*validator.GenesisValidator,
config *polybft.RootchainConfig,
key ethgo.Key,
key crypto.Key,
chainID int64) error {
initParams := &contractsapi.InitializeRootERC1155PredicateFn{
NewStateSender: config.StateSenderAddress,
Expand All @@ -239,7 +240,7 @@ var (
relayer txrelayer.TxRelayer,
genesisValidators []*validator.GenesisValidator,
config *polybft.RootchainConfig,
key ethgo.Key,
key crypto.Key,
chainID int64) error {
initParams := &contractsapi.InitializeChildMintableERC1155PredicateFn{
NewStateSender: config.StateSenderAddress,
Expand All @@ -255,7 +256,7 @@ var (
relayer txrelayer.TxRelayer,
genesisValidators []*validator.GenesisValidator,
config *polybft.RootchainConfig,
key ethgo.Key,
key crypto.Key,
chainID int64) error {
gvs := make([]*contractsapi.GenesisAccount, len(genesisValidators))
for i := 0; i < len(genesisValidators); i++ {
Expand Down Expand Up @@ -443,7 +444,7 @@ func deployContracts(outputter command.OutputFormatter, client *jsonrpc.Client,
if params.isTestMode {
deployerAddr := deployerKey.Address()

txn := helper.CreateTransaction(ethgo.ZeroAddress, &deployerAddr, nil, ethgo.Ether(1), true)
txn := helper.CreateTransaction(types.ZeroAddress, &deployerAddr, nil, ethgo.Ether(1), true)
if _, err = txRelayer.SendTransactionLocal(txn); err != nil {
return deploymentResultInfo{RootchainCfg: nil, CommandResults: nil}, err
}
Expand Down Expand Up @@ -589,7 +590,7 @@ func deployContracts(outputter command.OutputFormatter, client *jsonrpc.Client,
}
}

txn := helper.CreateTransaction(ethgo.ZeroAddress, nil, bytecode, nil, true)
txn := helper.CreateTransaction(types.ZeroAddress, nil, bytecode, nil, true)

receipt, err := txRelayer.SendTransaction(txn, deployerKey)
if err != nil {
Expand Down Expand Up @@ -711,14 +712,14 @@ func populateExistingTokenAddr(eth *jsonrpc.Eth, tokenAddr, tokenName string,
// initContract initializes arbitrary contract with given parameters deployed on a given address
func initContract(cmdOutput command.OutputFormatter, txRelayer txrelayer.TxRelayer,
initInputFn contractsapi.StateTransactionInput, contractAddr types.Address,
contractName string, deployerKey ethgo.Key) error {
contractName string, deployerKey crypto.Key) error {
input, err := initInputFn.EncodeAbi()
if err != nil {
return fmt.Errorf("failed to encode initialization params for %s.initialize. error: %w",
contractName, err)
}

if _, err := helper.SendTransaction(txRelayer, ethgo.Address(contractAddr),
if _, err := helper.SendTransaction(txRelayer, contractAddr,
input, contractName, deployerKey); err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion command/bridge/deploy/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/stretchr/testify/require"
"github.com/umbracle/ethgo"
"github.com/umbracle/ethgo/jsonrpc"
"github.com/umbracle/ethgo/testutil"

Expand All @@ -32,7 +33,7 @@ func TestDeployContracts_NoPanics(t *testing.T) {
testKey, err := helper.DecodePrivateKey("")
require.NoError(t, err)

receipt, err := server.Fund(testKey.Address())
receipt, err := server.Fund(ethgo.Address(testKey.Address()))
require.NoError(t, err)
require.Equal(t, uint64(types.ReceiptSuccess), receipt.Status)

Expand Down
19 changes: 8 additions & 11 deletions command/bridge/deposit/erc1155/deposit_erc1155.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"github.com/spf13/cobra"
"github.com/umbracle/ethgo"

"github.com/0xPolygon/polygon-edge/command"
"github.com/0xPolygon/polygon-edge/command/bridge/common"
Expand Down Expand Up @@ -245,8 +244,8 @@ func runCommand(cmd *cobra.Command, _ []string) {
}

// createDepositTxn encodes parameters for deposit function on rootchain predicate contract
func createDepositTxn(sender ethgo.Address, receivers []types.Address,
amounts, tokenIDs []*big.Int) (*ethgo.Transaction, error) {
func createDepositTxn(sender types.Address, receivers []types.Address,
amounts, tokenIDs []*big.Int) (*types.Transaction, error) {
depositBatchFn := &contractsapi.DepositBatchRootERC1155PredicateFn{
RootToken: types.StringToAddress(dp.TokenAddr),
Receivers: receivers,
Expand All @@ -259,14 +258,14 @@ func createDepositTxn(sender ethgo.Address, receivers []types.Address,
return nil, fmt.Errorf("failed to encode provided parameters: %w", err)
}

addr := ethgo.Address(types.StringToAddress(dp.PredicateAddr))
addr := types.StringToAddress(dp.PredicateAddr)

return helper.CreateTransaction(sender, &addr, input,
nil, !dp.ChildChainMintable), nil
}

// createMintTxn encodes parameters for mint function on rootchain token contract
func createMintTxn(sender, receiver types.Address, amounts, tokenIDs []*big.Int) (*ethgo.Transaction, error) {
func createMintTxn(sender, receiver types.Address, amounts, tokenIDs []*big.Int) (*types.Transaction, error) {
mintFn := &contractsapi.MintBatchRootERC1155Fn{
To: receiver,
Amounts: amounts,
Expand All @@ -278,16 +277,16 @@ func createMintTxn(sender, receiver types.Address, amounts, tokenIDs []*big.Int)
return nil, fmt.Errorf("failed to encode provided parameters: %w", err)
}

addr := ethgo.Address(types.StringToAddress(dp.TokenAddr))
addr := types.StringToAddress(dp.TokenAddr)

return helper.CreateTransaction(ethgo.Address(sender), &addr,
return helper.CreateTransaction(sender, &addr,
input, nil, !dp.ChildChainMintable), nil
}

// createApproveERC1155PredicateTxn sends approve transaction
// to ERC1155 token for ERC1155 predicate so that it is able to spend given tokens
func createApproveERC1155PredicateTxn(rootERC1155Predicate,
rootERC1155Token types.Address) (*ethgo.Transaction, error) {
rootERC1155Token types.Address) (*types.Transaction, error) {
approveFnParams := &contractsapi.SetApprovalForAllRootERC1155Fn{
Operator: rootERC1155Predicate,
Approved: true,
Expand All @@ -298,8 +297,6 @@ func createApproveERC1155PredicateTxn(rootERC1155Predicate,
return nil, fmt.Errorf("failed to encode parameters for RootERC1155.setApprovalForAll. error: %w", err)
}

addr := ethgo.Address(rootERC1155Token)

return helper.CreateTransaction(ethgo.ZeroAddress, &addr,
return helper.CreateTransaction(types.ZeroAddress, &rootERC1155Token,
input, nil, !dp.ChildChainMintable), nil
}
6 changes: 3 additions & 3 deletions command/bridge/deposit/erc20/deposit_erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func runCommand(cmd *cobra.Command, _ []string) {
}

// createDepositTxn encodes parameters for deposit function on rootchain predicate contract
func createDepositTxn(sender, receiver types.Address, amount *big.Int) (*ethgo.Transaction, error) {
func createDepositTxn(sender, receiver types.Address, amount *big.Int) (*types.Transaction, error) {
depositToFn := &contractsapi.DepositToRootERC20PredicateFn{
RootToken: types.StringToAddress(dp.TokenAddr),
Receiver: receiver,
Expand All @@ -279,8 +279,8 @@ func createDepositTxn(sender, receiver types.Address, amount *big.Int) (*ethgo.T
return nil, fmt.Errorf("failed to encode provided parameters: %w", err)
}

addr := ethgo.Address(types.StringToAddress(dp.PredicateAddr))
addr := types.StringToAddress(dp.PredicateAddr)

return helper.CreateTransaction(ethgo.Address(sender), &addr,
return helper.CreateTransaction(sender, &addr,
input, nil, !dp.ChildChainMintable), nil
}
22 changes: 10 additions & 12 deletions command/bridge/deposit/erc721/deposit_erc721.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"math/big"
"strings"

"github.com/spf13/cobra"

"github.com/0xPolygon/polygon-edge/command"
"github.com/0xPolygon/polygon-edge/command/bridge/common"
"github.com/0xPolygon/polygon-edge/command/bridge/helper"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
helperCommon "github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"
"github.com/spf13/cobra"
"github.com/umbracle/ethgo"
)

type depositERC721Params struct {
Expand Down Expand Up @@ -219,8 +219,8 @@ func runCommand(cmd *cobra.Command, _ []string) {
}

// createDepositTxn encodes parameters for deposit function on rootchain predicate contract
func createDepositTxn(sender ethgo.Address,
receivers []types.Address, tokenIDs []*big.Int) (*ethgo.Transaction, error) {
func createDepositTxn(sender types.Address,
receivers []types.Address, tokenIDs []*big.Int) (*types.Transaction, error) {
depositToRoot := &contractsapi.DepositBatchRootERC721PredicateFn{
RootToken: types.StringToAddress(dp.TokenAddr),
Receivers: receivers,
Expand All @@ -232,13 +232,13 @@ func createDepositTxn(sender ethgo.Address,
return nil, fmt.Errorf("failed to encode provided parameters: %w", err)
}

addr := ethgo.Address(types.StringToAddress(dp.PredicateAddr))
addr := types.StringToAddress(dp.PredicateAddr)

return helper.CreateTransaction(sender, &addr, input, nil, !dp.ChildChainMintable), nil
}

// createMintTxn encodes parameters for mint function on rootchain token contract
func createMintTxn(sender, receiver types.Address) (*ethgo.Transaction, error) {
func createMintTxn(sender, receiver types.Address) (*types.Transaction, error) {
mintFn := &contractsapi.MintRootERC721Fn{
To: receiver,
}
Expand All @@ -248,14 +248,14 @@ func createMintTxn(sender, receiver types.Address) (*ethgo.Transaction, error) {
return nil, fmt.Errorf("failed to encode provided parameters: %w", err)
}

addr := ethgo.Address(types.StringToAddress(dp.TokenAddr))
addr := types.StringToAddress(dp.TokenAddr)

return helper.CreateTransaction(ethgo.Address(sender), &addr,
return helper.CreateTransaction(sender, &addr,
input, nil, !dp.ChildChainMintable), nil
}

// createApproveERC721PredicateTxn sends approve transaction
func createApproveERC721PredicateTxn(rootERC721Predicate, rootERC721Token types.Address) (*ethgo.Transaction, error) {
func createApproveERC721PredicateTxn(rootERC721Predicate, rootERC721Token types.Address) (*types.Transaction, error) {
approveFnParams := &contractsapi.SetApprovalForAllRootERC721Fn{
Operator: rootERC721Predicate,
Approved: true,
Expand All @@ -266,8 +266,6 @@ func createApproveERC721PredicateTxn(rootERC721Predicate, rootERC721Token types.
return nil, fmt.Errorf("failed to encode parameters for RootERC721.approve. error: %w", err)
}

addr := ethgo.Address(rootERC721Token)

return helper.CreateTransaction(ethgo.ZeroAddress, &addr, input,
return helper.CreateTransaction(types.ZeroAddress, &rootERC721Token, input,
nil, !dp.ChildChainMintable), nil
}
7 changes: 3 additions & 4 deletions command/bridge/exit/exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strconv"

"github.com/spf13/cobra"
"github.com/umbracle/ethgo"
"github.com/umbracle/ethgo/jsonrpc"

"github.com/0xPolygon/polygon-edge/command"
Expand Down Expand Up @@ -156,7 +155,7 @@ func run(cmd *cobra.Command, _ []string) {
}

// createExitTxn encodes parameters for exit function on root chain ExitHelper contract
func createExitTxn(sender ethgo.Address, proof types.Proof) (*ethgo.Transaction,
func createExitTxn(sender types.Address, proof types.Proof) (*types.Transaction,
*contractsapi.L2StateSyncedEvent, error) {
exitInput, err := polybft.GetExitInputFromProof(proof)
if err != nil {
Expand All @@ -180,9 +179,9 @@ func createExitTxn(sender ethgo.Address, proof types.Proof) (*ethgo.Transaction,
return nil, nil, fmt.Errorf("failed to encode provided parameters: %w", err)
}

exitHelperAddr := ethgo.Address(types.StringToAddress(ep.exitHelperAddrRaw))
exitHelperAddr := types.StringToAddress(ep.exitHelperAddrRaw)
txn := helper.CreateTransaction(sender, &exitHelperAddr, input, nil, true)
txn.Gas = txrelayer.DefaultGasLimit
txn.SetGas(txrelayer.DefaultGasLimit)

return txn, exitEvent, err
}
Expand Down
9 changes: 5 additions & 4 deletions command/bridge/finalize/finalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/0xPolygon/polygon-edge/consensus/polybft"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
"github.com/0xPolygon/polygon-edge/consensus/polybft/validator"
"github.com/0xPolygon/polygon-edge/crypto"
"github.com/0xPolygon/polygon-edge/helper/hex"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"
Expand Down Expand Up @@ -105,7 +106,7 @@ func runCommand(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("enlist validator failed: %w", err)
}

bladeManagerAddr := ethgo.Address(params.bladeManagerAddr)
bladeManagerAddr := params.bladeManagerAddr

// finalize genesis accounts on BladeManager so that no one can stake and premine no more
encoded, err := finalizeGenesisABIFn.Encode([]interface{}{})
Expand Down Expand Up @@ -145,7 +146,7 @@ func runCommand(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("failed to encode genesis set input: %w", err)
}

genesisSetHexOut, err := txRelayer.Call(ethgo.ZeroAddress, bladeManagerAddr, genesisSetInput)
genesisSetHexOut, err := txRelayer.Call(types.ZeroAddress, bladeManagerAddr, genesisSetInput)
if err != nil {
return fmt.Errorf("failed to retrieve genesis set: %w", err)
}
Expand Down Expand Up @@ -315,7 +316,7 @@ func validatorSetToABISlice(o command.OutputFormatter,
func initializeCheckpointManager(outputter command.OutputFormatter,
txRelayer txrelayer.TxRelayer,
consensusConfig polybft.PolyBFTConfig, chainID int64,
deployerKey ethgo.Key) error {
deployerKey crypto.Key) error {
validatorSet, err := validatorSetToABISlice(outputter, consensusConfig.InitialValidatorSet)
if err != nil {
return fmt.Errorf("failed to convert validators to map: %w", err)
Expand All @@ -333,7 +334,7 @@ func initializeCheckpointManager(outputter command.OutputFormatter,
return fmt.Errorf("failed to encode initialization params for CheckpointManager.initialize. error: %w", err)
}

if _, err := bridgeHelper.SendTransaction(txRelayer, ethgo.Address(consensusConfig.Bridge.CheckpointManagerAddr),
if _, err := bridgeHelper.SendTransaction(txRelayer, consensusConfig.Bridge.CheckpointManagerAddr,
input, "CheckpointManager", deployerKey); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions command/bridge/fund/fund.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ func runCommand(cmd *cobra.Command, _ []string) {
return ctx.Err()

default:
fundAddr := ethgo.Address(params.addresses[i])
txn := helper.CreateTransaction(ethgo.ZeroAddress, &fundAddr, nil, params.amountValues[i], true)
fundAddr := params.addresses[i]
txn := helper.CreateTransaction(types.ZeroAddress, &fundAddr, nil, params.amountValues[i], true)

var (
receipt *ethgo.Receipt
Expand Down
Loading

0 comments on commit 4475fca

Please sign in to comment.