-
Notifications
You must be signed in to change notification settings - Fork 127
/
utils.go
145 lines (120 loc) · 3.84 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package utils
import (
"encoding/hex"
"fmt"
"log"
"math/big"
"strings"
errorsmod "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/crypto/types/multisig"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
"golang.org/x/crypto/sha3"
"github.com/ethereum/go-ethereum/common"
"github.com/haqq-network/haqq/crypto/ethsecp256k1"
)
const (
MainNetChainID = "haqq_11235"
TestEdge1ChainID = "haqq_53211"
TestEdge2ChainID = "haqq_54211"
LocalNetChainID = "haqq_121799"
// BaseDenom defines the Haqq Network mainnet denomination
BaseDenom = "aISLM"
)
func IsMainNetwork(chainID string) bool {
return strings.HasPrefix(chainID, MainNetChainID)
}
func IsTestEdge1Network(chainID string) bool {
return strings.HasPrefix(chainID, TestEdge1ChainID)
}
func IsTestEdge2Network(chainID string) bool {
return strings.HasPrefix(chainID, TestEdge2ChainID)
}
func IsLocalNetwork(chainID string) bool {
return strings.HasPrefix(chainID, LocalNetChainID)
}
// IsSupportedKey returns true if the pubkey type is supported by the chain
// (i.e eth_secp256k1, amino multisig, ed25519).
// NOTE: Nested multisigs are not supported.
func IsSupportedKey(pubkey cryptotypes.PubKey) bool {
switch pubkey := pubkey.(type) {
case *ethsecp256k1.PubKey, *ed25519.PubKey:
return true
case multisig.PubKey:
if len(pubkey.GetPubKeys()) == 0 {
return false
}
for _, pk := range pubkey.GetPubKeys() {
switch pk.(type) {
case *ethsecp256k1.PubKey, *ed25519.PubKey:
continue
default:
// Nested multisigs are unsupported
return false
}
}
return true
default:
return false
}
}
// GetHaqqAddressFromBech32 returns the sdk.Account address of given address,
// while also changing bech32 human readable prefix (HRP) to the value set on
// the global sdk.Config (eg: `haqq`).
// The function fails if the provided bech32 address is invalid.
func GetHaqqAddressFromBech32(address string) (sdk.AccAddress, error) {
bech32Prefix := strings.SplitN(address, "1", 2)[0]
if bech32Prefix == address {
return nil, errorsmod.Wrapf(errortypes.ErrInvalidAddress, "invalid bech32 address: %s", address)
}
addressBz, err := sdk.GetFromBech32(address, bech32Prefix)
if err != nil {
return nil, errorsmod.Wrapf(errortypes.ErrInvalidAddress, "invalid address %s, %s", address, err.Error())
}
// safety check: shouldn't happen
if err := sdk.VerifyAddressFormat(addressBz); err != nil {
return nil, err
}
return sdk.AccAddress(addressBz), nil
}
func GetAccAddrFromEthAddress(addrString string) sdk.AccAddress {
addr := common.HexToAddress(addrString).Bytes()
return sdk.AccAddress(addr)
}
// parseHexValue -> parses a hex string into a big.Int
func ParseHexValue(hexStr string) *big.Int {
hexStr = Remove0xPrefix(hexStr)
value := new(big.Int)
if _, ok := value.SetString(hexStr, 16); !ok {
log.Fatalf("Failed to parse hex string: %s", hexStr)
}
return value
}
// remove0xPrefix -> removes the 0x prefix from a hex string
func Remove0xPrefix(s string) string {
if len(s) > 1 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X') {
return s[2:]
}
return s
}
// keccak256 -> calculates the keccak256 hash of a byte slice
func Keccak256(data []byte) []byte {
hash := sha3.NewLegacyKeccak256()
hash.Write(data)
return hash.Sum(nil)
}
// calculateStorageKey -> calculates the storage key for a given address and index
func CalculateStorageKey(addr string, i int) string {
pos := fmt.Sprintf("%064x", i)
key := strings.ToLower(Remove0xPrefix(addr))
keyPadded := fmt.Sprintf("%064s", key)
combined := keyPadded + pos
combinedBytes, err := hex.DecodeString(combined)
if err != nil {
log.Fatalf("Failed to decode hex string: %v", err)
}
storageKey := Keccak256(combinedBytes)
return "0x" + hex.EncodeToString(storageKey)
}