From 715ad5351867a7ec948320f1865d503d8b46fe9a Mon Sep 17 00:00:00 2001 From: MikkySnow Date: Fri, 15 Mar 2024 14:57:46 +0000 Subject: [PATCH] feat: implement connector functions --- go-sgxvm/lib.go | 6 ++++ x/compliance/keeper/keeper.go | 4 +-- x/evm/keeper/sgxvm_connector.go | 49 ++++++++++++++++++++++++++++----- x/evm/types/interfaces.go | 4 +-- 4 files changed, 52 insertions(+), 11 deletions(-) diff --git a/go-sgxvm/lib.go b/go-sgxvm/lib.go index 8455e8fe..566465b0 100644 --- a/go-sgxvm/lib.go +++ b/go-sgxvm/lib.go @@ -42,6 +42,10 @@ type QueryBlockHashResponse = types.QueryBlockHashResponse type VerificationMethod = types.VerificationMethod type QueryVerificationMethods = types.QueryVerificationMethods type QueryVerificationMethodsResponse = types.QueryVerificationMethodsResponse +type QueryAddVerificationDetails = types.QueryAddVerificationDetails +type QueryAddVerificationDetailsResponse = types.QueryAddVerificationDetailsResponse +type QueryHasVerification = types.QueryHasVerification +type QueryHasVerificationResponse = types.QueryHasVerificationResponse // Storage requests type CosmosRequest_GetAccount = types.CosmosRequest_GetAccount @@ -54,6 +58,8 @@ type CosmosRequest_InsertStorageCell = types.CosmosRequest_InsertStorageCell type CosmosRequest_Remove = types.CosmosRequest_Remove type CosmosRequest_RemoveStorageCell = types.CosmosRequest_RemoveStorageCell type CosmosRequest_VerificationMethods = types.CosmosRequest_VerificationMethods +type CosmosRequest_AddVerificationDetails = types.CosmosRequest_AddVerificationDetails +type CosmosRequest_HasVerification = types.CosmosRequest_HasVerification // Backend requests type CosmosRequest_BlockHash = types.CosmosRequest_BlockHash diff --git a/x/compliance/keeper/keeper.go b/x/compliance/keeper/keeper.go index 9df928ba..1922e140 100644 --- a/x/compliance/keeper/keeper.go +++ b/x/compliance/keeper/keeper.go @@ -227,7 +227,7 @@ func (k Keeper) MarkAddressAsVerified(ctx sdk.Context, address sdk.Address) erro } // AddVerificationDetails writes details of passed verification by provided address. -func (k Keeper) AddVerificationDetails(ctx sdk.Context, userAddress sdk.Address, verificationType types.VerificationType, details types.VerificationDetails) error { +func (k Keeper) AddVerificationDetails(ctx sdk.Context, userAddress sdk.Address, verificationType types.VerificationType, details *types.VerificationDetails) error { // Check if issuer is verified and not banned issuerAddress, err := sdk.AccAddressFromBech32(details.IssuerAddress) if err != nil { @@ -300,7 +300,7 @@ func (k Keeper) GetVerificationDetails(ctx sdk.Context, verificationDetailsId [] // HasVerificationOfType checks if user has verifications of specific type (for example, passed KYC) from provided issuers. // If there is no provided expected issuers, this function will check if user has any verification of appropriate type. -func (k Keeper) HasVerificationOfType(ctx sdk.Context, userAddress sdk.Address, expectedType types.VerificationType, expectedIssuers ...sdk.Address) (bool, error) { +func (k Keeper) HasVerificationOfType(ctx sdk.Context, userAddress sdk.Address, expectedType types.VerificationType, expectedIssuers []sdk.Address) (bool, error) { // Obtain user address details userAddressDetails, err := k.GetAddressDetails(ctx, userAddress) if err != nil { diff --git a/x/evm/keeper/sgxvm_connector.go b/x/evm/keeper/sgxvm_connector.go index fd658b7c..b69bc946 100644 --- a/x/evm/keeper/sgxvm_connector.go +++ b/x/evm/keeper/sgxvm_connector.go @@ -8,6 +8,8 @@ import ( "github.com/ethereum/go-ethereum/core/vm" "github.com/golang/protobuf/proto" "math/big" + + compliancetypes "swisstronik/x/compliance/types" ) // Connector allows our VM interact with existing Cosmos application. @@ -62,6 +64,10 @@ func (q Connector) Query(req []byte) ([]byte, error) { // Returns verification methods for DID Document case *librustgo.CosmosRequest_VerificationMethods: return q.GetVerificationMethods(request) + case *librustgo.CosmosRequest_AddVerificationDetails: + return q.AddVerificationDetails(request) + case *librustgo.CosmosRequest_HasVerification: + return q.HasVerification(request) } return nil, errors.New("wrong query received") @@ -226,14 +232,43 @@ func (q Connector) GetVerificationMethods(req *librustgo.CosmosRequest_Verificat }) } -// SetVerificationDetails writes provided verification details to x/compliance module -func (q Connector) SetVerificationDetails() ([]byte, error) { - // TODO: Implement +// AddVerificationDetails writes provided verification details to x/compliance module +func (q Connector) AddVerificationDetails(req *librustgo.CosmosRequest_AddVerificationDetails) ([]byte, error) { + verificationType := compliancetypes.VerificationType(req.AddVerificationDetails.VerificationType) + userAddress := sdk.AccAddress(req.AddVerificationDetails.UserAddress) + issuerAddress := sdk.AccAddress(req.AddVerificationDetails.IssuerAddress).String() + + verificationDetails := &compliancetypes.VerificationDetails{ + IssuerAddress: issuerAddress, + OriginChain: "samplechain", // TODO: Read chain from proto + IssuanceTimestamp: req.AddVerificationDetails.IssuanceTimestamp, + ExpirationTimestamp: req.AddVerificationDetails.ExpirationTimestamp, + OriginalData: req.AddVerificationDetails.ProofData, + } + + if err := q.EVMKeeper.ComplianceKeeper.AddVerificationDetails(q.Context, userAddress, verificationType, verificationDetails); err != nil { + return nil, err + } + return nil, nil } -// GetVerificationDetails returns verification details from x/compliance module -func (q Connector) GetVerificationDetails() ([]byte, error) { - // TODO: Implement - return nil, nil +// HasVerification returns if user has verification of provided type from x/compliance module +func (q Connector) HasVerification(req *librustgo.CosmosRequest_HasVerification) ([]byte, error) { + userAddress := sdk.AccAddress(req.HasVerification.UserAddress) + verificationType := compliancetypes.VerificationType(req.HasVerification.VerificationType) + + var allowedIssuers []sdk.Address + for _, issuer := range req.HasVerification.AllowedIssuers { + allowedIssuers = append(allowedIssuers, sdk.AccAddress(issuer)) + } + + hasVerification, err := q.EVMKeeper.ComplianceKeeper.HasVerificationOfType(q.Context, userAddress, verificationType, allowedIssuers) + if err != nil { + return nil, err + } + + return proto.Marshal(&librustgo.QueryHasVerificationResponse{ + HasVerification: hasVerification, + }) } diff --git a/x/evm/types/interfaces.go b/x/evm/types/interfaces.go index a4959151..c42524af 100644 --- a/x/evm/types/interfaces.go +++ b/x/evm/types/interfaces.go @@ -73,8 +73,8 @@ type DIDKeeper interface { // ComplianceKeeper type ComplianceKeeper interface { - AddVerificationDetails(ctx sdk.Context, userAddress sdk.Address, verificationType compliancetypes.VerificationType, details compliancetypes.VerificationDetails) error - GetVerificationsOfType(ctx sdk.Context, userAddress sdk.Address, expectedType compliancetypes.VerificationType, expectedIssuers ...sdk.Address) ([]*compliancetypes.VerificationDetails, error) + AddVerificationDetails(ctx sdk.Context, userAddress sdk.Address, verificationType compliancetypes.VerificationType, details *compliancetypes.VerificationDetails) error + HasVerificationOfType(ctx sdk.Context, userAddress sdk.Address, expectedType compliancetypes.VerificationType, expectedIssuers []sdk.Address) (bool, error) } // Event Hooks