-
Notifications
You must be signed in to change notification settings - Fork 1
/
operation_list.go
90 lines (74 loc) · 2.71 KB
/
operation_list.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
package core
import (
tz "github.com/ecadlabs/gotez/v2"
"github.com/ecadlabs/gotez/v2/encoding"
)
type GroupContents interface {
GetSignature() (tz.Option[tz.Signature], error)
GroupContents()
Operations() []OperationContents
}
type OperationWithOptionalMetadataContents interface {
GetSignature() (tz.Option[tz.Signature], error)
OperationWithOptionalMetadataContents()
Operations() []OperationContents
}
type OperationsList[T GroupContents] struct {
Operations []*OperationsGroupImpl[T] `tz:"dyn,dyn" json:"operations"` // yes, twice
}
func (l *OperationsList[T]) GetGroups() []OperationsGroup {
out := make([]OperationsGroup, len(l.Operations))
for i, grp := range l.Operations {
out[i] = grp
}
return out
}
type OperationsGroup interface {
GetChainID() *tz.ChainID
GetHash() *tz.OperationHash
GetBranch() *tz.BlockHash
GetContents() GroupContents
}
type OperationsGroupImpl[T GroupContents] struct {
ChainID *tz.ChainID `json:"chain_id"`
Hash *tz.OperationHash `json:"hash"`
Branch *tz.BlockHash `tz:"dyn" json:"branch"`
Contents T `tz:"dyn" json:"contents"`
}
func (g *OperationsGroupImpl[T]) GetChainID() *tz.ChainID { return g.ChainID }
func (g *OperationsGroupImpl[T]) GetHash() *tz.OperationHash { return g.Hash }
func (g *OperationsGroupImpl[T]) GetBranch() *tz.BlockHash { return g.Branch }
func (g *OperationsGroupImpl[T]) GetContents() GroupContents { return g.Contents }
type OperationWithoutMetadata[T OperationContents] struct {
Contents []T `json:"contents"`
Signature *tz.GenericSignature `json:"signature"`
}
func (op *OperationWithoutMetadata[T]) DecodeTZ(data []byte, ctx *encoding.Context) (rest []byte, err error) {
if len(data) < tz.GenericSignatureBytesLen {
return nil, encoding.ErrBuffer(len(data))
}
tmp := data[:len(data)-tz.GenericSignatureBytesLen]
data = data[len(data)-tz.GenericSignatureBytesLen:]
if _, err := encoding.Decode(tmp, &op.Contents, encoding.Ctx(ctx)); err != nil {
return nil, err
}
return encoding.Decode(data, &op.Signature, encoding.Ctx(ctx))
}
func (ops *OperationWithoutMetadata[T]) Operations() []OperationContents {
out := make([]OperationContents, len(ops.Contents))
for i, op := range ops.Contents {
out[i] = op
}
return out
}
func (*OperationWithoutMetadata[T]) GroupContents() {}
type OperationWithOptionalMetadata[T OperationWithOptionalMetadataContents] struct {
Contents T `json:"contents"`
}
func (ops OperationWithOptionalMetadata[T]) Operations() []OperationContents {
return ops.Contents.Operations()
}
func (op OperationWithOptionalMetadata[T]) GetSignature() (tz.Option[tz.Signature], error) {
return op.Contents.GetSignature()
}
func (OperationWithOptionalMetadata[T]) GroupContents() {}