2022-12-16 09:48:38 +00:00
|
|
|
// Copyright 2021 Evmos Foundation
|
|
|
|
// This file is part of Evmos' Ethermint library.
|
|
|
|
//
|
|
|
|
// The Ethermint library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The Ethermint library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE
|
2021-01-06 20:56:40 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
2022-11-14 19:40:14 +00:00
|
|
|
errorsmod "cosmossdk.io/errors"
|
|
|
|
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
|
2021-09-03 18:06:36 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-01-06 20:56:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// IsEmptyHash returns true if the hash corresponds to an empty ethereum hex hash.
|
|
|
|
func IsEmptyHash(hash string) bool {
|
2021-09-03 18:06:36 +00:00
|
|
|
return bytes.Equal(common.HexToHash(hash).Bytes(), common.Hash{}.Bytes())
|
2021-01-06 20:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsZeroAddress returns true if the address corresponds to an empty ethereum hex address.
|
|
|
|
func IsZeroAddress(address string) bool {
|
2021-09-03 18:06:36 +00:00
|
|
|
return bytes.Equal(common.HexToAddress(address).Bytes(), common.Address{}.Bytes())
|
2021-01-06 20:56:40 +00:00
|
|
|
}
|
2021-05-14 06:52:18 +00:00
|
|
|
|
|
|
|
// ValidateAddress returns an error if the provided string is either not a hex formatted string address
|
|
|
|
func ValidateAddress(address string) error {
|
2021-09-03 18:06:36 +00:00
|
|
|
if !common.IsHexAddress(address) {
|
2022-11-14 19:40:14 +00:00
|
|
|
return errorsmod.Wrapf(
|
|
|
|
errortypes.ErrInvalidAddress, "address '%s' is not a valid ethereum hex address",
|
2021-05-14 06:52:18 +00:00
|
|
|
address,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-28 09:48:11 +00:00
|
|
|
|
2022-04-07 10:51:28 +00:00
|
|
|
// ValidateNonZeroAddress returns an error if the provided string is not a hex
|
|
|
|
// formatted string address or is equal to zero
|
|
|
|
func ValidateNonZeroAddress(address string) error {
|
|
|
|
if IsZeroAddress(address) {
|
2022-11-14 19:40:14 +00:00
|
|
|
return errorsmod.Wrapf(
|
|
|
|
errortypes.ErrInvalidAddress, "address '%s' must not be zero",
|
2022-04-07 10:51:28 +00:00
|
|
|
address,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return ValidateAddress(address)
|
|
|
|
}
|