Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce zero address check to IsValidAddress #56

Merged
merged 8 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion command/bridge/finalize/finalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func runCommand(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("enlist validator failed: %w", err)
}

bladeManagerAddr := ethgo.Address(types.StringToAddress(params.bladeManager))
bladeManagerAddr := ethgo.Address(params.bladeManagerAddr)

// finalize genesis accounts on BladeManager so that no one can stake and premine no more
encoded, err := finalizeGenesisABIFn.Encode([]interface{}{})
Expand Down
14 changes: 7 additions & 7 deletions command/bridge/finalize/params.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package finalize

import (
"errors"
"fmt"
"os"

Expand All @@ -17,27 +16,28 @@ type finalizeParams struct {
jsonRPC string
bladeManager string
genesisPath string

bladeManagerAddr types.Address
}

func (fp *finalizeParams) validateFlags() error {
var err error

if fp.privateKey == "" {
return validatorHelper.ValidateSecretFlags(fp.accountDir, fp.accountConfig)
}

if err := types.IsValidAddress(fp.bladeManager); err != nil {
fp.bladeManagerAddr, err = types.IsValidAddress(fp.bladeManager, false)
if err != nil {
return fmt.Errorf("invalid blade manager address is provided: %w", err)
}

if types.StringToAddress(fp.bladeManager) == types.ZeroAddress {
return errors.New("blade manager must be non-zero address")
}

if _, err := os.Stat(fp.genesisPath); err != nil {
return fmt.Errorf("provided genesis path '%s' is invalid. Error: %w ", fp.genesisPath, err)
}

// validate jsonrpc address
_, err := helper.ParseJSONRPCAddress(fp.jsonRPC)
_, err = helper.ParseJSONRPCAddress(fp.jsonRPC)

return err
}
11 changes: 5 additions & 6 deletions command/bridge/fund/fund.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func GetCommand() *cobra.Command {

func setFlags(cmd *cobra.Command) {
cmd.Flags().StringSliceVar(
&params.addresses,
&params.rawAddresses,
helper.AddressesFlag,
nil,
"validator addresses",
Expand Down Expand Up @@ -96,8 +96,7 @@ func runCommand(cmd *cobra.Command, _ []string) {
return ctx.Err()

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

var (
Expand All @@ -112,15 +111,15 @@ func runCommand(cmd *cobra.Command, _ []string) {
}

if err != nil {
return fmt.Errorf("failed to send fund validator '%s' transaction: %w", validatorAddr, err)
return fmt.Errorf("failed to send fund validator '%s' transaction: %w", fundAddr, err)
}

if receipt.Status == uint64(types.ReceiptFailed) {
return fmt.Errorf("failed to fund validator '%s'", validatorAddr)
return fmt.Errorf("failed to fund validator '%s'", fundAddr)
}

results[i] = &result{
ValidatorAddr: validatorAddr,
ValidatorAddr: types.Address(fundAddr),
TxHash: types.Hash(receipt.TransactionHash),
}
}
Expand Down
15 changes: 10 additions & 5 deletions command/bridge/fund/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,32 @@ var (
)

type fundParams struct {
addresses []string
rawAddresses []string
amounts []string
deployerPrivateKey string
jsonRPCAddress string

amountValues []*big.Int
addresses []types.Address
}

func (fp *fundParams) validateFlags() error {
if len(fp.addresses) == 0 {
if len(fp.rawAddresses) == 0 {
return bridgeHelper.ErrNoAddressesProvided
}

if len(fp.amounts) != len(fp.addresses) {
if len(fp.amounts) != len(fp.rawAddresses) {
return bridgeHelper.ErrInconsistentLength
}

for _, addr := range fp.addresses {
if err := types.IsValidAddress(addr); err != nil {
fp.addresses = make([]types.Address, 0, len(fp.rawAddresses))
for _, rawAddr := range fp.rawAddresses {
addr, err := types.IsValidAddress(rawAddr, true)
if err != nil {
return err
}

fp.addresses = append(fp.addresses, addr)
}

fp.amountValues = make([]*big.Int, len(fp.amounts))
Expand Down
10 changes: 5 additions & 5 deletions command/bridge/fund/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func Test_validateFlags(t *testing.T) {
// inconsistent length (addresses vs amounts)
buildParamsFn: func() *fundParams {
return &fundParams{
addresses: []string{"0x1", "0x2"},
amounts: []string{"10"},
rawAddresses: []string{"0x1", "0x2"},
amounts: []string{"10"},
}
},
err: errInconsistentLength.Error(),
Expand All @@ -37,8 +37,8 @@ func Test_validateFlags(t *testing.T) {
// address contains invalid characters
buildParamsFn: func() *fundParams {
return &fundParams{
addresses: []string{"0x10", "0x20"},
amounts: []string{"10", "20"},
rawAddresses: []string{"0x10", "0x20"},
amounts: []string{"10", "20"},
}
},
err: "address \x10 has invalid length",
Expand All @@ -47,7 +47,7 @@ func Test_validateFlags(t *testing.T) {
// valid scenario
buildParamsFn: func() *fundParams {
return &fundParams{
addresses: []string{
rawAddresses: []string{
types.StringToAddress("0x10").String(),
types.StringToAddress("0x20").String()},
amounts: []string{"10", "20"},
Expand Down
53 changes: 19 additions & 34 deletions command/bridge/premine/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package premine

import (
"bytes"
"errors"
"fmt"
"math/big"

Expand All @@ -13,51 +12,37 @@ import (
)

const (
premineAmountFlag = "premine-amount"
stakedAmountFlag = "stake-amount"
rootERC20PredicateFlag = "root-erc20-predicate"
premineAmountFlag = "premine-amount"
stakedAmountFlag = "stake-amount"
)

type premineParams struct {
accountDir string
accountConfig string
privateKey string
bladeManager string
rootERC20Predicate string
nativeTokenRoot string
jsonRPC string
stakedAmount string
premineAmount string

nonStakedValue *big.Int
stakedValue *big.Int
accountDir string
accountConfig string
privateKey string
bladeManager string
nativeTokenRoot string
jsonRPC string
stakedAmount string
premineAmount string

nonStakedValue *big.Int
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
nonStakedValue *big.Int
premineAmoountValue *big.Int

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stakedValue *big.Int
nativeTokenRootAddr types.Address
bladeManagerAddr types.Address
}

func (p *premineParams) validateFlags() (err error) {
if err := types.IsValidAddress(p.nativeTokenRoot); err != nil {
p.nativeTokenRootAddr, err = types.IsValidAddress(p.nativeTokenRoot, false)
if err != nil {
return fmt.Errorf("invalid erc20 token address is provided: %w", err)
}

if types.StringToAddress(p.nativeTokenRoot) == types.ZeroAddress {
return errors.New("native erc20 token address must be non-zero")
}

if err := types.IsValidAddress(p.rootERC20Predicate); err != nil {
return fmt.Errorf("invalid root erc20 predicate address is provided: %w", err)
}

if types.StringToAddress(p.rootERC20Predicate) == types.ZeroAddress {
return errors.New("root erc20 predicate address must be non-zero")
}

if err := types.IsValidAddress(p.bladeManager); err != nil {
p.bladeManagerAddr, err = types.IsValidAddress(p.bladeManager, false)
if err != nil {
return fmt.Errorf("invalid blade manager address is provided: %w", err)
}

if types.StringToAddress(p.bladeManager) == types.ZeroAddress {
return errors.New("blade manager address must be non-zero")
}

if p.nonStakedValue, err = common.ParseUint256orHex(&p.premineAmount); err != nil {
return err
}
Expand Down
16 changes: 3 additions & 13 deletions command/bridge/premine/premine.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,6 @@ func setFlags(cmd *cobra.Command) {
"address of root erc20 native token",
)

cmd.Flags().StringVar(
&params.rootERC20Predicate,
rootERC20PredicateFlag,
"",
"address of root erc20 predicate",
)

cmd.Flags().StringVar(
&params.bladeManager,
bridgeHelper.BladeManagerFlag,
Expand All @@ -96,7 +89,6 @@ func setFlags(cmd *cobra.Command) {
cmd.MarkFlagsMutuallyExclusive(polybftsecrets.PrivateKeyFlag, polybftsecrets.AccountConfigFlag)
cmd.MarkFlagsMutuallyExclusive(polybftsecrets.PrivateKeyFlag, polybftsecrets.AccountDirFlag)
_ = cmd.MarkFlagRequired(bridgeHelper.BladeManagerFlag)
_ = cmd.MarkFlagRequired(rootERC20PredicateFlag)
_ = cmd.MarkFlagRequired(bridgeHelper.Erc20TokenFlag)
}

Expand All @@ -123,8 +115,8 @@ func runCommand(cmd *cobra.Command, _ []string) error {

approveTxn, err := bridgeHelper.CreateApproveERC20Txn(
new(big.Int).Add(params.nonStakedValue, params.stakedValue),
types.StringToAddress(params.bladeManager),
types.StringToAddress(params.nativeTokenRoot), true)
params.bladeManagerAddr,
params.nativeTokenRootAddr, true)
if err != nil {
return err
}
Expand All @@ -149,9 +141,7 @@ func runCommand(cmd *cobra.Command, _ []string) error {
}

bladeManagerAddr := ethgo.Address(types.StringToAddress(params.bladeManager))
txn := bridgeHelper.CreateTransaction(ownerKey.Address(), &bladeManagerAddr, premineInput, nil, true)
txn.Gas = types.StateTransactionGasLimit * 2
txn.Type = ethgo.TransactionLegacy
txn := bridgeHelper.CreateTransaction(ownerKey.Address(), &bladeManagerAddr, premineInput, nil, false)

receipt, err = txRelayer.SendTransaction(txn, ownerKey)
if err != nil {
Expand Down
11 changes: 4 additions & 7 deletions command/genesis/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,11 @@ func (p *genesisParams) validateFlags() error {
return err
}

if err := types.IsValidAddress(params.stakeToken); err != nil {
return fmt.Errorf("stake token address is not a valid address: %w", err)
}

params.stakeTokenAddr = types.StringToAddress(params.stakeToken)
var err error

if params.stakeTokenAddr == types.ZeroAddress {
return errStakeTokenIsZeroAddress
p.stakeTokenAddr, err = types.IsValidAddress(params.stakeToken, false)
if err != nil {
return fmt.Errorf("stake token address is not a valid address: %w", err)
}
}

Expand Down
16 changes: 4 additions & 12 deletions command/genesis/polybft_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,15 +659,11 @@ func (p *genesisParams) validateProxyContractsAdmin() error {
return errProxyAdminNotProvided
}

if err := types.IsValidAddress(p.proxyContractsAdmin); err != nil {
proxyContractsAdminAddr, err := types.IsValidAddress(p.proxyContractsAdmin, false)
if err != nil {
return fmt.Errorf("proxy contracts admin address is not a valid address: %w", err)
}

proxyContractsAdminAddr := types.StringToAddress(p.proxyContractsAdmin)
if proxyContractsAdminAddr == types.ZeroAddress {
return errProxyAdminIsZeroAddress
}

if proxyContractsAdminAddr == contracts.SystemCaller {
return errProxyAdminIsSystemCaller
}
Expand All @@ -680,15 +676,11 @@ func (p *genesisParams) validateBladeAdminFlag() error {
return errBladeAdminNotProvided
}

if err := types.IsValidAddress(p.bladeAdmin); err != nil {
bladeAdminAddr, err := types.IsValidAddress(p.bladeAdmin, false)
if err != nil {
return fmt.Errorf("blade admin address is not a valid address: %w", err)
}

bladeAdminAddr := types.StringToAddress(p.proxyContractsAdmin)
if bladeAdminAddr == types.ZeroAddress {
return errBladeAdminIsZeroAddress
}

if bladeAdminAddr == contracts.SystemCaller {
return errBladeAdminIsSystemCaller
}
Expand Down
22 changes: 13 additions & 9 deletions command/genesis/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,32 @@ func parseBurnContractInfo(burnContractInfoRaw string) (*polybft.BurnContractInf
return nil, fmt.Errorf("failed to parse block number %s: %w", blockRaw, err)
}

contractAddress := burnContractParts[1]
if err = types.IsValidAddress(contractAddress); err != nil {
return nil, fmt.Errorf("failed to parse contract address %s: %w", contractAddress, err)
contractAddrRaw := burnContractParts[1]

contractAddr, err := types.IsValidAddress(contractAddrRaw, true)
if err != nil {
return nil, fmt.Errorf("failed to parse contract address %s: %w", contractAddrRaw, err)
}

if len(burnContractParts) == 2 {
return &polybft.BurnContractInfo{
BlockNumber: blockNum,
Address: types.StringToAddress(contractAddress),
Address: contractAddr,
DestinationAddress: types.ZeroAddress,
}, nil
}

destinationAddress := burnContractParts[2]
if err = types.IsValidAddress(destinationAddress); err != nil {
return nil, fmt.Errorf("failed to parse burn destination address %s: %w", destinationAddress, err)
destinationAddrRaw := burnContractParts[2]

destinationAddr, err := types.IsValidAddress(destinationAddrRaw, true)
if err != nil {
return nil, fmt.Errorf("failed to parse burn destination address %s: %w", destinationAddrRaw, err)
}

return &polybft.BurnContractInfo{
BlockNumber: blockNum,
Address: types.StringToAddress(contractAddress),
DestinationAddress: types.StringToAddress(destinationAddress),
Address: contractAddr,
DestinationAddress: destinationAddr,
}, nil
}

Expand Down
7 changes: 1 addition & 6 deletions command/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,10 @@ func ParseAmount(amount string) (*big.Int, error) {
}

func ValidateProxyContractsAdmin(proxyContractsAdmin string) error {
if err := types.IsValidAddress(proxyContractsAdmin); err != nil {
if _, err := types.IsValidAddress(proxyContractsAdmin, false); err != nil {
return fmt.Errorf("proxy contracts admin address is not valid: %w", err)
}

proxyContractsAdminAddr := types.StringToAddress(proxyContractsAdmin)
if proxyContractsAdminAddr == types.ZeroAddress {
return errors.New("proxy contracts admin address must not be zero address")
}

return nil
}

Expand Down
Loading