Skip to content

Commit 059cf53

Browse files
committed
sanitize tapscript proof
1 parent 1bd08aa commit 059cf53

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

x/lending/types/dlc.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ func BuildDLCMeta(borrowerPubKey string, borrowerAuthPubKey string, dcmPubKey st
4040

4141
liquidationScript, repaymentScript, timeoutRefundScript, _ := GetVaultScripts(borrowerPubKey, borrowerAuthPubKey, dcmPubKey, finalTimeout)
4242

43-
tapScriptTree := GetTapScriptTree([][]byte{liquidationScript, repaymentScript, timeoutRefundScript})
43+
tapscriptTree := GetTapscriptTree([][]byte{liquidationScript, repaymentScript, timeoutRefundScript})
44+
sanitizeTapscriptTreeProofs(tapscriptTree)
4445

45-
liquidationScriptProof := tapScriptTree.LeafMerkleProofs[0]
46-
repaymentScriptProof := tapScriptTree.LeafMerkleProofs[1]
47-
timeoutRefundScriptProof := tapScriptTree.LeafMerkleProofs[2]
46+
liquidationScriptProof := tapscriptTree.LeafMerkleProofs[0]
47+
repaymentScriptProof := tapscriptTree.LeafMerkleProofs[1]
48+
timeoutRefundScriptProof := tapscriptTree.LeafMerkleProofs[2]
4849

4950
liquidationScriptControlBlock, err := GetControlBlock(internalKey, liquidationScriptProof)
5051
if err != nil {
@@ -628,3 +629,22 @@ func getVaultUtxosFromDepositTx(depositTx *psbt.Packet, vaultPkScript []byte) ([
628629

629630
return utxos, nil
630631
}
632+
633+
// sanitizeTapscriptTreeProofs adjusts the merkle proofs of given tapscript tree
634+
// NOTE: This is a workaround because btcsuite.AssembleTaprootScriptTree overrides the proof if there exist same scripts
635+
// This method only works for three-leaf script tree
636+
func sanitizeTapscriptTreeProofs(tree *txscript.IndexedTapScriptTree) {
637+
proofTwo := tree.LeafMerkleProofs[1].InclusionProof
638+
639+
// abnormal proof
640+
if len(proofTwo) > 64 {
641+
// trim the second proof
642+
tree.LeafMerkleProofs[1].InclusionProof = proofTwo[0:64]
643+
644+
// get the last element
645+
lastProofElement := proofTwo[len(proofTwo)-32:]
646+
647+
// append to the first proof
648+
tree.LeafMerkleProofs[0].InclusionProof = append(tree.LeafMerkleProofs[0].InclusionProof, lastProofElement...)
649+
}
650+
}

x/lending/types/taproot.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ func CreatePubKeyTimeLockScript(pubKey []byte, lockTime int64) ([]byte, error) {
5757

5858
// CreateTaprootAddress creates the taproot address with the given internal key and scripts
5959
func CreateTaprootAddress(internalKey *secp256k1.PublicKey, scripts [][]byte, params *chaincfg.Params) (string, error) {
60-
tapScriptTree := GetTapScriptTree(scripts)
61-
scriptRoot := tapScriptTree.RootNode.TapHash()
60+
tapscriptTree := GetTapscriptTree(scripts)
61+
scriptRoot := tapscriptTree.RootNode.TapHash()
6262

6363
taprootOutKey := txscript.ComputeTaprootOutputKey(internalKey, scriptRoot[:])
6464

@@ -145,7 +145,7 @@ func GetInternalKey(borrowerPubKey []byte, dcmPubKey []byte) *btcec.PublicKey {
145145
return btcec.NewPublicKey(&P.X, &P.Y)
146146
}
147147

148-
func GetTapScriptTree(scripts [][]byte) *txscript.IndexedTapScriptTree {
148+
func GetTapscriptTree(scripts [][]byte) *txscript.IndexedTapScriptTree {
149149
leaves := []txscript.TapLeaf{}
150150

151151
for _, script := range scripts {

0 commit comments

Comments
 (0)