Skip to content

Commit

Permalink
fix taproot address generation
Browse files Browse the repository at this point in the history
  • Loading branch information
keithsue committed Jan 12, 2025
1 parent d4c40bf commit 0b89079
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion x/lending/types/taproot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

0 comments on commit 0b89079

Please sign in to comment.