Skip to content

Commit 9052d67

Browse files
authored
Merge pull request #18 from cheqd/fix-resource-dereferencing
fix: Resource dereferencing
2 parents e3a35d7 + db161d5 commit 9052d67

4 files changed

Lines changed: 14 additions & 6 deletions

File tree

services/ledger_service.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (ls LedgerService) QueryDIDDoc(did string) (cheqd.Did, cheqd.Metadata, bool
6464
}
6565

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

7979
defer mustCloseGRPCConnection(conn)
8080

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

8383
client := resource.NewQueryClient(conn)
8484
resourceResponse, err := client.Resource(context.Background(), &resource.QueryGetResourceRequest{CollectionId: collectionId, Id: resourceId})
8585
if err != nil {
86+
log.Info().Msgf("Resource not found %s", err.Error())
8687
return resource.Resource{}, false, nil
8788
}
8889

tests/pytest/helpers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
TESTNET_DID = "did:cheqd:testnet:zFWM1mKVGGU2gHYuLAQcTJfZBebqBpGf"
1111
TESTNET_FRAGMENT = TESTNET_DID + "#key1"
1212
FAKE_TESTNET_DID = "did:cheqd:testnet:zF7rhDBfUt9d1gJPjx7s1JXfUY7oVWkY"
13+
TESTNET_RESOURCE = "did:cheqd:testnet:DAzMQo4MDMxCjgwM" + "/resources/44547089-170b-4f5a-bcbc-06e46e0089e4"
1314
FAKE_TESTNET_FRAGMENT = TESTNET_DID + "#fake_key"
1415
FAKE_TESTNET_RESOURCE = TESTNET_DID + "/resources/76471e8c-0d1c-4b97-9b11-17b65e024334"
1516

tests/pytest/test_resolution.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from helpers import run, TESTNET_DID, MAINNET_DID, TESTNET_FRAGMENT, MAINNET_FRAGMENT, \
77
FAKE_TESTNET_DID, FAKE_MAINNET_DID, FAKE_TESTNET_FRAGMENT, FAKE_MAINNET_FRAGMENT, RESOLVER_URL, PATH, \
8-
LDJSON, DIDJSON, DIDLDJSON, HTML, FAKE_TESTNET_RESOURCE
8+
LDJSON, DIDJSON, DIDLDJSON, HTML, FAKE_TESTNET_RESOURCE, TESTNET_RESOURCE
99

1010

1111
@pytest.mark.parametrize(
@@ -29,6 +29,8 @@
2929
(FAKE_MAINNET_FRAGMENT, r"\"contentStream\":null,\"contentMetadata\":\[\],"
3030
r"\"dereferencingMetadata(.*?)\"error\":\"notFound\""),
3131
32+
(TESTNET_RESOURCE, fr"\"contentStream\":(.*?)collectionId(.*?),\"contentMetadata\":(.*?),"
33+
r"\"dereferencingMetadata(.*?)"),
3234
(FAKE_TESTNET_RESOURCE, r"\"contentStream\":null,\"contentMetadata\":\[\],"
3335
r"\"dereferencingMetadata(.*?)\"error\":\"notFound\""),
3436
]

utils/did_url.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ import (
44
"regexp"
55
)
66

7-
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}`)
7+
var (
8+
ResourcePath, _ = regexp.Compile(`resources\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}`)
9+
ResourceId, _ = regexp.Compile(`[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}`)
10+
)
811

912
func GetResourceId(didUrlPath string) (id string) {
10-
match := ResourcePath.FindStringSubmatch(didUrlPath)
11-
if len(match) != 1 {
13+
if !ResourcePath.Match([]byte(didUrlPath)) {
1214
return ""
1315
}
16+
17+
match := ResourceId.FindStringSubmatch(didUrlPath)
1418
return match[0]
1519
}

0 commit comments

Comments
 (0)