-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtransaction_test.go
More file actions
337 lines (262 loc) · 8.38 KB
/
transaction_test.go
File metadata and controls
337 lines (262 loc) · 8.38 KB
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
package inspector
import (
"bytes"
"crypto/rand"
"encoding/binary"
"fmt"
"io"
"github.com/tokenized/pkg/bitcoin"
"github.com/tokenized/specification/dist/golang/protocol"
"github.com/pkg/errors"
)
// func Test_Transaction_Serialize(t *testing.T) {
// ctx := context.Background()
// txb := txbuilder.NewTxBuilder(0.5, 0.25)
// var previousTxID bitcoin.Hash32
// rand.Read(previousTxID[:])
// fromKey, err := bitcoin.GenerateKey(bitcoin.MainNet)
// if err != nil {
// t.Fatalf("Failed to generate key : %s", err)
// }
// fromLockingScript, err := fromKey.LockingScript()
// if err != nil {
// t.Fatalf("Failed to locking script : %s", err)
// }
// txb.AddInputUTXO(bitcoin.UTXO{
// Hash: previousTxID,
// Index: 1,
// Value: 10000,
// LockingScript: fromLockingScript,
// })
// changeKey, err := bitcoin.GenerateKey(bitcoin.MainNet)
// if err != nil {
// t.Fatalf("Failed to generate key : %s", err)
// }
// changeAddress, err := changeKey.RawAddress()
// if err != nil {
// t.Fatalf("Failed to generate address : %s", err)
// }
// txb.SetChangeAddress(changeAddress, "")
// toKey, err := bitcoin.GenerateKey(bitcoin.MainNet)
// if err != nil {
// t.Fatalf("Failed to generate key : %s", err)
// }
// toLockingScript, err := toKey.LockingScript()
// if err != nil {
// t.Fatalf("Failed to locking script : %s", err)
// }
// if err := txb.AddOutput(toLockingScript, 9000, false, false); err != nil {
// t.Fatalf("Failed to add output : %s", err)
// }
// offer := &actions.ContractOffer{
// ContractName: "Test Contract",
// }
// script, err := protocol.Serialize(offer, true)
// if err != nil {
// t.Fatalf("Failed to serialize action : %s", err)
// }
// if err := txb.AddOutput(script, 0, false, false); err != nil {
// t.Fatalf("Failed to add output : %s", err)
// }
// if _, err := txb.Sign([]bitcoin.Key{fromKey}); err != nil {
// t.Fatalf("Failed to sign tx : %s", err)
// }
// tx, err := NewTransactionFromTxBuilder(ctx, txb, true)
// if err != nil {
// t.Fatalf("Failed to create inspector tx : %s", err)
// }
// t.Logf("Tx %s", tx.String(bitcoin.MainNet))
// buf := &bytes.Buffer{}
// if err := tx.Write(buf); err != nil {
// t.Fatalf("Failed to write tx : %s", err)
// }
// readTx := &Transaction{}
// if err := readTx.Read(buf, true); err != nil {
// t.Fatalf("Failed to read tx : %s", err)
// }
// t.Logf("Read Tx %s", readTx.String(bitcoin.MainNet))
// if err := tx.Equal(*readTx); err != nil {
// t.Fatalf("Read tx not equal : %s", err)
// }
// }
// func Test_Transaction_Serialize_v2_to_v3(t *testing.T) {
// ctx := context.Background()
// txb := txbuilder.NewTxBuilder(0.5, 0.25)
// var previousTxID bitcoin.Hash32
// rand.Read(previousTxID[:])
// fromKey, err := bitcoin.GenerateKey(bitcoin.MainNet)
// if err != nil {
// t.Fatalf("Failed to generate key : %s", err)
// }
// fromLockingScript, err := fromKey.LockingScript()
// if err != nil {
// t.Fatalf("Failed to locking script : %s", err)
// }
// txb.AddInputUTXO(bitcoin.UTXO{
// Hash: previousTxID,
// Index: 1,
// Value: 10000,
// LockingScript: fromLockingScript,
// })
// changeKey, err := bitcoin.GenerateKey(bitcoin.MainNet)
// if err != nil {
// t.Fatalf("Failed to generate key : %s", err)
// }
// changeAddress, err := changeKey.RawAddress()
// if err != nil {
// t.Fatalf("Failed to generate address : %s", err)
// }
// txb.SetChangeAddress(changeAddress, "")
// toKey, err := bitcoin.GenerateKey(bitcoin.MainNet)
// if err != nil {
// t.Fatalf("Failed to generate key : %s", err)
// }
// toLockingScript, err := toKey.LockingScript()
// if err != nil {
// t.Fatalf("Failed to locking script : %s", err)
// }
// if err := txb.AddOutput(toLockingScript, 9000, false, false); err != nil {
// t.Fatalf("Failed to add output : %s", err)
// }
// offer := &actions.ContractOffer{
// ContractName: "Test Contract",
// }
// script, err := protocol.Serialize(offer, true)
// if err != nil {
// t.Fatalf("Failed to serialize action : %s", err)
// }
// if err := txb.AddOutput(script, 0, false, false); err != nil {
// t.Fatalf("Failed to add output : %s", err)
// }
// if _, err := txb.Sign([]bitcoin.Key{fromKey}); err != nil {
// t.Fatalf("Failed to sign tx : %s", err)
// }
// tx, err := NewTransactionFromTxBuilder(ctx, txb, true)
// if err != nil {
// t.Fatalf("Failed to create inspector tx : %s", err)
// }
// t.Logf("Tx %s", tx.String(bitcoin.MainNet))
// buf := &bytes.Buffer{}
// if err := tx.Write_v2(buf); err != nil {
// t.Fatalf("Failed to write tx : %s", err)
// }
// readTx := &Transaction{}
// if err := readTx.Read(buf, true); err != nil {
// t.Fatalf("Failed to read tx : %s", err)
// }
// t.Logf("Read Tx %s", readTx.String(bitcoin.MainNet))
// if err := tx.Equal(*readTx); err != nil {
// t.Fatalf("Read tx not equal : %s", err)
// }
// }
func (itx Transaction) Equal(itx2 Transaction) error {
if !itx.Hash.Equal(&itx2.Hash) {
return fmt.Errorf("Wrong hash : got %s, want %s", itx.Hash, itx2.Hash)
}
if itx.RejectCode != itx2.RejectCode {
return fmt.Errorf("Wrong RejectCode : got %d, want %d", itx.RejectCode, itx2.RejectCode)
}
buf := &bytes.Buffer{}
if err := itx.MsgTx.Serialize(buf); err != nil {
return errors.Wrap(err, "serialize tx")
}
buf2 := &bytes.Buffer{}
if err := itx2.MsgTx.Serialize(buf2); err != nil {
return errors.Wrap(err, "serialize tx2")
}
if !bytes.Equal(buf.Bytes(), buf2.Bytes()) {
return fmt.Errorf("Wrong tx bytes : \ngot %x\nwant %x", buf.Bytes(), buf2.Bytes())
}
if len(itx.Inputs) != len(itx2.Inputs) {
return fmt.Errorf("Wrong Inputs count : got %d, want %d", len(itx.Inputs), len(itx2.Inputs))
}
for i, input := range itx.Inputs {
if input.Value != itx2.Inputs[i].Value {
return fmt.Errorf("Wrong input %d value : got %d, want %d", i, input.Value,
itx2.Inputs[i].Value)
}
if !bytes.Equal(input.LockingScript, itx2.Inputs[i].LockingScript) {
return fmt.Errorf("Wrong input %d locking script : \ngot %s\nwant %s", i,
input.LockingScript, itx2.Inputs[i].LockingScript)
}
if input.Action == nil {
if itx2.Inputs[i].Action != nil {
return fmt.Errorf("Left input %d missing action", i)
}
} else {
if itx2.Inputs[i].Action == nil {
return fmt.Errorf("Right input %d missing action", i)
}
script, err := protocol.Serialize(input.Action, true)
if err != nil {
return errors.Wrapf(err, "serialize input %d action", i)
}
script2, err := protocol.Serialize(itx2.Inputs[i].Action, true)
if err != nil {
return errors.Wrapf(err, "serialize input %d action2", i)
}
if !bytes.Equal(script, script2) {
return fmt.Errorf("Wrong input %d action : \ngot %x\nwant %x", i,
script, script2)
}
}
}
for i, output := range itx.Outputs {
if output.Action == nil {
if itx2.Outputs[i].Action != nil {
return fmt.Errorf("Left output %d missing action", i)
}
} else {
if itx2.Outputs[i].Action == nil {
return fmt.Errorf("Right output %d missing action", i)
}
script, err := protocol.Serialize(output.Action, true)
if err != nil {
return errors.Wrapf(err, "serialize output %d action", i)
}
script2, err := protocol.Serialize(itx2.Outputs[i].Action, true)
if err != nil {
return errors.Wrapf(err, "serialize output %d action2", i)
}
if !bytes.Equal(script, script2) {
return fmt.Errorf("Wrong output %d action : \ngot %x\nwant %x", i,
script, script2)
}
}
}
return nil
}
func (itx *Transaction) Write_v2(w io.Writer) error {
// Version
if _, err := w.Write([]byte{2}); err != nil {
return errors.Wrap(err, "version")
}
if err := itx.MsgTx.Serialize(w); err != nil {
return errors.Wrap(err, "tx")
}
if err := binary.Write(w, binary.LittleEndian, uint32(len(itx.Inputs))); err != nil {
return errors.Wrap(err, "inputs count")
}
for i, _ := range itx.Inputs {
if err := itx.Inputs[i].Write_v2(w); err != nil {
return errors.Wrapf(err, "input %d", i)
}
}
if _, err := w.Write([]byte{uint8(itx.RejectCode)}); err != nil {
return errors.Wrap(err, "reject code")
}
return nil
}
func (in Input) Write_v2(w io.Writer) error {
// UTXO with random hash and index values
utxo := bitcoin.UTXO{
Index: 1,
Value: in.Value,
LockingScript: in.LockingScript,
}
rand.Read(utxo.Hash[:])
if err := utxo.Write(w); err != nil {
return err
}
return nil
}