Skip to content

Commit

Permalink
Fix issue with sample DID resource (#56)
Browse files Browse the repository at this point in the history
* Fix `swisstronikd debug sample-did-resource` (#55)

* fix: fix issue with derivation of collection id

* fix: fix issue with signature for creation of resource

---------

Co-authored-by: Denis <[email protected]>
Co-authored-by: kenta92115 <[email protected]>
  • Loading branch information
3 people authored Dec 1, 2023
1 parent c9c6550 commit ccbd9f8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
13 changes: 10 additions & 3 deletions cmd/swisstronikd/cmd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,13 @@ func SampleDIDResource() *cobra.Command {
err error
)
// Check if private key was provided.
if len(args) != 1 {
if len(args) == 1 {
_, privateKey, err = ed25519.GenerateKey(rand.Reader)
if err != nil {
return err
}
} else {
privateKeyBytes, err := base64.StdEncoding.DecodeString(args[0])
privateKeyBytes, err := base64.StdEncoding.DecodeString(args[1])
if err != nil {
return err
}
Expand All @@ -210,12 +210,19 @@ func SampleDIDResource() *cobra.Command {
if !didtypes.IsValidDID(did, didtypes.DIDMethod) {
return fmt.Errorf("provided DID is invalid")
}

// Derive collection id from provided DID
_, collectionId, err := didtypes.TrySplitDID(did)
if err != nil {
return err
}

resource := didtypes.MsgCreateResourcePayload{
CollectionId: did,
CollectionId: collectionId,
Id: uuid.NewString(),
Name: "sample-resource",
Version: "sample-version",
ResourceType: "SampleResourceType",
AlsoKnownAs: []*types.AlternativeUri{
{
Uri: "http://example.com/example-did",
Expand Down
3 changes: 2 additions & 1 deletion x/did/client/cli/tx_create_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"os"
"encoding/json"

"swisstronik/x/did/types"
"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -70,7 +71,7 @@ Example payload file:

// Unmarshal payload
var payload types.MsgCreateResourcePayload
err = clientCtx.Codec.UnmarshalJSON(payloadJSON, &payload)
err = json.Unmarshal(payloadJSON, &payload)
if err != nil {
return err
}
Expand Down
12 changes: 5 additions & 7 deletions x/did/types/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,22 @@ var (

// TrySplitDID Validates generic format of DID. It doesn't validate method, name and id content.
// Call ValidateDID for further validation.
func TrySplitDID(did string) (method string, namespace string, id string, err error) {
func TrySplitDID(did string) (method string, id string, err error) {
// Example: did:swtr:base58str1ng1111
// match [0] - the whole string
// match [1] - swtr - method
// match [2] - :testnet
// match [3] - testnet - namespace
// match [4] - base58str1ng1111 - id
matches := SplitDIDRegexp.FindAllStringSubmatch(did, -1)
if len(matches) != 1 {
return "", "", "", errors.New("unable to split did into method, namespace and id")
return "", "", errors.New("unable to split did into method, namespace and id")
}

match := matches[0]
return match[1], match[3], match[4], nil
return match[1], match[4], nil
}

func MustSplitDID(did string) (method string, namespace string, id string) {
method, namespace, id, err := TrySplitDID(did)
method, id, err := TrySplitDID(did)
if err != nil {
panic(err.Error())
}
Expand Down Expand Up @@ -62,7 +60,7 @@ func ReplaceDIDInDIDURLList(didURLList []string, oldDid string, newDid string) [

// ValidateDID checks method and allowed namespaces only when the corresponding parameters are specified.
func ValidateDID(did string, method string) error {
sMethod, _, sUniqueID, err := TrySplitDID(did)
sMethod, sUniqueID, err := TrySplitDID(did)
if err != nil {
return err
}
Expand Down

0 comments on commit ccbd9f8

Please sign in to comment.