forked from ordishs/go-bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
End2End_test.go
118 lines (96 loc) · 2.23 KB
/
End2End_test.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
package bitcoin
import (
"testing"
"github.com/bitcoinsv/bsvd/bsvec"
"github.com/libsv/go-bt"
"github.com/libsv/go-bt/bscript"
)
func TestSendRawTransactions(t *testing.T) {
// First we need a private key
priv, err := bsvec.NewPrivateKey(bsvec.S256())
if err != nil {
t.Fatal(err)
}
address, err := bscript.NewAddressFromPublicKey(priv.PubKey(), false) // false means "not mainnet"
if err != nil {
t.Fatal(err)
}
lockingScript, err := bscript.NewP2PKHFromAddress(address.AddressString)
if err != nil {
t.Fatal(err)
}
b, err := New("localhost", 18332, "bitcoin", "bitcoin", false)
if err != nil {
t.Fatal(err)
}
txid, err := b.SendToAddress(address.AddressString, 1.0)
if err != nil {
t.Fatal(err)
}
fundingTxHex, err := b.GetRawTransactionHex(txid)
if err != nil {
t.Fatal(err)
}
fundingTx, err := bt.NewTxFromString(*fundingTxHex)
if err != nil {
t.Fatal(err)
}
var vout int = -1
// Find the output that we can spend...
for i, out := range fundingTx.Outputs {
if out.LockingScript.ToString() == lockingScript.ToString() {
vout = i
break
}
}
if vout == -1 {
t.Fatal("Did not find a UTXO")
}
tx := bt.NewTx()
if err := tx.From(fundingTx.GetTxID(), uint32(vout), fundingTx.Outputs[vout].GetLockingScriptHexString(), fundingTx.Outputs[vout].Satoshis); err != nil {
t.Fatal(err)
}
amount := fundingTx.Outputs[vout].Satoshis / 2
if err := tx.PayTo(address.AddressString, amount); err != nil {
t.Fatal(err)
}
fees := []*bt.Fee{
{
FeeType: "standard",
MiningFee: bt.FeeUnit{
Satoshis: 500,
Bytes: 1000,
},
RelayFee: bt.FeeUnit{
Satoshis: 250,
Bytes: 1000,
},
},
{
FeeType: "data",
MiningFee: bt.FeeUnit{
Satoshis: 500,
Bytes: 1000,
},
RelayFee: bt.FeeUnit{
Satoshis: 250,
Bytes: 1000,
},
},
}
if err := tx.ChangeToAddress(address.AddressString, fees); err != nil {
t.Fatal(err)
}
if _, err = tx.SignAuto(&bt.InternalSigner{PrivateKey: priv, SigHashFlag: 0}); err != nil {
t.Fatal(err)
}
_, err = b.SendRawTransaction(tx.ToString())
if err != nil {
t.Log(err)
}
newTxid2, err := b.SendRawTransactionWithoutFeeCheckOrScriptCheck(tx.ToString())
if err != nil {
t.Log(err)
}
t.Logf("%+v", newTxid2)
}