Skip to content

Commit 395749e

Browse files
committed
add control block to script
1 parent c2d67d0 commit 395749e

8 files changed

Lines changed: 1503 additions & 877 deletions

File tree

api/side/lending/lending.pulsar.go

Lines changed: 940 additions & 397 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/side/lending/lending.proto

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,17 @@ enum CetType {
236236
REPAYMENT = 2;
237237
}
238238

239+
// LeafScript defines the tap leaf script
240+
message LeafScript {
241+
string script = 1;
242+
string control_block = 2;
243+
}
244+
239245
message CetInfo {
240246
uint64 event_id = 1;
241247
uint32 outcome_index = 2;
242248
string signature_point = 3;
243-
string script = 4;
244-
string control_block = 5;
249+
LeafScript script = 4 [(gogoproto.nullable) = false];
245250
}
246251

247252
message LiquidationCet {
@@ -267,9 +272,9 @@ message DLCMeta {
267272
string timeout_refund_tx = 4;
268273
repeated side.btcbridge.UTXO vault_utxos = 5;
269274
string internal_key = 6;
270-
string liquidation_script = 7;
271-
string repayment_script = 8;
272-
string timeout_refund_script = 9;
275+
LeafScript liquidation_script = 7 [(gogoproto.nullable) = false];
276+
LeafScript repayment_script = 8 [(gogoproto.nullable) = false];
277+
LeafScript timeout_refund_script = 9 [(gogoproto.nullable) = false];
273278
}
274279

275280
enum DepositStatus {

x/lending/keeper/dlc.go

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"encoding/hex"
66

7-
"github.com/btcsuite/btcd/btcec/v2/schnorr"
87
"github.com/btcsuite/btcd/btcutil/psbt"
98
"github.com/btcsuite/btcd/txscript"
109

@@ -57,33 +56,14 @@ func (k Keeper) UpdateDLCMeta(ctx sdk.Context, loanId string, depositTxs []*psbt
5756
return err
5857
}
5958

60-
liquidationScript, _ := hex.DecodeString(dlcMeta.LiquidationScript)
61-
repaymentScript, _ := hex.DecodeString(dlcMeta.RepaymentScript)
62-
timeoutRefundScript, _ := hex.DecodeString(dlcMeta.TimeoutRefundScript)
59+
internalKey, _ := hex.DecodeString(dlcMeta.InternalKey)
6360

64-
merkleTree := types.GetTapscriptTree([][]byte{
65-
liquidationScript, repaymentScript, timeoutRefundScript,
66-
})
67-
68-
liquidationScriptProof := merkleTree.LeafMerkleProofs[0]
69-
repaymentScriptProof := merkleTree.LeafMerkleProofs[1]
70-
71-
internalKeyBytes, _ := hex.DecodeString(dlcMeta.InternalKey)
72-
internalKey, _ := schnorr.ParsePubKey(internalKeyBytes)
73-
74-
liquidationScriptControlBlock, err := types.GetControlBlock(internalKey, liquidationScriptProof)
75-
if err != nil {
76-
return err
77-
}
78-
79-
repaymentScriptControlBlock, err := types.GetControlBlock(internalKey, repaymentScriptProof)
80-
if err != nil {
81-
return err
82-
}
61+
liquidationScript, liquidationScriptControlBlock, _ := types.UnwrapLeafScript(dlcMeta.LiquidationScript)
62+
repaymentScript, repaymentScriptControlBlock, _ := types.UnwrapLeafScript(dlcMeta.RepaymentScript)
8363

8464
for i := range liquidationCetPsbt.Inputs {
8565
liquidationCetPsbt.Inputs[i].SighashType = txscript.SigHashDefault
86-
liquidationCetPsbt.Inputs[i].TaprootInternalKey = internalKeyBytes
66+
liquidationCetPsbt.Inputs[i].TaprootInternalKey = internalKey
8767
liquidationCetPsbt.Inputs[i].TaprootLeafScript = []*psbt.TaprootTapLeafScript{
8868
{
8969
ControlBlock: liquidationScriptControlBlock,
@@ -95,7 +75,7 @@ func (k Keeper) UpdateDLCMeta(ctx sdk.Context, loanId string, depositTxs []*psbt
9575

9676
for i := range repaymentCetPsbt.Inputs {
9777
repaymentCetPsbt.Inputs[i].SighashType = txscript.SigHashDefault
98-
repaymentCetPsbt.Inputs[i].TaprootInternalKey = internalKeyBytes
78+
repaymentCetPsbt.Inputs[i].TaprootInternalKey = internalKey
9979
repaymentCetPsbt.Inputs[i].TaprootLeafScript = []*psbt.TaprootTapLeafScript{
10080
{
10181
ControlBlock: repaymentScriptControlBlock,
@@ -120,7 +100,7 @@ func (k Keeper) UpdateDLCMeta(ctx sdk.Context, loanId string, depositTxs []*psbt
120100
return err
121101
}
122102

123-
timeoutRefundTx, err := types.CreateTimeoutRefundTransaction(depositTxs, vaultPkScript, borrowerPkScript, internalKeyBytes, [][]byte{liquidationScript, repaymentScript, timeoutRefundScript}, 1)
103+
timeoutRefundTx, err := types.CreateTimeoutRefundTransaction(depositTxs, vaultPkScript, borrowerPkScript, internalKey, dlcMeta.TimeoutRefundScript, 1)
124104
if err != nil {
125105
return err
126106
}
@@ -153,27 +133,8 @@ func (k Keeper) GetCetInfos(ctx sdk.Context, loanId string, collateralAmount sdk
153133
dlcMeta := k.GetDLCMeta(ctx, loanId)
154134
poolConfig := k.GetPool(ctx, loan.PoolId).Config
155135

156-
liquidationScript, _ := hex.DecodeString(dlcMeta.LiquidationScript)
157-
repaymentScript, _ := hex.DecodeString(dlcMeta.RepaymentScript)
158-
timeoutRefundScript, _ := hex.DecodeString(dlcMeta.TimeoutRefundScript)
159-
160-
merkleTree := types.GetTapscriptTree([][]byte{liquidationScript, repaymentScript, timeoutRefundScript})
161-
162-
liquidationScriptProof := merkleTree.LeafMerkleProofs[0]
163-
repaymentScriptProof := merkleTree.LeafMerkleProofs[1]
164-
165-
internalKeyBytes, _ := hex.DecodeString(dlcMeta.InternalKey)
166-
internalKey, _ := schnorr.ParsePubKey(internalKeyBytes)
167-
168-
liquidationScriptControlBlock, err := types.GetControlBlock(internalKey, liquidationScriptProof)
169-
if err != nil {
170-
return nil, err
171-
}
172-
173-
repaymentScriptControlBlock, err := types.GetControlBlock(internalKey, repaymentScriptProof)
174-
if err != nil {
175-
return nil, err
176-
}
136+
liquidationScript, liquidationScriptControlBlock, _ := types.UnwrapLeafScript(dlcMeta.LiquidationScript)
137+
repaymentScript, repaymentScriptControlBlock, _ := types.UnwrapLeafScript(dlcMeta.RepaymentScript)
177138

178139
var liquidationEvent *dlctypes.DLCEvent
179140
if loan.LiquidationEventId != 0 {

x/lending/keeper/msg_server_loan.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ func (m msgServer) Apply(goCtx context.Context, msg *types.MsgApply) (*types.Msg
8686
return nil, types.ErrDuplicatedVault
8787
}
8888

89+
dlcMeta, err := types.BuildDLCMeta(msg.BorrowerPubkey, msg.BorrowerAuthPubkey, dcm.Pubkey, finalTimeout)
90+
if err != nil {
91+
return nil, err
92+
}
93+
8994
if !m.dlcKeeper.HasEventByDate(ctx, maturityTime) {
9095
return nil, errorsmod.Wrap(types.ErrInvalidEvent, "default liquidation event does not exist")
9196
}
@@ -128,7 +133,7 @@ func (m msgServer) Apply(goCtx context.Context, msg *types.MsgApply) (*types.Msg
128133
m.SetLoanByAddress(ctx, loan)
129134

130135
// set dlc meta
131-
m.SetDLCMeta(ctx, vault, types.NewDLCMeta(loan.BorrowerPubKey, loan.BorrowerAuthPubKey, loan.DCM, loan.FinalTimeout))
136+
m.SetDLCMeta(ctx, loan.VaultAddress, dlcMeta)
132137

133138
ctx.EventManager().EmitEvent(
134139
sdk.NewEvent(types.EventTypeApply,
@@ -343,16 +348,14 @@ func (m msgServer) Redeem(goCtx context.Context, msg *types.MsgRedeem) (*types.M
343348
return nil, errorsmod.Wrap(types.ErrInvalidLoanStatus, "loan collateral not redeemable")
344349
}
345350

351+
dlcMeta := m.GetDLCMeta(ctx, msg.LoanId)
352+
346353
p, _ := psbt.NewFromRawBytes(bytes.NewReader([]byte(msg.Tx)), true)
347354

348355
borrowerPubKey, _ := hex.DecodeString(loan.BorrowerPubKey)
349356

350-
internalKey, _ := hex.DecodeString(m.GetDLCMeta(ctx, msg.LoanId).InternalKey)
351-
352-
script, controlBlock, err := m.GetRedemptionScript(ctx, msg.LoanId)
353-
if err != nil {
354-
return nil, err
355-
}
357+
internalKey, _ := hex.DecodeString(dlcMeta.InternalKey)
358+
script, controlBlock, _ := types.UnwrapLeafScript(dlcMeta.RepaymentScript)
356359

357360
sigHashes := []string{}
358361

x/lending/keeper/redemption.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/hex"
66
"fmt"
77

8-
"github.com/btcsuite/btcd/btcec/v2/schnorr"
98
"github.com/btcsuite/btcd/btcutil/psbt"
109
"github.com/btcsuite/btcd/txscript"
1110

@@ -87,36 +86,6 @@ func (k Keeper) HandleRedemptionSignatures(ctx sdk.Context, id uint64, signature
8786
return nil
8887
}
8988

90-
// GetRedemptionScript gets the script along with the corresponding control block for redemption
91-
func (k Keeper) GetRedemptionScript(ctx sdk.Context, loanId string) ([]byte, []byte, error) {
92-
dlcMeta := k.GetDLCMeta(ctx, loanId)
93-
if len(dlcMeta.RepaymentCet.Tx) > 0 {
94-
repaymentCet, _ := psbt.NewFromRawBytes(bytes.NewReader([]byte(dlcMeta.RepaymentCet.Tx)), true)
95-
96-
script := repaymentCet.Inputs[0].TaprootLeafScript[0].Script
97-
controlBlock := repaymentCet.Inputs[0].TaprootLeafScript[0].ControlBlock
98-
99-
return script, controlBlock, nil
100-
}
101-
102-
liquidationScript, _ := hex.DecodeString(dlcMeta.LiquidationScript)
103-
repaymentScript, _ := hex.DecodeString(dlcMeta.RepaymentScript)
104-
timeoutRefundScript, _ := hex.DecodeString(dlcMeta.TimeoutRefundScript)
105-
106-
merkleTree := types.GetTapscriptTree([][]byte{liquidationScript, repaymentScript, timeoutRefundScript})
107-
repaymentScriptProof := merkleTree.LeafMerkleProofs[1]
108-
109-
internalKeyBytes, _ := hex.DecodeString(dlcMeta.InternalKey)
110-
internalKey, _ := schnorr.ParsePubKey(internalKeyBytes)
111-
112-
repaymentScriptControlBlock, err := types.GetControlBlock(internalKey, repaymentScriptProof)
113-
if err != nil {
114-
return nil, nil, err
115-
}
116-
117-
return repaymentScript, repaymentScriptControlBlock, nil
118-
}
119-
12089
// GetRedemptionId gets the current redemption id
12190
func (k Keeper) GetRedemptionId(ctx sdk.Context) uint64 {
12291
store := ctx.KVStore(k.storeKey)

0 commit comments

Comments
 (0)