Skip to content

Commit

Permalink
Merge pull request #31 from hypersign-protocol/update-did-ssi-module
Browse files Browse the repository at this point in the history
Update did ssi module
  • Loading branch information
arnabghose997 committed Feb 11, 2022
2 parents fccb58b + 880007f commit e34dcd2
Show file tree
Hide file tree
Showing 18 changed files with 2,332 additions and 229 deletions.
53 changes: 52 additions & 1 deletion docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19795,7 +19795,53 @@ definitions:
description: >-
MsgCreateVestingAccountResponse defines the Msg/CreateVestingAccount
response type.
hypersignprotocol.hidnode.ssi.DidDocStruct:
hypersignprotocol.hidnode.ssi.DidDocStructCreateDID:
type: object
properties:
context:
type: array
items:
type: string
type:
type: string
id:
type: string
name:
type: string
publicKey:
type: array
items:
type: object
properties:
context:
type: string
id:
type: string
type:
type: string
publicKeyBase58:
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:
type: string
hypersignprotocol.hidnode.ssi.DidDocStructUpdateDID:
type: object
properties:
context:
Expand Down Expand Up @@ -19847,6 +19893,11 @@ definitions:
id:
type: string
format: uint64
hypersignprotocol.hidnode.ssi.MsgUpdateDIDResponse:
type: object
properties:
updateId:
type: string
hypersignprotocol.hidnode.ssi.Params:
type: object
description: Params defines the parameters for the module.
Expand Down
13 changes: 11 additions & 2 deletions proto/ssi/v1/did.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ option go_package = "github.com/hypersign-protocol/hid-node/x/ssi/types";
import "ssi/v1/tx.proto";

message Did {
uint64 id = 1;
DidDocStruct didDocString = 2;
repeated string context = 1;
string type = 2;
string id = 3;
string name = 4;
repeated PublicKeyStruct publicKey = 5;
repeated string authentication = 6;
repeated string assertionMethod = 7;
repeated string keyAgreement = 8;
repeated string capabilityInvocation = 9;
string created = 10;
string updated = 11;
}
30 changes: 28 additions & 2 deletions proto/ssi/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,31 @@ option go_package = "github.com/hypersign-protocol/hid-node/x/ssi/types";
// Msg defines the Msg service.
service Msg {
rpc CreateDID(MsgCreateDID) returns (MsgCreateDIDResponse);
rpc UpdateDID(MsgUpdateDID) returns (MsgUpdateDIDResponse);
// this line is used by starport scaffolding # proto/tx/rpc
}

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

message DidDocStruct {
message DidDocStructCreateDID {
repeated string context = 1;
string type = 2;
string id = 3;
string name = 4;
repeated PublicKeyStruct publicKey = 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 type = 2;
string id = 3;
Expand Down Expand Up @@ -46,4 +61,15 @@ message SignInfo {
string verification_method_id = 1;
string signature = 2;
}

message MsgUpdateDID {
DidDocStructUpdateDID didDocString = 1;
repeated SignInfo signatures = 2;
string creator = 3;
}

message MsgUpdateDIDResponse {
string updateId = 1;
}

// this line is used by starport scaffolding # proto/tx/message
1 change: 1 addition & 0 deletions x/ssi/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func GetTxCmd() *cobra.Command {
}

cmd.AddCommand(CmdCreateDID())
cmd.AddCommand(CmdUpdateDID())
// this line is used by starport scaffolding # 1

return cmd
Expand Down
56 changes: 55 additions & 1 deletion 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.DidDocStruct
var didDoc types.DidDocStructCreateDID
err = clientCtx.Codec.UnmarshalJSON([]byte(argDidDocString), &didDoc)
if err != nil {
return err
Expand Down Expand Up @@ -65,3 +65,57 @@ func CmdCreateDID() *cobra.Command {
cmd.Flags().String(VerKeyFlag, "", "Base64 encoded ed25519 private key to sign identity message with. ")
return cmd
}

func CmdUpdateDID() *cobra.Command {
cmd := &cobra.Command{
Use: "update-did [did-doc-string] [verification-method-id]",
Short: "Updates the DID",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argDidDocString := args[0]
argVerificationMethodId := args[1]

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

// Unmarshal DidDocString
var didDoc types.DidDocStructUpdateDID
err = clientCtx.Codec.UnmarshalJSON([]byte(argDidDocString), &didDoc)
if err != nil {
return err
}

verKeyPriv, err := getVerKey(cmd, clientCtx)
if err != nil {
return err
}

// // Build identity message
signBytes := didDoc.GetSignBytes()
signatureBytes := ed25519.Sign(verKeyPriv, signBytes)

signInfo := types.SignInfo{
VerificationMethodId: argVerificationMethodId,
Signature: base64.StdEncoding.EncodeToString(signatureBytes),
}

msg := types.MsgUpdateDID{
Creator: clientCtx.GetFromAddress().String(),
DidDocString: &didDoc,
Signatures: []*types.SignInfo{&signInfo},
}

if err := msg.ValidateBasic(); err != nil {
return err
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}

flags.AddTxFlagsToCmd(cmd)
cmd.Flags().String(VerKeyFlag, "", "Base64 encoded ed25519 private key to sign identity message with. ")
return cmd
}
3 changes: 1 addition & 2 deletions x/ssi/client/cli/tx_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/spf13/cobra"
)


func getVerKey(cmd *cobra.Command, clientCtx client.Context) (ed25519.PrivateKey, error) {
// Try getting from arg
verKeyPrivBase64, err := cmd.Flags().GetString(VerKeyFlag)
Expand All @@ -23,4 +22,4 @@ func getVerKey(cmd *cobra.Command, clientCtx client.Context) (ed25519.PrivateKey
}

return verKeyPrivBytes, nil
}
}
3 changes: 3 additions & 0 deletions x/ssi/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
case *types.MsgCreateDID:
res, err := msgServer.CreateDID(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgUpdateDID:
res, err := msgServer.UpdateDID(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
// this line is used by starport scaffolding # 1
default:
errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg)
Expand Down
36 changes: 30 additions & 6 deletions x/ssi/keeper/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/hypersign-protocol/hid-node/utils"
"github.com/hypersign-protocol/hid-node/x/ssi/types"
)
Expand All @@ -30,6 +31,25 @@ func (k Keeper) HasDid(ctx sdk.Context, id string) bool {
return store.Has(utils.UnsafeStrToBytes(id))
}

// Retrieves the DID from the store
func (k Keeper) GetDid(ctx *sdk.Context, id string) (*types.DidDocStructCreateDID, 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 bytes = store.Get([]byte(id))
if err := k.cdc.Unmarshal(bytes, &value); err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidType, err.Error())
}

return &value, nil
}

func (k Keeper) SetDidCount(ctx sdk.Context, count uint64) {
// Get the store using storeKey (which is "blog") and PostCountKey (which is "Post-count-")
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.DidCountKey))
Expand All @@ -42,19 +62,23 @@ func (k Keeper) SetDidCount(ctx sdk.Context, count uint64) {
store.Set(byteKey, bz)
}

// SetDid set a specific did in the store
func (k Keeper) SetDid(ctx sdk.Context, did types.Did) error {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.DidKey))
b := k.cdc.MustMarshal(&did)
store.Set([]byte(did.Id), b)
return nil
}

func (k Keeper) AppendDID(ctx sdk.Context, didSpec types.Did) uint64 {
// Get the current number of posts in the store
count := k.GetDidCount(ctx)
// Assign an ID to the post based on the number of posts in the store
didSpec.Id = count
// Get the store
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.DidKey))
// Convert the post ID into bytes
byteKey := make([]byte, 8)
binary.BigEndian.PutUint64(byteKey, didSpec.Id)
// Marshal the post into bytes
didDocString := k.cdc.MustMarshal(didSpec.DidDocString)
store.Set(utils.UnsafeStrToBytes(didSpec.DidDocString.Id), didDocString)
didDocString := k.cdc.MustMarshal(&didSpec)
store.Set(utils.UnsafeStrToBytes(didSpec.Id), didDocString)
// Update the post count
k.SetDidCount(ctx, count+1)
return count
Expand Down
18 changes: 14 additions & 4 deletions x/ssi/keeper/msg_server_create_did.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
utils "github.com/hypersign-protocol/hid-node/x/ssi/utils"
"github.com/hypersign-protocol/hid-node/x/ssi/types"
utils "github.com/hypersign-protocol/hid-node/x/ssi/utils"
)

func (k msgServer) CreateDID(goCtx context.Context, msg *types.MsgCreateDID) (*types.MsgCreateDIDResponse, error) {
Expand All @@ -24,8 +24,8 @@ func (k msgServer) CreateDID(goCtx context.Context, msg *types.MsgCreateDID) (*t
didDocCheck := utils.IsValidDidDoc(msg.DidDocString)
if didDocCheck != "" {
return nil, sdkerrors.Wrap(types.ErrInvalidDidDoc, didDocCheck)
}
}

// Signature check
if err := k.VerifySignature(&ctx, didMsg, didMsg.GetSigners(), msg.GetSignatures()); err != nil {
return nil, err
Expand All @@ -37,7 +37,17 @@ func (k msgServer) CreateDID(goCtx context.Context, msg *types.MsgCreateDID) (*t
}

var didSpec = types.Did{
DidDocString: msg.DidDocString,
Context: didMsg.GetContext(),
Type: didMsg.GetType(),
Id: didMsg.GetId(),
Name: didMsg.GetName(),
PublicKey: didMsg.GetPublicKey(),
Authentication: didMsg.GetAuthentication(),
AssertionMethod: didMsg.GetAssertionMethod(),
KeyAgreement: didMsg.GetKeyAgreement(),
CapabilityInvocation: didMsg.GetCapabilityInvocation(),
Created: didMsg.GetCreated(),
Updated: didMsg.GetUpdated(),
}
// Add a DID to the store and get back the ID
id := k.AppendDID(ctx, didSpec)
Expand Down
16 changes: 0 additions & 16 deletions x/ssi/keeper/msg_server_test.go

This file was deleted.

Loading

0 comments on commit e34dcd2

Please sign in to comment.