forked from hacash/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
satoshi.go
352 lines (295 loc) · 9.88 KB
/
satoshi.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
package actions
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/hacash/core/fields"
"github.com/hacash/core/interfaces"
"github.com/hacash/core/interfacev2"
)
type Action_8_SimpleSatoshiTransfer struct {
ToAddress fields.Address
Amount fields.Satoshi
// data ptr
belong_trs interfacev2.Transaction
belong_trs_v3 interfaces.Transaction
}
func NewAction_8_SimpleSatoshiTransfer(addr fields.Address, amt fields.Satoshi) *Action_8_SimpleSatoshiTransfer {
return &Action_8_SimpleSatoshiTransfer{
ToAddress: addr,
Amount: amt,
}
}
func (elm *Action_8_SimpleSatoshiTransfer) Kind() uint16 {
return 8
}
// json api
func (elm *Action_8_SimpleSatoshiTransfer) Describe() map[string]interface{} {
var data = map[string]interface{}{}
return data
}
func (elm *Action_8_SimpleSatoshiTransfer) Serialize() ([]byte, error) {
var kindByte = make([]byte, 2)
binary.BigEndian.PutUint16(kindByte, elm.Kind())
var addrBytes, _ = elm.ToAddress.Serialize()
var amtBytes, _ = elm.Amount.Serialize()
var buffer bytes.Buffer
buffer.Write(kindByte)
buffer.Write(addrBytes)
buffer.Write(amtBytes)
return buffer.Bytes(), nil
}
func (elm *Action_8_SimpleSatoshiTransfer) Parse(buf []byte, seek uint32) (uint32, error) {
var e error = nil
moveseek, e := elm.ToAddress.Parse(buf, seek)
if e != nil {
return 0, e
}
moveseek2, e := elm.Amount.Parse(buf, moveseek)
if e != nil {
return 0, e
}
return moveseek2, nil
}
func (elm *Action_8_SimpleSatoshiTransfer) Size() uint32 {
return 2 + elm.ToAddress.Size() + elm.Amount.Size()
}
func (elm *Action_8_SimpleSatoshiTransfer) RequestSignAddresses() []fields.Address {
return []fields.Address{}
}
func (act *Action_8_SimpleSatoshiTransfer) WriteInChainState(state interfaces.ChainStateOperation) error {
if act.belong_trs_v3 == nil {
panic("Action belong to transaction not be nil !")
}
if act.Amount <= 0 {
// Transfer cannot be 0 or negative
return fmt.Errorf("Amount <%d> error.", act.Amount)
}
// transfer
fromAddress := act.belong_trs_v3.GetAddress()
return DoSimpleSatoshiTransferFromChainStateV3(state, fromAddress, act.ToAddress, act.Amount)
}
func (act *Action_8_SimpleSatoshiTransfer) WriteinChainState(state interfacev2.ChainStateOperation) error {
if act.belong_trs == nil {
panic("Action belong to transaction not be nil !")
}
if act.Amount <= 0 {
// Transfer cannot be 0 or negative
return fmt.Errorf("Amount <%d> error.", act.Amount)
}
// transfer
fromAddress := act.belong_trs.GetAddress()
return DoSimpleSatoshiTransferFromChainState(state, fromAddress, act.ToAddress, act.Amount)
}
func (act *Action_8_SimpleSatoshiTransfer) RecoverChainState(state interfacev2.ChainStateOperation) error {
if act.belong_trs == nil {
panic("Action belong to transaction not be nil !")
}
// Fallback
return DoSimpleSatoshiTransferFromChainState(state, act.ToAddress, act.belong_trs.GetAddress(), act.Amount)
}
// Set belongs to long_ trs
func (act *Action_8_SimpleSatoshiTransfer) SetBelongTransaction(trs interfacev2.Transaction) {
act.belong_trs = trs
}
func (act *Action_8_SimpleSatoshiTransfer) SetBelongTrs(trs interfaces.Transaction) {
act.belong_trs_v3 = trs
}
// burning fees // 是否销毁本笔交易的 90% 的交易费用
func (act *Action_8_SimpleSatoshiTransfer) IsBurning90PersentTxFees() bool {
return false
}
///////////////////////////////////////////////////////////////////////////////////////////////
type Action_11_FromToSatoshiTransfer struct {
FromAddress fields.Address
ToAddress fields.Address
Amount fields.Satoshi
// data ptr
belong_trs interfacev2.Transaction
belong_trs_v3 interfaces.Transaction
}
func NewAction_11_FromToSatoshiTransfer(fromaddr fields.Address, toaddr fields.Address, amt fields.Satoshi) *Action_11_FromToSatoshiTransfer {
return &Action_11_FromToSatoshiTransfer{
FromAddress: fromaddr,
ToAddress: toaddr,
Amount: amt,
}
}
func (elm *Action_11_FromToSatoshiTransfer) Kind() uint16 {
return 11
}
// json api
func (elm *Action_11_FromToSatoshiTransfer) Describe() map[string]interface{} {
var data = map[string]interface{}{}
return data
}
func (elm *Action_11_FromToSatoshiTransfer) Serialize() ([]byte, error) {
var kindByte = make([]byte, 2)
binary.BigEndian.PutUint16(kindByte, elm.Kind())
var addr1Bytes, _ = elm.FromAddress.Serialize()
var addr2Bytes, _ = elm.ToAddress.Serialize()
var amtBytes, _ = elm.Amount.Serialize()
var buffer bytes.Buffer
buffer.Write(kindByte)
buffer.Write(addr1Bytes)
buffer.Write(addr2Bytes)
buffer.Write(amtBytes)
return buffer.Bytes(), nil
}
func (elm *Action_11_FromToSatoshiTransfer) Parse(buf []byte, seek uint32) (uint32, error) {
seek, e := elm.FromAddress.Parse(buf, seek)
if e != nil {
return 0, e
}
seek, e = elm.ToAddress.Parse(buf, seek)
if e != nil {
return 0, e
}
seek, e = elm.Amount.Parse(buf, seek)
if e != nil {
return 0, e
}
return seek, nil
}
func (elm *Action_11_FromToSatoshiTransfer) Size() uint32 {
return 2 + elm.FromAddress.Size() + +elm.ToAddress.Size() + elm.Amount.Size()
}
func (elm *Action_11_FromToSatoshiTransfer) RequestSignAddresses() []fields.Address {
return []fields.Address{
elm.FromAddress, // From signature required
}
}
func (act *Action_11_FromToSatoshiTransfer) WriteInChainState(state interfaces.ChainStateOperation) error {
if act.belong_trs_v3 == nil {
panic("Action belong to transaction not be nil !")
}
if act.Amount <= 0 {
// Transfer cannot be 0 or negative
return fmt.Errorf("Amount <%d> error.", act.Amount)
}
// transfer
return DoSimpleSatoshiTransferFromChainStateV3(state, act.FromAddress, act.ToAddress, act.Amount)
}
func (act *Action_11_FromToSatoshiTransfer) WriteinChainState(state interfacev2.ChainStateOperation) error {
if act.belong_trs == nil {
panic("Action belong to transaction not be nil !")
}
if act.Amount <= 0 {
// Transfer cannot be 0 or negative
return fmt.Errorf("Amount <%d> error.", act.Amount)
}
// transfer
return DoSimpleSatoshiTransferFromChainState(state, act.FromAddress, act.ToAddress, act.Amount)
}
func (act *Action_11_FromToSatoshiTransfer) RecoverChainState(state interfacev2.ChainStateOperation) error {
if act.belong_trs == nil {
panic("Action belong to transaction not be nil !")
}
// Fallback
return DoSimpleSatoshiTransferFromChainState(state, act.ToAddress, act.FromAddress, act.Amount)
}
// Set belongs to long_ trs
func (act *Action_11_FromToSatoshiTransfer) SetBelongTransaction(trs interfacev2.Transaction) {
act.belong_trs = trs
}
func (act *Action_11_FromToSatoshiTransfer) SetBelongTrs(trs interfaces.Transaction) {
act.belong_trs_v3 = trs
}
// burning fees // 是否销毁本笔交易的 90% 的交易费用
func (act *Action_11_FromToSatoshiTransfer) IsBurning90PersentTxFees() bool {
return false
}
///////////////////////////////////////////////////////////////////////////////////////////////
type Action_28_FromSatoshiTransfer struct {
FromAddress fields.Address
Amount fields.Satoshi
// data ptr
belong_trs interfacev2.Transaction
belong_trs_v3 interfaces.Transaction
}
func NewAction_28_FromSatoshiTransfer(fromaddr fields.Address, amt fields.Satoshi) *Action_28_FromSatoshiTransfer {
return &Action_28_FromSatoshiTransfer{
FromAddress: fromaddr,
Amount: amt,
}
}
func (elm *Action_28_FromSatoshiTransfer) Kind() uint16 {
return 28
}
// json api
func (elm *Action_28_FromSatoshiTransfer) Describe() map[string]interface{} {
var data = map[string]interface{}{}
return data
}
func (elm *Action_28_FromSatoshiTransfer) Serialize() ([]byte, error) {
var kindByte = make([]byte, 2)
binary.BigEndian.PutUint16(kindByte, elm.Kind())
var addr1Bytes, _ = elm.FromAddress.Serialize()
var amtBytes, _ = elm.Amount.Serialize()
var buffer bytes.Buffer
buffer.Write(kindByte)
buffer.Write(addr1Bytes)
buffer.Write(amtBytes)
return buffer.Bytes(), nil
}
func (elm *Action_28_FromSatoshiTransfer) Parse(buf []byte, seek uint32) (uint32, error) {
seek, e := elm.FromAddress.Parse(buf, seek)
if e != nil {
return 0, e
}
seek, e = elm.Amount.Parse(buf, seek)
if e != nil {
return 0, e
}
return seek, nil
}
func (elm *Action_28_FromSatoshiTransfer) Size() uint32 {
return 2 + elm.FromAddress.Size() + elm.Amount.Size()
}
func (elm *Action_28_FromSatoshiTransfer) RequestSignAddresses() []fields.Address {
return []fields.Address{
elm.FromAddress, // From signature required
}
}
func (act *Action_28_FromSatoshiTransfer) WriteInChainState(state interfaces.ChainStateOperation) error {
if act.belong_trs_v3 == nil {
panic("Action belong to transaction not be nil !")
}
if act.Amount <= 0 {
// Transfer cannot be 0 or negative
return fmt.Errorf("Amount <%d> error.", act.Amount)
}
// transfer
toAddress := act.belong_trs_v3.GetAddress()
return DoSimpleSatoshiTransferFromChainStateV3(state, act.FromAddress, toAddress, act.Amount)
}
func (act *Action_28_FromSatoshiTransfer) WriteinChainState(state interfacev2.ChainStateOperation) error {
if act.belong_trs == nil {
panic("Action belong to transaction not be nil !")
}
if act.Amount <= 0 {
// Transfer cannot be 0 or negative
return fmt.Errorf("Amount <%d> error.", act.Amount)
}
// transfer
toAddress := act.belong_trs.GetAddress()
return DoSimpleSatoshiTransferFromChainState(state, act.FromAddress, toAddress, act.Amount)
}
func (act *Action_28_FromSatoshiTransfer) RecoverChainState(state interfacev2.ChainStateOperation) error {
if act.belong_trs == nil {
panic("Action belong to transaction not be nil !")
}
// Fallback
return DoSimpleSatoshiTransferFromChainState(state, act.belong_trs.GetAddress(), act.FromAddress, act.Amount)
}
// Set belongs to long_ trs
func (act *Action_28_FromSatoshiTransfer) SetBelongTransaction(trs interfacev2.Transaction) {
act.belong_trs = trs
}
func (act *Action_28_FromSatoshiTransfer) SetBelongTrs(trs interfaces.Transaction) {
act.belong_trs_v3 = trs
}
// burning fees // 是否销毁本笔交易的 90% 的交易费用
func (act *Action_28_FromSatoshiTransfer) IsBurning90PersentTxFees() bool {
return false
}