Skip to content

Commit 8121d9b

Browse files
committed
add MsgSubmitRepaymentAdaptorSignature
1 parent b779be6 commit 8121d9b

File tree

10 files changed

+2000
-279
lines changed

10 files changed

+2000
-279
lines changed

api/side/lending/lending.pulsar.go

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

api/side/lending/tx.pulsar.go

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

api/side/lending/tx_grpc.pb.go

Lines changed: 45 additions & 8 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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ message Repayment {
9494
string txid = 2;
9595
string tx = 3;
9696
string repay_adaptor_point = 4;
97-
string borrower_signature = 5;
98-
google.protobuf.Timestamp create_at = 6 [(gogoproto.nullable) = false, (gogoproto.stdtime) = true];
97+
string dca_adaptor_signature = 5;
98+
string borrower_signature = 6;
99+
google.protobuf.Timestamp create_at = 7 [(gogoproto.nullable) = false, (gogoproto.stdtime) = true];
99100
}

proto/side/lending/tx.proto

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ service Msg {
1818
rpc Approve(MsgApprove) returns (MsgApproveResponse);
1919
rpc Redeem(MsgRedeem) returns (MsgRedeemResponse);
2020
rpc Repay(MsgRepay) returns (MsgRepayResponse);
21+
rpc SubmitRepaymentAdaptorSignature(MsgSubmitRepaymentAdaptorSignature) returns (MsgSubmitRepaymentAdaptorSignatureResponse);
2122
rpc Close(MsgClose) returns (MsgCloseResponse);
2223
}
2324

@@ -107,6 +108,18 @@ message MsgApproveResponse {
107108

108109
}
109110

111+
message MsgSubmitRepaymentAdaptorSignature {
112+
option (cosmos.msg.v1.signer) = "relayer";
113+
114+
string relayer = 1;
115+
string loan_id = 2;
116+
string adaptor_signature = 3;
117+
}
118+
119+
message MsgSubmitRepaymentAdaptorSignatureResponse {
120+
121+
}
122+
110123
message MsgClose {
111124
option (cosmos.msg.v1.signer) = "relayer";
112125

x/lending/keeper/msg_server.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import (
44
"bytes"
55
"context"
66
"encoding/base64"
7+
"encoding/hex"
78
"fmt"
89

910
"cosmossdk.io/math"
1011
"github.com/btcsuite/btcd/btcutil/psbt"
1112

1213
sdk "github.com/cosmos/cosmos-sdk/types"
1314

15+
"github.com/sideprotocol/side/crypto/adaptor"
1416
dlc "github.com/sideprotocol/side/x/dlc/types"
1517
"github.com/sideprotocol/side/x/lending/types"
1618
)
@@ -275,6 +277,51 @@ func (m msgServer) Repay(goCtx context.Context, msg *types.MsgRepay) (*types.Msg
275277
return &types.MsgRepayResponse{}, nil
276278
}
277279

280+
// SubmitRepaymentAdaptorSignature implements types.MsgServer.
281+
func (m msgServer) SubmitRepaymentAdaptorSignature(goCtx context.Context, msg *types.MsgSubmitRepaymentAdaptorSignature) (*types.MsgSubmitRepaymentAdaptorSignatureResponse, error) {
282+
if err := msg.ValidateBasic(); err != nil {
283+
return nil, err
284+
}
285+
286+
ctx := sdk.UnwrapSDKContext(goCtx)
287+
288+
if !m.HasLoan(ctx, msg.LoanId) {
289+
return nil, types.ErrLoanNotExists
290+
}
291+
292+
if !m.HasRepayment(ctx, msg.LoanId) {
293+
return nil, types.ErrInvalidRepayment
294+
}
295+
296+
repayment := m.GetRepayment(ctx, msg.LoanId)
297+
if len(repayment.DcaAdaptorSignature) != 0 {
298+
return nil, types.ErrRepaymentAdaptorSigAlreadyExists
299+
}
300+
301+
loan := m.GetLoan(ctx, msg.LoanId)
302+
303+
adaptorSigBytes, _ := hex.DecodeString(msg.AdaptorSignature)
304+
adaptorPointBytes, _ := hex.DecodeString(repayment.RepayAdaptorPoint)
305+
pubKeyBytes, _ := hex.DecodeString(loan.Agency)
306+
307+
// TODO: calculate sig hash
308+
sigHash := []byte{}
309+
310+
if !adaptor.Verify(adaptorSigBytes, sigHash, pubKeyBytes, adaptorPointBytes) {
311+
return nil, types.ErrInvalidAdaptorSignature
312+
}
313+
314+
repayment.DcaAdaptorSignature = msg.AdaptorSignature
315+
m.SetRepayment(ctx, repayment)
316+
317+
m.EmitEvent(ctx, msg.Relayer,
318+
sdk.NewAttribute("loan_id", msg.LoanId),
319+
sdk.NewAttribute("adaptor_signature", msg.AdaptorSignature),
320+
)
321+
322+
return &types.MsgSubmitRepaymentAdaptorSignatureResponse{}, nil
323+
}
324+
278325
// Close implements types.MsgServer.
279326
func (m msgServer) Close(goCtx context.Context, msg *types.MsgClose) (*types.MsgCloseResponse, error) {
280327
ctx := sdk.UnwrapSDKContext(goCtx)

x/lending/types/errors.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ var (
3838
ErrEmptyLoanSecret = errorsmod.Register(ModuleName, 5002, "invalid loan secret")
3939
ErrMismatchLoanSecret = errorsmod.Register(ModuleName, 5003, "mismatch loan secret")
4040

41-
ErrEmptyAdaptorPoint = errorsmod.Register(ModuleName, 6001, "invalid adaptor point")
42-
ErrInvalidRepayment = errorsmod.Register(ModuleName, 6002, "invalid repayment")
43-
ErrInvalidRepaymentTx = errorsmod.Register(ModuleName, 6003, "invalid repayment tx")
44-
ErrInvalidRepaymentSecret = errorsmod.Register(ModuleName, 6003, "invalid repayment secret")
41+
ErrEmptyAdaptorPoint = errorsmod.Register(ModuleName, 6001, "invalid adaptor point")
42+
ErrInvalidRepayment = errorsmod.Register(ModuleName, 6002, "invalid repayment")
43+
ErrInvalidRepaymentTx = errorsmod.Register(ModuleName, 6003, "invalid repayment tx")
44+
ErrInvalidRepaymentSecret = errorsmod.Register(ModuleName, 6004, "invalid repayment secret")
45+
ErrRepaymentAdaptorSigAlreadyExists = errorsmod.Register(ModuleName, 6005, "repayment adaptor signature already exists")
46+
ErrInvalidAdaptorSignature = errorsmod.Register(ModuleName, 6006, "invalid adaptor signature")
47+
ErrEmptyLoanId = errorsmod.Register(ModuleName, 6007, "empty loan id")
4548
)

0 commit comments

Comments
 (0)