@@ -14,6 +14,7 @@ import (
14
14
"sync"
15
15
16
16
"github.com/btcsuite/btcd/btcec/v2"
17
+ "github.com/setavenger/go-bip352"
17
18
)
18
19
19
20
func ComputeTweaksForBlock (block * types.Block ) ([]types.Tweak , error ) {
@@ -287,24 +288,15 @@ func ComputeTweakPerTx(tx types.Transaction) (*types.Tweak, error) {
287
288
288
289
x , y := curve .ScalarMult (summedKey .X (), summedKey .Y (), hash [:])
289
290
290
- // sometimes an uneven number hex string is returned, so we have to pad the zeros
291
- s := fmt .Sprintf ("%x" , x )
292
- s = fmt .Sprintf ("%064s" , s )
291
+ tweakBytes := [33 ]byte {}
293
292
mod := y .Mod (y , big .NewInt (2 ))
294
293
if mod .Cmp (big .NewInt (0 )) == 0 {
295
- s = "02" + s
294
+ tweakBytes [ 0 ] = 0x02
296
295
} else {
297
- s = "03" + s
296
+ tweakBytes [ 0 ] = 0x03
298
297
}
299
298
300
- decodedString , err := hex .DecodeString (s )
301
-
302
- if err != nil {
303
- common .ErrorLogger .Println (err )
304
- return nil , err
305
- }
306
- tweakBytes := [33 ]byte {}
307
- copy (tweakBytes [:], decodedString )
299
+ x .FillBytes (tweakBytes [1 :])
308
300
309
301
highestValue , err := FindBiggestOutputFromTx (tx )
310
302
if err != nil {
@@ -353,7 +345,7 @@ func extractPubKeys(tx types.Transaction) []string {
353
345
switch vin .Prevout .ScriptPubKey .Type {
354
346
case "witness_v1_taproot" :
355
347
// todo needs some extra parsing see reference implementation and bitcoin core wallet
356
- pubKey , err := extractPubKeyHashFromP2TR (vin )
348
+ pubKey , err := extractPubKeyFromP2TR (vin )
357
349
if err != nil {
358
350
common .DebugLogger .Println ("txid:" , tx .Txid )
359
351
common .DebugLogger .Println ("Could not extract public key" )
@@ -428,7 +420,7 @@ func extractFromP2PKH(vin types.Vin) ([]byte, error) {
428
420
return nil , nil
429
421
}
430
422
431
- func extractPubKeyHashFromP2TR (vin types.Vin ) (string , error ) {
423
+ func extractPubKeyFromP2TR (vin types.Vin ) (string , error ) {
432
424
witnessStack := vin .Txinwitness
433
425
434
426
if len (witnessStack ) >= 1 {
@@ -489,21 +481,9 @@ func sumPublicKeys(pubKeys []string) (*btcec.PublicKey, error) {
489
481
if idx == 0 {
490
482
lastPubKey = publicKey
491
483
} else {
492
- var decodeString []byte
493
484
x , y := curve .Add (lastPubKey .X (), lastPubKey .Y (), publicKey .X (), publicKey .Y ())
494
485
495
- // in case big int omits leading zero
496
- sX := fmt .Sprintf ("%x" , x )
497
- sY := fmt .Sprintf ("%x" , y )
498
- sX = fmt .Sprintf ("%064s" , sX )
499
- sY = fmt .Sprintf ("%064s" , sY )
500
- decodeString , err = hex .DecodeString (fmt .Sprintf ("04%s%s" , sX , sY ))
501
- if err != nil {
502
- common .ErrorLogger .Println (err )
503
- return nil , err
504
- }
505
-
506
- lastPubKey , err = btcec .ParsePubKey (decodeString )
486
+ lastPubKey , err = bip352 .ConvertPointsToPublicKey (x , y )
507
487
if err != nil {
508
488
common .ErrorLogger .Println (err )
509
489
return nil , err
0 commit comments