Skip to content

Commit 0b89079

Browse files
committed
fix taproot address generation
1 parent d4c40bf commit 0b89079

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

x/lending/types/taproot.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ import (
55

66
sdk "github.com/cosmos/cosmos-sdk/types"
77

8+
"github.com/btcsuite/btcd/btcec/v2"
89
"github.com/btcsuite/btcd/btcutil"
910
"github.com/btcsuite/btcd/chaincfg"
1011
"github.com/btcsuite/btcd/txscript"
12+
13+
"github.com/sideprotocol/side/crypto/hash"
14+
"github.com/sideprotocol/side/x/dlc/types"
1115
)
1216

1317
func HashLoanSecret(secret string) string {
@@ -103,8 +107,9 @@ func createTaprootAddress(branches [][]byte, params *chaincfg.Params) (string, e
103107
if err != nil {
104108
return "", err
105109
}
110+
106111
// Derive Taproot output key
107-
taprootPubKey := txscript.ComputeTaprootOutputKey(nil, scriptRoot)
112+
taprootPubKey := txscript.ComputeTaprootOutputKey(GetInternalKey(), scriptRoot)
108113
// Generate Taproot address
109114
address, err := btcutil.NewAddressTaproot(taprootPubKey.SerializeCompressed(), params)
110115
if err != nil {
@@ -138,3 +143,19 @@ func CreateVaultAddress(borrowerPubkey string, dcaPubkey string, loanSecretHash
138143
}
139144
return taprootAddress, nil
140145
}
146+
147+
// GetInternalKey gets the pub key used for taproot address generation
148+
// Generated by hash("lending") * G for now
149+
func GetInternalKey() *btcec.PublicKey {
150+
hash := hash.Sha256([]byte(types.ModuleName))
151+
152+
var s btcec.ModNScalar
153+
s.SetByteSlice(hash[:])
154+
155+
var p btcec.JacobianPoint
156+
btcec.ScalarBaseMultNonConst(&s, &p)
157+
158+
p.ToAffine()
159+
160+
return btcec.NewPublicKey(&p.X, &p.Y)
161+
}

0 commit comments

Comments
 (0)