-
Notifications
You must be signed in to change notification settings - Fork 2
/
account.go
185 lines (145 loc) · 3.63 KB
/
account.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
package caip
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"io"
"regexp"
"strconv"
"strings"
"github.com/ethereum/go-ethereum/common"
)
type EVMAddressable interface {
Address() common.Address
}
type AccountID struct {
ChainID ChainID `json:"chain_id"`
Address string `json:"account_address"`
}
var (
accountRegex = regexp.MustCompile("[a-zA-Z0-9]{1,64}")
)
func NewAccountID(chainID ChainID, address string) (AccountID, error) {
aID := AccountID{chainID, address}
if err := aID.Validate(); err != nil {
return AccountID{}, err
}
return aID, nil
}
func UnsafeAccountID(chainID ChainID, address string) AccountID {
return AccountID{chainID, address}
}
func (c AccountID) Validate() error {
if err := c.ChainID.Validate(); err != nil {
return err
}
if ok := accountRegex.Match([]byte(c.Address)); !ok {
return errors.New("namespace does not match spec")
}
return nil
}
func (c AccountID) String() string {
if err := c.Validate(); err != nil {
panic(err)
}
return c.ChainID.String() + ":" + c.Address
}
func (c *AccountID) Parse(s string) error {
split := strings.SplitN(s, ":", 3)
if len(split) != 3 {
return fmt.Errorf("invalid account id: %s", s)
}
*c = AccountID{ChainID{split[0], split[1]}, split[2]}
if err := c.Validate(); err != nil {
return err
}
return nil
}
func (c *AccountID) ParseX(s string) {
if err := c.Parse(s); err != nil {
panic(err)
}
}
func (c *AccountID) UnmarshalJSON(data []byte) error {
type AccountIDAlias AccountID
ca := (*AccountIDAlias)(c)
if err := json.Unmarshal(data, &ca); err != nil {
return err
}
if err := c.Validate(); err != nil {
return err
}
return nil
}
func (c AccountID) MarshalJSON() ([]byte, error) {
if err := c.Validate(); err != nil {
return nil, err
}
type AccountIDAlias AccountID
ca := (AccountIDAlias)(c)
return json.Marshal(ca)
}
func (c AccountID) Value() (driver.Value, error) {
return c.String(), nil
}
func (c *AccountID) 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 := c.Parse(i.String); err != nil {
return err
}
return nil
}
func (c AccountID) MarshalGQL(w io.Writer) {
fmt.Fprint(w, strconv.Quote(strings.ToUpper(c.String())))
}
func (c *AccountID) UnmarshalGQL(v interface{}) error {
if id, ok := v.(string); ok {
if err := c.Parse(id); err != nil {
return fmt.Errorf("unmarshalling account id: %w", err)
}
}
return nil
}
type EVMAccountID struct {
EVMAddressable
AccountID
}
func NewEVMAccountID(chainID ChainID, address string) (EVMAccountID, error) {
aID := EVMAccountID{AccountID: AccountID{chainID, address}}
if err := aID.Validate(); err != nil {
return EVMAccountID{}, err
}
aID.AccountID.Address = common.HexToAddress(aID.AccountID.Address).Hex()
return aID, nil
}
func UnsafeEVMAccountID(chainID ChainID, address string) EVMAccountID {
aID := AccountID{chainID, common.HexToAddress(address).Hex()}
return EVMAccountID{AccountID: aID}
}
func (c *EVMAccountID) Parse(s string) error {
if err := c.AccountID.Parse(s); err != nil {
return err
}
c.AccountID.Address = common.HexToAddress(c.AccountID.Address).Hex()
return nil
}
func (a EVMAccountID) Validate() error {
if ok := common.IsHexAddress(a.AccountID.Address); !ok {
return fmt.Errorf("invalid eth address: %s", a.AccountID.Address)
}
if a.ChainID.Namespace != "eip155" {
return fmt.Errorf("invalid chain namespace: %s", a.ChainID.Namespace)
}
return a.AccountID.Validate()
}
func (a EVMAccountID) Address() common.Address {
return common.HexToAddress(a.AccountID.Address)
}