Skip to content

Commit 19ae1d6

Browse files
setavengerottosch
andauthored
Long overdue style fixes (#23)
* Refactor: - Use FillBytes() for zero padding instead of string formatting; - Rename PubKeyHash to PubKey in extractPubKeyHashFromP2TR; * Use bip352.ConvertPointsToPublicKey, add go-bip352 as depedency --------- Co-authored-by: ottosch <ottosch@local>
1 parent 4381d2b commit 19ae1d6

File tree

3 files changed

+24
-32
lines changed

3 files changed

+24
-32
lines changed

go.mod

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ module SilentPaymentAppBackend
33
go 1.20
44

55
require (
6-
github.com/btcsuite/btcd/btcec/v2 v2.2.0
7-
github.com/btcsuite/btcd/btcutil v1.1.3
8-
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1
6+
github.com/btcsuite/btcd/btcec/v2 v2.3.3
7+
github.com/btcsuite/btcd/btcutil v1.1.5
8+
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0
99
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d
1010
github.com/gin-contrib/cors v1.7.2
1111
github.com/gin-gonic/gin v1.9.1
@@ -17,7 +17,7 @@ require (
1717

1818
require (
1919
github.com/aead/siphash v1.0.1 // indirect
20-
github.com/btcsuite/btcd v0.23.4 // indirect
20+
github.com/btcsuite/btcd v0.23.5-0.20231215221805-96c9fd8078fd // indirect
2121
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f // indirect
2222
github.com/bytedance/sonic v1.11.6 // indirect
2323
github.com/bytedance/sonic/loader v0.1.1 // indirect
@@ -46,6 +46,7 @@ require (
4646
github.com/pelletier/go-toml/v2 v2.2.1 // indirect
4747
github.com/sagikazarmark/locafero v0.4.0 // indirect
4848
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
49+
github.com/setavenger/go-bip352 v0.1.6 // indirect
4950
github.com/sourcegraph/conc v0.3.0 // indirect
5051
github.com/spf13/afero v1.11.0 // indirect
5152
github.com/spf13/cast v1.6.0 // indirect

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,25 @@ github.com/btcsuite/btcd v0.22.0-beta.0.20220111032746-97732e52810c/go.mod h1:tj
55
github.com/btcsuite/btcd v0.23.0/go.mod h1:0QJIIN1wwIXF/3G/m87gIwGniDMDQqjVn4SZgnFpsYY=
66
github.com/btcsuite/btcd v0.23.4 h1:IzV6qqkfwbItOS/sg/aDfPDsjPP8twrCOE2R93hxMlQ=
77
github.com/btcsuite/btcd v0.23.4/go.mod h1:0QJIIN1wwIXF/3G/m87gIwGniDMDQqjVn4SZgnFpsYY=
8+
github.com/btcsuite/btcd v0.23.5-0.20231215221805-96c9fd8078fd h1:js1gPwhcFflTZ7Nzl7WHaOTlTr5hIrR4n1NM4v9n4Kw=
9+
github.com/btcsuite/btcd v0.23.5-0.20231215221805-96c9fd8078fd/go.mod h1:nm3Bko6zh6bWP60UxwoT5LzdGJsQJaPo6HjduXq9p6A=
810
github.com/btcsuite/btcd/btcec/v2 v2.1.0/go.mod h1:2VzYrv4Gm4apmbVVsSq5bqf1Ec8v56E48Vt0Y/umPgA=
911
github.com/btcsuite/btcd/btcec/v2 v2.1.3/go.mod h1:ctjw4H1kknNJmRN4iP1R7bTQ+v3GJkZBd6mui8ZsAZE=
1012
github.com/btcsuite/btcd/btcec/v2 v2.2.0 h1:fzn1qaOt32TuLjFlkzYSsBC35Q3KUjT1SwPxiMSCF5k=
1113
github.com/btcsuite/btcd/btcec/v2 v2.2.0/go.mod h1:U7MHm051Al6XmscBQ0BoNydpOTsFAn707034b5nY8zU=
14+
github.com/btcsuite/btcd/btcec/v2 v2.3.3 h1:6+iXlDKE8RMtKsvK0gshlXIuPbyWM/h84Ensb7o3sC0=
15+
github.com/btcsuite/btcd/btcec/v2 v2.3.3/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04=
1216
github.com/btcsuite/btcd/btcutil v1.0.0/go.mod h1:Uoxwv0pqYWhD//tfTiipkxNfdhG9UrLwaeswfjfdF0A=
1317
github.com/btcsuite/btcd/btcutil v1.1.0/go.mod h1:5OapHB7A2hBBWLm48mmw4MOHNJCcUBTwmWH/0Jn8VHE=
1418
github.com/btcsuite/btcd/btcutil v1.1.3 h1:xfbtw8lwpp0G6NwSHb+UE67ryTFHJAiNuipusjXSohQ=
1519
github.com/btcsuite/btcd/btcutil v1.1.3/go.mod h1:UR7dsSJzJUfMmFiiLlIrMq1lS9jh9EdCV7FStZSnpi0=
20+
github.com/btcsuite/btcd/btcutil v1.1.5 h1:+wER79R5670vs/ZusMTF1yTcRYE5GUsFbdjdisflzM8=
21+
github.com/btcsuite/btcd/btcutil v1.1.5/go.mod h1:PSZZ4UitpLBWzxGd5VGOrLnmOjtPP/a6HaFo12zMs00=
1622
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.0/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
1723
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U=
1824
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
25+
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 h1:59Kx4K6lzOW5w6nFlA0v5+lk/6sjybR934QNHSJZPTQ=
26+
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
1927
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f h1:bAs4lUbRJpnnkd9VhRV3jjAVU7DJVjMaK+IsvSeZvFo=
2028
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
2129
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d h1:yJzD/yFppdVCf6ApMkVy8cUxV0XrxdP9rVf6D87/Mng=
@@ -81,6 +89,7 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
8189
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
8290
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
8391
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
92+
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
8493
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
8594
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
8695
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
@@ -131,6 +140,8 @@ github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6ke
131140
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
132141
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
133142
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
143+
github.com/setavenger/go-bip352 v0.1.6 h1:7D1/RMLa+1XaP1ccdseGwgUUfUY20jVf5unAY679x3Y=
144+
github.com/setavenger/go-bip352 v0.1.6/go.mod h1:ajjkB64QrjbF0+MEUjeeBlBxDaJk7VmYUN8XbOK+EKo=
134145
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
135146
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
136147
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=

src/core/tweak.go

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"sync"
1515

1616
"github.com/btcsuite/btcd/btcec/v2"
17+
"github.com/setavenger/go-bip352"
1718
)
1819

1920
func ComputeTweaksForBlock(block *types.Block) ([]types.Tweak, error) {
@@ -287,24 +288,15 @@ func ComputeTweakPerTx(tx types.Transaction) (*types.Tweak, error) {
287288

288289
x, y := curve.ScalarMult(summedKey.X(), summedKey.Y(), hash[:])
289290

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{}
293292
mod := y.Mod(y, big.NewInt(2))
294293
if mod.Cmp(big.NewInt(0)) == 0 {
295-
s = "02" + s
294+
tweakBytes[0] = 0x02
296295
} else {
297-
s = "03" + s
296+
tweakBytes[0] = 0x03
298297
}
299298

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:])
308300

309301
highestValue, err := FindBiggestOutputFromTx(tx)
310302
if err != nil {
@@ -353,7 +345,7 @@ func extractPubKeys(tx types.Transaction) []string {
353345
switch vin.Prevout.ScriptPubKey.Type {
354346
case "witness_v1_taproot":
355347
// todo needs some extra parsing see reference implementation and bitcoin core wallet
356-
pubKey, err := extractPubKeyHashFromP2TR(vin)
348+
pubKey, err := extractPubKeyFromP2TR(vin)
357349
if err != nil {
358350
common.DebugLogger.Println("txid:", tx.Txid)
359351
common.DebugLogger.Println("Could not extract public key")
@@ -428,7 +420,7 @@ func extractFromP2PKH(vin types.Vin) ([]byte, error) {
428420
return nil, nil
429421
}
430422

431-
func extractPubKeyHashFromP2TR(vin types.Vin) (string, error) {
423+
func extractPubKeyFromP2TR(vin types.Vin) (string, error) {
432424
witnessStack := vin.Txinwitness
433425

434426
if len(witnessStack) >= 1 {
@@ -489,21 +481,9 @@ func sumPublicKeys(pubKeys []string) (*btcec.PublicKey, error) {
489481
if idx == 0 {
490482
lastPubKey = publicKey
491483
} else {
492-
var decodeString []byte
493484
x, y := curve.Add(lastPubKey.X(), lastPubKey.Y(), publicKey.X(), publicKey.Y())
494485

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)
507487
if err != nil {
508488
common.ErrorLogger.Println(err)
509489
return nil, err

0 commit comments

Comments
 (0)