This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
address.go
425 lines (371 loc) · 10.4 KB
/
address.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Copyright (c) 2020-2022 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package tezos
import (
"errors"
"fmt"
"io"
"strings"
"blockwatch.cc/tzgo/base58"
)
var (
// ErrChecksumMismatch describes an error where decoding failed due
// to a bad checksum.
ErrChecksumMismatch = errors.New("tezos: checksum mismatch")
// ErrUnknownAddressType describes an error where an address can not
// decoded as a specific address type due to the string encoding
// begining with an identifier byte unknown to any standard or
// registered (via Register) network.
ErrUnknownAddressType = errors.New("tezos: unknown address type")
// InvalidAddress is an empty invalid address
InvalidAddress = NewAddress(AddressTypeInvalid, nil)
// ZeroAddress is a tz1 address with all bytes zero
ZeroAddress = NewAddress(AddressTypeEd25519, make([]byte, HashTypePkhEd25519.Len))
ZeroContract = NewAddress(AddressTypeContract, make([]byte, HashTypePkhNocurve.Len))
// Burn Address
BurnAddress = MustParseAddress("tz1burnburnburnburnburnburnburjAYjjX")
)
const MAX_ADDRESS_LEN = 37 // tx rollup address
// AddressType represents the type of a Tezos signature.
type AddressType byte
// addressTypeData is an internal type used to store address related config
// options in a single place
type addressTypeData struct {
Id byte
Tag byte
Name string
HashType HashType
KeyType KeyType
}
const (
AddressTypeInvalid AddressType = iota // 0
AddressTypeEd25519 // 1
AddressTypeSecp256k1 // 2
AddressTypeP256 // 3
AddressTypeContract // 4
AddressTypeBlinded // 5
AddressTypeBls12_381 // 6
AddressTypeTxRollup // 7
AddressTypeSmartRollup // 8
)
var (
addressTypes = []addressTypeData{
{0, 255, "invalid", HashTypeInvalid, KeyTypeInvalid},
{1, 0, "ed25519", HashTypePkhEd25519, KeyTypeEd25519},
{2, 1, "secp256k1", HashTypePkhSecp256k1, KeyTypeSecp256k1},
{3, 2, "p256", HashTypePkhP256, KeyTypeP256},
{4, 255, "contract", HashTypePkhNocurve, KeyTypeInvalid},
{5, 3, "blinded", HashTypePkhBlinded, KeyTypeInvalid},
{6, 4, "bls12_381", HashTypePkhBls12_381, KeyTypeBls12_381},
{7, 255, "tx_rollup", HashTypeTxRollupAddress, KeyTypeInvalid},
{8, 255, "smart_rollup", HashTypeSmartRollupAddress, KeyTypeInvalid},
}
addressTags = []AddressType{
AddressTypeEd25519, // 0
AddressTypeSecp256k1, // 1
AddressTypeP256, // 2
AddressTypeBlinded, // 3
AddressTypeBls12_381, // 4
}
)
func ParseAddressType(s string) AddressType {
for _, v := range addressTypes {
if s == v.HashType.B58Prefix || s == v.Name {
return AddressType(v.Id)
}
}
return AddressTypeInvalid
}
func (t AddressType) IsValid() bool {
return t != AddressTypeInvalid
}
func (t AddressType) String() string {
return addressTypes[int(t)].Name
}
func (t AddressType) Prefix() string {
return addressTypes[int(t)].HashType.B58Prefix
}
func (t AddressType) Tag() byte {
return addressTypes[int(t)].Tag
}
func (t AddressType) HashType() HashType {
return addressTypes[int(t)].HashType
}
func (t AddressType) KeyType() KeyType {
return addressTypes[int(t)].KeyType
}
func (t AddressType) asByte() byte {
return byte(t)
}
func parseAddressTag(b byte) byte {
t := AddressTypeInvalid
if int(b) < len(addressTags) {
t = addressTags[int(b)]
}
return t.asByte()
}
func (t *AddressType) UnmarshalText(data []byte) error {
typ := ParseAddressType(string(data))
if !typ.IsValid() {
return ErrUnknownAddressType
}
*t = typ
return nil
}
func (t AddressType) MarshalText() ([]byte, error) {
return []byte(t.String()), nil
}
func HasAddressPrefix(s string) bool {
for _, typ := range addressTypes[1:] {
// ED25519_PUBLIC_KEY_HASH_PREFIX, // tz1
// SECP256K1_PUBLIC_KEY_HASH_PREFIX, // tz2
// P256_PUBLIC_KEY_HASH_PREFIX, // tz3
// NOCURVE_PUBLIC_KEY_HASH_PREFIX, // KT1
// BLINDED_PUBLIC_KEY_HASH_PREFIX, // btz1
// BLS12_381_PUBLIC_KEY_HASH_PREFIX, // tz4
// TX_ROLLUP_ADDRESS_PREFIX, // txr1
// SMART_ROLLUP_ADDRESS_PREFIX, // sr1
if strings.HasPrefix(s, typ.HashType.B58Prefix) {
return true
}
}
return false
}
func DetectAddressType(s string) AddressType {
for _, typ := range addressTypes[1:] {
if strings.HasPrefix(s, typ.HashType.B58Prefix) {
return AddressType(typ.Id)
}
}
return AddressTypeInvalid
}
// Address represents a typed tezos address
type Address [21]byte
func NewAddress(typ AddressType, hash []byte) (a Address) {
isValid := typ.HashType().Len == len(hash)
a[0] = typ.asByte() * byte(b2i(isValid))
copy(a[1:], hash)
return
}
func (a Address) Type() AddressType {
return AddressType(a[0])
}
func (a Address) Hash() []byte {
return a[1:]
}
func (a Address) KeyType() KeyType {
return AddressType(a[0]).KeyType()
}
func (a Address) IsValid() bool {
return a.Type() != AddressTypeInvalid
}
func (a Address) IsEOA() bool {
return a.Type().KeyType().IsValid()
}
func (a Address) IsContract() bool {
return a.Type() == AddressTypeContract
}
func (a Address) IsRollup() bool {
return a.Type() == AddressTypeSmartRollup || a.Type() == AddressTypeTxRollup
}
func (a Address) Equal(b Address) bool {
return a == b
}
func (a Address) Clone() (b Address) {
copy(b[:], a[:])
return
}
// String returns the string encoding of the address.
func (a Address) String() string {
return EncodeAddress(a.Type(), a[1:])
}
func (a *Address) UnmarshalText(data []byte) error {
if len(data) > MAX_ADDRESS_LEN {
data = data[:MAX_ADDRESS_LEN]
}
astr, _, _ := strings.Cut(string(data), "%")
addr, err := ParseAddress(astr)
if err != nil {
return err
}
*a = addr
return nil
}
func (a Address) MarshalText() ([]byte, error) {
return []byte(a.String()), nil
}
// Bytes returns the 21 (implicit) or 22 byte (contract) tagged and optionally padded
// binary hash value of the address.
// func (a Address) Bytes() []byte {
func (a Address) Encode() []byte {
var buf [22]byte
switch a.Type() {
case AddressTypeInvalid:
return nil
case AddressTypeContract:
buf[0] = 1
copy(buf[1:], a[1:])
case AddressTypeTxRollup:
buf[0] = 2
copy(buf[1:], a[1:])
case AddressTypeSmartRollup:
buf[0] = 3
copy(buf[1:], a[1:])
default:
// 21 byte version for implicit addresses
buf[0] = a.Type().Tag()
copy(buf[1:], a[1:])
return buf[:21]
}
return buf[:]
}
// Bytes22 returns the 22 byte tagged and padded binary encoding for contracts
// and EOAs (tz1/2/3). In contrast to Bytes which outputs the 21 byte address for EOAs
// here we add a leading 0-byte.
// func (a Address) Bytes22() []byte {
func (a Address) EncodePadded() []byte {
var buf [22]byte
switch a.Type() {
case AddressTypeInvalid:
return nil
case AddressTypeContract:
buf[0] = 1
copy(buf[1:], a[1:])
case AddressTypeTxRollup:
buf[0] = 2
copy(buf[1:], a[1:])
case AddressTypeSmartRollup:
buf[0] = 3
copy(buf[1:], a[1:])
default:
buf[1] = a.Type().Tag()
copy(buf[2:], a[1:])
}
return buf[:]
}
// MarshalBinary outputs the 21 byte TzGo version of an address containing
// a one byte type tag and the 20 byte address hash.
func (a Address) MarshalBinary() ([]byte, error) {
return a[:], nil
}
// UnmarshalBinary reads the 21 byte TzGo version of an address containing
// a one byte type tag and the 20 byte address hash.
func (a *Address) UnmarshalBinary(b []byte) error {
if len(b) != 21 {
return io.ErrShortBuffer
}
copy(a[:], b)
return nil
}
// Decode reads a 21 byte or 22 byte address versions and is
// resilient to longer byte strings that contain extra padding or a suffix
// (e.g. an entrypoint suffix as found in smart contract data).
func (a *Address) Decode(b []byte) error {
a[0] = 0
switch {
case len(b) >= 22 && b[0] <= 3:
switch b[0] {
case 0:
a[0] = parseAddressTag(b[1])
copy(a[1:], b[2:22])
case 1:
a[0] = AddressTypeContract.asByte()
copy(a[1:], b[1:21])
case 2:
a[0] = AddressTypeTxRollup.asByte()
copy(a[1:], b[1:21])
case 3:
a[0] = AddressTypeSmartRollup.asByte()
copy(a[1:], b[1:21])
default:
return fmt.Errorf("tezos: invalid binary address prefix %x", b[0])
}
case len(b) >= 21:
a[0] = parseAddressTag(b[0])
copy(a[1:], b[1:21])
default:
return fmt.Errorf("tezos: invalid binary address length %d", len(b))
}
if !a.IsValid() {
return ErrUnknownAddressType
}
return nil
}
// IsAddressBytes checks whether a buffer likely contains a binary encoded address.
func IsAddressBytes(b []byte) bool {
if len(b) < 21 {
return false
}
switch {
case len(b) == 22 && (b[0] <= 3):
return true
case len(b) == 21:
return parseAddressTag(b[0]) != AddressTypeInvalid.asByte()
default:
return false
}
}
// ContractAddress returns the string encoding of the address when used
// as originated contract.
func (a Address) ContractAddress() string {
return EncodeAddress(AddressTypeContract, a[1:])
}
// TxRollupAddress returns the string encoding of the address when used
// as rollup contract.
func (a Address) TxRollupAddress() string {
return EncodeAddress(AddressTypeTxRollup, a[1:])
}
// SmartRollupAddress returns the string encoding of the address when used
// as smart rollup contract.
func (a Address) SmartRollupAddress() string {
return EncodeAddress(AddressTypeSmartRollup, a[1:])
}
// Set implements the flags.Value interface for use in command line argument parsing.
func (a *Address) Set(addr string) (err error) {
*a, err = ParseAddress(addr)
return
}
func MustParseAddress(addr string) Address {
a, err := ParseAddress(addr)
if err != nil {
panic(err)
}
return a
}
func ParseAddress(addr string) (a Address, err error) {
// accept empty strings, but return an invalid address
if len(addr) == 0 {
return
}
if len(addr) > MAX_ADDRESS_LEN {
err = fmt.Errorf("tezos: invalid base58 address length")
return
}
typ := DetectAddressType(addr)
if !typ.IsValid() {
err = fmt.Errorf("tezos: unknown address type for %q", addr)
return
}
ht := typ.HashType()
ibuf := bufPool32.Get()
dec, _, err2 := base58.CheckDecode(addr, len(ht.Id), ibuf.([]byte))
if err2 != nil {
bufPool32.Put(ibuf)
if err2 == base58.ErrChecksum {
err = ErrChecksumMismatch
return
}
err = fmt.Errorf("tezos: invalid %s address: %w", typ, err2)
return
}
a[0] = typ.asByte()
copy(a[1:], dec)
bufPool32.Put(ibuf)
return
}
func EncodeAddress(typ AddressType, hash []byte) string {
if typ == AddressTypeInvalid {
return ""
}
return base58.CheckEncode(hash, typ.HashType().Id)
}