-
Notifications
You must be signed in to change notification settings - Fork 26
/
txjson_node.go
254 lines (224 loc) · 5.34 KB
/
txjson_node.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
package bt
import (
"encoding/json"
"errors"
"github.com/libsv/go-bt/v2/bscript"
)
type nodeTxWrapper struct {
*Tx
}
type nodeTxsWrapper Txs
type nodeOutputWrapper struct {
*Output
}
type nodeTxJSON struct {
Version uint32 `json:"version"`
LockTime uint32 `json:"locktime"`
TxID string `json:"txid"`
Hash string `json:"hash"`
Size int `json:"size"`
Hex string `json:"hex"`
Inputs []*nodeInputJSON `json:"vin"`
Outputs []*nodeOutputJSON `json:"vout"`
}
type nodeInputJSON struct {
ScriptSig *struct {
Asm string `json:"asm"`
Hex string `json:"hex"`
} `json:"scriptSig,omitempty"`
TxID string `json:"txid"`
Vout uint32 `json:"vout"`
Sequence uint32 `json:"sequence"`
}
type nodeOutputJSON struct {
Value float64 `json:"value"`
Index int `json:"n"`
ScriptPubKey *struct {
Asm string `json:"asm"`
Hex string `json:"hex"`
ReqSigs int `json:"reqSigs,omitempty"`
Type string `json:"type"`
} `json:"scriptPubKey,omitempty"`
}
func (n *nodeTxWrapper) MarshalJSON() ([]byte, error) {
if n == nil || n.Tx == nil {
return nil, errors.New("tx is nil so cannot be marshalled")
}
tx := n.Tx
oo := make([]*nodeOutputJSON, 0, len(tx.Outputs))
for i, o := range tx.Outputs {
out := &nodeOutputJSON{}
if err := out.fromOutput(o); err != nil {
return nil, err
}
out.Index = i
oo = append(oo, out)
}
ii := make([]*nodeInputJSON, 0, len(tx.Inputs))
for _, i := range tx.Inputs {
in := &nodeInputJSON{}
if err := in.fromInput(i); err != nil {
return nil, err
}
ii = append(ii, in)
}
txj := nodeTxJSON{
Version: tx.Version,
LockTime: tx.LockTime,
Inputs: ii,
Outputs: oo,
TxID: tx.TxID(),
Hash: tx.TxID(),
Size: len(tx.Bytes()),
Hex: tx.String(),
}
return json.Marshal(txj)
}
// UnmarshalJSON will unmarshall a transaction that has been marshalled with this library.
func (n *nodeTxWrapper) UnmarshalJSON(b []byte) error {
tx := n.Tx
var txj nodeTxJSON
if err := json.Unmarshal(b, &txj); err != nil {
return err
}
// quick convert
if txj.Hex != "" {
t, err := NewTxFromString(txj.Hex)
if err != nil {
return err
}
*tx = *t
return nil
}
oo := make([]*Output, 0, len(txj.Outputs))
for _, o := range txj.Outputs {
out, err := o.toOutput()
if err != nil {
return err
}
oo = append(oo, out)
}
ii := make([]*Input, 0, len(txj.Inputs))
for _, i := range txj.Inputs {
in, err := i.toInput()
if err != nil {
return err
}
ii = append(ii, in)
}
tx.Inputs = ii
tx.Outputs = oo
tx.LockTime = txj.LockTime
tx.Version = txj.Version
return nil
}
func (o *nodeOutputJSON) fromOutput(out *Output) error {
asm, err := out.LockingScript.ToASM()
if err != nil {
return err
}
addresses, err := out.LockingScript.Addresses()
if err != nil {
return err
}
*o = nodeOutputJSON{
Value: float64(out.Satoshis) / 100000000,
Index: 0,
ScriptPubKey: &struct {
Asm string `json:"asm"`
Hex string `json:"hex"`
ReqSigs int `json:"reqSigs,omitempty"`
Type string `json:"type"`
}{
Asm: asm,
Hex: out.LockingScriptHexString(),
ReqSigs: len(addresses),
Type: out.LockingScript.ScriptType(),
},
}
return nil
}
func (o *nodeOutputJSON) toOutput() (*Output, error) {
out := &Output{}
s, err := bscript.NewFromHexString(o.ScriptPubKey.Hex)
if err != nil {
return nil, err
}
out.Satoshis = uint64(o.Value * 100000000)
out.LockingScript = s
return out, nil
}
func (i *nodeInputJSON) toInput() (*Input, error) {
input := &Input{}
s, err := bscript.NewFromHexString(i.ScriptSig.Hex)
if err != nil {
return nil, err
}
input.UnlockingScript = s
input.PreviousTxOutIndex = i.Vout
input.SequenceNumber = i.Sequence
if err = input.PreviousTxIDAddStr(i.TxID); err != nil {
return nil, err
}
return input, nil
}
func (i *nodeInputJSON) fromInput(input *Input) error {
asm, err := input.UnlockingScript.ToASM()
if err != nil {
return err
}
i.ScriptSig = &struct {
Asm string `json:"asm"`
Hex string `json:"hex"`
}{
Asm: asm,
Hex: input.UnlockingScript.String(),
}
i.Vout = input.PreviousTxOutIndex
i.Sequence = input.SequenceNumber
i.TxID = input.PreviousTxIDStr()
return nil
}
// MarshalJSON will marshal a transaction that has been marshalled with this library.
func (nn nodeTxsWrapper) MarshalJSON() ([]byte, error) {
txs := make([]*nodeTxWrapper, len(nn))
for i, n := range nn {
txs[i] = n.NodeJSON().(*nodeTxWrapper)
}
return json.Marshal(txs)
}
// UnmarshalJSON will unmarshal a transaction that has been marshalled with this library.
func (nn *nodeTxsWrapper) UnmarshalJSON(b []byte) error {
var jj []json.RawMessage
if err := json.Unmarshal(b, &jj); err != nil {
return err
}
*nn = make(nodeTxsWrapper, 0)
for _, j := range jj {
tx := NewTx()
if err := json.Unmarshal(j, tx.NodeJSON()); err != nil {
return err
}
*nn = append(*nn, tx)
}
return nil
}
func (n *nodeOutputWrapper) MarshalJSON() ([]byte, error) {
oj := &nodeOutputJSON{}
if err := oj.fromOutput(n.Output); err != nil {
return nil, err
}
return json.Marshal(oj)
}
func (n *nodeOutputWrapper) UnmarshalJSON(b []byte) error {
oj := &nodeOutputJSON{}
if err := json.Unmarshal(b, &oj); err != nil {
return err
}
o, err := oj.toOutput()
if err != nil {
return err
}
*n.Output = *o
return nil
}