-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchain.go
More file actions
94 lines (79 loc) · 2.62 KB
/
chain.go
File metadata and controls
94 lines (79 loc) · 2.62 KB
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
package go_mhda
import (
"fmt"
"strconv"
"strings"
)
type ChainId string
// ChainKey - string identifier for declaration any chain or subchain
type ChainKey string
type Chain struct {
networkType NetworkType
coinType CoinType
chainId ChainId
}
func NewChain(networkType NetworkType, coinType CoinType, chainId ChainId) *Chain {
return &Chain{networkType: networkType, coinType: coinType, chainId: chainId}
}
// ChainFromKey parses a Chain from a key produced by Chain.Key().
func ChainFromKey(chainKey ChainKey) (*Chain, error) {
return ChainFromNSS(string(chainKey))
}
// ChainFromNSS parses just the chain-domain components ("nt", "ct", "ci")
// from the given NSS string. Other components are tolerated and ignored.
func ChainFromNSS(src string) (*Chain, error) {
components, err := parseNSS(src)
if err != nil {
return nil, err
}
if _, ok := components[compNetworkType]; !ok {
return nil, ErrMissingNetworkType
}
return parseChain(components)
}
// parseChain extracts and validates the chain-domain components from a
// component map produced by the NSS parser. Shared by parseAddress and
// ChainFromNSS.
func parseChain(m map[string]string) (*Chain, error) {
networkType := normalize(m[compNetworkType])
if networkType == `` {
return nil, ErrMissingNetworkType
}
if !NetworkType(networkType).IsValid() {
return nil, fmt.Errorf("%w: %q", ErrInvalidNetworkType, networkType)
}
ct := strings.TrimSpace(m[compCoinType])
if ct == `` {
return nil, ErrMissingCoinType
}
coinType, err := strconv.ParseUint(ct, 0, 32)
if err != nil {
return nil, fmt.Errorf("%w: %q", ErrInvalidCoinType, ct)
}
chainID, ok := m[compChainId]
if !ok || strings.TrimSpace(chainID) == "" {
return nil, ErrMissingChainID
}
return &Chain{
networkType: NetworkType(networkType),
coinType: CoinType(coinType),
chainId: ChainId(chainID),
}, nil
}
func (c *Chain) SetNetworkType(networkType NetworkType) { c.networkType = networkType }
func (c *Chain) SetCoinType(coinType CoinType) { c.coinType = coinType }
func (c *Chain) SetChainId(chainId ChainId) { c.chainId = chainId }
func (c *Chain) NetworkType() NetworkType { return c.networkType }
func (c *Chain) CoinType() CoinType { return c.coinType }
func (c *Chain) ChainId() ChainId { return c.chainId }
func (c *Chain) Key() ChainKey {
return ChainKey(c.String())
}
// String returns the canonical NSS-style chain key:
//
// nt:<network>:ct:<coin>:ci:<chainid>
//
// This format is round-trippable via ChainFromNSS / ChainFromKey.
func (c *Chain) String() string {
return fmt.Sprintf("nt:%s:ct:%d:ci:%s", c.networkType, c.coinType, c.chainId)
}