-
Notifications
You must be signed in to change notification settings - Fork 26
/
txoutput_test.go
256 lines (217 loc) · 7.1 KB
/
txoutput_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
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
package bt_test
import (
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/libsv/go-bk/bip32"
"github.com/libsv/go-bk/chaincfg"
"github.com/libsv/go-bt/v2"
"github.com/libsv/go-bt/v2/bscript"
)
func TestNewP2PKHOutputFromPubKeyHashStr(t *testing.T) {
t.Parallel()
t.Run("empty pubkey hash", func(t *testing.T) {
tx := bt.NewTx()
err := tx.AddP2PKHOutputFromPubKeyHashStr(
"",
uint64(5000),
)
assert.NoError(t, err)
assert.Equal(t,
"76a91488ac",
tx.Outputs[0].LockingScriptHexString(),
)
})
t.Run("invalid pubkey hash", func(t *testing.T) {
tx := bt.NewTx()
err := tx.AddP2PKHOutputFromPubKeyHashStr(
"0",
uint64(5000),
)
assert.Error(t, err)
})
t.Run("valid output", func(t *testing.T) {
// This is the PKH for address mtdruWYVEV1wz5yL7GvpBj4MgifCB7yhPd
tx := bt.NewTx()
err := tx.AddP2PKHOutputFromPubKeyHashStr(
"8fe80c75c9560e8b56ed64ea3c26e18d2c52211b",
uint64(5000),
)
assert.NoError(t, err)
assert.Equal(t,
"76a9148fe80c75c9560e8b56ed64ea3c26e18d2c52211b88ac",
tx.Outputs[0].LockingScriptHexString(),
)
})
}
func TestNewHashPuzzleOutput(t *testing.T) {
t.Parallel()
t.Run("invalid public key", func(t *testing.T) {
tx := bt.NewTx()
err := tx.AddHashPuzzleOutput("", "0", uint64(5000))
assert.Error(t, err)
})
t.Run("missing secret and public key", func(t *testing.T) {
tx := bt.NewTx()
err := tx.AddHashPuzzleOutput("", "", uint64(5000))
assert.NoError(t, err)
assert.Equal(t,
"a914b472a266d0bd89c13706a4132ccfb16f7c3b9fcb8876a90088ac",
tx.Outputs[0].LockingScriptHexString(),
)
})
t.Run("valid puzzle output", func(t *testing.T) {
addr, err := bscript.NewAddressFromString("myFhJggmsaA2S8Qe6ZQDEcVCwC4wLkvC4e")
assert.NoError(t, err)
assert.NotNil(t, addr)
tx := bt.NewTx()
err = tx.AddHashPuzzleOutput("secret1", addr.PublicKeyHash, uint64(5000))
assert.NoError(t, err)
assert.Equal(t,
"a914d3f9e3d971764be5838307b175ee4e08ba427b908876a914c28f832c3d539933e0c719297340b34eee0f4c3488ac",
tx.Outputs[0].LockingScriptHexString(),
)
})
}
func TestNewOpReturnOutput(t *testing.T) {
t.Parallel()
data := "On February 4th, 2020 The Return to Genesis was activated to restore the Satoshi Vision for Bitcoin. " +
"It is locked in irrevocably by this transaction. Bitcoin can finally be Bitcoin again and the miners can " +
"continue to write the Chronicle of everything. Thank you and goodnight from team SV."
dataBytes := []byte(data)
tx := bt.NewTx()
err := tx.AddOpReturnOutput(dataBytes)
assert.NoError(t, err)
script := tx.Outputs[0].LockingScriptHexString()
dataLength := bt.VarInt(uint64(len(dataBytes))).Bytes()
assert.Equal(t, "006a4d2201"+hex.EncodeToString(dataBytes), script)
assert.Equal(t, "fd2201", fmt.Sprintf("%x", dataLength))
}
func TestNewOpReturnPartsOutput(t *testing.T) {
t.Parallel()
dataBytes := [][]byte{[]byte("hi"), []byte("how"), []byte("are"), []byte("you")}
tx := bt.NewTx()
err := tx.AddOpReturnPartsOutput(dataBytes)
assert.NoError(t, err)
assert.Equal(t, "006a02686903686f770361726503796f75", tx.Outputs[0].LockingScriptHexString())
}
func TestTx_TotalOutputSatoshis(t *testing.T) {
t.Parallel()
t.Run("greater than zero", func(t *testing.T) {
tx, err := bt.NewTxFromString("020000000180f1ada3ad8e861441d9ceab40b68ed98f13695b185cc516226a46697cc01f80010000006b483045022100fa3a0f8fa9fbf09c372b7a318fa6175d022c1d782f7b8bc5949a7c8f59ce3f35022005e0e84c26f26d892b484ff738d803a57626679389c8b302939460dab29a5308412103e46b62eea5db5898fb65f7dc840e8a1dbd8f08a19781a23f1f55914f9bedcd49feffffff02dec537b2000000001976a914ba11bcc46ecf8d88e0828ddbe87997bf759ca85988ac00943577000000001976a91418392a59fc1f76ad6a3c7ffcea20cfcb17bda9eb88ac6e000000")
assert.NoError(t, err)
assert.NotNil(t, tx)
assert.Equal(t, uint64((29.89999582+20.00)*1e8), tx.TotalOutputSatoshis())
})
t.Run("zero Outputs", func(t *testing.T) {
tx := bt.NewTx()
assert.NotNil(t, tx)
assert.Equal(t, uint64(0), tx.TotalOutputSatoshis())
})
}
func TestTx_PayToAddress(t *testing.T) {
t.Run("missing pay to address", func(t *testing.T) {
tx := bt.NewTx()
assert.NotNil(t, tx)
err := tx.From(
"07912972e42095fe58daaf09161c5a5da57be47c2054dc2aaa52b30fefa1940b",
0,
"76a914af2590a45ae401651fdbdf59a76ad43d1862534088ac",
4000000)
assert.NoError(t, err)
err = tx.PayToAddress("", 100)
assert.Error(t, err)
})
t.Run("invalid pay to address", func(t *testing.T) {
tx := bt.NewTx()
assert.NotNil(t, tx)
err := tx.From(
"07912972e42095fe58daaf09161c5a5da57be47c2054dc2aaa52b30fefa1940b",
0,
"76a914af2590a45ae401651fdbdf59a76ad43d1862534088ac",
4000000)
assert.NoError(t, err)
err = tx.PayToAddress("1234567", 100)
assert.Error(t, err)
})
t.Run("valid pay to address", func(t *testing.T) {
tx := bt.NewTx()
assert.NotNil(t, tx)
err := tx.From(
"07912972e42095fe58daaf09161c5a5da57be47c2054dc2aaa52b30fefa1940b",
0,
"76a914af2590a45ae401651fdbdf59a76ad43d1862534088ac",
4000000)
assert.NoError(t, err)
err = tx.PayToAddress("1GHMW7ABrFma2NSwiVe9b9bZxkMB7tuPZi", 100)
assert.NoError(t, err)
assert.Equal(t, 1, tx.OutputCount())
})
}
func TestTx_PayTo(t *testing.T) {
t.Parallel()
tests := map[string]struct {
script *bscript.Script
err error
}{
"valid p2pkh script should create valid output": {
script: func() *bscript.Script {
s, err := bscript.NewP2PKHFromAddress("1GHMW7ABrFma2NSwiVe9b9bZxkMB7tuPZi")
assert.NoError(t, err)
return s
}(),
err: nil,
}, "empty p2pkh script should return error": {
script: &bscript.Script{},
err: errors.New("'empty' is not a valid P2PKH script: invalid script type"),
}, "non p2pkh script should return error": {
script: bscript.NewFromBytes([]byte("test")),
err: errors.New("'nonstandard' is not a valid P2PKH script: invalid script type"),
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
tx := bt.NewTx()
assert.NotNil(t, tx)
err := tx.From(
"07912972e42095fe58daaf09161c5a5da57be47c2054dc2aaa52b30fefa1940b",
0,
"76a914af2590a45ae401651fdbdf59a76ad43d1862534088ac",
4000000)
assert.NoError(t, err)
err = tx.PayTo(test.script, 100)
if test.err == nil {
assert.NoError(t, err)
assert.Equal(t, 1, tx.OutputCount())
return
}
assert.EqualError(t, err, test.err.Error())
assert.Equal(t, 0, tx.OutputCount())
})
}
}
func TestTx_AddP2PKHOutputFromBip32ExtKey(t *testing.T) {
t.Run("output is added", func(t *testing.T) {
tx := bt.NewTx()
var b [64]byte
_, err := rand.Read(b[:])
assert.NoError(t, err)
key, err := bip32.NewMaster(b[:], &chaincfg.TestNet)
assert.NoError(t, err)
derivationPath, err := tx.AddP2PKHOutputFromBip32ExtKey(key, 6000)
assert.NoError(t, err)
assert.NotEmpty(t, derivationPath)
assert.Equal(t, 1, len(tx.Outputs))
assert.Equal(t, uint64(6000), tx.Outputs[0].Satoshis)
})
t.Run("invalid private key errors", func(t *testing.T) {
tx := bt.NewTx()
derivationPath, err := tx.AddP2PKHOutputFromBip32ExtKey(&bip32.ExtendedKey{}, 6000)
assert.Error(t, err)
assert.Empty(t, derivationPath)
assert.Equal(t, 0, len(tx.Outputs))
})
}