Skip to content

Commit b833b41

Browse files
authored
[PRODCRE-1746] Move core chain types into common keystore (#1830)
* Minor. * Minor. * Minor.
1 parent 41229b2 commit b833b41

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

keystore/corekeys/chaintype.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package corekeys
2+
3+
import (
4+
"fmt"
5+
"slices"
6+
"strings"
7+
)
8+
9+
// ChainType denotes the chain or network to work with
10+
type ChainType string
11+
12+
const (
13+
// EVM for Ethereum or other chains supporting the EVM
14+
EVM ChainType = "evm"
15+
// Cosmos for the Cosmos chain
16+
Cosmos ChainType = "cosmos"
17+
// Solana for the Solana chain
18+
Solana ChainType = "solana"
19+
// StarkNet for the StarkNet chain
20+
StarkNet ChainType = "starknet"
21+
// Aptos for the Aptos chain
22+
Aptos ChainType = "aptos"
23+
// Tron for the Tron chain
24+
Tron ChainType = "tron"
25+
// TON for the TON chain
26+
TON ChainType = "ton"
27+
// Sui for the Sui chain
28+
Sui ChainType = "sui"
29+
// Offchain is used by the MultichainKeyringAdapter when we are signing for offchain (eg. for DKG).
30+
Offchain ChainType = "offchain"
31+
)
32+
33+
type ChainTypes []ChainType
34+
35+
func (c ChainTypes) String() (out string) {
36+
var sb strings.Builder
37+
for i, chain := range c {
38+
if i != 0 {
39+
sb.WriteString(", ")
40+
}
41+
sb.WriteString(string(chain))
42+
}
43+
return sb.String()
44+
}
45+
46+
func NewChainType(typ uint8) (ChainType, error) {
47+
switch typ {
48+
case 1:
49+
return EVM, nil
50+
case 2:
51+
return Solana, nil
52+
case 3:
53+
return Cosmos, nil
54+
case 4:
55+
return StarkNet, nil
56+
case 5:
57+
return Aptos, nil
58+
case 6:
59+
return Tron, nil
60+
case 7:
61+
return TON, nil
62+
case 8:
63+
return Sui, nil
64+
case 9:
65+
return Offchain, nil
66+
default:
67+
return "", fmt.Errorf("unexpected chaintype.ChainType: %#v", typ)
68+
}
69+
}
70+
71+
func (c ChainType) Type() (uint8, error) {
72+
switch c {
73+
case EVM:
74+
return 1, nil
75+
case Solana:
76+
return 2, nil
77+
case Cosmos:
78+
return 3, nil
79+
case StarkNet:
80+
return 4, nil
81+
case Aptos:
82+
return 5, nil
83+
case Tron:
84+
return 6, nil
85+
case TON:
86+
return 7, nil
87+
case Sui:
88+
return 8, nil
89+
case Offchain:
90+
return 9, nil
91+
default:
92+
return 0, fmt.Errorf("unexpected chaintype.ChainType: %#v", c)
93+
}
94+
}
95+
96+
// SupportedChainTypes contain all chains that are supported
97+
var SupportedChainTypes = ChainTypes{EVM, Cosmos, Solana, StarkNet, Aptos, Tron, TON, Sui}
98+
99+
// ErrInvalidChainType is an error to indicate an unsupported chain type
100+
var ErrInvalidChainType error
101+
102+
func init() {
103+
supported := make([]string, 0, len(SupportedChainTypes))
104+
for _, chainType := range SupportedChainTypes {
105+
supported = append(supported, fmt.Sprintf(`"%s"`, chainType))
106+
}
107+
ErrInvalidChainType = fmt.Errorf("valid types include: [%s]", strings.Join(supported, ", "))
108+
}
109+
110+
// IsSupportedChainType checks to see if the chain is supported
111+
func IsSupportedChainType(chainType ChainType) bool {
112+
return slices.Contains(SupportedChainTypes, chainType)
113+
}
114+
115+
// NewErrInvalidChainType returns an error wrapping ErrInvalidChainType for an unsupported chain
116+
func NewErrInvalidChainType(chainType ChainType) error {
117+
return fmt.Errorf(`%w: unknown chain type "%s"`, ErrInvalidChainType, chainType)
118+
}

0 commit comments

Comments
 (0)