-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsend_test.go
209 lines (179 loc) · 5.37 KB
/
send_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
package bip352
import (
"encoding/hex"
"errors"
"fmt"
"testing"
)
func TestSenderCreateOutputs(t *testing.T) {
caseData, err := LoadFullCaseData(t)
if err != nil {
t.Errorf("Error: %s", err)
return
}
for i, cases := range caseData {
if cases.Comment == "Single recipient: taproot input with NUMS point" ||
cases.Comment == "P2PKH and P2WPKH Uncompressed Keys are skipped" ||
cases.Comment == "Skip invalid P2SH inputs" ||
cases.Comment == "No valid inputs, sender generates no outputs" {
// This library does not focus on extracting the correct public keys from an input.
// This feature might be added in future versions but as of now this will be skipped.
// The focus is to provide the basic wrapper around the EC computations and other lower level computations.
// Check the BIP352 reference implementation or https://github.com/setavenger/BlindBit-Backend/blob/61a47e5c657bd48c55ac6acec91e0a26d115e7f2/src/core/tweak.go#L307
// to see how sanitizing the inputs could be attempted
// todo add module to sanitize
continue
}
for _, testCase := range cases.Sending {
fmt.Println(i, cases.Comment)
var vins []*Vin
var recipients []*Recipient
for _, vin := range testCase.Given.Vin {
var txid []byte
var secKey []byte
txid, err = hex.DecodeString(vin.Txid)
if err != nil {
t.Errorf("Error: %s", err)
return
}
secKey, err = hex.DecodeString(vin.PrivateKey)
if err != nil {
t.Errorf("Error: %s", err)
return
}
var scriptPubKey []byte
scriptPubKey, err = hex.DecodeString(vin.Prevout.ScriptPubKey.Hex)
if err != nil {
t.Errorf("Error: %s", err)
return
}
interimSecKey := ConvertToFixedLength32(secKey)
vins = append(vins, &Vin{
Txid: ConvertToFixedLength32(txid),
Vout: vin.Vout,
PublicKey: nil,
SecretKey: &interimSecKey,
Taproot: isP2TR(scriptPubKey),
})
}
for _, recipient := range testCase.Given.Recipients {
recipients = append(recipients, &Recipient{
SilentPaymentAddress: recipient,
})
}
err = SenderCreateOutputs(recipients, vins, true, false)
if err != nil {
t.Errorf("Error: %s", err)
return
}
var containsMap = map[string]struct{}{}
for _, recipient := range recipients {
//t.Logf("B_scan: %x", recipient.ScanPubKey.SerializeCompressed())
containsMap[hex.EncodeToString(recipient.Output[:])] = struct{}{}
}
foundCounter := 0
for _, targetOutput := range testCase.Expected.Outputs {
_, exists := containsMap[targetOutput]
if exists {
foundCounter++
}
}
if foundCounter != len(recipients) {
t.Errorf("Error: did not find enough correct outputs")
t.Errorf("Error: Found %d expected %d", foundCounter, len(recipients))
return
}
}
}
}
func TestSenderCreateOutputsWithVinCheck(t *testing.T) {
caseData, err := LoadFullCaseData(t)
if err != nil {
t.Errorf("Error: %s", err)
return
}
for i, cases := range caseData {
fmt.Println(i, cases.Comment)
for _, testCase := range cases.Sending {
var vins []*Vin
var recipients []*Recipient
for _, vin := range testCase.Given.Vin {
var txid []byte
var secKey []byte
txid, err = hex.DecodeString(vin.Txid)
if err != nil {
t.Errorf("Error: %s", err)
return
}
secKey, err = hex.DecodeString(vin.PrivateKey)
if err != nil {
t.Errorf("Error: %s", err)
return
}
var scriptPubKey []byte
scriptPubKey, err = hex.DecodeString(vin.Prevout.ScriptPubKey.Hex)
if err != nil {
t.Errorf("Error: %s", err)
return
}
var scriptSig []byte
scriptSig, err = hex.DecodeString(vin.ScriptSig)
if err != nil {
t.Errorf("Error: %s", err)
return
}
witness, _ := hex.DecodeString(vin.Txinwitness)
var witnessScript [][]byte
if len(witness) > 0 {
witnessScript, err = ParseWitnessScript(witness)
if err != nil {
t.Errorf("Error: %s", err)
return
}
}
interimSecKey := ConvertToFixedLength32(secKey)
vins = append(vins, &Vin{
Txid: ConvertToFixedLength32(txid),
Vout: vin.Vout,
PublicKey: nil,
ScriptPubKey: scriptPubKey,
ScriptSig: scriptSig,
Witness: witnessScript,
SecretKey: &interimSecKey,
Taproot: isP2TR(scriptPubKey),
})
}
for _, recipient := range testCase.Given.Recipients {
recipients = append(recipients, &Recipient{
SilentPaymentAddress: recipient,
})
}
err = SenderCreateOutputs(recipients, vins, true, true)
if err != nil {
if cases.Comment == "No valid inputs, sender generates no outputs" && errors.Is(err, ErrNoEligibleVins) {
continue
}
t.Errorf("Error: %s", err)
return
}
var containsMap = map[string]struct{}{}
for _, recipient := range recipients {
//t.Logf("B_scan: %x", recipient.ScanPubKey.SerializeCompressed())
containsMap[hex.EncodeToString(recipient.Output[:])] = struct{}{}
}
foundCounter := 0
for _, targetOutput := range testCase.Expected.Outputs {
_, exists := containsMap[targetOutput]
if exists {
foundCounter++
}
}
if foundCounter != len(recipients) {
t.Errorf("Error: did not find enough correct outputs")
t.Errorf("Error: Found %d expected %d", foundCounter, len(recipients))
return
}
}
}
}
// todo write test to check that stored data in recipient stays there