-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreceive_test.go
259 lines (223 loc) · 6.76 KB
/
receive_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
257
258
259
package bip352
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"testing"
)
func TestReceiverScanTransaction(t *testing.T) {
caseData, err := LoadFullCaseData(t)
if err != nil {
return
}
for iOuter, cases := range caseData {
for _, testCase := range cases.Receiving {
fmt.Println(iOuter, cases.Comment)
// extract privateKeys
var secKeyScanBytes []byte
secKeyScanBytes, err = hex.DecodeString(testCase.Given.KeyMaterial.ScanPrivKey)
if err != nil {
t.Errorf("Error: %s", err)
return
}
secKeyScan := ConvertToFixedLength32(secKeyScanBytes)
// extract keys
var secKeySpendBytes []byte
secKeySpendBytes, err = hex.DecodeString(testCase.Given.KeyMaterial.SpendPrivKey)
if err != nil {
t.Errorf("Error: %s", err)
return
}
secKeySpend := ConvertToFixedLength32(secKeySpendBytes)
_, scanPubKey := btcec.PrivKeyFromBytes(secKeyScan[:])
_, spendPubKey := btcec.PrivKeyFromBytes(secKeySpend[:])
// compute label data
var labels []*Label
for _, labelInt := range testCase.Given.Labels {
var label Label
label, err = CreateLabel(secKeyScan, labelInt)
if err != nil {
t.Errorf("Error: %s", err)
return
}
var labeledAddress string
// todo not happy with this API yet should be able to create an address without introducing the secretKey again
labeledAddress, err = CreateLabeledAddress(
ConvertToFixedLength33(scanPubKey.SerializeCompressed()),
ConvertToFixedLength33(spendPubKey.SerializeCompressed()),
true,
0,
secKeyScan,
labelInt,
)
if err != nil {
t.Errorf("Error: %s", err)
return
}
label.Address = labeledAddress
labels = append(labels, &label)
}
// check the generated addresses
containsMap := make(map[string]struct{})
var address string
address, err = CreateAddress(
ConvertToFixedLength33(scanPubKey.SerializeCompressed()),
ConvertToFixedLength33(spendPubKey.SerializeCompressed()),
true,
0,
)
if err != nil {
t.Errorf("Error: %s", err)
return
}
containsMap[address] = struct{}{}
for _, label := range labels {
containsMap[label.Address] = struct{}{}
}
for _, targetAddress := range testCase.Expected.Addresses {
_, exists := containsMap[targetAddress]
if !exists {
t.Errorf("Error: missing %s", targetAddress)
return
}
}
// get the txOutputs of the transactions
var txOutputs [][32]byte
for _, output := range testCase.Given.Outputs {
var decodedString []byte
decodedString, err = hex.DecodeString(output)
if err != nil {
t.Errorf("Error: %s", err)
return
}
txOutputs = append(txOutputs, ConvertToFixedLength32(decodedString))
}
// compute tweak data
var publicComponent [33]byte
var inputHash [32]byte
publicComponent, inputHash, err = ExtractTweak(testCase.Given.Vin)
if err != nil && !errors.Is(err, noErrJustSkip) {
t.Errorf("Error: %s", err)
return
} else if err != nil && errors.Is(err, noErrJustSkip) {
t.Log("we skipped")
continue
}
var foundOutputs []*FoundOutput
foundOutputs, err = ReceiverScanTransaction(
secKeyScan,
ConvertToFixedLength33(spendPubKey.SerializeCompressed()),
labels,
txOutputs,
publicComponent,
&inputHash,
)
if err != nil {
t.Errorf("Error: %s", err)
return
}
if len(foundOutputs) != len(testCase.Expected.Outputs) {
t.Errorf("Error: wrong number outputs found %d != %d", len(foundOutputs), len(testCase.Expected.Outputs))
return
}
// todo come up with test to check for labels properly found and added to foundOutput
for iInner, foundOutput := range foundOutputs {
targetPubKey, _ := hex.DecodeString(testCase.Expected.Outputs[iInner].PubKey)
targetPrivKeyTweak, _ := hex.DecodeString(testCase.Expected.Outputs[iInner].PrivKeyTweak)
if !bytes.Equal(foundOutput.Output[:], targetPubKey) {
t.Errorf("Error: output not matched %x != %x", foundOutput.Output, targetPubKey)
return
}
if !bytes.Equal(foundOutput.SecKeyTweak[:], targetPrivKeyTweak) {
t.Errorf("Error: output not matched %x != %x", foundOutput.Output, targetPubKey)
return
}
// check signatures
// Message and auxiliary data
message := []byte("message")
aux := []byte("random auxiliary data")
// Hashing message and auxiliary data
msgHash := sha256.Sum256(message)
auxHash := sha256.Sum256(aux)
fullPrivKeyBytes := AddPrivateKeys(secKeySpend, foundOutput.SecKeyTweak)
fullPrivKey, _ := btcec.PrivKeyFromBytes(fullPrivKeyBytes[:])
// Sign the message with auxiliary data influencing the nonce
var signature *schnorr.Signature
signature, err = schnorr.Sign(fullPrivKey, msgHash[:], schnorr.CustomNonce(auxHash))
if err != nil {
t.Errorf("Error: %s", err)
return
}
var parsedOutput *btcec.PublicKey
parsedOutput, err = btcec.ParsePubKey(append([]byte{0x02}, foundOutput.Output[:]...))
if err != nil {
t.Errorf("Error: %s", err)
return
}
valid := signature.Verify(msgHash[:], parsedOutput)
if !valid {
t.Errorf("Error: signature was not valid")
return
}
}
}
}
}
// ExtractTweak
// returns: publicComponent, inputHash, error
func ExtractTweak(caseDataVins []VinReceiveTestCase) ([33]byte, [32]byte, error) {
// convert the test vins into proper vins
var pubKeys [][33]byte
var vins []*Vin
for _, vin := range caseDataVins {
txid, _ := hex.DecodeString(vin.Txid)
scriptSig, _ := hex.DecodeString(vin.ScriptSig)
scriptPubKey, _ := hex.DecodeString(vin.Prevout.ScriptPubKey.Hex)
witness, _ := hex.DecodeString(vin.Txinwitness)
var witnessScript [][]byte
if len(witness) > 0 {
var err error
witnessScript, err = ParseWitnessScript(witness)
if err != nil {
return [33]byte{}, [32]byte{}, err
}
}
vinInner := &Vin{
Txid: ConvertToFixedLength32(txid),
Vout: vin.Vout,
Witness: witnessScript,
ScriptPubKey: scriptPubKey,
ScriptSig: scriptSig,
}
vins = append(vins, vinInner)
pubKey, utxoType := ExtractPubKey(vinInner)
if utxoType == Unknown {
continue
}
// skip in case no pub key was extracted and no error was thrown
if pubKey == nil {
continue
}
if utxoType == P2TR {
pubKey = append([]byte{0x02}, pubKey...)
}
pubKeys = append(pubKeys, ConvertToFixedLength33(pubKey))
}
if len(pubKeys) == 0 {
return [33]byte{}, [32]byte{}, noErrJustSkip
}
sumPublicKeys, err := SumPublicKeys(pubKeys)
if err != nil {
return [33]byte{}, [32]byte{}, err
}
// compute inputHash
inputHash, err := ComputeInputHash(vins, sumPublicKeys)
if err != nil {
return [33]byte{}, [32]byte{}, err
}
return sumPublicKeys, inputHash, err
}