-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtxbuilder_test.go
More file actions
877 lines (732 loc) · 26.5 KB
/
txbuilder_test.go
File metadata and controls
877 lines (732 loc) · 26.5 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
package txbuilder
import (
"bytes"
"crypto/rand"
"encoding/hex"
"fmt"
"strconv"
"testing"
"github.com/tokenized/pkg/bitcoin"
"github.com/tokenized/pkg/wire"
"github.com/pkg/errors"
)
func Test_DustLimit(t *testing.T) {
tests := []struct {
dustFeeRateString string
dustFeeRate float32
dust uint64
}{
{
dustFeeRateString: "0.0",
dustFeeRate: 0,
dust: 1,
},
{
dustFeeRateString: "0",
dustFeeRate: 0,
dust: 1,
},
{
dustFeeRateString: "0.25",
dustFeeRate: 0.25,
dust: 136,
},
{
dustFeeRateString: "0.5",
dustFeeRate: 0.5,
dust: 273,
},
{
dustFeeRateString: "1.0",
dustFeeRate: 1.0,
dust: 546,
},
{
dustFeeRateString: "1",
dustFeeRate: 1,
dust: 546,
},
}
for _, tt := range tests {
t.Run(tt.dustFeeRateString, func(t *testing.T) {
dustFeeRate64, err := strconv.ParseFloat(tt.dustFeeRateString, 32)
if err != nil {
return
}
dustFeeRate := float32(dustFeeRate64)
if dustFeeRate != tt.dustFeeRate {
t.Errorf("Wrong dust fee rate : got %f, want %f", dustFeeRate, tt.dustFeeRate)
}
dust := DustLimit(P2PKHOutputSize, dustFeeRate)
if dust != tt.dust {
t.Errorf("Wrong dust : got %d, want %d", dust, tt.dust)
}
})
}
}
func TestBasic(t *testing.T) {
key, err := bitcoin.GenerateKey(bitcoin.TestNet)
if err != nil {
t.Errorf("Failed to create private key : %s", err)
}
pkh := bitcoin.Hash160(key.PublicKey().Bytes())
address, err := bitcoin.NewRawAddressPKH(pkh)
if err != nil {
t.Errorf("Failed to create pkh address : %s", err)
}
key2, err := bitcoin.GenerateKey(bitcoin.TestNet)
if err != nil {
t.Errorf("Failed to create private key 2 : %s", err)
}
pkh2 := bitcoin.Hash160(key2.PublicKey().Bytes())
address2, err := bitcoin.NewRawAddressPKH(pkh2)
if err != nil {
t.Errorf("Failed to create pkh address 2 : %s", err)
}
inputTx := NewTxBuilder(1.1, 1.0)
inputTx.SetChangeAddress(address2, "")
err = inputTx.AddPaymentOutput(address, 10000, true)
if err != nil {
t.Errorf("Failed to add output : %s", err)
}
tx := NewTxBuilder(1.0, 1.0)
tx.SetChangeAddress(address, "")
err = tx.AddInput(wire.OutPoint{Hash: *inputTx.MsgTx.TxHash(), Index: 0},
inputTx.MsgTx.TxOut[0].LockingScript, uint64(inputTx.MsgTx.TxOut[0].Value))
if err != nil {
t.Errorf("Failed to add input : %s", err)
}
err = tx.AddPaymentOutput(address2, 5000, false)
if err != nil {
t.Errorf("Failed to add output : %s", err)
}
err = tx.AddDustOutput(address, true)
if err != nil {
t.Errorf("Failed to add output : %s", err)
}
// Test single valid private key
var signingKeys []bitcoin.Key
signingKeys, err = tx.Sign([]bitcoin.Key{key})
if err != nil {
t.Errorf("Failed to sign tx : %s", err)
}
t.Logf("Tx Fee : %d", tx.Fee())
if len(signingKeys) != 1 {
t.Fatalf("Wrong signingKeys count : got %d, want %d", len(signingKeys), 1)
}
if !signingKeys[0].Equal(key) {
t.Errorf("Wrong signing key : got %s, want %s", signingKeys[0].String(), key.String())
}
// Test extra private key
signingKeys, err = tx.Sign([]bitcoin.Key{key, key2})
if err != nil {
t.Errorf("Failed to sign tx with both keys : %s", err)
}
t.Logf("Tx Fee : %d", tx.Fee())
if len(signingKeys) != 1 {
t.Fatalf("Wrong signingKeys count : got %d, want %d", len(signingKeys), 1)
}
if !signingKeys[0].Equal(key) {
t.Errorf("Wrong signing key : got %s, want %s", signingKeys[0].String(), key.String())
}
// Test wrong private key
_, err = tx.Sign([]bitcoin.Key{key2})
if errors.Cause(err) == ErrWrongPrivateKey {
if err != nil {
t.Errorf("Failed to return wrong private key error : %s", err)
} else {
t.Errorf("Failed to return wrong private key error")
}
}
t.Logf("Tx Fee : %d", tx.Fee())
// Test bad LockingScript
txMalformed := NewTxBuilder(1.0, 1.0)
txMalformed.SetChangeAddress(address, "")
err = txMalformed.AddInput(wire.OutPoint{Hash: *inputTx.MsgTx.TxHash(), Index: 0},
append(inputTx.MsgTx.TxOut[0].LockingScript, 5), uint64(inputTx.MsgTx.TxOut[0].Value))
if errors.Cause(err) == ErrWrongScriptTemplate {
if err != nil {
t.Errorf("Failed to return \"Not P2PKH Script\" error : %s", err)
} else {
t.Errorf("Failed to return \"Not P2PKH Script\" error")
}
}
}
func TestSample(t *testing.T) {
// Load your private key
wif := "cQDgbH4C7HP3LSJevMSb1dPMCviCPoLwJ28mxnDRJueMSCa72xjm"
key, err := bitcoin.KeyFromStr(wif)
if err != nil {
t.Fatalf("Failed to decode key : %s", err)
}
// Decode an address to use for "change".
// Middle return parameter is the network detected. This should be checked to ensure the address
// was encoded for the currently specified network.
changeAddress, _ := bitcoin.DecodeAddress("mq4htwkZSAG9isuVbEvcLaAiNL59p26W64")
// Create an instance of the TxBuilder using 1.1 as the dust rate and 1.1 sat/byte fee rate.
builder := NewTxBuilder(1.1, 1.0)
builder.SetChangeAddress(bitcoin.NewRawAddressFromAddress(changeAddress), "")
// Add an input
// To spend an input you need the txid, output index, and the locking script and value from that output.
hash, _ := bitcoin.NewHash32FromStr("c762a29a4beb4821ad843590c3f11ffaed38b7eadc74557bdf36da3539921531")
index := uint32(0)
value := uint64(2000)
spendAddress, _ := bitcoin.DecodeAddress("mupiWN44gq3NZmvZuMMyx8KbRwism69Gbw")
lockingScript, _ := bitcoin.NewRawAddressFromAddress(spendAddress).LockingScript()
_ = builder.AddInput(*wire.NewOutPoint(hash, index), lockingScript, value)
// add an output to the recipient
paymentAddress, _ := bitcoin.DecodeAddress("n1kBjpqmH82jgiRnEHLmFMNv77kvugBomm")
_ = builder.AddPaymentOutput(bitcoin.NewRawAddressFromAddress(paymentAddress), 1000, false)
// sign the first and only input
builder.Sign([]bitcoin.Key{key})
// get the raw TX bytes
_, _ = builder.Serialize()
}
func TestTxSigHash(t *testing.T) {
txData, err := hex.DecodeString("0100000002e5a2041ebfcdb5594616fd090d1065b48dbb3bb0cf75dbc0028ba3e82404665a000000006b483045022100875980a2c82af1ccb3493cf857c3d807f182c334458749b5284e7b207d16e5f402200eaba277fdb8d15e862e488074284bde0b4adfbcfe43cdbc96db29dbd380ad334121034f31d5c213db1a2847fa1a3425e9bdc5f8104d11f74b68434d8365f17acfb6c3ffffffff681506afb99bf4a98a1ab8082438003aee835ebcdc90b0fd5701769d42ac4ef3020000006a47304402201754f8aec11c2aab1c41df9e5717b9f88616cc9b0992f6a8c1f9b510f2d88429022040496964eacd71feaa628ae2824c251b2e098a9b8afb4b61cd4c34daae1d1cf24121034f31d5c213db1a2847fa1a3425e9bdc5f8104d11f74b68434d8365f17acfb6c3ffffffff03b30d0000000000001976a9145ca2479d4bc988bff6a5b67d6bebd24a4ef3ff3d88ac000000000000000095006a02bd000e746573742e746f6b656e697a6564041a0241314c7a0a08fffffffffffffff0100120015080c2d72f5a034c4f596260121e546f6b656e416972204672657175656e7420466c79657220506f696e7473188080d488b4d1a3cc1520808080eb8b91f7fc192a2a4672657175656e7420666c79657220706f696e747320666f7220546f6b656e41697220666c6967687473ce182f00000000001976a9148c9420efb9f98392397a999100a1e62cc7419ec588ac00000000")
if err != nil {
t.Fatalf("Failed to deserialize tx hex : %s", err)
}
msg := &wire.MsgTx{}
buf := bytes.NewBuffer(txData)
err = msg.Deserialize(buf)
if err != nil {
t.Fatalf("Failed to deserialize tx : %s", err)
}
txData1, err := hex.DecodeString("01000000017fe4b224d93776bd79b6385f30489f61e5cce5799fe7de925f030ae46bf1e3cd000000006a473044022045afb3609660866da172ca51a6185f37a0083ef8bd06f1e93663992fbee26e52022056b1ee844c8d3df24429d59849f297dd7703fe10691b61d7ded4dc82c94405be41210222499db2ea899df5f3ea80cb4fbf8160f1d2de3ee368fce8df72760f4c50154fffffffff02de0c0000000000001976a9148c9420efb9f98392397a999100a1e62cc7419ec588ac0000000000000000356a0e746573742e746f6b656e697a656424004d3201000000000111004d657373616765204d616c666f726d65646928c10e98d5b81500000000")
if err != nil {
t.Fatalf("Failed to deserialize tx 1 hex : %s", err)
}
msg1 := &wire.MsgTx{}
buf = bytes.NewBuffer(txData1)
err = msg1.Deserialize(buf)
if err != nil {
t.Fatalf("Failed to deserialize tx 1 : %s", err)
}
txData2, err := hex.DecodeString("0100000002f5c02af0fdddab094e1be7c8ac1265f74bfef9d27051d356cf82eaa9bac2b020000000006b4830450221008c6cc6ad165d599eeef3fd9c653496503258abe13a65d75ff66620487dba01e102207c9e28304faafa3398a8622e96c56ee4fe77bc97a25eb3d7c55b5f6721eba22f4121034f31d5c213db1a2847fa1a3425e9bdc5f8104d11f74b68434d8365f17acfb6c3ffffffffafa8a25e86285de9d20e4e2b0e6ded75535eeae151a39811fc0b48047b3bc9d7020000006a4730440220407f4cd703d3c587077cc342a6e77af696e89b95eb864a879cb7a044fae4251a02204cea701b47f3ebbcfd4a24d7908e481187371d9b5676c4ad62f4ad4f8645e3764121034f31d5c213db1a2847fa1a3425e9bdc5f8104d11f74b68434d8365f17acfb6c3ffffffff03ee0d0000000000001976a914b6cd97de385a23dc38b6b9b511d1da3a548c77f688ac0000000000000000926a0e746573742e746f6b656e697a65644c800041314c4f5908fffffffffffffff001000001000000000000e1f505000000005e000000001e546f6b656e416972204672657175656e7420466c79657220506f696e7473000015418b8e9815000060bd88dcf9192a004672657175656e7420666c79657220706f696e747320666f7220546f6b656e41697220666c6967687473eb1b2f00000000001976a9148c9420efb9f98392397a999100a1e62cc7419ec588ac00000000")
if err != nil {
t.Fatalf("Failed to deserialize tx 2 hex : %s", err)
}
msg2 := &wire.MsgTx{}
buf = bytes.NewBuffer(txData2)
err = msg2.Deserialize(buf)
if err != nil {
t.Fatalf("Failed to deserialize tx 2 : %s", err)
}
inputs := []*wire.MsgTx{msg1, msg2}
tx, err := NewTxBuilderFromWire(1.1, 1.0, msg, inputs)
if err != nil {
t.Fatalf("Failed to build tx : %s", err)
}
address, _ := bitcoin.DecodeAddress("1DpK41vJhoPLimRNqJYHJ2ZjG6aMBCgm3D")
changeAddress := bitcoin.NewRawAddressFromAddress(address)
tx.SetChangeAddress(changeAddress, "")
hashCache := &SigHashCache{}
sighash, err := SignatureHash(msg, 0, msg1.TxOut[0].LockingScript, msg1.TxOut[0].Value,
SigHashAll+SigHashForkID, hashCache)
if err != nil {
t.Fatalf("Failed to generate signature hash : %s", err)
}
sighashHex := hex.EncodeToString(sighash[:])
if sighashHex != "54638b5c5126e187cb3a6e62c28fa595c925d3c1dec50780d5a2116879eaf381" {
t.Fatalf("Wrong sig hash : \n got %s\n want %s", sighashHex,
"54638b5c5126e187cb3a6e62c28fa595c925d3c1dec50780d5a2116879eaf381")
}
}
func randomTxId() *bitcoin.Hash32 {
rb := make([]byte, 32)
rand.Read(rb)
result, _ := bitcoin.NewHash32(rb)
return result
}
func randomLockingScript() []byte {
rb := make([]byte, 20)
rand.Read(rb)
ra, _ := bitcoin.NewRawAddressPKH(rb)
result, _ := ra.LockingScript()
return result
}
func randomAddress() bitcoin.RawAddress {
rb := make([]byte, 20)
rand.Read(rb)
result, _ := bitcoin.NewRawAddressPKH(rb)
return result
}
func TestAddFunding(t *testing.T) {
utxos := []bitcoin.UTXO{
bitcoin.UTXO{
Hash: *randomTxId(),
Index: 0,
Value: 10000,
LockingScript: randomLockingScript(),
KeyID: "m/0/1",
},
bitcoin.UTXO{
Hash: *randomTxId(),
Index: 0,
Value: 2000,
LockingScript: randomLockingScript(),
KeyID: "m/0/2",
},
bitcoin.UTXO{
Hash: *randomTxId(),
Index: 0,
Value: 1000,
LockingScript: randomLockingScript(),
KeyID: "m/0/3",
},
}
toAddress := randomAddress()
toScript, err := toAddress.LockingScript()
if err != nil {
t.Fatalf("Failed to create locking script : %s", err)
}
changeAddress := randomAddress()
changeScript, err := changeAddress.LockingScript()
if err != nil {
t.Fatalf("Failed to create locking script : %s", err)
}
// Change address needed ***********************************************************************
tx := NewTxBuilder(1.1, 1.0)
if err != nil {
t.Fatalf("Failed to build max send tx : %s", err)
}
tx.SetChangeAddress(changeAddress, "")
err = tx.AddPaymentOutput(toAddress, 600, false)
if err != nil {
t.Fatalf("Failed to payment : %s", err)
}
err = tx.AddFunding(utxos)
if err != nil {
t.Fatalf("Failed to add funding : %s", err)
}
fee := float32(tx.Fee())
t.Logf("Fee : %d", uint64(fee))
t.Logf("Estimated Fee : %d", uint64(float32(tx.EstimatedSize())*1.1))
estimatedFee := float32(tx.EstimatedSize()) * 1.1
low := estimatedFee * 0.95
high := estimatedFee * 1.05
if fee < low || fee > high {
t.Fatalf("Incorrect fee : got %f, want %f", fee, estimatedFee)
}
if !bytes.Equal(tx.MsgTx.TxOut[0].LockingScript, toScript) {
t.Fatalf("Incorrect locking script : \ngot %s\nwant %s", tx.MsgTx.TxOut[0].LockingScript, toScript)
}
if !bytes.Equal(tx.MsgTx.TxOut[1].LockingScript, changeScript) {
t.Fatalf("Incorrect locking script : \ngot %s\nwant %s", tx.MsgTx.TxOut[1].LockingScript, changeScript)
}
// Already has change output *******************************************************************
tx = NewTxBuilder(1.1, 1.0)
if err != nil {
t.Fatalf("Failed to build max send tx : %s", err)
}
tx.SetChangeAddress(changeAddress, "")
err = tx.AddPaymentOutput(toAddress, 600, false)
if err != nil {
t.Fatalf("Failed to payment : %s", err)
}
err = tx.AddPaymentOutput(changeAddress, 700, true)
if err != nil {
t.Fatalf("Failed to payment : %s", err)
}
err = tx.AddFunding(utxos)
if err != nil {
t.Fatalf("Failed to add funding : %s", err)
}
fee = float32(tx.Fee())
t.Logf("Fee : %d", uint64(fee))
t.Logf("Estimated Fee : %d", uint64(float32(tx.EstimatedSize())*1.1))
estimatedFee = float32(tx.EstimatedSize()) * 1.1
low = estimatedFee * 0.95
high = estimatedFee * 1.05
if fee < low || fee > high {
t.Fatalf("Incorrect fee : got %f, want %f", fee, estimatedFee)
}
if !bytes.Equal(tx.MsgTx.TxOut[0].LockingScript, toScript) {
t.Fatalf("Incorrect locking script : \ngot %s\nwant %s", tx.MsgTx.TxOut[0].LockingScript, toScript)
}
if !bytes.Equal(tx.MsgTx.TxOut[1].LockingScript, changeScript) {
t.Fatalf("Incorrect locking script : \ngot %s\nwant %s", tx.MsgTx.TxOut[1].LockingScript, changeScript)
}
// Change is dust ******************************************************************************
tx = NewTxBuilder(1.1, 1.0)
if err != nil {
t.Fatalf("Failed to build max send tx : %s", err)
}
tx.SetChangeAddress(changeAddress, "")
err = tx.AddPaymentOutput(toAddress, 600, false)
if err != nil {
t.Fatalf("Failed to payment : %s", err)
}
utxos[0].Value = 900
err = tx.AddFunding(utxos)
if err != nil {
t.Fatalf("Failed to add funding : %s", err)
}
fee = float32(tx.Fee())
t.Logf("Fee : %d", uint64(fee))
t.Logf("Estimated Fee : %d", uint64(float32(tx.EstimatedSize())*1.1))
estimatedFee = float32(tx.EstimatedSize()) * 1.1
low = estimatedFee * 0.95
high = 305
if fee < low || fee > high {
t.Fatalf("Incorrect fee : got %f, want %f", fee, estimatedFee)
}
if !bytes.Equal(tx.MsgTx.TxOut[0].LockingScript, toScript) {
t.Fatalf("Incorrect locking script : \ngot %s\nwant %s", tx.MsgTx.TxOut[0].LockingScript, toScript)
}
if len(tx.Outputs) != 1 {
t.Fatalf("Incorrect output count : got %d, want %d", len(tx.Outputs), 1)
}
}
func TestSendMax(t *testing.T) {
utxos := []bitcoin.UTXO{
bitcoin.UTXO{
Hash: *randomTxId(),
Index: 0,
Value: 10000,
LockingScript: randomLockingScript(),
KeyID: "m/0/1",
},
bitcoin.UTXO{
Hash: *randomTxId(),
Index: 0,
Value: 2000,
LockingScript: randomLockingScript(),
KeyID: "m/0/2",
},
bitcoin.UTXO{
Hash: *randomTxId(),
Index: 0,
Value: 1000,
LockingScript: randomLockingScript(),
KeyID: "m/0/3",
},
}
toAddress := randomAddress()
toScript, err := toAddress.LockingScript()
if err != nil {
t.Fatalf("Failed to create locking script : %s", err)
}
toAddress2 := randomAddress()
toScript2, err := toAddress2.LockingScript()
if err != nil {
t.Fatalf("Failed to create locking script : %s", err)
}
tx := NewTxBuilder(1.1, 1.0)
if err != nil {
t.Fatalf("Failed to build max send tx : %s", err)
}
tx.AddPaymentOutput(toAddress, 1000, false)
if err := tx.AddMaxOutput(toAddress2); err != nil {
t.Fatalf("Failed to add max output : %s", err)
}
err = tx.AddFunding(utxos[:1])
if err != nil {
t.Fatalf("Failed to add funding : %s", err)
}
if len(tx.Inputs) != 1 {
t.Fatalf("Incorrect input count : got %d, want %d", len(tx.Inputs), 1)
}
if len(tx.Outputs) != 2 {
t.Fatalf("Incorrect output count : got %d, want %d", len(tx.Outputs), 2)
}
if !bytes.Equal(tx.MsgTx.TxOut[0].LockingScript, toScript) {
t.Fatalf("Incorrect locking script : \ngot %s\nwant %s", tx.MsgTx.TxOut[0].LockingScript, toScript)
}
fee := float32(tx.Fee())
estimatedFee := float32(tx.EstimatedSize()) * 1.1
low := estimatedFee * 0.95
high := estimatedFee * 1.05
if fee < low || fee > high {
t.Fatalf("Incorrect fee : got %f, want %f", fee, estimatedFee)
}
// Attempt with 3 inputs ***********************************************************************
tx = NewTxBuilder(1.1, 1.0)
if err != nil {
t.Fatalf("Failed to build max send tx : %s", err)
}
tx.AddMaxOutput(toAddress)
err = tx.AddFunding(utxos)
if err != nil {
t.Fatalf("Failed to add funding : %s", err)
}
if len(tx.Inputs) != 3 {
t.Fatalf("Incorrect input count : got %d, want %d", len(tx.Inputs), 3)
}
if len(tx.Outputs) != 1 {
t.Fatalf("Incorrect output count : got %d, want %d", len(tx.Outputs), 1)
}
if !bytes.Equal(tx.MsgTx.TxOut[0].LockingScript, toScript) {
t.Fatalf("Incorrect locking script : \ngot %s\nwant %s", tx.MsgTx.TxOut[0].LockingScript, toScript)
}
fee = float32(tx.Fee())
estimatedFee = float32(tx.EstimatedSize()) * 1.1
low = estimatedFee * 0.95
high = estimatedFee * 1.05
if fee < low || fee > high {
t.Fatalf("Incorrect fee : got %f, want %f", fee, estimatedFee)
}
// Attempt with 2 addresses ********************************************************************
tx = NewTxBuilder(1.1, 1.0)
if err != nil {
t.Fatalf("Failed to build max send tx : %s", err)
}
tx.AddPaymentOutput(toAddress, 5000, false)
tx.AddMaxOutput(toAddress2)
err = tx.AddFunding(utxos)
if err != nil {
t.Fatalf("Failed to add funding : %s", err)
}
if len(tx.Inputs) != 3 {
t.Fatalf("Incorrect input count : got %d, want %d", len(tx.Inputs), 3)
}
if len(tx.Outputs) != 2 {
t.Fatalf("Incorrect output count : got %d, want %d", len(tx.Outputs), 2)
}
if !bytes.Equal(tx.MsgTx.TxOut[0].LockingScript, toScript) {
t.Fatalf("Incorrect locking script : \ngot %s\nwant %s", tx.MsgTx.TxOut[0].LockingScript, toScript)
}
if !bytes.Equal(tx.MsgTx.TxOut[1].LockingScript, toScript2) {
t.Fatalf("Incorrect locking script : \ngot %s\nwant %s", tx.MsgTx.TxOut[1].LockingScript, toScript2)
}
fee = float32(tx.Fee())
estimatedFee = float32(tx.EstimatedSize()) * 1.1
low = estimatedFee * 0.95
high = estimatedFee * 1.05
if fee < low || fee > high {
t.Fatalf("Incorrect fee : got %f, want %f", fee, estimatedFee)
}
}
func Test_LowFeeFunding(t *testing.T) {
sendKey, err := bitcoin.GenerateKey(bitcoin.MainNet)
if err != nil {
t.Fatalf("Failed to generate key : %s", err)
}
sendLockingScript, err := sendKey.LockingScript()
if err != nil {
t.Fatalf("Failed to create locking script : %s", err)
}
receiveKey, err := bitcoin.GenerateKey(bitcoin.MainNet)
if err != nil {
t.Fatalf("Failed to generate key : %s", err)
}
receiveLockingScript, err := receiveKey.LockingScript()
if err != nil {
t.Fatalf("Failed to create locking script : %s", err)
}
changeAddresses := make([]AddressKeyID, 5)
for i := range changeAddresses {
changeKey, err := bitcoin.GenerateKey(bitcoin.MainNet)
if err != nil {
t.Fatalf("Failed to generate key : %s", err)
}
ra, err := changeKey.RawAddress()
if err != nil {
t.Fatalf("Failed to create address : %s", err)
}
changeAddresses[i] = AddressKeyID{
Address: ra,
}
}
utxos := []bitcoin.UTXO{
{
Index: 0,
Value: 10000,
LockingScript: sendLockingScript,
},
}
rand.Read(utxos[0].Hash[:])
scriptString := "OP_FALSE OP_RETURN 0xbd01 OP_1 0x49 0x11 OP_FALSE OP_4 OP_3 OP_1 OP_1 OP_1 OP_2 OP_1 0x7374616e64617264 OP_2 OP_1 OP_2 0xe803 OP_3 0x743fb262 OP_4 0x844db262"
script, err := bitcoin.StringToScript(scriptString)
if err != nil {
t.Fatalf("Failed to parse script : %s", err)
}
tx := NewTxBuilder(0.05, 0.05)
tx.AddOutput(receiveLockingScript, 1000, false, false)
tx.AddOutput(script, 0, false, false)
if err := tx.AddFundingBreakChange(utxos, 10000, changeAddresses); err != nil {
t.Fatalf("Failed to add funding : %s", err)
}
t.Logf(tx.String(bitcoin.MainNet))
t.Logf("Estimated Size : %d", tx.EstimatedSize())
t.Logf("Fee : %d", tx.Fee())
t.Logf("Estimated Fee : %d", tx.EstimatedFee())
if tx.Fee() != tx.EstimatedFee() {
t.Fatalf("Wrong fee : got %d, want %d", tx.Fee(), tx.EstimatedFee())
}
}
func Test_Sign_LowFeeChange(t *testing.T) {
for i := 0; i < 20; i++ {
if err := test_Sign_LowFeeChange(t); err != nil {
t.Errorf("Failed test %d : %s", i, err)
}
}
}
func test_Sign_LowFeeChange(t *testing.T) error {
contractKey, err := bitcoin.GenerateKey(bitcoin.MainNet)
if err != nil {
return errors.Wrap(err, "generate key")
}
contractLockingScript, err := contractKey.LockingScript()
if err != nil {
return errors.Wrap(err, "generate locking script")
}
changeKey, err := bitcoin.GenerateKey(bitcoin.MainNet)
if err != nil {
return errors.Wrap(err, "generate key")
}
changeLockingScript, err := changeKey.LockingScript()
if err != nil {
return errors.Wrap(err, "generate locking script")
}
receiver1Key, err := bitcoin.GenerateKey(bitcoin.MainNet)
if err != nil {
return errors.Wrap(err, "generate key")
}
receiver1LockingScript, err := receiver1Key.LockingScript()
if err != nil {
return errors.Wrap(err, "generate locking script")
}
receiver2Key, err := bitcoin.GenerateKey(bitcoin.MainNet)
if err != nil {
return errors.Wrap(err, "generate key")
}
receiver2LockingScript, err := receiver2Key.LockingScript()
if err != nil {
return errors.Wrap(err, "generate locking script")
}
feeKey, err := bitcoin.GenerateKey(bitcoin.MainNet)
if err != nil {
return errors.Wrap(err, "generate key")
}
feeLockingScript, err := feeKey.LockingScript()
if err != nil {
return errors.Wrap(err, "generate locking script")
}
utxo := bitcoin.UTXO{
Index: 0,
Value: 2021,
LockingScript: contractLockingScript,
}
rand.Read(utxo.Hash[:])
settlementScriptString := "OP_0 OP_RETURN 0xbd01 OP_1 \"test.TKN\" OP_3 0x00 \"T2\" 0x0a2b12034343591a14fa26cd2b12baee089cb5cc00fb52489974231424220710f8a8dbc3f4022205080110882710d9f3effde8e9eb8417"
settlementScript, err := bitcoin.StringToScript(settlementScriptString)
if err != nil {
return errors.Wrap(err, "parse script")
}
tx := NewTxBuilder(0.05, 0.00)
tx.SetChangeLockingScript(changeLockingScript, "")
tx.AddInputUTXO(utxo)
tx.AddOutput(receiver1LockingScript, 1, false, false)
tx.AddOutput(receiver2LockingScript, 1, false, false)
tx.AddOutput(feeLockingScript, 2000, false, false)
tx.AddOutput(settlementScript, 0, false, false)
if _, err := tx.Sign([]bitcoin.Key{contractKey}); err != nil {
return errors.Wrap(err, "sign")
}
for i, txout := range tx.MsgTx.TxOut {
if txout.Value != 0 {
continue
}
if txout.LockingScript[0] == bitcoin.OP_FALSE {
continue
}
t.Logf(tx.String(bitcoin.MainNet))
return fmt.Errorf("Zero output : %d", i)
}
fee := tx.Fee()
estimatedFee := tx.EstimatedFee()
t.Logf(tx.String(bitcoin.MainNet))
t.Logf("Size : %d", tx.MsgTx.SerializeSize())
t.Logf("Estimated Size : %d", tx.EstimatedSize())
t.Logf("Fee : %d", fee)
t.Logf("Estimated Fee : %d", estimatedFee)
_, changeInputFee, _ := OutputTotalCost(changeLockingScript, tx.FeeRate)
if fee < estimatedFee || fee > estimatedFee+changeInputFee {
return fmt.Errorf("Wrong fee : got %d, want %d", fee, estimatedFee)
}
return nil
}
// Test_Sign_LowFee_Payload was designed around a specific instance found where the fee was over 5%
// off the estimate, but only off 1 satoshi. So the signing function attempted 3 adjustements, but
// only flipped from 1 to -1 fee difference.
func Test_Sign_LowFee_Payload(t *testing.T) {
for i := 0; i < 500; i++ {
if err := test_Sign_LowFee_Payload(t); err != nil {
t.Fatalf("Failed test %d : %s", i, err)
}
}
}
func test_Sign_LowFee_Payload(t *testing.T) error {
contractKey, err := bitcoin.GenerateKey(bitcoin.MainNet)
if err != nil {
return errors.Wrap(err, "generate key")
}
contractLockingScript, err := contractKey.LockingScript()
if err != nil {
return errors.Wrap(err, "generate locking script")
}
senderKey, err := bitcoin.GenerateKey(bitcoin.MainNet)
if err != nil {
return errors.Wrap(err, "generate key")
}
senderLockingScript, err := senderKey.LockingScript()
if err != nil {
return errors.Wrap(err, "generate locking script")
}
utxo := bitcoin.UTXO{
Index: 0,
Value: 200,
LockingScript: senderLockingScript,
}
rand.Read(utxo.Hash[:])
transferScriptString := "OP_0 OP_RETURN 0xbd01 OP_1 \"test.TKN\" OP_3 0x00 \"T1\" 0x0a7312034343591a14e14896b76a4a8eaeb3e23f59547d5e58c871408422031084182a1a0a152010f41740bdf088a667d13d3a02f9c248daa553b710d00f2a1a0a152085db4c79be1d8863c70c35f4d5b1ec1ada555f8610b2082a190a15202f4d84d5cabd6b38f0f6ce8912e49be2b8b980cf1002"
transferScript, err := bitcoin.StringToScript(transferScriptString)
if err != nil {
return errors.Wrap(err, "parse script")
}
tx := NewTxBuilder(0.05, 0.00)
tx.AddInputUTXO(utxo)
tx.AddOutput(contractLockingScript, 100, true, false)
tx.AddOutput(transferScript, 0, false, false)
if _, err := tx.Sign([]bitcoin.Key{senderKey}); err != nil {
return errors.Wrap(err, "sign")
}
if tx.MsgTx.TxOut[0].Value != 183 {
return nil
}
actualSize := tx.MsgTx.SerializeSize()
feeRate := float64(tx.Fee()) / float64(actualSize)
if feeRate >= 0.05 {
return nil
}
t.Errorf("Fee Rate : %0.4f", feeRate)
t.Logf(tx.String(bitcoin.MainNet))
t.Logf("Unlocking script size : %d", len(tx.MsgTx.TxIn[0].UnlockingScript))
t.Logf("Estimated Size : %d", tx.EstimatedSize())
t.Logf("Actual Size : %d", actualSize)
t.Logf("Estimated Fee : %d", tx.EstimatedFee())
t.Logf("Actual Fee : %d", tx.Fee())
estimatedSize := BaseTxSize + wire.VarIntSerializeSize(uint64(len(tx.MsgTx.TxIn))) +
wire.VarIntSerializeSize(uint64(len(tx.MsgTx.TxOut)))
// t.Logf("Estimated base size : %d", estimatedSize)
for i, input := range tx.Inputs {
size, err := InputSize(input.LockingScript)
if err != nil {
t.Errorf("Error calculating input size : %s", err)
estimatedSize += MaximumP2PKHInputSize // Fall back to P2PKH
continue
}
t.Logf("Estimated input size : %d", size)
t.Logf("Actual input size : %d", tx.MsgTx.TxIn[i].SerializeSize())
estimatedSize += size
}
// t.Logf("Estimated size with inputs : %d", estimatedSize)
for _, output := range tx.MsgTx.TxOut {
t.Logf("Output size : %d", output.SerializeSize())
estimatedSize += output.SerializeSize()
}
// t.Logf("Estimated size with outputs : %d", estimatedSize)
return errors.New("Failed")
}