Skip to content

Commit

Permalink
add MsgSubmitRepaymentAdaptorSignature
Browse files Browse the repository at this point in the history
  • Loading branch information
keithsue committed Jan 14, 2025
1 parent b779be6 commit 8121d9b
Show file tree
Hide file tree
Showing 10 changed files with 2,000 additions and 279 deletions.
161 changes: 118 additions & 43 deletions api/side/lending/lending.pulsar.go

Large diffs are not rendered by default.

1,222 changes: 1,126 additions & 96 deletions api/side/lending/tx.pulsar.go

Large diffs are not rendered by default.

53 changes: 45 additions & 8 deletions api/side/lending/tx_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions proto/side/lending/lending.proto
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ message Repayment {
string txid = 2;
string tx = 3;
string repay_adaptor_point = 4;
string borrower_signature = 5;
google.protobuf.Timestamp create_at = 6 [(gogoproto.nullable) = false, (gogoproto.stdtime) = true];
string dca_adaptor_signature = 5;
string borrower_signature = 6;
google.protobuf.Timestamp create_at = 7 [(gogoproto.nullable) = false, (gogoproto.stdtime) = true];
}
13 changes: 13 additions & 0 deletions proto/side/lending/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ service Msg {
rpc Approve(MsgApprove) returns (MsgApproveResponse);
rpc Redeem(MsgRedeem) returns (MsgRedeemResponse);
rpc Repay(MsgRepay) returns (MsgRepayResponse);
rpc SubmitRepaymentAdaptorSignature(MsgSubmitRepaymentAdaptorSignature) returns (MsgSubmitRepaymentAdaptorSignatureResponse);
rpc Close(MsgClose) returns (MsgCloseResponse);
}

Expand Down Expand Up @@ -107,6 +108,18 @@ message MsgApproveResponse {

}

message MsgSubmitRepaymentAdaptorSignature {
option (cosmos.msg.v1.signer) = "relayer";

string relayer = 1;
string loan_id = 2;
string adaptor_signature = 3;
}

message MsgSubmitRepaymentAdaptorSignatureResponse {

}

message MsgClose {
option (cosmos.msg.v1.signer) = "relayer";

Expand Down
47 changes: 47 additions & 0 deletions x/lending/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"bytes"
"context"
"encoding/base64"
"encoding/hex"
"fmt"

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

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

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

// SubmitRepaymentAdaptorSignature implements types.MsgServer.
func (m msgServer) SubmitRepaymentAdaptorSignature(goCtx context.Context, msg *types.MsgSubmitRepaymentAdaptorSignature) (*types.MsgSubmitRepaymentAdaptorSignatureResponse, error) {
if err := msg.ValidateBasic(); err != nil {
return nil, err
}

ctx := sdk.UnwrapSDKContext(goCtx)

if !m.HasLoan(ctx, msg.LoanId) {
return nil, types.ErrLoanNotExists
}

if !m.HasRepayment(ctx, msg.LoanId) {
return nil, types.ErrInvalidRepayment
}

repayment := m.GetRepayment(ctx, msg.LoanId)
if len(repayment.DcaAdaptorSignature) != 0 {
return nil, types.ErrRepaymentAdaptorSigAlreadyExists
}

loan := m.GetLoan(ctx, msg.LoanId)

adaptorSigBytes, _ := hex.DecodeString(msg.AdaptorSignature)
adaptorPointBytes, _ := hex.DecodeString(repayment.RepayAdaptorPoint)
pubKeyBytes, _ := hex.DecodeString(loan.Agency)

// TODO: calculate sig hash
sigHash := []byte{}

if !adaptor.Verify(adaptorSigBytes, sigHash, pubKeyBytes, adaptorPointBytes) {
return nil, types.ErrInvalidAdaptorSignature
}

repayment.DcaAdaptorSignature = msg.AdaptorSignature
m.SetRepayment(ctx, repayment)

m.EmitEvent(ctx, msg.Relayer,
sdk.NewAttribute("loan_id", msg.LoanId),
sdk.NewAttribute("adaptor_signature", msg.AdaptorSignature),
)

return &types.MsgSubmitRepaymentAdaptorSignatureResponse{}, nil
}

// Close implements types.MsgServer.
func (m msgServer) Close(goCtx context.Context, msg *types.MsgClose) (*types.MsgCloseResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
Expand Down
11 changes: 7 additions & 4 deletions x/lending/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ var (
ErrEmptyLoanSecret = errorsmod.Register(ModuleName, 5002, "invalid loan secret")
ErrMismatchLoanSecret = errorsmod.Register(ModuleName, 5003, "mismatch loan secret")

ErrEmptyAdaptorPoint = errorsmod.Register(ModuleName, 6001, "invalid adaptor point")
ErrInvalidRepayment = errorsmod.Register(ModuleName, 6002, "invalid repayment")
ErrInvalidRepaymentTx = errorsmod.Register(ModuleName, 6003, "invalid repayment tx")
ErrInvalidRepaymentSecret = errorsmod.Register(ModuleName, 6003, "invalid repayment secret")
ErrEmptyAdaptorPoint = errorsmod.Register(ModuleName, 6001, "invalid adaptor point")
ErrInvalidRepayment = errorsmod.Register(ModuleName, 6002, "invalid repayment")
ErrInvalidRepaymentTx = errorsmod.Register(ModuleName, 6003, "invalid repayment tx")
ErrInvalidRepaymentSecret = errorsmod.Register(ModuleName, 6004, "invalid repayment secret")
ErrRepaymentAdaptorSigAlreadyExists = errorsmod.Register(ModuleName, 6005, "repayment adaptor signature already exists")
ErrInvalidAdaptorSignature = errorsmod.Register(ModuleName, 6006, "invalid adaptor signature")
ErrEmptyLoanId = errorsmod.Register(ModuleName, 6007, "empty loan id")
)
Loading

0 comments on commit 8121d9b

Please sign in to comment.