-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic.go
156 lines (138 loc) · 4.23 KB
/
generic.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
package signer
import (
"encoding/hex"
"log"
"math/big"
)
// GenericOperation parses an operation with a generic magic byte
type GenericOperation struct {
hex []byte
}
// Kind of different types of generic operations
// Defined in gitlb.com/tezos/tezos:
// https://gitlab.com/tezos/tezos/blob/mainnet/src/proto_003_PsddFKi3/lib_protocol/src/operation_repr.ml
const (
opKindUnknown = 0xff
opKindProposals = 0x05
opKindBallot = 0x06
opKindTransaction = 0x6C
)
// GetGenericOperation to parse specific Generic fields
func GetGenericOperation(op *Operation) *GenericOperation {
if op.MagicByte() != opMagicByteGeneric {
return nil
}
return &GenericOperation{
hex: op.Hex(),
}
}
// Kind of the generic operation
func (op *GenericOperation) Kind() uint8 {
// Must be at least long enough to get the kind byte
if len(op.hex) <= 33 {
return opKindUnknown
}
return op.hex[33]
}
// TransactionSource address that funds are being moved from
func (op *GenericOperation) TransactionSource() string {
if op.Kind() != opKindTransaction {
return ""
}
return hex.EncodeToString(op.hex[36:56])
}
// TransactionFee that's being paid along with this tx
func (op *GenericOperation) TransactionFee() *big.Int {
if op.Kind() != opKindTransaction {
return nil
}
return op.parseSerializedNumberOffset(0)
}
// TransactionCounter ensuring idempotency of this tx
func (op *GenericOperation) TransactionCounter() *big.Int {
if op.Kind() != opKindTransaction {
return nil
}
return op.parseSerializedNumberOffset(1)
}
// TransactionGasLimit of this tx
func (op *GenericOperation) TransactionGasLimit() *big.Int {
if op.Kind() != opKindTransaction {
return nil
}
return op.parseSerializedNumberOffset(2)
}
// TransactionStorageLimit of this tx
func (op *GenericOperation) TransactionStorageLimit() *big.Int {
if op.Kind() != opKindTransaction {
return nil
}
return op.parseSerializedNumberOffset(3)
}
// TransactionAmount that's moving with this tx
func (op *GenericOperation) TransactionAmount() *big.Int {
if op.Kind() != opKindTransaction {
return nil
}
return op.parseSerializedNumberOffset(4)
}
// TransactionDestination address we're sending funds to
func (op *GenericOperation) TransactionDestination() string {
if op.Kind() != opKindTransaction {
return ""
}
// Verify these indices align with the end_index of transaction amount
numberIndex := 56
for i := 0; i <= 4; i++ {
_, numberIndex = op.parseSerializedNumber(numberIndex)
}
start := len(op.hex) - 21
end := len(op.hex) - 1
if start-numberIndex != 2 {
log.Println("[WARN] Incorrect offset between numbers and destination. Unsure where we're sending.")
return ""
}
return hex.EncodeToString(op.hex[start:end])
}
// TransactionValue is the total value of all XTZ that could be spent in this tx
func (op *GenericOperation) TransactionValue() *big.Int {
if op.Kind() != opKindTransaction {
return nil
}
total := &big.Int{}
total.Add(total, op.TransactionFee())
total.Add(total, op.TransactionAmount())
total.Add(total, op.TransactionGasLimit())
total.Add(total, op.TransactionStorageLimit())
return total
}
// Private funcs to parse sequentially serialized numbers in the operation's hex
func (op *GenericOperation) parseSerializedNumberOffset(offset int) *big.Int {
var num *big.Int
// Numbers always begin at this index
index := 56
for i := 0; i <= offset; i++ {
num, index = op.parseSerializedNumber(index)
}
return num
}
// Parse a numbers starting at the provided index. Return the number and
// the index of the next byte in the operation. Follows the recursive reading
// fn @ https://gitlab.com/tezos/tezos/blob/master/src/lib_data_encoding/binary_reader.ml#L174
func (op *GenericOperation) parseSerializedNumber(startIndex int) (*big.Int, int) {
if len(op.hex) <= startIndex {
log.Println("[WARN] Ran into end of bytes while parsing. Returning zero.")
return new(big.Int).SetInt64(0), startIndex
}
b := op.hex[startIndex]
nextIndex := startIndex + 1
base := new(big.Int).SetInt64(int64(0))
top := new(big.Int).SetInt64(int64(b))
top.Mod(top, new(big.Int).SetInt64(0x80))
if b >= 0x80 {
var result *big.Int
result, nextIndex = op.parseSerializedNumber(nextIndex)
base.Mul(new(big.Int).SetInt64(0x80), result)
}
return top.Add(top, base), nextIndex
}