-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtypes.go
89 lines (73 loc) · 1.78 KB
/
types.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
package gotez
//go:generate go run generate.go
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"time"
"github.com/ecadlabs/gotez/v2/encoding"
)
const (
ProofOfWorkNonceBytesLen = 8
ChainIdBytesLen = 4
SecretBytesLen = 20
AddressBytesLen = 20
ContractHashBytesLen = 20
SeedNonceBytesLen = 32
CycleNonceBytesLen = 32
HashBytesLen = 32
SlotHeaderBytesLen = 48
GenericSignatureBytesLen = 64
BLSSignatureBytesLen = 96
)
type Base58Encoder interface {
ToBase58() []byte
String() string
}
type Comparable[K any] interface {
comparable
ToKey() K
}
type ToComparable[H Comparable[K], K any] interface {
ToComparable() H
}
type String string
func (str *String) DecodeTZ(data []byte, ctx *encoding.Context) ([]byte, error) {
if len(data) < 1 {
return nil, fmt.Errorf("(string) %w", encoding.ErrBuffer{1, len(data)})
}
length := int(data[0])
if len(data) < 1+length {
return nil, fmt.Errorf("(string) %w", encoding.ErrBuffer{1 + length, len(data)})
}
*str = String(data[1 : length+1])
return data[length+1:], nil
}
func (str String) EncodeTZ(ctx *encoding.Context) ([]byte, error) {
var buf bytes.Buffer
if len(str) > 255 {
return nil, errors.New("string is too long")
}
buf.WriteByte(byte(len(str)))
buf.Write([]byte(str))
return buf.Bytes(), nil
}
type Timestamp int64
func (t Timestamp) Time() time.Time {
return time.Unix(int64(t), 0).UTC()
}
func (t Timestamp) String() string {
return t.Time().String()
}
type Bytes []byte
func (b Bytes) MarshalText() (text []byte, err error) {
dst := make([]byte, hex.EncodedLen(len(b)))
hex.Encode(dst, b)
return dst, nil
}
func (b *Bytes) UnmarshalText(text []byte) error {
*b = make(Bytes, hex.DecodedLen(len(text)))
_, err := hex.Decode(*b, text)
return err
}