From 0b890796452465f6fe534c3bfedeca6e8366fb3a Mon Sep 17 00:00:00 2001 From: keithsue Date: Mon, 13 Jan 2025 01:34:07 +0800 Subject: [PATCH] fix taproot address generation --- x/lending/types/taproot.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/x/lending/types/taproot.go b/x/lending/types/taproot.go index 23648f2..5188927 100644 --- a/x/lending/types/taproot.go +++ b/x/lending/types/taproot.go @@ -5,9 +5,13 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/txscript" + + "github.com/sideprotocol/side/crypto/hash" + "github.com/sideprotocol/side/x/dlc/types" ) func HashLoanSecret(secret string) string { @@ -103,8 +107,9 @@ func createTaprootAddress(branches [][]byte, params *chaincfg.Params) (string, e if err != nil { return "", err } + // Derive Taproot output key - taprootPubKey := txscript.ComputeTaprootOutputKey(nil, scriptRoot) + taprootPubKey := txscript.ComputeTaprootOutputKey(GetInternalKey(), scriptRoot) // Generate Taproot address address, err := btcutil.NewAddressTaproot(taprootPubKey.SerializeCompressed(), params) if err != nil { @@ -138,3 +143,19 @@ func CreateVaultAddress(borrowerPubkey string, dcaPubkey string, loanSecretHash } return taprootAddress, nil } + +// GetInternalKey gets the pub key used for taproot address generation +// Generated by hash("lending") * G for now +func GetInternalKey() *btcec.PublicKey { + hash := hash.Sha256([]byte(types.ModuleName)) + + var s btcec.ModNScalar + s.SetByteSlice(hash[:]) + + var p btcec.JacobianPoint + btcec.ScalarBaseMultNonConst(&s, &p) + + p.ToAffine() + + return btcec.NewPublicKey(&p.X, &p.Y) +}