Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Errors documentation #171

Merged
merged 79 commits into from
Nov 9, 2021
Merged
Changes from 1 commit
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
ab28d07
Add schema, cred_def, did, attr
Sep 13, 2021
2da7dcd
Rename cred def, update protos
Sep 15, 2021
e31e21a
Bugfix for proto files
Sep 15, 2021
1f92ac6
Update types dir, remove wrong methods
Sep 15, 2021
52db074
Update general handler
Sep 15, 2021
165d069
Update attrib model
Sep 16, 2021
019f1fc
Merge branch 'main' of github.com:cheqd/cheqd-node into identity-impl…
Sep 17, 2021
75dd446
Added DIDDoc to protobufs
Sep 25, 2021
35dd91e
Updated proto entities fields and codebase
Sep 25, 2021
34ba388
Update keeper files for the new prototypes
Sep 27, 2021
6772661
Move CredDef to oneof feature and verification methods to list of string
Sep 29, 2021
14513f4
Make changes in tx module too
Sep 29, 2021
382e12f
fixed integration tests and build
Sep 30, 2021
b128396
fixed namespace in handling request
Sep 30, 2021
4c678cc
implemented signature verification
Sep 30, 2021
67c6661
Merge remote-tracking branch 'origin/main' into feature/signature-ver…
Oct 4, 2021
18b8840
move to cosmos 44
Oct 4, 2021
c3ad7fb
Merge pull request #151 from cheqd/fix-build
Toktar Oct 4, 2021
2de0975
Merge pull request #152 from cheqd/feature/signature-verification
Toktar Oct 4, 2021
ff3304b
fixed go lint
Oct 4, 2021
4c9e8e1
fixed proto
Oct 4, 2021
2fb8a21
improved CLI
Oct 5, 2021
c995665
gitbook purge
ankurdotb Oct 9, 2021
2b5e6b0
added state protobuf
Oct 11, 2021
85f4ff1
changed the way how we store state
Oct 13, 2021
d017e4e
added check that did doc already exists
Oct 13, 2021
61e4f85
replay protection
Oct 13, 2021
7a7740a
updated did proto
Oct 14, 2021
b460944
added write request validation
Oct 14, 2021
a102ac6
signature implementation;
Oct 14, 2021
f1a80c8
add validation to cred def
Oct 14, 2021
a289054
add validation to cred def Id
Oct 15, 2021
93cf6a3
added schema validation
Oct 15, 2021
f36a200
fixed integration test
Oct 15, 2021
dcba9ca
fixed check did doc
Oct 15, 2021
50193b1
added strings utils functions
Oct 17, 2021
19a1462
added update did signature validation
Oct 17, 2021
76f568d
added validation to create and update did
Oct 19, 2021
5ef82ce
fixed models after review
Oct 19, 2021
119f8d8
removed magic const
Oct 19, 2021
c1cb235
fixed lint errors
Oct 19, 2021
fba051f
fixed unit test
Oct 19, 2021
5fe68d1
removed unused function
Oct 19, 2021
6ec0722
fixed did tests
Oct 19, 2021
2dc6c80
fixed go lint
Oct 19, 2021
958e49a
fixed build
Oct 19, 2021
3220750
added MaxAttrNamesCount constant
Oct 19, 2021
4592fdb
add namespace to ValidateBasic
Oct 20, 2021
464735e
fixed unit tests
Oct 20, 2021
3455d77
fixed unit tests and lint
Oct 20, 2021
628eee2
process review
Oct 20, 2021
6363083
changed build #put
Oct 20, 2021
1771529
added fixtures
Oct 20, 2021
3a02b6d
added more tests for did creation
Oct 20, 2021
3bc97d0
added update did tests
Oct 21, 2021
4bffc95
Merge remote-tracking branch 'origin/main' into identity-implementation
Oct 21, 2021
b6ddb53
Revert adr 002 changes
askolesov Oct 21, 2021
2097499
fixed test
Oct 24, 2021
565e19b
fixed routing
Oct 24, 2021
d1a80c4
removed map from protobuf
Oct 25, 2021
05d917e
Review issues fixes
askolesov Oct 25, 2021
a8f078e
fixed unmarshalling
Oct 25, 2021
c4ee196
Remove unnecessary memKey, basicValidation. Fix tests.
askolesov Oct 25, 2021
86d1bc4
Make state keys naming consistent
askolesov Oct 26, 2021
0031a04
process review comments
Oct 28, 2021
149885a
fixed unit tests
Oct 28, 2021
a202a58
processed review comments
Oct 28, 2021
9edbd2b
fixed tests
Oct 28, 2021
2d7373f
changed signature derivation
Oct 28, 2021
1bd4ff1
removed cred_def and schema from protobuf
Oct 28, 2021
d1319da
removed cred_def and schema from handler and keeper and types
Oct 28, 2021
c50b01e
removed cred_def and schema from keys and utils
Oct 28, 2021
e4b183f
removed cred_def and schema from comments
Oct 28, 2021
0ccc79c
added decode multibase function
Oct 28, 2021
f8a8cb0
added verification method check
Oct 28, 2021
b91ead3
changed function name according PR
Oct 29, 2021
f700673
fixed lint
Oct 29, 2021
c9c9fa7
fixed tests
Oct 29, 2021
da7e7a2
documented cheqd errors
Oct 29, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added update did signature validation
  • Loading branch information
Alexander Martynov committed Oct 17, 2021
commit 19a146269858f08b226d36709c89eeb80271956f
80 changes: 75 additions & 5 deletions x/cheqd/keeper/msg_server_did.go
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@ package keeper
import (
"context"
"fmt"
"github.com/cheqd/cheqd-node/x/cheqd/utils/strings"
"reflect"

"github.com/cheqd/cheqd-node/x/cheqd/types"
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -62,20 +64,20 @@ func (k msgServer) UpdateDid(goCtx context.Context, msg *types.MsgWriteRequest)
return nil, err
}

if err := k.VerifySignature(&ctx, msg, didMsg.GetSigners()); err != nil {
return nil, err
}

// Checks that the element exists
if !k.HasDid(ctx, didMsg.Id) {
return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprintf("key %s doesn't exist", didMsg.Id))
}

_, metadata, err := k.GetDid(&ctx, didMsg.Id)
oldDIDDoc, metadata, err := k.GetDid(&ctx, didMsg.Id)
if err != nil {
return nil, err
}

if err := k.UpdateDidVerifySignature(&ctx, msg, oldDIDDoc, didMsg); err != nil {
return nil, err
}

versionId, exists := msg.Metadata["versionId"]
if !exists {
return nil, sdkerrors.Wrap(types.ErrUnexpectedDidVersion, "Metadata doesn't contain `versionId`")
@@ -106,3 +108,71 @@ func (k msgServer) UpdateDid(goCtx context.Context, msg *types.MsgWriteRequest)
Id: didMsg.Id,
}, nil
}

func (k msgServer) UpdateDidVerifySignature(ctx *sdk.Context, msg *types.MsgWriteRequest, oldDIDDoc *types.Did, newDIDDoc *types.MsgUpdateDid) error {
var signers = newDIDDoc.GetSigners()

// Get Old DID Doc controller if it's nil then assign self
oldController := oldDIDDoc.Controller
if len(oldController) == 0 {
oldController = []string{oldDIDDoc.Id}
}

// Get New DID Doc controller if it's nil then assign self
newController := newDIDDoc.Controller
if len(newController) == 0 {
newController = []string{newDIDDoc.Id}
}

// DID Doc controller has been changed
if removedControllers := strings.Complement(oldController, newController); len(removedControllers) > 0 {
for _, controller := range removedControllers {
signers = append(signers, types.Signer{Signer: controller})
}
}

for _, oldVM := range oldDIDDoc.VerificationMethod {
newVM := FindVerificationMethod(newDIDDoc.VerificationMethod, oldVM.Id)

// Verification Method has been deleted
if newVM == nil {
signers = AppendSignerIfNeed(signers, oldVM.Controller, newDIDDoc)
continue
}

// Verification Method has been changed
if !reflect.DeepEqual(oldVM, newVM) {
signers = AppendSignerIfNeed(signers, newVM.Controller, newDIDDoc)
}

// Verification Method Controller has been changed, need to add old controller
if newVM.Controller != oldVM.Controller {
signers = AppendSignerIfNeed(signers, oldVM.Controller, newDIDDoc)
}
}

if err := k.VerifySignature(ctx, msg, signers); err != nil {
return err
}

return nil
}

func AppendSignerIfNeed(signers []types.Signer, controller string, msg *types.MsgUpdateDid) []types.Signer {
for _, signer := range signers {
if signer.Signer == controller {
return signers
}
}

signer := types.Signer{
Signer: controller,
}

if controller == msg.Id {
signer.VerificationMethod = msg.VerificationMethod
signer.Authentication = msg.Authentication
}

return append(signers, signer)
}
50 changes: 50 additions & 0 deletions x/cheqd/keeper/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package keeper

import (
"crypto/ed25519"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/btcsuite/btcutil/base58"
"github.com/cheqd/cheqd-node/x/cheqd/types"
)

func BuildSigningInput(msg *types.MsgWriteRequest) ([]byte, error) {
metadataBytes, err := json.Marshal(&msg.Metadata)
if err != nil {
return nil, types.ErrInvalidSignature.Wrap("An error has occurred during metadata marshalling")
}

dataBytes := msg.Data.Value
signingInput := ([]byte)(base64.StdEncoding.EncodeToString(metadataBytes) + base64.StdEncoding.EncodeToString(dataBytes))
return signingInput, nil
}

func FindPublicKey(signer types.Signer, id string) (ed25519.PublicKey, error) {
for _, authentication := range signer.Authentication {
if authentication == id {
for _, vm := range signer.VerificationMethod {
if vm.Id == id {
return base58.Decode(vm.PublicKeyMultibase[1:]), nil
}
}

msg := fmt.Sprintf("Verification Method %s not found", id)
return nil, errors.New(msg)
}
}

msg := fmt.Sprintf("Authentication %s not found", id)
return nil, errors.New(msg)
}

func FindVerificationMethod(vms []*types.VerificationMethod, id string) *types.VerificationMethod {
for _, vm := range vms {
if vm.Id == id {
return vm
}
}

return nil
}
37 changes: 6 additions & 31 deletions x/cheqd/keeper/verify.go
Original file line number Diff line number Diff line change
@@ -3,10 +3,8 @@ package keeper
import (
"crypto/ed25519"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/btcsuite/btcutil/base58"
"github.com/cheqd/cheqd-node/x/cheqd/types"
"github.com/cheqd/cheqd-node/x/cheqd/utils"
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -27,7 +25,7 @@ func (k *Keeper) VerifySignature(ctx *sdk.Context, msg *types.MsgWriteRequest, s
if signer.VerificationMethod == nil {
didDoc, _, err := k.GetDid(ctx, signer.Signer)
if err != nil {
return sdkerrors.Wrap(types.ErrInvalidSignature, err.Error())
return types.ErrDidDocNotFound.Wrap(signer.Signer)
}

signer.Authentication = didDoc.Authentication
@@ -47,19 +45,9 @@ func (k *Keeper) VerifySignature(ctx *sdk.Context, msg *types.MsgWriteRequest, s
return nil
}

func BuildSigningInput(msg *types.MsgWriteRequest) ([]byte, error) {
metadataBytes, err := json.Marshal(&msg.Metadata)
if err != nil {
return nil, types.ErrInvalidSignature.Wrap("An error has occurred during metadata marshalling")
}

dataBytes := msg.Data.Value
signingInput := ([]byte)(base64.StdEncoding.EncodeToString(metadataBytes) + base64.StdEncoding.EncodeToString(dataBytes))
return signingInput, nil
}

func VerifyIdentitySignature(signer types.Signer, signatures map[string]string, signingInput []byte) (bool, error) {
result := true
foundOne := false

for id, signature := range signatures {
did, _ := utils.SplitDidUrlIntoDidAndFragment(id)
@@ -75,26 +63,13 @@ func VerifyIdentitySignature(signer types.Signer, signatures map[string]string,
}

result = result && ed25519.Verify(pubKey, signingInput, signature)
foundOne = true
}
}

return result, nil
}

func FindPublicKey(signer types.Signer, id string) (ed25519.PublicKey, error) {
for _, authentication := range signer.Authentication {
if authentication == id {
for _, vm := range signer.VerificationMethod {
if vm.Id == id {
return base58.Decode(vm.PublicKeyMultibase[1:]), nil
}
}

msg := fmt.Sprintf("Verification Method %s not found", id)
return nil, errors.New(msg)
}
if !foundOne {
return false, errors.New(fmt.Sprintf("signature %s not found", signer.Signer))
}

msg := fmt.Sprintf("Authentication %s not found", id)
return nil, errors.New(msg)
return result, nil
}
47 changes: 14 additions & 33 deletions x/cheqd/handler_test.go → x/cheqd/tests/handler_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cheqd
package tests

import (
"crypto/ed25519"
@@ -14,7 +14,7 @@ import (
func TestHandler_CreateDid(t *testing.T) {
setup := Setup()

_, did, _ := setup.InitDid()
_, did, _ := setup.InitDid("did:cheqd:test:alice")

// query Did
receivedDid, _, _ := setup.Keeper.GetDid(&setup.Ctx, did.Id)
@@ -37,7 +37,7 @@ func TestHandler_UpdateDid(t *testing.T) {
setup := Setup()

//Init did
privKey, did, _ := setup.InitDid()
keys, did, _ := setup.InitDid("did:cheqd:test:alice")

// query Did
receivedDid, didMetadata, _ := setup.Keeper.GetDid(&setup.Ctx, did.Id)
@@ -52,7 +52,7 @@ func TestHandler_UpdateDid(t *testing.T) {

didMsgUpdate := setup.UpdateDid(receivedDid, newPubKey)
dataUpdate, _ := ptypes.NewAnyWithValue(didMsgUpdate)
resultUpdate, _ := setup.Handler(setup.Ctx, setup.WrapRequest(privKey, dataUpdate, metadata))
resultUpdate, _ := setup.Handler(setup.Ctx, setup.WrapRequest(dataUpdate, keys, metadata))

didUpdated := types.MsgUpdateDidResponse{}
errUpdate := didUpdated.Unmarshal(resultUpdate.Data)
@@ -78,33 +78,14 @@ func TestHandler_UpdateDid(t *testing.T) {
require.NotEqual(t, receivedDid.VerificationMethod, receivedUpdatedDid.VerificationMethod)
}

func TestHandler_UpdateDidInvalidSignature(t *testing.T) {
setup := Setup()

_, did, _ := setup.InitDid()

// query Did
receivedDid, _, _ := setup.Keeper.GetDid(&setup.Ctx, did.Id)

//Init priv key
newPubKey, newPrivKey, _ := ed25519.GenerateKey(rand.Reader)

// add new Did
didMsgUpdate := setup.UpdateDid(receivedDid, newPubKey)
dataUpdate, _ := ptypes.NewAnyWithValue(didMsgUpdate)
_, err := setup.Handler(setup.Ctx, setup.WrapRequest(newPrivKey, dataUpdate, make(map[string]string)))
require.Error(t, err)
require.Equal(t, "did:cheqd:test:alice: invalid signature detected", err.Error())
}

func TestHandler_CreateSchema(t *testing.T) {
setup := Setup()

privKey, _, _ := setup.InitDid()
keys, _, _ := setup.InitDid("did:cheqd:test:alice")
msg := setup.CreateSchema()

data, _ := ptypes.NewAnyWithValue(msg)
result, _ := setup.Handler(setup.Ctx, setup.WrapRequest(privKey, data, make(map[string]string)))
result, _ := setup.Handler(setup.Ctx, setup.WrapRequest(data, keys, make(map[string]string)))

schema := types.MsgCreateSchemaResponse{}
err := schema.Unmarshal(result.Data)
@@ -127,11 +108,11 @@ func TestHandler_CreateSchema(t *testing.T) {
func TestHandler_CreateCredDef(t *testing.T) {
setup := Setup()

privKey, _, _ := setup.InitDid()
keys, _, _ := setup.InitDid("did:cheqd:test:alice")
msg := setup.CreateCredDef()

data, _ := ptypes.NewAnyWithValue(msg)
result, _ := setup.Handler(setup.Ctx, setup.WrapRequest(privKey, data, make(map[string]string)))
result, _ := setup.Handler(setup.Ctx, setup.WrapRequest(data, keys, make(map[string]string)))

credDef := types.MsgCreateCredDefResponse{}
err := credDef.Unmarshal(result.Data)
@@ -157,24 +138,24 @@ func TestHandler_CreateCredDef(t *testing.T) {
func TestHandler_DidDocAlreadyExists(t *testing.T) {
setup := Setup()

privKey, _, _ := setup.InitDid()
_, _, err := setup.InitDid()
keys, _, _ := setup.InitDid("did:cheqd:test:alice")
_, _, err := setup.InitDid("did:cheqd:test:alice")

require.Error(t, err)
require.Equal(t, "DID DOC already exists for DID did:cheqd:test:alice: DID Doc exists", err.Error())

credDefMsg := setup.CreateCredDef()
data, _ := ptypes.NewAnyWithValue(credDefMsg)
_, _ = setup.Handler(setup.Ctx, setup.WrapRequest(privKey, data, make(map[string]string)))
_, err = setup.Handler(setup.Ctx, setup.WrapRequest(privKey, data, make(map[string]string)))
_, _ = setup.Handler(setup.Ctx, setup.WrapRequest(data, keys, make(map[string]string)))
_, err = setup.Handler(setup.Ctx, setup.WrapRequest(data, keys, make(map[string]string)))

require.Error(t, err)
require.Equal(t, "DID DOC already exists for CredDef did:cheqd:test:cred-def-1/credDef: DID Doc exists", err.Error())

schemaMsg := setup.CreateSchema()
data, _ = ptypes.NewAnyWithValue(schemaMsg)
_, _ = setup.Handler(setup.Ctx, setup.WrapRequest(privKey, data, make(map[string]string)))
_, err = setup.Handler(setup.Ctx, setup.WrapRequest(privKey, data, make(map[string]string)))
_, _ = setup.Handler(setup.Ctx, setup.WrapRequest(data, keys, make(map[string]string)))
_, err = setup.Handler(setup.Ctx, setup.WrapRequest(data, keys, make(map[string]string)))

require.Error(t, err)
require.Equal(t, "DID DOC already exists for Schema did:cheqd:test:schema-1/schema: DID Doc exists", err.Error())
Loading