-
Notifications
You must be signed in to change notification settings - Fork 671
/
addresses.go
139 lines (118 loc) · 3.88 KB
/
addresses.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package avax
import (
"fmt"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/formatting/address"
"github.com/ava-labs/avalanchego/utils/set"
)
var _ AddressManager = (*addressManager)(nil)
type AddressManager interface {
// ParseLocalAddress takes in an address for this chain and produces the ID
ParseLocalAddress(addrStr string) (ids.ShortID, error)
// ParseAddress takes in an address and produces the ID of the chain it's
// for and the ID of the address
ParseAddress(addrStr string) (ids.ID, ids.ShortID, error)
// FormatLocalAddress takes in a raw address and produces the formatted
// address for this chain
FormatLocalAddress(addr ids.ShortID) (string, error)
// FormatAddress takes in a chainID and a raw address and produces the
// formatted address for that chain
FormatAddress(chainID ids.ID, addr ids.ShortID) (string, error)
}
type addressManager struct {
ctx *snow.Context
}
func NewAddressManager(ctx *snow.Context) AddressManager {
return &addressManager{
ctx: ctx,
}
}
func (a *addressManager) ParseLocalAddress(addrStr string) (ids.ShortID, error) {
chainID, addr, err := a.ParseAddress(addrStr)
if err != nil {
return ids.ShortID{}, err
}
if chainID != a.ctx.ChainID {
return ids.ShortID{}, fmt.Errorf(
"expected chainID to be %q but was %q",
a.ctx.ChainID,
chainID,
)
}
return addr, nil
}
func (a *addressManager) ParseAddress(addrStr string) (ids.ID, ids.ShortID, error) {
chainIDAlias, hrp, addrBytes, err := address.Parse(addrStr)
if err != nil {
return ids.ID{}, ids.ShortID{}, err
}
chainID, err := a.ctx.BCLookup.Lookup(chainIDAlias)
if err != nil {
return ids.ID{}, ids.ShortID{}, err
}
expectedHRP := constants.GetHRP(a.ctx.NetworkID)
if hrp != expectedHRP {
return ids.ID{}, ids.ShortID{}, fmt.Errorf(
"expected hrp %q but got %q",
expectedHRP,
hrp,
)
}
addr, err := ids.ToShortID(addrBytes)
if err != nil {
return ids.ID{}, ids.ShortID{}, err
}
return chainID, addr, nil
}
func (a *addressManager) FormatLocalAddress(addr ids.ShortID) (string, error) {
return a.FormatAddress(a.ctx.ChainID, addr)
}
func (a *addressManager) FormatAddress(chainID ids.ID, addr ids.ShortID) (string, error) {
chainIDAlias, err := a.ctx.BCLookup.PrimaryAlias(chainID)
if err != nil {
return "", err
}
hrp := constants.GetHRP(a.ctx.NetworkID)
return address.Format(chainIDAlias, hrp, addr.Bytes())
}
func ParseLocalAddresses(a AddressManager, addrStrs []string) (set.Set[ids.ShortID], error) {
addrs := make(set.Set[ids.ShortID], len(addrStrs))
for _, addrStr := range addrStrs {
addr, err := a.ParseLocalAddress(addrStr)
if err != nil {
return nil, fmt.Errorf("couldn't parse address %q: %w", addrStr, err)
}
addrs.Add(addr)
}
return addrs, nil
}
// ParseServiceAddress get address ID from address string, being it either localized (using address manager,
// doing also components validations), or not localized.
// If both attempts fail, reports error from localized address parsing
func ParseServiceAddress(a AddressManager, addrStr string) (ids.ShortID, error) {
addr, err := ids.ShortFromString(addrStr)
if err == nil {
return addr, nil
}
addr, err = a.ParseLocalAddress(addrStr)
if err != nil {
return addr, fmt.Errorf("couldn't parse address %q: %w", addrStr, err)
}
return addr, nil
}
// ParseServiceAddress get addresses IDs from addresses strings, being them either localized or not
func ParseServiceAddresses(a AddressManager, addrStrs []string) (set.Set[ids.ShortID], error) {
addrs := set.NewSet[ids.ShortID](len(addrStrs))
for _, addrStr := range addrStrs {
addr, err := ParseServiceAddress(a, addrStr)
if err != nil {
return nil, err
}
addrs.Add(addr)
}
return addrs, nil
}