Skip to content

Commit

Permalink
chore: replaced deprecated cosmos-sdk error package with cosmossdk.io…
Browse files Browse the repository at this point in the history
…/errors package
  • Loading branch information
arnabghose997 committed Dec 10, 2023
1 parent 20c9d60 commit b53647a
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 88 deletions.
14 changes: 7 additions & 7 deletions app/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth/ante"
"github.com/cosmos/ibc-go/v7/modules/core/keeper"

errorsmod "cosmossdk.io/errors"
"cosmossdk.io/errors"

wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmTypes "github.com/CosmWasm/wasmd/x/wasm/types"
Expand All @@ -29,22 +29,22 @@ type HandlerOptions struct {

func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
if options.AccountKeeper == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "account keeper is required for AnteHandler")
return nil, errors.Wrap(sdkerrors.ErrLogic, "account keeper is required for AnteHandler")
}
if options.BankKeeper == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "bank keeper is required for AnteHandler")
return nil, errors.Wrap(sdkerrors.ErrLogic, "bank keeper is required for AnteHandler")
}
if options.SignModeHandler == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for ante builder")
return nil, errors.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for ante builder")
}
if options.WasmConfig == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "wasm config is required for ante builder")
return nil, errors.Wrap(sdkerrors.ErrLogic, "wasm config is required for ante builder")
}
if options.TXCounterStoreKey == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "tx counter key is required for ante builder")
return nil, errors.Wrap(sdkerrors.ErrLogic, "tx counter key is required for ante builder")
}
if options.SsiKeeper == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "ssi keeper is required for ante builder")
return nil, errors.Wrap(sdkerrors.ErrLogic, "ssi keeper is required for ante builder")
}

sigGasConsumer := options.SigGasConsumer
Expand Down
19 changes: 10 additions & 9 deletions x/ssi/ante/decorators.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ante
import (
"fmt"

"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -42,7 +43,7 @@ func NewMempoolFeeDecorator() MempoolFeeDecorator {
func (mfd MempoolFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
feeTx, ok := tx.(sdk.FeeTx)
if !ok {
return ctx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
return ctx, errors.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
}

feeCoins := feeTx.GetFee()
Expand Down Expand Up @@ -70,7 +71,7 @@ func (mfd MempoolFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate b
}

if !feeCoins.IsAnyGTE(requiredFees) {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrInsufficientFee, "insufficient fees; got: %s required: %s", feeCoins, requiredFees)
return ctx, errors.Wrapf(sdkerrors.ErrInsufficientFee, "insufficient fees; got: %s required: %s", feeCoins, requiredFees)
}
}
}
Expand Down Expand Up @@ -100,7 +101,7 @@ func NewDeductFeeDecorator(ak AccountKeeper, bk BankKeeper, fk FeegrantKeeper, i
func (mfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
feeTx, ok := tx.(sdk.FeeTx)
if !ok {
return ctx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
return ctx, errors.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
}

if addr := mfd.ak.GetModuleAddress(types.FeeCollectorName); addr == nil {
Expand All @@ -116,11 +117,11 @@ func (mfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bo
// this works with only when feegrant enabled.
if feeGranter != nil {
if mfd.feegrantKeeper == nil {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "fee grants are not enabled")
return ctx, errors.Wrap(sdkerrors.ErrInvalidRequest, "fee grants are not enabled")
} else if !feeGranter.Equals(feePayer) {
err := mfd.feegrantKeeper.UseGrantedFees(ctx, feeGranter, feePayer, fee, tx.GetMsgs())
if err != nil {
return ctx, sdkerrors.Wrapf(err, "%s not allowed to pay fees from %s", feeGranter, feePayer)
return ctx, errors.Wrapf(err, "%s not allowed to pay fees from %s", feeGranter, feePayer)
}
}

Expand All @@ -130,7 +131,7 @@ func (mfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bo

// Get the mint module address
if deductFeesFromAcc == nil {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "fee payer address: %s does not exist", deductFeesFrom)
return ctx, errors.Wrapf(sdkerrors.ErrUnknownAddress, "fee payer address: %s does not exist", deductFeesFrom)
}

// Filter SSI messages from Tx messages
Expand All @@ -150,7 +151,7 @@ func (mfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bo
errMsg2 := "The fee provided MUST BE equal to the sum of all fixed-fee x/ssi messages. "
errMsg3 := "To know about the fixed-fee cost of all x/ssi transactions, refer the API endpoint /hypersign-protocol/hidnode/fixedfee . "

return ctx, sdkerrors.Wrapf(
return ctx, errors.Wrapf(
sdkerrors.ErrInsufficientFee,
errMsg1+errMsg2+errMsg3+"expected fees to be %v, got %v",
fixedSSIFee.String(),
Expand Down Expand Up @@ -199,12 +200,12 @@ func (mfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bo
// deductFees deducts fees from the given account.
func deductFees(bankKeeper BankKeeper, ctx sdk.Context, acc types.AccountI, fees sdk.Coins) error {
if !fees.IsValid() {
return sdkerrors.Wrapf(sdkerrors.ErrInsufficientFee, "invalid fee amount: %s", fees)
return errors.Wrapf(sdkerrors.ErrInsufficientFee, "invalid fee amount: %s", fees)
}

err := bankKeeper.SendCoinsFromAccountToModule(ctx, acc.GetAddress(), types.FeeCollectorName, fees)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
return errors.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion x/ssi/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ssi
import (
"fmt"

"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/hypersign-protocol/hid-node/x/ssi/keeper"
Expand Down Expand Up @@ -39,7 +40,7 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
return sdk.WrapServiceResult(ctx, res, err)
default:
errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg)
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
return nil, errors.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
}
}
}
4 changes: 2 additions & 2 deletions x/ssi/keeper/grpc_query_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package keeper
import (
"context"

"cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/hypersign-protocol/hid-node/x/ssi/types"
"google.golang.org/grpc/codes"
Expand All @@ -23,7 +23,7 @@ func (k Keeper) CredentialStatusByID(

cred, err := k.getCredentialStatusFromState(&ctx, req.CredId)
if err != nil {
return nil, sdkerrors.Wrap(types.ErrCredentialStatusNotFound, err.Error())
return nil, errors.Wrap(types.ErrCredentialStatusNotFound, err.Error())
}

return &types.QueryCredentialStatusResponse{CredentialStatus: cred}, nil
Expand Down
6 changes: 3 additions & 3 deletions x/ssi/keeper/grpc_query_did.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package keeper
import (
"context"

"cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"

"github.com/hypersign-protocol/hid-node/x/ssi/types"
Expand All @@ -25,7 +25,7 @@ func (k Keeper) DidDocuments(goCtx context.Context, req *types.QueryDidDocuments
_, err := query.Paginate(store, req.Pagination, func(key []byte, value []byte) error {
didDoc, err := k.getDidDocumentState(&ctx, string(key))
if err != nil {
return sdkerrors.Wrap(types.ErrDidDocNotFound, err.Error())
return errors.Wrap(types.ErrDidDocNotFound, err.Error())
}

didDocuments = append(didDocuments, didDoc)
Expand All @@ -52,7 +52,7 @@ func (k Keeper) DidDocumentByID(goCtx context.Context, req *types.QueryDidDocume
// Check if DID Document exists
didDoc, err := k.getDidDocumentState(&ctx, req.DidId)
if err != nil {
return nil, sdkerrors.Wrap(types.ErrDidDocNotFound, err.Error())
return nil, errors.Wrap(types.ErrDidDocNotFound, err.Error())
}

return &types.QueryDidDocumentResponse{
Expand Down
17 changes: 9 additions & 8 deletions x/ssi/keeper/msg_server_create_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"time"

"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/hypersign-protocol/hid-node/x/ssi/types"
Expand All @@ -24,7 +25,7 @@ func (k msgServer) RegisterCredentialStatus(goCtx context.Context, msg *types.Ms
// Check the format of Credential Status ID
err := verification.IsValidID(credId, chainNamespace, "credDocument")
if err != nil {
return nil, sdkerrors.Wrap(types.ErrInvalidCredentialStatusID, err.Error())
return nil, errors.Wrap(types.ErrInvalidCredentialStatusID, err.Error())
}

// Check if the credential already exist in the store
Expand All @@ -35,22 +36,22 @@ func (k msgServer) RegisterCredentialStatus(goCtx context.Context, msg *types.Ms
// Check if the DID of the issuer exists
issuerId := msgCredStatus.GetIssuer()
if !k.hasDidDocument(ctx, issuerId) {
return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprintf("Issuer`s DID %s doesnt exists", issuerId))
return nil, errors.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprintf("Issuer`s DID %s doesnt exists", issuerId))
}

// Check if issuer's DID is deactivated
issuerDidDocument, err := k.getDidDocumentState(&ctx, issuerId)
if err != nil {
return nil, sdkerrors.Wrap(types.ErrInvalidDidDoc, err.Error())
return nil, errors.Wrap(types.ErrInvalidDidDoc, err.Error())
}
if issuerDidDocument.DidDocumentMetadata.Deactivated {
return nil, sdkerrors.Wrap(types.ErrDidDocDeactivated, fmt.Sprintf("%s is deactivated and cannot used be used to register credential status", issuerDidDocument.DidDocument.Id))
return nil, errors.Wrap(types.ErrDidDocDeactivated, fmt.Sprintf("%s is deactivated and cannot used be used to register credential status", issuerDidDocument.DidDocument.Id))
}

issuanceDate := msgCredStatus.GetIssuanceDate()
issuanceDateParsed, err := time.Parse(time.RFC3339, issuanceDate)
if err != nil {
return nil, sdkerrors.Wrapf(types.ErrInvalidDate, fmt.Sprintf("invalid issuance date format: %s", issuanceDate))
return nil, errors.Wrapf(types.ErrInvalidDate, fmt.Sprintf("invalid issuance date format: %s", issuanceDate))
}

// Check if the created date before issuance date
Expand All @@ -59,12 +60,12 @@ func (k msgServer) RegisterCredentialStatus(goCtx context.Context, msg *types.Ms
return nil, err
}
if currentDate.Before(issuanceDateParsed) {
return nil, sdkerrors.Wrapf(types.ErrInvalidDate, "proof attached has a creation date before issuance date")
return nil, errors.Wrapf(types.ErrInvalidDate, "proof attached has a creation date before issuance date")
}

// Validate Merkle Root Hash
if err := verifyCredentialMerkleRootHash(msgCredStatus.GetCredentialMerkleRootHash()); err != nil {
return nil, sdkerrors.Wrapf(types.ErrInvalidCredentialMerkleRootHash, err.Error())
return nil, errors.Wrapf(types.ErrInvalidCredentialMerkleRootHash, err.Error())
}

// Validate Document Proof
Expand All @@ -75,7 +76,7 @@ func (k msgServer) RegisterCredentialStatus(goCtx context.Context, msg *types.Ms
// Verify Signature
err = k.VerifyDocumentProof(ctx, msgCredStatus, msgCredProof)
if err != nil {
return nil, sdkerrors.Wrapf(types.ErrInvalidSignature, err.Error())
return nil, errors.Wrapf(types.ErrInvalidSignature, err.Error())
}

cred := &types.CredentialStatusState{
Expand Down
20 changes: 10 additions & 10 deletions x/ssi/keeper/msg_server_create_did.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"fmt"

"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/hypersign-protocol/hid-node/x/ssi/types"
"github.com/hypersign-protocol/hid-node/x/ssi/utils"
"github.com/hypersign-protocol/hid-node/x/ssi/verification"
Expand All @@ -22,23 +22,23 @@ func (k msgServer) RegisterDID(goCtx context.Context, msg *types.MsgRegisterDID)

// Validate DID Document
if err := msgDidDocument.ValidateDidDocument(); err != nil {
return nil, sdkerrors.Wrap(types.ErrInvalidDidDoc, err.Error())
return nil, errors.Wrap(types.ErrInvalidDidDoc, err.Error())
}

// Validate namespace in DID Document
chainNamespace := k.GetChainNamespace(&ctx)
if err := types.DidChainNamespaceValidation(msgDidDocument, chainNamespace); err != nil {
return nil, sdkerrors.Wrap(types.ErrInvalidDidDoc, err.Error())
return nil, errors.Wrap(types.ErrInvalidDidDoc, err.Error())
}

// Validate ownership of method specific id
if err := checkMethodSpecificIdOwnership(msgDidDocument.VerificationMethod, msgDidDocument.Id); err != nil {
return nil, sdkerrors.Wrap(types.ErrInvalidDidDoc, err.Error())
return nil, errors.Wrap(types.ErrInvalidDidDoc, err.Error())
}

// Checks if the Did Document is already registered
if k.hasDidDocument(ctx, msgDidDocument.Id) {
return nil, sdkerrors.Wrap(types.ErrDidDocExists, msgDidDocument.Id)
return nil, errors.Wrap(types.ErrDidDocExists, msgDidDocument.Id)
}

// Validate Document Proofs
Expand All @@ -52,7 +52,7 @@ func (k msgServer) RegisterDID(goCtx context.Context, msg *types.MsgRegisterDID)
for _, vm := range msgDidDocument.VerificationMethod {
if vm.BlockchainAccountId != "" {
if existingDidDocId := k.getBlockchainAddressFromStore(&ctx, vm.BlockchainAccountId); len(existingDidDocId) != 0 {
return nil, sdkerrors.Wrapf(
return nil, errors.Wrapf(
types.ErrInvalidDidDoc,
"blockchainAccountId %v of verification method %v is already part of DID Document %v",
vm.BlockchainAccountId,
Expand All @@ -67,27 +67,27 @@ func (k msgServer) RegisterDID(goCtx context.Context, msg *types.MsgRegisterDID)
controllerList := getControllersForCreateDID(msgDidDocument)

if err := k.checkControllerPresenceInState(ctx, controllerList, msgDidDocument.Id); err != nil {
return nil, sdkerrors.Wrap(types.ErrInvalidDidDoc, err.Error())
return nil, errors.Wrap(types.ErrInvalidDidDoc, err.Error())
}

// Collect necessary Verification Methods which are needed to be valid
requiredVMs, err := getVerificationMethodsForCreateDID(msgDidDocument)
if err != nil {
return nil, sdkerrors.Wrap(types.ErrVerificationMethodNotFound, err.Error())
return nil, errors.Wrap(types.ErrVerificationMethodNotFound, err.Error())
}

// Associate Signatures
signMap := makeSignatureMap(msgDidDocumentProofs)

requiredVmMap, err := k.formMustControllerVmListMap(ctx, controllerList, requiredVMs, signMap)
if err != nil {
return nil, sdkerrors.Wrap(types.ErrInvalidDidDoc, err.Error())
return nil, errors.Wrap(types.ErrInvalidDidDoc, err.Error())
}

// Verify Signatures
err = verification.VerifySignatureOfEveryController(msgDidDocument, requiredVmMap)
if err != nil {
return nil, sdkerrors.Wrapf(types.ErrInvalidSignature, err.Error())
return nil, errors.Wrapf(types.ErrInvalidSignature, err.Error())
}

// Formt DID Document Metadata
Expand Down
Loading

0 comments on commit b53647a

Please sign in to comment.