Skip to content

Commit

Permalink
Merge pull request #75 from hypersign-protocol/ssi-module-code-restru…
Browse files Browse the repository at this point in the history
…cture-service

Code Refactor; Implementation of `Service` and `capabilityDelegation` in didSpec
  • Loading branch information
arnabghose997 committed Feb 20, 2022
2 parents fde4e88 + e2fe324 commit 42ee9fe
Show file tree
Hide file tree
Showing 28 changed files with 1,605 additions and 2,751 deletions.
82 changes: 43 additions & 39 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11931,6 +11931,21 @@ paths:
type: array
items:
type: string
capabilityDelegation:
type: array
items:
type: string
service:
type: array
items:
type: object
properties:
id:
type: string
type:
type: string
serviceEndpoint:
type: string
created:
type: string
updated:
Expand Down Expand Up @@ -20161,7 +20176,7 @@ definitions:
description: >-
MsgCreateVestingAccountResponse defines the Msg/CreateVestingAccount
response type.
hypersignprotocol.hidnode.ssi.DidDocStructCreateDID:
hypersignprotocol.hidnode.ssi.Did:
type: object
properties:
context:
Expand Down Expand Up @@ -20207,28 +20222,11 @@ definitions:
type: array
items:
type: string
created:
type: string
updated:
type: string
hypersignprotocol.hidnode.ssi.DidDocStructUpdateDID:
type: object
properties:
context:
type: array
items:
type: string
id:
type: string
controller:
capabilityDelegation:
type: array
items:
type: string
alsoKnownAs:
type: array
items:
type: string
verificationMethod:
service:
type: array
items:
type: object
Expand All @@ -20237,26 +20235,8 @@ definitions:
type: string
type:
type: string
controller:
type: string
publicKeyMultibase:
serviceEndpoint:
type: string
authentication:
type: array
items:
type: string
assertionMethod:
type: array
items:
type: string
keyAgreement:
type: array
items:
type: string
capabilityInvocation:
type: array
items:
type: string
created:
type: string
updated:
Expand Down Expand Up @@ -20336,6 +20316,21 @@ definitions:
type: array
items:
type: string
capabilityDelegation:
type: array
items:
type: string
service:
type: array
items:
type: object
properties:
id:
type: string
type:
type: string
serviceEndpoint:
type: string
created:
type: string
updated:
Expand Down Expand Up @@ -20499,6 +20494,15 @@ definitions:
type: string
additionalProperties:
type: boolean
hypersignprotocol.hidnode.ssi.Service:
type: object
properties:
id:
type: string
type:
type: string
serviceEndpoint:
type: string
hypersignprotocol.hidnode.ssi.SignInfo:
type: object
properties:
Expand Down
26 changes: 22 additions & 4 deletions proto/ssi/v1/did.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ syntax = "proto3";
package hypersignprotocol.hidnode.ssi;
option go_package = "github.com/hypersign-protocol/hid-node/x/ssi/types";

import "ssi/v1/tx.proto";

message Did {
repeated string context = 1;
string id = 2;
Expand All @@ -14,6 +12,26 @@ message Did {
repeated string assertionMethod = 7;
repeated string keyAgreement = 8;
repeated string capabilityInvocation = 9;
string created = 10;
string updated = 11;
repeated string capabilityDelegation = 10;
repeated Service service = 11;
string created = 12;
string updated = 13;
}

message VerificationMethod {
string id = 1;
string type = 2;
string controller = 3;
string publicKeyMultibase = 4;
}

message Service {
string id = 1;
string type = 2;
string serviceEndpoint = 3;
}

message SignInfo {
string verification_method_id = 1;
string signature = 2;
}
2 changes: 1 addition & 1 deletion proto/ssi/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ message QueryGetDidDocByIdRequest {
}

message QueryGetDidDocByIdResponse {
DidDocStructCreateDID didDoc = 1;
Did didDoc = 1;
}

message QueryDidDocCountRequest {
Expand Down
45 changes: 3 additions & 42 deletions proto/ssi/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package hypersignprotocol.hidnode.ssi;
option go_package = "github.com/hypersign-protocol/hid-node/x/ssi/types";

import "ssi/v1/schema.proto";
import "ssi/v1/did.proto";

// Msg defines the Msg service.
service Msg {
Expand All @@ -16,57 +17,17 @@ service Msg {
}

message MsgCreateDID {
DidDocStructCreateDID didDocString = 1;
Did didDocString = 1;
repeated SignInfo signatures = 2;
string creator = 3;
}

message DidDocStructCreateDID {
repeated string context = 1;
string id = 2;
repeated string controller = 3; // DID Controller Spec: https://www.w3.org/TR/did-core/#did-controller
repeated string alsoKnownAs = 4;
repeated VerificationMethod verificationMethod = 5;
repeated string authentication = 6;
repeated string assertionMethod = 7;
repeated string keyAgreement = 8;
repeated string capabilityInvocation = 9;
string created = 10;
string updated = 11;
}

message DidDocStructUpdateDID {
repeated string context = 1;
string id = 2;
repeated string controller = 3; // DID Controller Spec: https://www.w3.org/TR/did-core/#did-controller
repeated string alsoKnownAs = 4;
repeated VerificationMethod verificationMethod = 5;
repeated string authentication = 6;
repeated string assertionMethod = 7;
repeated string keyAgreement = 8;
repeated string capabilityInvocation = 9;
string created = 10;
string updated = 11;
}

message VerificationMethod {
string id = 1;
string type = 2;
string controller = 3;
string publicKeyMultibase = 4;
}

message MsgCreateDIDResponse {
uint64 id = 1;
}

message SignInfo {
string verification_method_id = 1;
string signature = 2;
}

message MsgUpdateDID {
DidDocStructUpdateDID didDocString = 1;
Did didDocString = 1;
repeated SignInfo signatures = 2;
string creator = 3;
}
Expand Down
4 changes: 2 additions & 2 deletions x/ssi/client/cli/tx_ssi.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func CmdCreateDID() *cobra.Command {
}

// Unmarshal DidDocString
var didDoc types.DidDocStructCreateDID
var didDoc types.Did
err = clientCtx.Codec.UnmarshalJSON([]byte(argDidDocString), &didDoc)
if err != nil {
return err
Expand Down Expand Up @@ -81,7 +81,7 @@ func CmdUpdateDID() *cobra.Command {
}

// Unmarshal DidDocString
var didDoc types.DidDocStructUpdateDID
var didDoc types.Did
err = clientCtx.Codec.UnmarshalJSON([]byte(argDidDocString), &didDoc)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions x/ssi/client/cli/tx_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
)

func getVerKey(cmd *cobra.Command, clientCtx client.Context) (ed25519.PrivateKey, error) {
// Try getting from arg
// Get the verification key from --ver-key flag
verKeyPrivBase64, err := cmd.Flags().GetString(VerKeyFlag)
if err != nil {
return nil, err
}

// Decode key
// Decode key into bytes
verKeyPrivBytes, err := base64.StdEncoding.DecodeString(verKeyPrivBase64)
if err != nil {
return nil, err
Expand Down
25 changes: 11 additions & 14 deletions x/ssi/keeper/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
)

func (k Keeper) GetDidCount(ctx sdk.Context) uint64 {
// Get the store using storeKey (which is "blog") and PostCountKey (which is "Post-count-")
// Get the store using storeKey and DidCountKey (which is "Did-count-")
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.DidCountKey))
// Convert the PostCountKey to bytes
// Convert the DidCountKey to bytes
byteKey := []byte(types.DidCountKey)
// Get the value of the count
bz := store.Get(byteKey)
// Return zero if the count value is not found (for example, it's the first post)
// Return zero if the count value is not found
if bz == nil {
return 0
}
Expand All @@ -32,16 +32,14 @@ func (k Keeper) HasDid(ctx sdk.Context, id string) bool {
}

// Retrieves the DID from the store
func (k Keeper) GetDid(ctx *sdk.Context, id string) (*types.DidDocStructCreateDID, error) {
func (k Keeper) GetDid(ctx *sdk.Context, id string) (*types.Did, error) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.DidKey))

if !k.HasDid(*ctx, id) {
return nil, sdkerrors.ErrNotFound
}

// TODO: Currently, we can interchange DidDocStructCreateDID with DidDocStructUpdateDID
// as they are similar. They need to have a similar Interface
var value types.DidDocStructCreateDID
var value types.Did
var bytes = store.Get([]byte(id))
if err := k.cdc.Unmarshal(bytes, &value); err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidType, err.Error())
Expand All @@ -51,14 +49,14 @@ func (k Keeper) GetDid(ctx *sdk.Context, id string) (*types.DidDocStructCreateDI
}

func (k Keeper) SetDidCount(ctx sdk.Context, count uint64) {
// Get the store using storeKey (which is "blog") and PostCountKey (which is "Post-count-")
// Get the store using storeKey and SchemaCountKey (which is "Did-count-")
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.DidCountKey))
// Convert the PostCountKey to bytes
// Convert the DidCountKey to bytes
byteKey := []byte(types.DidCountKey)
// Convert count from uint64 to string and get bytes
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, count)
// Set the value of Post-count- to count
// Set the value of Did-count- to count
store.Set(byteKey, bz)
}

Expand All @@ -71,15 +69,14 @@ func (k Keeper) SetDid(ctx sdk.Context, did types.Did) error {
}

func (k Keeper) AppendDID(ctx sdk.Context, didSpec types.Did) uint64 {
// Get the current number of posts in the store
// Get the current number of DIDs in the store
count := k.GetDidCount(ctx)
// Get the store
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.DidKey))
// Convert the post ID into bytes
// Marshal the post into bytes
// Marshal the DID into bytes
didDocString := k.cdc.MustMarshal(&didSpec)
store.Set(utils.UnsafeStrToBytes(didSpec.Id), didDocString)
// Update the post count
// Update the Did count
k.SetDidCount(ctx, count+1)
return count
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ import (
"google.golang.org/grpc/status"
)

func (k Keeper) DidDocCount(goCtx context.Context, req *types.QueryDidDocCountRequest) (*types.QueryDidDocCountResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ctx := sdk.UnwrapSDKContext(goCtx)

var didDocCount uint64 = k.GetDidCount(ctx)

return &types.QueryDidDocCountResponse{Count: didDocCount}, nil
}

func (k Keeper) GetDidDocById(goCtx context.Context, req *types.QueryGetDidDocByIdRequest) (*types.QueryGetDidDocByIdResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
Expand Down
22 changes: 0 additions & 22 deletions x/ssi/keeper/grpc_query_did_doc_count.go

This file was deleted.

0 comments on commit 42ee9fe

Please sign in to comment.