Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

chore: upgrade to Credo 0.5.2 #4

Merged
merged 4 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Aries JavaScript did:web AnonCreds
# Credo did:web AnonCreds

AnonCreds did:web method registry for [aries-framework-javascript](https://github.com/hyperledger/aries-framework-javascript).
AnonCreds did:web method registry for [Credo](https://github.com/openwallet-foundation/credo-ts).

Based on current draft spec being written in this [repo](https://github.com/2060-io/did-web-anoncreds-method).

## Usage

In order to plug-in this into an AFJ Agent instance (from 0.4.0 onwards) you must register `DidWebAnonCredsRegistry` into the `AnonCredsModule` configuration:
In order to plug-in this into an Credo Agent instance you must register `DidWebAnonCredsRegistry` into the `AnonCredsModule` configuration:

```ts
import { DidWebAnonCredsRegistry } from 'aries-javascript-didweb-anoncreds'
import { DidWebAnonCredsRegistry } from 'credo-ts-didweb-anoncreds'

const agent = new Agent({
config: {
Expand Down
21 changes: 8 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@
"registry": "https://registry.npmjs.org/"
},
"devDependencies": {
"@credo-ts/node": "0.5.0-alpha.116",
"@credo-ts/askar": "^0.5.2",
"@credo-ts/node": "^0.5.2",
"@hyperledger/aries-askar-nodejs": "^0.2.1",
"@types/jest": "^26.0.23",
"@types/node-fetch": "^2.6.10",
"@types/uuid": "^9.0.0",
"@typescript-eslint/eslint-plugin": "^4.28.2",
"@typescript-eslint/parser": "^4.17.0",
"jest": "^27.0.4",
"nock": "^13.5.4",
"node-fetch": "^2.6.4",
"prettier": "^2.2.1",
"release-it": "^17.0.1",
Expand All @@ -49,19 +52,11 @@
"typescript": "~4.9.5"
},
"dependencies": {
"@credo-ts/anoncreds": "^0.5.0-alpha.116",
"@credo-ts/core": "^0.5.0-alpha.116",
"@hyperledger/anoncreds-shared": "^0.2.0-dev.9",
"@credo-ts/anoncreds": "^0.5.2",
"@credo-ts/core": "^0.5.2",
"@hyperledger/anoncreds-shared": "^0.2.1",
"canonicalize": "^1.0.8",
"class-transformer": "0.5.1",
"class-validator": "0.13.1",
"crypto": "^1.0.1",
"did-resolver": "^4.1.0",
"query-string": "7.1.3",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.2.0",
"tsyringe": "^4.8.0",
"uuid": "^9.0.0"
"query-string": "7.1.3"
},
"release-it": {
"github": {
Expand Down
23 changes: 14 additions & 9 deletions src/anoncreds/DidWebAnonCredsRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import {
RegisterSchemaOptions,
RegisterSchemaReturn,
} from '@credo-ts/anoncreds'
import { AgentContext, CacheModuleConfig, DidsApi } from '@credo-ts/core'
import { parse } from 'did-resolver'
import { AgentContext, CacheModuleConfig, DidsApi, parseDid } from '@credo-ts/core'
import { parseUrl } from 'query-string'
import { AnonCredsResourceResolutionResult } from './AnonCredsResourceResolutionResult'
import { calculateResourceId, verifyResourceId } from './utils'
Expand All @@ -35,8 +34,8 @@ export class DidWebAnonCredsRegistry implements AnonCredsRegistry {

private cacheSettings: CacheSettings

public constructor(options: { cacheOptions?: CacheSettings }) {
this.cacheSettings = options.cacheOptions ?? { allowCaching: true, cacheDurationInSeconds: 300 }
public constructor(options?: { cacheOptions?: CacheSettings }) {
this.cacheSettings = options?.cacheOptions ?? { allowCaching: true, cacheDurationInSeconds: 300 }
}

public async getSchema(agentContext: AgentContext, schemaId: string): Promise<GetSchemaReturn> {
Expand Down Expand Up @@ -99,9 +98,9 @@ export class DidWebAnonCredsRegistry implements AnonCredsRegistry {
}
}
} catch (error) {
agentContext.config.logger.debug(`error: ${error}`)
agentContext.config.logger.debug(`Error resolving schema with id ${schemaId}: ${error}`, { error })
return {
resolutionMetadata: { error: 'invalid' },
resolutionMetadata: { error: 'invalid', message: error instanceof Error ? error.message : `${error}` },
schemaMetadata: {},
schemaId,
}
Expand Down Expand Up @@ -187,8 +186,11 @@ export class DidWebAnonCredsRegistry implements AnonCredsRegistry {
}
}
} catch (error) {
agentContext.config.logger.debug(`Error resolving schema with id ${credentialDefinitionId}: ${error}`, {
error,
})
return {
resolutionMetadata: { error: 'invalid' },
resolutionMetadata: { error: 'invalid', message: error instanceof Error ? error.message : `${error}` },
credentialDefinitionMetadata: {},
credentialDefinitionId,
}
Expand Down Expand Up @@ -279,8 +281,11 @@ export class DidWebAnonCredsRegistry implements AnonCredsRegistry {
}
}
} catch (error) {
agentContext.config.logger.debug(`Error resolving schema with id ${revocationRegistryDefinitionId}: ${error}`, {
error,
})
return {
resolutionMetadata: { error: 'invalid' },
resolutionMetadata: { error: 'invalid', message: error instanceof Error ? error.message : `${error}` },
revocationRegistryDefinitionMetadata: {},
revocationRegistryDefinitionId,
}
Expand Down Expand Up @@ -377,7 +382,7 @@ export class DidWebAnonCredsRegistry implements AnonCredsRegistry {
}

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

if (!parsedDid) {
throw new Error(`${didUrl} is not a valid resource identifier`)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"resource": {
"attrNames": ["hello"],
"name": "a",
"issuerId": "did:web:nnu4z2pauw49.share.zrok.io:paradym-public-metadata:440ef8ba-36f6-4362-bbac-391dc47e2747",
"version": "1.0"
},
"resourceMetadata": {}
}
25 changes: 25 additions & 0 deletions test/__fixtures__/credentialDefinition1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"issuerId": "did:web:ca.dev.2060.io",
"schemaId": "did:web:ca.dev.2060.io?service=anoncreds&relativeRef=/schema/3hawjUu6FYNG9jHa9PU68o9taq3WPkjgjgWsM1mHJsMS",
"type": "CL",
"tag": "latest",
"value": {
"primary": {
"n": "779...397",
"r": {
"birthdate": "294...298",
"birthlocation": "533...284",
"citizenship": "894...102",
"expiry_date": "650...011",
"facephoto": "870...274",
"firstname": "656...226",
"link_secret": "521...922",
"name": "410...200",
"uuid": "226...757"
},
"rctxt": "774...977",
"s": "750..893",
"z": "632...005"
}
}
}
68 changes: 68 additions & 0 deletions test/__fixtures__/did2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"@context": [
"https://w3id.org/did/v1",
"https://w3id.org/security/suites/x25519-2019/v1",
"https://w3id.org/security/suites/jws-2020/v1"
],
"id": "did:web:nnu4z2pauw49.share.zrok.io:paradym-public-metadata:440ef8ba-36f6-4362-bbac-391dc47e2747",
"verificationMethod": [
{
"id": "did:web:nnu4z2pauw49.share.zrok.io:paradym-public-metadata:440ef8ba-36f6-4362-bbac-391dc47e2747#z6MkvqQGA2m3YhLUDx9GKVmjiaLS4EoFxUZT6oU3BAMesCLr",
"type": "Ed25519VerificationKey2018",
"controller": "did:web:nnu4z2pauw49.share.zrok.io:paradym-public-metadata:440ef8ba-36f6-4362-bbac-391dc47e2747",
"publicKeyBase58": "HP9DZnWcD9r17TJZdvotsUnSEfXQYbK6QnZ7LtPdwyZU"
},
{
"id": "did:web:nnu4z2pauw49.share.zrok.io:paradym-public-metadata:440ef8ba-36f6-4362-bbac-391dc47e2747#zDnaeWfRfd58DmjUtjYZKFa7BBmAg268D65toArbv3PVuGVvn",
"type": "JsonWebKey2020",
"controller": "did:web:nnu4z2pauw49.share.zrok.io:paradym-public-metadata:440ef8ba-36f6-4362-bbac-391dc47e2747",
"publicKeyJwk": {
"kty": "EC",
"crv": "P-256",
"x": "XJjFoh8k1iHMzYa_WPCsUc6hT70KNPDMnqBK3iLtXLc",
"y": "5Jtn2iKTUzoQrK8a2-Yc7PDQcguA1mpf8JE1eWWln2A"
}
},
{
"id": "did:web:nnu4z2pauw49.share.zrok.io:paradym-public-metadata:440ef8ba-36f6-4362-bbac-391dc47e2747#z6Mkj1x8an2CxEeSyncjBXhczqY9ATRUa7JgAeo3EZtbPUuw",
"type": "Ed25519VerificationKey2018",
"controller": "did:web:nnu4z2pauw49.share.zrok.io:paradym-public-metadata:440ef8ba-36f6-4362-bbac-391dc47e2747",
"publicKeyBase58": "5Zh5zXmmch9ysHn2Vxjn9jz9Lt9dAE4KUdt7QHvaUG8Z"
},
{
"id": "did:web:nnu4z2pauw49.share.zrok.io:paradym-public-metadata:440ef8ba-36f6-4362-bbac-391dc47e2747#z6LSmSFDYb9prkcA1KZqiDPuSNeXQUymiUn4J8Xsx3oDn2Wh",
"type": "JsonWebKey2020",
"controller": "did:web:nnu4z2pauw49.share.zrok.io:paradym-public-metadata:440ef8ba-36f6-4362-bbac-391dc47e2747",
"publicKeyJwk": { "kty": "OKP", "crv": "X25519", "x": "kQNR1agw-aHsBHA1P4iMPPOnPjJs3QKPIrYNQantLGY" }
}
],
"service": [
{
"id": "did:web:nnu4z2pauw49.share.zrok.io:paradym-public-metadata:440ef8ba-36f6-4362-bbac-391dc47e2747#didcomm",
"serviceEndpoint": "https://l9ifxc8ial1o.share.zrok.io/didcomm",
"type": "did-communication",
"priority": 0,
"recipientKeys": [
"did:web:nnu4z2pauw49.share.zrok.io:paradym-public-metadata:440ef8ba-36f6-4362-bbac-391dc47e2747#z6LSmSFDYb9prkcA1KZqiDPuSNeXQUymiUn4J8Xsx3oDn2Wh"
],
"routingKeys": [],
"accept": ["didcomm/aip2;env=rfc19"]
},
{
"id": "did:web:nnu4z2pauw49.share.zrok.io:paradym-public-metadata:440ef8ba-36f6-4362-bbac-391dc47e2747#anoncreds",
"serviceEndpoint": "https://nnu4z2pauw49.share.zrok.io/paradym-public-metadata/440ef8ba-36f6-4362-bbac-391dc47e2747/anoncreds",
"type": "AnonCredsRegistry"
}
],
"authentication": [
"did:web:nnu4z2pauw49.share.zrok.io:paradym-public-metadata:440ef8ba-36f6-4362-bbac-391dc47e2747#z6MkvqQGA2m3YhLUDx9GKVmjiaLS4EoFxUZT6oU3BAMesCLr",
"did:web:nnu4z2pauw49.share.zrok.io:paradym-public-metadata:440ef8ba-36f6-4362-bbac-391dc47e2747#zDnaeWfRfd58DmjUtjYZKFa7BBmAg268D65toArbv3PVuGVvn"
],
"assertionMethod": [
"did:web:nnu4z2pauw49.share.zrok.io:paradym-public-metadata:440ef8ba-36f6-4362-bbac-391dc47e2747#z6MkvqQGA2m3YhLUDx9GKVmjiaLS4EoFxUZT6oU3BAMesCLr",
"did:web:nnu4z2pauw49.share.zrok.io:paradym-public-metadata:440ef8ba-36f6-4362-bbac-391dc47e2747#zDnaeWfRfd58DmjUtjYZKFa7BBmAg268D65toArbv3PVuGVvn"
],
"keyAgreement": [
"did:web:nnu4z2pauw49.share.zrok.io:paradym-public-metadata:440ef8ba-36f6-4362-bbac-391dc47e2747#z6LSmSFDYb9prkcA1KZqiDPuSNeXQUymiUn4J8Xsx3oDn2Wh"
]
}
16 changes: 16 additions & 0 deletions test/__fixtures__/revocationRegistryDefinition1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"issuerId": "did:web:ca.dev.2060.io",
"revocDefType": "CL_ACCUM",
"credDefId": "did:web:ca.dev.2060.io?service=anoncreds&relativeRef=/credential-definition/7fvypL5Mpe1no7z1vHkGhgHzPoFjrG6BoYMdhKMT6YZn",
"tag": "MyCustomCredentialDefinition",
"value": {
"publicKeys": {
"accumKey": {
"z": "1 0BB...386"
}
},
"maxCredNum": 666,
"tailsLocation": "https://my.revocations.tails/tailsfile.txt",
"tailsHash": "91zvq2cFmBZmHCcLqFyzv7bfehHH5rMhdAG5wTjqy2PE"
}
}
6 changes: 6 additions & 0 deletions test/__fixtures__/schema1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"issuerId": "did:web:api.anoncreds.idlab.app:acme",
"name": "idlab_demo",
"version": "0.0.1",
"attrNames": ["id", "name"]
}
22 changes: 22 additions & 0 deletions test/agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { AskarModule } from '@credo-ts/askar'
import { Agent, KeyDerivationMethod } from '@credo-ts/core'
import { agentDependencies } from '@credo-ts/node'
import { ariesAskar } from '@hyperledger/aries-askar-nodejs'
import { randomUUID } from 'node:crypto'

export const agent = new Agent({
config: {
label: 'test',
walletConfig: {
id: `anoncreds-${randomUUID()}`,
key: ariesAskar.storeGenerateRawKey({}),
keyDerivationMethod: KeyDerivationMethod.Raw,
},
},
dependencies: agentDependencies,
modules: {
askar: new AskarModule({
ariesAskar,
}),
},
})
71 changes: 71 additions & 0 deletions test/credentialDefinition.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { DidWebAnonCredsRegistry } from '../src/anoncreds'
import didDocument1 from './__fixtures__/did1.json'
import credentialDefinition1 from './__fixtures__/credentialDefinition1.json'
import nock, { cleanAll, enableNetConnect } from 'nock'
import { agent } from './agent'
import { calculateResourceId } from '../src/anoncreds/utils'

describe('Credential Definition', () => {
beforeAll(async () => {
await agent.initialize()
})

afterEach(() => {
cleanAll()
enableNetConnect()
})

test('resolves credential definition correctly', async () => {
const resourceId = calculateResourceId(credentialDefinition1)

// did document
nock('https://ca.dev.2060.io').get('/.well-known/did.json').reply(200, didDocument1)

// Get schema
nock('https://anoncreds.ca.dev.2060.io').get(`/v1/credential-definition/${resourceId}`).reply(200, {
resource: credentialDefinition1,
resourceMetadata: {},
})

const registry = new DidWebAnonCredsRegistry()

const schemaResponse = await registry.getCredentialDefinition(
agent.context,
`did:web:ca.dev.2060.io?service=anoncreds&relativeRef=/credential-definition/${resourceId}`
)

expect(schemaResponse).toEqual({
resolutionMetadata: {},
credentialDefinition: credentialDefinition1,
credentialDefinitionId: `did:web:ca.dev.2060.io?service=anoncreds&relativeRef=/credential-definition/${resourceId}`,
credentialDefinitionMetadata: {},
})
})

test('throws error when credential definition resourceId does not match', async () => {
// did document
nock('https://ca.dev.2060.io').get('/.well-known/did.json').reply(200, didDocument1)

// Get schema
nock('https://anoncreds.ca.dev.2060.io').get(`/v1/credential-definition/1234`).reply(200, {
resource: credentialDefinition1,
resourceMetadata: {},
})

const registry = new DidWebAnonCredsRegistry()

const credentialDefinitionResponse = await registry.getCredentialDefinition(
agent.context,
`did:web:ca.dev.2060.io?service=anoncreds&relativeRef=/credential-definition/1234`
)

expect(credentialDefinitionResponse).toEqual({
resolutionMetadata: {
error: 'invalid',
message: 'Wrong resource Id',
},
credentialDefinitionId: 'did:web:ca.dev.2060.io?service=anoncreds&relativeRef=/credential-definition/1234',
credentialDefinitionMetadata: {},
})
})
})
Loading
Loading