Skip to content

Commit

Permalink
refactor: object to resource
Browse files Browse the repository at this point in the history
  • Loading branch information
genaris committed Apr 15, 2023
1 parent dbf5045 commit 96c3255
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 45 deletions.
4 changes: 0 additions & 4 deletions src/anoncreds/AnonCredsObjectResolutionResult.ts

This file was deleted.

4 changes: 4 additions & 0 deletions src/anoncreds/AnonCredsResourceResolutionResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface AnonCredsResourceResolutionResult {
resource: Record<string, unknown> | null
resourceMetadata: Record<string, unknown>
}
75 changes: 39 additions & 36 deletions src/anoncreds/DidWebAnonCredsRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import {
import { AgentContext, DidsApi } from '@aries-framework/core'
import { parse } from 'did-resolver'
import { parseUrl } from 'query-string'
import { AnonCredsObjectResolutionResult } from './AnonCredsObjectResolutionResult'
import { calculateObjectId, verifyObjectId } from './utils'
import { AnonCredsResourceResolutionResult } from './AnonCredsResourceResolutionResult'
import { calculateResourceId, verifyResourceId } from './utils'

export class DidWebAnonCredsRegistry implements AnonCredsRegistry {
public readonly methodName = 'web'
Expand All @@ -26,14 +26,14 @@ export class DidWebAnonCredsRegistry implements AnonCredsRegistry {

public async getSchema(agentContext: AgentContext, schemaId: string): Promise<GetSchemaReturn> {
try {
const { response, objectId } = await this.parseIdAndFetchObject(agentContext, schemaId)
const { response, resourceId } = await this.parseIdAndFetchResource(agentContext, schemaId)

if (response.status === 200) {
const result = (await response.json()) as AnonCredsObjectResolutionResult
const schema = result.object as unknown as AnonCredsSchema
const schemaMetadata = result.objectMetadata
if (!verifyObjectId(schema, objectId)) {
throw new Error('Wrong object Id')
const result = (await response.json()) as AnonCredsResourceResolutionResult
const schema = result.resource as unknown as AnonCredsSchema
const schemaMetadata = result.resourceMetadata
if (!verifyResourceId(schema, resourceId)) {
throw new Error('Wrong resource Id')
}

return {
Expand Down Expand Up @@ -64,9 +64,9 @@ export class DidWebAnonCredsRegistry implements AnonCredsRegistry {
options: RegisterSchemaOptions
): Promise<RegisterSchemaReturn> {
// Nothing to actually do other than generating a schema id
const objectId = calculateObjectId(options.schema)
const resourceId = calculateResourceId(options.schema)

const schemaId = `${options.schema.issuerId}?service=anoncreds&relativeRef=/schema/${objectId}`
const schemaId = `${options.schema.issuerId}?service=anoncreds&relativeRef=/schema/${resourceId}`
return {
schemaState: { state: 'finished', schema: options.schema, schemaId },
registrationMetadata: {},
Expand All @@ -79,14 +79,14 @@ export class DidWebAnonCredsRegistry implements AnonCredsRegistry {
credentialDefinitionId: string
): Promise<GetCredentialDefinitionReturn> {
try {
const { response, objectId } = await this.parseIdAndFetchObject(agentContext, credentialDefinitionId)
const { response, resourceId } = await this.parseIdAndFetchResource(agentContext, credentialDefinitionId)
if (response.status === 200) {
const result = (await response.json()) as AnonCredsObjectResolutionResult
const credentialDefinition = result.object as unknown as AnonCredsCredentialDefinition
const credentialDefinitionMetadata = result.objectMetadata
const result = (await response.json()) as AnonCredsResourceResolutionResult
const credentialDefinition = result.resource as unknown as AnonCredsCredentialDefinition
const credentialDefinitionMetadata = result.resourceMetadata

if (!verifyObjectId(credentialDefinition, objectId)) {
throw new Error('Wrong object Id')
if (!verifyResourceId(credentialDefinition, resourceId)) {
throw new Error('Wrong resource Id')
}

return {
Expand Down Expand Up @@ -117,9 +117,9 @@ export class DidWebAnonCredsRegistry implements AnonCredsRegistry {
options: RegisterCredentialDefinitionOptions
): Promise<RegisterCredentialDefinitionReturn> {
// Nothing to actually do other than generating a credential definition id
const objectId = calculateObjectId(options.credentialDefinition)
const resourceId = calculateResourceId(options.credentialDefinition)

const credentialDefinitionId = `${options.credentialDefinition.issuerId}?service=anoncreds&relativeRef=/credDef/${objectId}`
const credentialDefinitionId = `${options.credentialDefinition.issuerId}?service=anoncreds&relativeRef=/credDef/${resourceId}`

return {
credentialDefinitionState: {
Expand All @@ -137,14 +137,14 @@ export class DidWebAnonCredsRegistry implements AnonCredsRegistry {
revocationRegistryDefinitionId: string
): Promise<GetRevocationRegistryDefinitionReturn> {
try {
const { response, objectId } = await this.parseIdAndFetchObject(agentContext, revocationRegistryDefinitionId)
const { response, resourceId } = await this.parseIdAndFetchResource(agentContext, revocationRegistryDefinitionId)
if (response.status === 200) {
const result = (await response.json()) as AnonCredsObjectResolutionResult
const revocationRegistryDefinition = result.object as unknown as AnonCredsRevocationRegistryDefinition
const revocationRegistryDefinitionMetadata = result.objectMetadata
const result = (await response.json()) as AnonCredsResourceResolutionResult
const revocationRegistryDefinition = result.resource as unknown as AnonCredsRevocationRegistryDefinition
const revocationRegistryDefinitionMetadata = result.resourceMetadata

if (!verifyObjectId(revocationRegistryDefinition, objectId)) {
throw new Error('Wrong object Id')
if (!verifyResourceId(revocationRegistryDefinition, resourceId)) {
throw new Error('Wrong resource Id')
}

return {
Expand Down Expand Up @@ -190,9 +190,9 @@ export class DidWebAnonCredsRegistry implements AnonCredsRegistry {
})

if (response.status === 200) {
const result = (await response.json()) as AnonCredsObjectResolutionResult
const revocationStatusList = result.object as unknown as AnonCredsRevocationStatusList
const revocationStatusListMetadata = result.objectMetadata
const result = (await response.json()) as AnonCredsResourceResolutionResult
const revocationStatusList = result.resource as unknown as AnonCredsRevocationStatusList
const revocationStatusListMetadata = result.resourceMetadata

return {
revocationStatusList,
Expand All @@ -214,11 +214,11 @@ export class DidWebAnonCredsRegistry implements AnonCredsRegistry {
}
}

private async parseIdAndFetchObject(agentContext: AgentContext, didUrl: string) {
private async parseIdAndFetchResource(agentContext: AgentContext, didUrl: string) {
const parsedDid = parse(didUrl)

if (!parsedDid) {
throw new Error(`${didUrl} is not a valid object identifier`)
throw new Error(`${didUrl} is not a valid resource identifier`)
}

if (parsedDid.method != 'web') {
Expand All @@ -240,11 +240,11 @@ export class DidWebAnonCredsRegistry implements AnonCredsRegistry {
throw new Error('No valid relativeRef query present in the ID')
}

// The last segment of relativeRef is the objectId
const objectId = relativeRef.split('/').pop()
// The last segment of relativeRef is the resourceId
const resourceId = relativeRef.split('/').pop()

if (!objectId) {
throw new Error('Could not get objectId from relativeRef')
if (!resourceId) {
throw new Error('Could not get resourceId from relativeRef')
}

const baseEndpoint = didDocument.service?.find(
Expand All @@ -255,8 +255,11 @@ export class DidWebAnonCredsRegistry implements AnonCredsRegistry {
throw new Error(`No valid endpoint has been found for the service ${queriedService}`)
}

const fetchObjectUrl = `${baseEndpoint}${relativeRef}`
agentContext.config.logger.debug(`getting AnonCreds object at URL: ${fetchObjectUrl}`)
return { response: await agentContext.config.agentDependencies.fetch(fetchObjectUrl, { method: 'GET' }), objectId }
const fetchResourceUrl = `${baseEndpoint}${relativeRef}`
agentContext.config.logger.debug(`getting AnonCreds resource at URL: ${fetchResourceUrl}`)
return {
response: await agentContext.config.agentDependencies.fetch(fetchResourceUrl, { method: 'GET' }),
resourceId,
}
}
}
10 changes: 5 additions & 5 deletions src/anoncreds/utils.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { TypedArrayEncoder, Hasher } from '@aries-framework/core'
import canonicalize from 'canonicalize'

export function calculateObjectId(objectValue: unknown) {
const objectString = canonicalize(objectValue)
export function calculateResourceId(resourceObjectValue: unknown) {
const objectString = canonicalize(resourceObjectValue)

if (!objectString) {
throw new Error('Cannot canonicalize object')
throw new Error('Cannot canonicalize resource object')
}

return TypedArrayEncoder.toBase58(Hasher.hash(TypedArrayEncoder.fromString(objectString), 'sha2-256'))
}

export function verifyObjectId(objectValue: unknown, objectId: string) {
return calculateObjectId(objectValue) === objectId
export function verifyResourceId(resourceObjectValue: unknown, resourceId: string) {
return calculateResourceId(resourceObjectValue) === resourceId
}

0 comments on commit 96c3255

Please sign in to comment.