Skip to content

Commit d9b2a0e

Browse files
committed
verification signatures in tss module
1 parent 569418b commit d9b2a0e

File tree

10 files changed

+100
-158
lines changed

10 files changed

+100
-158
lines changed

x/dlc/keeper/attestation.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package keeper
22

33
import (
4-
"encoding/hex"
5-
6-
errorsmod "cosmossdk.io/errors"
74
storetypes "cosmossdk.io/store/types"
85
sdk "github.com/cosmos/cosmos-sdk/types"
96

10-
"github.com/sideprotocol/side/bitcoin/crypto/schnorr"
117
"github.com/sideprotocol/side/x/dlc/types"
128
)
139

1410
// HandleAttestation performs the attestation handling
11+
// Assume that the signature has already been verified
1512
func (k Keeper) HandleAttestation(ctx sdk.Context, sender string, eventId uint64, signature string) error {
1613
if !k.HasEvent(ctx, eventId) {
1714
return types.ErrEventDoesNotExist
@@ -26,18 +23,6 @@ func (k Keeper) HandleAttestation(ctx sdk.Context, sender string, eventId uint64
2623
return types.ErrAttestationAlreadyExists
2724
}
2825

29-
if signature[0:64] != event.Nonce {
30-
return errorsmod.Wrap(types.ErrInvalidSignature, "signature r does not match the event nonce")
31-
}
32-
33-
pubKeyBytes, _ := hex.DecodeString(event.Pubkey)
34-
sigBytes, _ := hex.DecodeString(signature)
35-
msg := types.GetEventOutcomeHash(event, int(event.OutcomeIndex))
36-
37-
if !schnorr.Verify(sigBytes, msg, pubKeyBytes) {
38-
return errorsmod.Wrap(types.ErrInvalidSignature, "failed to verify the signature")
39-
}
40-
4126
attestation := types.DLCAttestation{
4227
Id: k.IncrementAttestationId(ctx),
4328
EventId: eventId,

x/lending/keeper/liquidation.go

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
package keeper
22

33
import (
4-
"bytes"
5-
"encoding/hex"
6-
7-
"github.com/btcsuite/btcd/btcutil/psbt"
8-
94
errorsmod "cosmossdk.io/errors"
105
sdkmath "cosmossdk.io/math"
116
sdk "github.com/cosmos/cosmos-sdk/types"
127

13-
"github.com/sideprotocol/side/bitcoin/crypto/schnorr"
148
"github.com/sideprotocol/side/x/lending/types"
159
)
1610

1711
// HandleLiquidationSignatures handles the liquidation signatures
12+
// Assume that signatures have already been verified
1813
func (k Keeper) HandleLiquidationSignatures(ctx sdk.Context, loanId string, signatures []string) error {
1914
if !k.HasLoan(ctx, loanId) {
2015
return types.ErrLoanDoesNotExist
@@ -30,34 +25,14 @@ func (k Keeper) HandleLiquidationSignatures(ctx sdk.Context, loanId string, sign
3025
return types.ErrLiquidationSignaturesAlreadyExist
3126
}
3227

33-
p, _ := psbt.NewFromRawBytes(bytes.NewReader([]byte(dlcMeta.LiquidationCet.Tx)), true)
34-
if len(signatures) != len(p.Inputs) {
35-
return errorsmod.Wrap(types.ErrInvalidSignatures, "mismatched signature number")
36-
}
37-
38-
script, _ := hex.DecodeString(dlcMeta.MultisigScript)
39-
dcmPubKey, _ := hex.DecodeString(loan.DCM)
40-
41-
for i, input := range p.Inputs {
42-
sigHash, err := types.CalcTapscriptSigHash(p, i, input.SighashType, script)
43-
if err != nil {
44-
return err
45-
}
46-
47-
sigBytes, _ := hex.DecodeString(signatures[i])
48-
49-
if !schnorr.Verify(sigBytes, sigHash, dcmPubKey) {
50-
return types.ErrInvalidSignature
51-
}
52-
}
53-
5428
dlcMeta.LiquidationCet.DCMSignatures = signatures
5529
k.SetDLCMeta(ctx, loanId, dlcMeta)
5630

5731
return nil
5832
}
5933

6034
// handleDefaultLiquidationSignatures handles the default liquidation signatures
35+
// Assume that signatures have already been verified
6136
func (k Keeper) handleDefaultLiquidationSignatures(ctx sdk.Context, loanId string, signatures []string) error {
6237
if !k.HasLoan(ctx, loanId) {
6338
return types.ErrLoanDoesNotExist
@@ -73,27 +48,6 @@ func (k Keeper) handleDefaultLiquidationSignatures(ctx sdk.Context, loanId strin
7348
return types.ErrLiquidationSignaturesAlreadyExist
7449
}
7550

76-
p, _ := psbt.NewFromRawBytes(bytes.NewReader([]byte(dlcMeta.DefaultLiquidationCet.Tx)), true)
77-
if len(signatures) != len(p.Inputs) {
78-
return errorsmod.Wrap(types.ErrInvalidSignatures, "mismatched signature number")
79-
}
80-
81-
script, _ := hex.DecodeString(dlcMeta.MultisigScript)
82-
dcmPubKey, _ := hex.DecodeString(loan.DCM)
83-
84-
for i, input := range p.Inputs {
85-
sigHash, err := types.CalcTapscriptSigHash(p, i, input.SighashType, script)
86-
if err != nil {
87-
return err
88-
}
89-
90-
sigBytes, _ := hex.DecodeString(signatures[i])
91-
92-
if !schnorr.Verify(sigBytes, sigHash, dcmPubKey) {
93-
return types.ErrInvalidSignature
94-
}
95-
}
96-
9751
dlcMeta.DefaultLiquidationCet.DCMSignatures = signatures
9852
k.SetDLCMeta(ctx, loanId, dlcMeta)
9953

x/lending/keeper/redemption.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ import (
88
"github.com/btcsuite/btcd/btcutil/psbt"
99
"github.com/btcsuite/btcd/txscript"
1010

11-
errorsmod "cosmossdk.io/errors"
1211
sdk "github.com/cosmos/cosmos-sdk/types"
1312

14-
"github.com/sideprotocol/side/bitcoin/crypto/schnorr"
1513
"github.com/sideprotocol/side/x/lending/types"
1614
)
1715

1816
// HandleRedemptionSignatures handles the redemption signatures
17+
// Assume that signatures have already been verified
1918
func (k Keeper) HandleRedemptionSignatures(ctx sdk.Context, id uint64, signatures []string) error {
2019
if !k.HasRedemption(ctx, id) {
2120
return types.ErrRedemptionDoesNotExist
@@ -29,9 +28,6 @@ func (k Keeper) HandleRedemptionSignatures(ctx sdk.Context, id uint64, signature
2928
loan := k.GetLoan(ctx, redemption.LoanId)
3029

3130
p, _ := psbt.NewFromRawBytes(bytes.NewReader([]byte(redemption.Tx)), true)
32-
if len(signatures) != len(p.Inputs) {
33-
return errorsmod.Wrap(types.ErrInvalidSignatures, "mismatched signature number")
34-
}
3531

3632
borrowerPubKey, _ := hex.DecodeString(loan.BorrowerPubKey)
3733
dcmPubKey, _ := hex.DecodeString(loan.DCM)
@@ -42,17 +38,7 @@ func (k Keeper) HandleRedemptionSignatures(ctx sdk.Context, id uint64, signature
4238
for i, ti := range p.UnsignedTx.TxIn {
4339
prevTxHash := ti.PreviousOutPoint.Hash.String()
4440

45-
sigHash, err := types.CalcTapscriptSigHash(p, i, types.DefaultSigHashType, script)
46-
if err != nil {
47-
return err
48-
}
49-
5041
sigBytes, _ := hex.DecodeString(signatures[i])
51-
52-
if !schnorr.Verify(sigBytes, sigHash, dcmPubKey) {
53-
return types.ErrInvalidSignature
54-
}
55-
5642
borrowerSig, _ := hex.DecodeString(redemption.Signatures[i])
5743

5844
p.Inputs[i].TaprootScriptSpendSig = []*psbt.TaprootScriptSpendSig{

x/lending/keeper/repayment.go

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
package keeper
22

33
import (
4-
"bytes"
54
"encoding/hex"
65

7-
"github.com/btcsuite/btcd/btcutil/psbt"
8-
96
errorsmod "cosmossdk.io/errors"
107
sdkmath "cosmossdk.io/math"
118
sdk "github.com/cosmos/cosmos-sdk/types"
129

13-
"github.com/sideprotocol/side/bitcoin/crypto/adaptor"
1410
dlctypes "github.com/sideprotocol/side/x/dlc/types"
1511
"github.com/sideprotocol/side/x/lending/types"
1612
tsstypes "github.com/sideprotocol/side/x/tss/types"
@@ -44,6 +40,7 @@ func (k Keeper) InitiateRepaymentCetSigningRequest(ctx sdk.Context, loanId strin
4440
}
4541

4642
// HandleRepaymentAdaptorSignatures handles repayment adaptor signatures
43+
// Assume that signatures have already been verified
4744
func (k Keeper) HandleRepaymentAdaptorSignatures(ctx sdk.Context, loanId string, adaptorSignatures []string) error {
4845
if !k.HasLoan(ctx, loanId) {
4946
return types.ErrLoanDoesNotExist
@@ -55,34 +52,10 @@ func (k Keeper) HandleRepaymentAdaptorSignatures(ctx sdk.Context, loanId string,
5552
}
5653

5754
dlcMeta := k.GetDLCMeta(ctx, loanId)
58-
59-
repaymentCet := dlcMeta.RepaymentCet
60-
if len(repaymentCet.DCMAdaptorSignatures) != 0 {
55+
if len(dlcMeta.RepaymentCet.DCMAdaptorSignatures) != 0 {
6156
return types.ErrRepaymentAdaptorSigsAlreadyExist
6257
}
6358

64-
p, _ := psbt.NewFromRawBytes(bytes.NewReader([]byte(repaymentCet.Tx)), true)
65-
if len(adaptorSignatures) != len(p.Inputs) {
66-
return errorsmod.Wrap(types.ErrInvalidAdaptorSignatures, "mismatched adaptor signature number")
67-
}
68-
69-
script, _ := hex.DecodeString(dlcMeta.MultisigScript)
70-
adaptorPoint, _ := k.GetRepaymentCetAdaptorPoint(ctx, loanId)
71-
dcmPubKey, _ := hex.DecodeString(loan.DCM)
72-
73-
for i, input := range p.Inputs {
74-
sigHash, err := types.CalcTapscriptSigHash(p, i, input.SighashType, script)
75-
if err != nil {
76-
return err
77-
}
78-
79-
adaptorSigBytes, _ := hex.DecodeString(adaptorSignatures[i])
80-
81-
if !adaptor.Verify(adaptorSigBytes, sigHash, dcmPubKey, adaptorPoint) {
82-
return types.ErrInvalidAdaptorSignature
83-
}
84-
}
85-
8659
dlcMeta.RepaymentCet.DCMAdaptorSignatures = adaptorSignatures
8760
k.SetDLCMeta(ctx, loanId, dlcMeta)
8861

x/liquidation/keeper/settlement.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010
errorsmod "cosmossdk.io/errors"
1111
sdk "github.com/cosmos/cosmos-sdk/types"
1212

13-
"github.com/sideprotocol/side/bitcoin/crypto/schnorr"
1413
"github.com/sideprotocol/side/x/liquidation/types"
1514
)
1615

1716
// HandleSettlementSignatures handles the settlement tx signatures
17+
// Assume that signatures have already been verified
1818
func (k Keeper) HandleSettlementSignatures(ctx sdk.Context, sender string, liquidationId uint64, signatures []string) error {
1919
if !k.HasLiquidation(ctx, liquidationId) {
2020
return types.ErrLiquidationDoesNotExist
@@ -30,25 +30,8 @@ func (k Keeper) HandleSettlementSignatures(ctx sdk.Context, sender string, liqui
3030
return err
3131
}
3232

33-
if len(signatures) != len(settlementTxPsbt.Inputs) {
34-
return errorsmod.Wrap(types.ErrInvalidSignatures, "mismatched signature number")
35-
}
36-
37-
dcmPubKey, _ := hex.DecodeString(liquidation.DCM)
38-
verificationKey := types.GetTaprootOutKey(dcmPubKey)
39-
40-
for i, input := range settlementTxPsbt.Inputs {
41-
sigHash, err := types.CalcTaprootSigHash(settlementTxPsbt, i, input.SighashType)
42-
if err != nil {
43-
return err
44-
}
45-
33+
for i := range settlementTxPsbt.Inputs {
4634
sigBytes, _ := hex.DecodeString(signatures[i])
47-
48-
if !schnorr.Verify(sigBytes, sigHash, verificationKey) {
49-
return types.ErrInvalidSignature
50-
}
51-
5235
settlementTxPsbt.Inputs[i].TaprootKeySpendSig = sigBytes
5336
}
5437

x/liquidation/types/liquidation.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import (
55
"strconv"
66
"strings"
77

8-
"github.com/btcsuite/btcd/btcec/v2/schnorr"
9-
"github.com/btcsuite/btcd/txscript"
10-
118
sdk "github.com/cosmos/cosmos-sdk/types"
129
)
1310

@@ -19,16 +16,6 @@ func GetPricePair(liquidation *Liquidation) string {
1916
return fmt.Sprintf("%s%s", strings.ToUpper(liquidation.CollateralAsset.PriceSymbol), strings.ToUpper(liquidation.DebtAsset.PriceSymbol))
2017
}
2118

22-
// GetTaprootOutKey gets the taproot output key according to the given pub key
23-
// Assume that the given pub key is valid taproot pub key
24-
func GetTaprootOutKey(pubKeyBytes []byte) []byte {
25-
pubKey, _ := schnorr.ParsePubKey(pubKeyBytes)
26-
27-
taprootOutKey := txscript.ComputeTaprootKeyNoScript(pubKey)
28-
29-
return schnorr.SerializePubKey(taprootOutKey)
30-
}
31-
3219
// ToScopedId converts the given local id to the scoped id
3320
func ToScopedId(id uint64) string {
3421
return fmt.Sprintf("%d", id)

x/tss/keeper/msg_server.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,16 @@ func (m msgServer) SubmitSignatures(goCtx context.Context, msg *types.MsgSubmitS
5454
}
5555

5656
req := m.GetSigningRequest(ctx, msg.Id)
57-
if req.Status != types.SigningStatus_SIGNING_STATUS_PENDING {
58-
return nil, errorsmod.Wrap(types.ErrInvalidSigningStatus, "signing request non pending")
57+
if req.Status == types.SigningStatus_SIGNING_STATUS_SIGNED {
58+
return nil, errorsmod.Wrap(types.ErrInvalidSigningStatus, "signing request has been signed")
5959
}
6060

61+
// verify signatures
62+
if err := m.VerifySignatures(ctx, req, msg.Signatures); err != nil {
63+
return nil, err
64+
}
65+
66+
// callback to the module handler
6167
if err := m.GetSigningRequestCompletedHandler(req.Module)(ctx, msg.Sender, req.Id, req.ScopedId, req.Type, req.Intent, req.PubKey, msg.Signatures); err != nil {
6268
return nil, err
6369
}

0 commit comments

Comments
 (0)