@@ -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+ }
0 commit comments