-
Notifications
You must be signed in to change notification settings - Fork 3
/
wallet_record.go
127 lines (113 loc) · 3.58 KB
/
wallet_record.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
package types
import (
fmt "fmt"
"regexp"
"filippo.io/edwards25519"
"github.com/btcsuite/btcutil/base58"
errorsmod "cosmossdk.io/errors"
)
func WalletRecordFormats() map[string]string {
return map[string]string{
// Bitcoin
"BITCOIN_MAINNET_MAINNET": "BITCOIN",
"BITCOIN_TESTNET_TESTNET": "BITCOIN",
// DEFAULT
"BITCOIN_DEFAULT_DEFAULT": "BITCOIN",
// EVM
"ETHEREUM_MAINNET_MAINNET": "ETHEREUM",
"ETHEREUM_TESTNET_GOERLI": "ETHEREUM",
"ETHEREUM_TESTNET_SEPOLIA": "ETHEREUM",
// Polygon
"POLYGON_MAINNET_MAINNET": "ETHEREUM",
"POLYGON_TESTNET_MUMBAI": "ETHEREUM",
// BNB
"BNB_MAINNET_MAINNET": "ETHEREUM",
"BNB_TESTNET_TESTNET": "ETHEREUM",
// Avalanche
"AVALANCHE_MAINNET_CCHAIN": "ETHEREUM",
"AVALANCHE_TESTNET_FUJI": "ETHEREUM",
// Gnosis
"GNOSIS_MAINNET_MAINNET": "ETHEREUM",
"GNOSIS_TESTNET_CHIADO": "ETHEREUM",
// Optimism
"OPTIMISM_MAINNET_MAINNET": "ETHEREUM",
"OPTIMISM_TESTNET_GOERLI": "ETHEREUM",
// Arbitrum
"ARBITRUM_MAINNET_MAINNET": "ETHEREUM",
"ARBITRUM_TESTNET_GOERLI": "ETHEREUM",
// Shardeum
"SHARDEUM_BETANET_SPHINX": "ETHEREUM",
// ZetaChain
"ZETA_TESTNET_ATHENS": "ETHEREUM",
// DEFAULT
"EVM_DEFAULT_DEFAULT": "ETHEREUM",
// Move
"APTOS_MAINNET_MAINNET": "MOVE",
"APTOS_TESTNET_TESTNET": "MOVE",
"SUI_MAINNET_MAINNET": "MOVE",
"SUI_TESTNET_TESTNET": "MOVE",
// DEFAULT
"MOVE_DEFAULT_DEFAULT": "MOVE",
// Solana
"SOLANA_MAINNET_MAINNET": "SOLANA",
"SOLANA_TESTNET_TESTNET": "SOLANA",
// DEFAULT
"SOLANA_DEFAULT_DEFAULT": "SOLANA",
}
}
func WalletAddressRegex() map[string]string {
return map[string]string{
"BITCOIN": "^(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,39}$",
"ETHEREUM": "^0x[a-fA-F0-9]{40}$",
"MOVE": "^0x[a-fA-F0-9]{64}$",
}
}
func ValidateEd25519PublicKey(walletAddressFormat string, address string) (err error) {
decodedBytes := base58.Decode(address)
_, err = new(edwards25519.Point).SetBytes(decodedBytes)
if err != nil {
err = errorsmod.Wrapf(ErrInvalidWalletAddress, "%s %s", walletAddressFormat, address)
}
return err
}
func ValidateWalletAddressWithRegex(walletAddressFormat string, address string) (err error) {
walletAddressRegex, isFound := WalletAddressRegex()[walletAddressFormat]
if !isFound {
panic(fmt.Sprintf("Wallet address format %s is not found in WalletAddressRegex", walletAddressFormat))
}
regex := regexp.MustCompile(walletAddressRegex)
if !regex.MatchString(address) {
err = errorsmod.Wrapf(ErrInvalidWalletAddress, "%s %s", walletAddressFormat, address)
}
return err
}
func ValidateWalletAddress(walletAddressFormat string, address string) (err error) {
switch walletAddressFormat {
case "BITCOIN", "ETHEREUM", "MOVE":
err = ValidateWalletAddressWithRegex(walletAddressFormat, address)
case "SOLANA":
err = ValidateEd25519PublicKey(walletAddressFormat, address)
default:
panic(fmt.Sprintf("Wallet address format %s is not found in WalletAddressRegex", walletAddressFormat))
}
return err
}
// GetDefaultRecordType(walletRecordType) returns the default wallet address for a given wallet record type.
// This is used when a wallet record is not found in the domain record.
func GetDefaultWalletRecordType(walletRecordType string) string {
wrf := WalletRecordFormats()
network := wrf[walletRecordType]
switch network {
case "BITCOIN":
return "BITCOIN_DEFAULT_DEFAULT"
case "ETHEREUM":
return "EVM_DEFAULT_DEFAULT"
case "MOVE":
return "MOVE_DEFAULT_DEFAULT"
case "SOLANA":
return "SOLANA_DEFAULT_DEFAULT"
default:
// This should never happen
panic(fmt.Sprintf("Wallet record type %s is not found in WalletRecordFormats", walletRecordType))
}
}