-
Notifications
You must be signed in to change notification settings - Fork 2
/
asset.go
196 lines (157 loc) · 4.08 KB
/
asset.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package caip
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"io"
"regexp"
"strconv"
"strings"
"github.com/ethereum/go-ethereum/common"
)
type AssetID struct {
ChainID ChainID `json:"chain_id"`
Namespace string `json:"asset_namespace"`
Reference string `json:"asset_reference"`
}
var (
assetNamespaceRegex = regexp.MustCompile("[-a-z0-9]{3,8}")
assetReferenceRegex = regexp.MustCompile("[-a-zA-Z0-9]{1,64}")
)
func NewAssetID(chainID ChainID, namespace, reference string) (AssetID, error) {
aID := AssetID{chainID, namespace, reference}
if err := aID.Validate(); err != nil {
return AssetID{}, err
}
return aID, nil
}
func UnsafeAssetID(chainID ChainID, namespace, reference string) AssetID {
return AssetID{chainID, namespace, reference}
}
func (a AssetID) Validate() error {
if ok := assetNamespaceRegex.Match([]byte(a.Namespace)); !ok {
return errors.New("asset namespace does not match spec")
}
if ok := assetReferenceRegex.Match([]byte(a.Reference)); !ok {
return errors.New("asset reference does not match spec")
}
return nil
}
func (a AssetID) String() string {
if err := a.Validate(); err != nil {
panic(err)
}
return a.ChainID.String() + "/" + a.Namespace + ":" + a.Reference
}
func (a *AssetID) Parse(s string) error {
components := strings.SplitN(s, "/", 2)
if len(components) != 2 {
return fmt.Errorf("invalid asset id: %s", s)
}
cID := new(ChainID)
if err := cID.Parse(components[0]); err != nil {
return err
}
asset := strings.SplitN(components[1], ":", 2)
if len(asset) != 2 {
return fmt.Errorf("invalid asset id: %s", s)
}
*a = AssetID{*cID, asset[0], asset[1]}
if err := a.Validate(); err != nil {
return err
}
return nil
}
func (a *AssetID) ParseX(s string) {
if err := a.Parse(s); err != nil {
panic(err)
}
}
func (a *AssetID) UnmarshalJSON(data []byte) error {
type AssetIDAlias AssetID
aa := (*AssetIDAlias)(a)
if err := json.Unmarshal(data, &aa); err != nil {
return err
}
if err := a.Validate(); err != nil {
return err
}
return nil
}
func (a AssetID) MarshalJSON() ([]byte, error) {
if err := a.Validate(); err != nil {
return nil, err
}
type AssetIDAlias AssetID
ca := (AssetIDAlias)(a)
return json.Marshal(ca)
}
func (a AssetID) Value() (driver.Value, error) {
return a.String(), nil
}
func (a *AssetID) Scan(src interface{}) error {
var i sql.NullString
if err := i.Scan(src); err != nil {
return fmt.Errorf("scanning account id: %w", err)
}
if !i.Valid {
return nil
}
if err := a.Parse(i.String); err != nil {
return err
}
return nil
}
func (a AssetID) MarshalGQL(w io.Writer) {
fmt.Fprint(w, strconv.Quote(strings.ToUpper(a.String())))
}
func (a *AssetID) UnmarshalGQL(v interface{}) error {
if id, ok := v.(string); ok {
if err := a.Parse(id); err != nil {
return fmt.Errorf("unmarshalling asset id: %w", err)
}
}
return nil
}
type EVMAssetID struct {
EVMAddressable
AssetID
}
func NewEVMAssetID(chainID ChainID, namespace, reference string) (EVMAssetID, error) {
aID := EVMAssetID{AssetID: AssetID{chainID, namespace, reference}}
if err := aID.Validate(); err != nil {
return EVMAssetID{}, err
}
aID.checksum()
return aID, nil
}
func UnsafeEVMAssetID(chainID ChainID, namespace, reference string) EVMAssetID {
aID := EVMAssetID{AssetID: AssetID{chainID, namespace, reference}}
aID.checksum()
return aID
}
func (a *EVMAssetID) checksum() {
split := strings.Split(a.Reference, "/")
// Make reference checksummed
split[0] = a.Address().Hex()
a.Reference = strings.Join(split, "/")
}
func (a EVMAssetID) Validate() error {
split := strings.Split(a.Reference, "/")
if ok := common.IsHexAddress(split[0]); !ok {
return fmt.Errorf("invalid eth address: %s", split[0])
}
if a.ChainID.Namespace != "eip155" {
return fmt.Errorf("invalid chain namespace: %s", a.ChainID.Namespace)
}
return a.AssetID.Validate()
}
func (a EVMAssetID) Address() common.Address {
split := strings.Split(a.Reference, "/")
return common.HexToAddress(split[0])
}
func (a EVMAssetID) AccountID() EVMAccountID {
return EVMAccountID{AccountID: AccountID{a.ChainID, a.Reference}}
}