-
Notifications
You must be signed in to change notification settings - Fork 18
/
serialize_types.go
212 lines (170 loc) · 4.11 KB
/
serialize_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
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
package types
import (
"bytes"
"errors"
)
func (h Hash) Serialize() ([]byte, error) {
return h.Bytes(), nil
}
func (t ScriptHashType) Serialize() ([]byte, error) {
if t == HashTypeData {
return []byte{00}, nil
} else if t == HashTypeType {
return []byte{01}, nil
} else if t == HashTypeData1 {
return []byte{02}, nil
}
return nil, errors.New("invalid script hash type")
}
// Serialize dep type
func (t DepType) Serialize() ([]byte, error) {
if t == DepTypeCode {
return []byte{00}, nil
} else if t == DepTypeDepGroup {
return []byte{01}, nil
}
return nil, errors.New("invalid dep group")
}
// Serialize script
func (script *Script) Serialize() ([]byte, error) {
h, err := script.CodeHash.Serialize()
if err != nil {
return nil, err
}
t, err := script.HashType.Serialize()
if err != nil {
return nil, err
}
a := SerializeBytes(script.Args)
return SerializeTable([][]byte{h, t, a}), nil
}
// Serialize outpoint
func (o *OutPoint) Serialize() ([]byte, error) {
h, err := o.TxHash.Serialize()
if err != nil {
return nil, err
}
i := SerializeUint(o.Index)
b := new(bytes.Buffer)
b.Write(h)
b.Write(i)
return b.Bytes(), nil
}
// Serialize cell input
func (i *CellInput) Serialize() ([]byte, error) {
s := SerializeUint64(i.Since)
o, err := i.PreviousOutput.Serialize()
if err != nil {
return nil, err
}
return SerializeStruct([][]byte{s, o}), nil
}
// Serialize cell output
func (o *CellOutput) Serialize() ([]byte, error) {
c := SerializeUint64(o.Capacity)
l, err := o.Lock.Serialize()
if err != nil {
return nil, err
}
t, err := SerializeOption(o.Type)
if err != nil {
return nil, err
}
return SerializeTable([][]byte{c, l, t}), nil
}
// Serialize cell dep
func (d *CellDep) Serialize() ([]byte, error) {
o, err := d.OutPoint.Serialize()
if err != nil {
return nil, err
}
dd, err := d.DepType.Serialize()
if err != nil {
return nil, err
}
return SerializeStruct([][]byte{o, dd}), nil
}
// Serialize transaction
func (t *Transaction) Serialize() ([]byte, error) {
v := SerializeUint(t.Version)
// Ok, no way around this
deps := make([]Serializer, len(t.CellDeps))
for i, v := range t.CellDeps {
deps[i] = v
}
cds, err := SerializeArray(deps)
if err != nil {
return nil, err
}
cdsBytes := SerializeFixVec(cds)
hds := make([][]byte, len(t.HeaderDeps))
for i := 0; i < len(t.HeaderDeps); i++ {
hd, err := t.HeaderDeps[i].Serialize()
if err != nil {
return nil, err
}
hds[i] = hd
}
hdsBytes := SerializeFixVec(hds)
ips := make([][]byte, len(t.Inputs))
for i := 0; i < len(t.Inputs); i++ {
ip, err := t.Inputs[i].Serialize()
if err != nil {
return nil, err
}
ips[i] = ip
}
ipsBytes := SerializeFixVec(ips)
ops := make([][]byte, len(t.Outputs))
for i := 0; i < len(t.Outputs); i++ {
op, err := t.Outputs[i].Serialize()
if err != nil {
return nil, err
}
ops[i] = op
}
opsBytes := SerializeDynVec(ops)
ods := make([][]byte, len(t.OutputsData))
for i := 0; i < len(t.OutputsData); i++ {
od := SerializeBytes(t.OutputsData[i])
ods[i] = od
}
odsBytes := SerializeDynVec(ods)
fields := [][]byte{v, cdsBytes, hdsBytes, ipsBytes, opsBytes, odsBytes}
return SerializeTable(fields), nil
}
func (w *WitnessArgs) Serialize() ([]byte, error) {
l, err := SerializeOptionBytes(w.Lock)
if err != nil {
return nil, err
}
i, err := SerializeOptionBytes(w.InputType)
if err != nil {
return nil, err
}
o, err := SerializeOptionBytes(w.OutputType)
if err != nil {
return nil, err
}
return SerializeTable([][]byte{l, i, o}), nil
}
func SerializeHashType(hashType ScriptHashType) (string, error) {
if HashTypeData == hashType {
return "00", nil
} else if HashTypeType == hashType {
return "01", nil
} else if HashTypeData1 == hashType {
return "02", nil
}
return "", errors.New("Invalid script hash_type: " + string(hashType))
}
func DeserializeHashType(hashType string) (ScriptHashType, error) {
if "00" == hashType {
return HashTypeData, nil
} else if "01" == hashType {
return HashTypeType, nil
} else if "02" == hashType {
return HashTypeData1, nil
}
return "", errors.New("Invalid script hash_type: " + hashType)
}