-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.go
120 lines (107 loc) · 3.1 KB
/
transaction.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
package kernel
import (
"encoding/json"
"github.com/MixinNetwork/mixin/common"
)
type Deposit struct {
Chain string `json:"chain"`
AssetKey string `json:"asset_key"`
Transaction string `json:"transaction"`
Index uint64 `json:"index"`
Amount string `json:"amount"`
}
type MintInput struct {
Group string `json:"group"`
Batch uint64 `json:"batch"`
Amount string `json:"amount"`
}
type Input struct {
Hash string `json:"hash"`
Index int `json:"index"`
Genesis string `json:"genesis,omitempty"`
Deposit *Deposit `json:"deposit,omitempty"`
Mint *MintInput `json:"mint,omitempty"`
}
type Withdrawal struct {
Address string `json:"address"`
Tag string `json:"tag"`
}
type Output struct {
Type uint8 `json:"type"`
Amount string `json:"amount"`
Keys []string `json:"keys,omitempty"`
Mask string `json:"mask,omitempty"`
Script string `json:"script,omitempty"`
Withdrawal *Withdrawal `json:"withdrawal,omitempty"`
}
type Transaction struct {
Version uint8 `json:"version"`
Asset string `json:"asset"`
Inputs []*Input `json:"inputs"`
Outputs []*Output `json:"outputs"`
Extra string `json:"extra"`
Hash string `json:"hash"`
Raw string `json:"hex"`
Snapshot string `json:"snapshot"`
References []string `json:"references"`
}
func (m *MixinNetwork) SendRawTransaction(raw string) (*Transaction, error) {
body, err := m.callRPC("sendrawtransaction", []any{raw})
if err != nil {
return nil, err
}
var tx Transaction
err = json.Unmarshal(body, &tx)
return &tx, err
}
func (m *MixinNetwork) GetTransaction(hash string) (*Transaction, error) {
b, err := m.callRPC("gettransaction", []any{hash})
if err != nil || b == nil {
return nil, err
}
var tx Transaction
err = json.Unmarshal(b, &tx)
if err != nil || tx.Hash == "" {
return nil, err
}
return &tx, err
}
func (tx *Transaction) TransactionType() uint8 {
for _, in := range tx.Inputs {
if in.Mint != nil {
return common.TransactionTypeMint
}
if in.Deposit != nil {
return common.TransactionTypeDeposit
}
if in.Genesis != "" {
return common.TransactionTypeUnknown
}
}
isScript := true
for _, out := range tx.Outputs {
switch out.Type {
case common.OutputTypeWithdrawalSubmit:
return common.TransactionTypeWithdrawalSubmit
case common.OutputTypeWithdrawalClaim:
return common.TransactionTypeWithdrawalClaim
case common.OutputTypeNodePledge:
return common.TransactionTypeNodePledge
case common.OutputTypeNodeCancel:
return common.TransactionTypeNodeCancel
case common.OutputTypeNodeAccept:
return common.TransactionTypeNodeAccept
case common.OutputTypeNodeRemove:
return common.TransactionTypeNodeRemove
case common.OutputTypeCustodianUpdateNodes:
return common.TransactionTypeCustodianUpdateNodes
case common.OutputTypeCustodianSlashNodes:
return common.TransactionTypeCustodianSlashNodes
}
isScript = isScript && out.Type == common.OutputTypeScript
}
if isScript {
return common.TransactionTypeScript
}
return common.TransactionTypeUnknown
}