Skip to content

Commit

Permalink
fix: Resource dereferencing
Browse files Browse the repository at this point in the history
  • Loading branch information
Toktar committed Jul 18, 2022
1 parent e3a35d7 commit fa91905
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
6 changes: 4 additions & 2 deletions services/ledger_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (ls LedgerService) QueryDIDDoc(did string) (cheqd.Did, cheqd.Metadata, bool
}

func (ls LedgerService) QueryResource(did string, resourceId string) (resource.Resource, bool, error) {
collectionId, namespace, _, _ := cheqdUtils.TrySplitDID(did)
_, namespace, collectionId, _ := cheqdUtils.TrySplitDID(did)
serverAddr, namespaceFound := ls.ledgers[namespace]
if !namespaceFound {
return resource.Resource{}, false, fmt.Errorf("namespace not supported: %s", namespace)
Expand All @@ -78,11 +78,13 @@ func (ls LedgerService) QueryResource(did string, resourceId string) (resource.R

defer mustCloseGRPCConnection(conn)

log.Info().Msgf("Querying did resource: %s, %s", did, resourceId)
log.Info().Msgf("Querying did resource: %s, %s", collectionId, resourceId)

client := resource.NewQueryClient(conn)
resourceResponse, err := client.Resource(context.Background(), &resource.QueryGetResourceRequest{CollectionId: collectionId, Id: resourceId})
if err != nil {
log.Info().Msgf("Resource not found %r", err.Error())

return resource.Resource{}, false, nil
}

Expand Down
1 change: 1 addition & 0 deletions tests/pytest/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
TESTNET_DID = "did:cheqd:testnet:zFWM1mKVGGU2gHYuLAQcTJfZBebqBpGf"
TESTNET_FRAGMENT = TESTNET_DID + "#key1"
FAKE_TESTNET_DID = "did:cheqd:testnet:zF7rhDBfUt9d1gJPjx7s1JXfUY7oVWkY"
TESTNET_RESOURCE = "did:cheqd:testnet:DAzMQo4MDMxCjgwM" + "/resources/44547089-170b-4f5a-bcbc-06e46e0089e4"
FAKE_TESTNET_FRAGMENT = TESTNET_DID + "#fake_key"
FAKE_TESTNET_RESOURCE = TESTNET_DID + "/resources/76471e8c-0d1c-4b97-9b11-17b65e024334"

Expand Down
4 changes: 3 additions & 1 deletion tests/pytest/test_resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from helpers import run, TESTNET_DID, MAINNET_DID, TESTNET_FRAGMENT, MAINNET_FRAGMENT, \
FAKE_TESTNET_DID, FAKE_MAINNET_DID, FAKE_TESTNET_FRAGMENT, FAKE_MAINNET_FRAGMENT, RESOLVER_URL, PATH, \
LDJSON, DIDJSON, DIDLDJSON, HTML, FAKE_TESTNET_RESOURCE
LDJSON, DIDJSON, DIDLDJSON, HTML, FAKE_TESTNET_RESOURCE, TESTNET_RESOURCE


@pytest.mark.parametrize(
Expand All @@ -29,6 +29,8 @@
(FAKE_MAINNET_FRAGMENT, r"\"contentStream\":null,\"contentMetadata\":\[\],"
r"\"dereferencingMetadata(.*?)\"error\":\"notFound\""),
(TESTNET_RESOURCE, fr"\"contentStream\":(.*?)collectionId(.*?),\"contentMetadata\":(.*?),"
r"\"dereferencingMetadata(.*?)"),
(FAKE_TESTNET_RESOURCE, r"\"contentStream\":null,\"contentMetadata\":\[\],"
r"\"dereferencingMetadata(.*?)\"error\":\"notFound\""),
]
Expand Down
6 changes: 4 additions & 2 deletions utils/did_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
)

var ResourcePath, _ = regexp.Compile(`resources\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}`)
var ResourceId, _ = regexp.Compile(`[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}`)

func GetResourceId(didUrlPath string) (id string) {
match := ResourcePath.FindStringSubmatch(didUrlPath)
if len(match) != 1 {
if !ResourcePath.Match([]byte(didUrlPath)) {
return ""
}

match := ResourceId.FindStringSubmatch(didUrlPath)
return match[0]
}

0 comments on commit fa91905

Please sign in to comment.