Skip to content

Commit

Permalink
feat: Add cheqd demo and localnet for tests (#1435)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaevMithran authored Apr 19, 2023
1 parent cf98f76 commit 1ffb011
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 26 deletions.
14 changes: 14 additions & 0 deletions .github/actions/setup-cheqd/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Setup cheqd
description: Setup a cheqd network to perform tests
author: '[email protected]'

runs:
using: composite
steps:
- name: Start cheqd localnet
run: docker run --rm -d -p 26657:26657 ghcr.io/cheqd/cheqd-testnet:latest
shell: bash

branding:
icon: scissors
color: purple
3 changes: 3 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ jobs:
with:
seed: ${TEST_AGENT_PUBLIC_DID_SEED}

- name: Setup Cheqd
uses: ./.github/actions/setup-cheqd

- name: Setup Postgres
uses: ./.github/actions/setup-postgres

Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ Aries Framework JavaScript is a framework written in TypeScript for building **S
</a>
</td>
</tr>
<tr>
<td>@aries-framework/cheqd</td>
<td>
<a href="https://npmjs.com/package/@aries-framework/cheqd">
<img alt="@aries-framework/cheqd version" src="https://img.shields.io/npm/v/@aries-framework/cheqd"/>
</a>
</td>
</tr>
<tr>
<td>@aries-framework/askar</td>
<td>
Expand Down
1 change: 1 addition & 0 deletions demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@aries-framework/core": "*",
"@aries-framework/indy-sdk": "*",
"@aries-framework/indy-vdr": "*",
"@aries-framework/cheqd": "*",
"@aries-framework/node": "*",
"@types/figlet": "^1.5.4",
"@types/indy-sdk": "^1.16.26",
Expand Down
23 changes: 21 additions & 2 deletions demo/src/BaseAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import {
} from '@aries-framework/anoncreds'
import { AnonCredsRsModule } from '@aries-framework/anoncreds-rs'
import { AskarModule } from '@aries-framework/askar'
import {
CheqdAnonCredsRegistry,
CheqdDidRegistrar,
CheqdDidResolver,
CheqdModule,
CheqdModuleConfig,
} from '@aries-framework/cheqd'
import {
ConnectionsModule,
DidsModule,
Expand Down Expand Up @@ -129,7 +136,7 @@ function getAskarAnonCredsIndyModules() {
],
}),
anoncreds: new AnonCredsModule({
registries: [new IndyVdrAnonCredsRegistry()],
registries: [new IndyVdrAnonCredsRegistry(), new CheqdAnonCredsRegistry()],
}),
anoncredsRs: new AnonCredsRsModule({
anoncreds,
Expand All @@ -138,8 +145,20 @@ function getAskarAnonCredsIndyModules() {
indyVdr,
networks: [indyNetworkConfig],
}),
cheqd: new CheqdModule(
new CheqdModuleConfig({
networks: [
{
network: 'testnet',
cosmosPayerSeed:
'robust across amount corn curve panther opera wish toe ring bleak empower wreck party abstract glad average muffin picnic jar squeeze annual long aunt',
},
],
})
),
dids: new DidsModule({
resolvers: [new IndyVdrSovDidResolver()],
resolvers: [new IndyVdrSovDidResolver(), new CheqdDidResolver()],
registrars: [new CheqdDidRegistrar()],
}),
askar: new AskarModule({
ariesAskar,
Expand Down
34 changes: 24 additions & 10 deletions demo/src/Faber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import { ui } from 'inquirer'
import { BaseAgent } from './BaseAgent'
import { Color, greenText, Output, purpleText, redText } from './OutputClass'

export enum RegistryOptions {
indy = 'did:indy',
cheqd = 'did:cheqd',
}

export class Faber extends BaseAgent {
public outOfBandId?: string
public credentialDefinition?: RegisterCredentialDefinitionReturnStateFinished
Expand All @@ -22,25 +27,34 @@ export class Faber extends BaseAgent {
public static async build(): Promise<Faber> {
const faber = new Faber(9001, 'faber')
await faber.initializeAgent()
return faber
}

public async importDid(registry: string) {
// NOTE: we assume the did is already registered on the ledger, we just store the private key in the wallet
// and store the existing did in the wallet
const privateKey = TypedArrayEncoder.fromString('afjdemoverysercure00000000000000')

const key = await faber.agent.wallet.createKey({
const key = await this.agent.wallet.createKey({
keyType: KeyType.Ed25519,
privateKey,
})

// did is first 16 bytes of public key encoded as base58
const unqualifiedIndyDid = TypedArrayEncoder.toBase58(key.publicKey.slice(0, 16))
await faber.agent.dids.import({
did: `did:sov:${unqualifiedIndyDid}`,
})

faber.anonCredsIssuerId = unqualifiedIndyDid

return faber
const cheqdDid = 'did:cheqd:testnet:2d6841a0-8614-44c0-95c5-d54c61e420f2'
switch (registry) {
case RegistryOptions.indy:
await this.agent.dids.import({
did: `did:sov:${unqualifiedIndyDid}`,
})
this.anonCredsIssuerId = unqualifiedIndyDid
break
case RegistryOptions.cheqd:
await this.agent.dids.import({
did: cheqdDid,
})
this.anonCredsIssuerId = cheqdDid
break
}
}

private async getConnectionRecord() {
Expand Down
4 changes: 3 additions & 1 deletion demo/src/FaberInquirer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { textSync } from 'figlet'
import { prompt } from 'inquirer'

import { BaseInquirer, ConfirmOptions } from './BaseInquirer'
import { Faber } from './Faber'
import { Faber, RegistryOptions } from './Faber'
import { Listener } from './Listener'
import { Title } from './OutputClass'

Expand Down Expand Up @@ -89,6 +89,8 @@ export class FaberInquirer extends BaseInquirer {
}

public async credential() {
const registry = await prompt([this.inquireOptions([RegistryOptions.indy, RegistryOptions.cheqd])])
await this.faber.importDid(registry.options)
await this.faber.issueCredential()
const title = 'Is the credential offer accepted?'
await this.listener.newAcceptedPrompt(title, this)
Expand Down
35 changes: 35 additions & 0 deletions packages/cheqd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<p align="center">
<br />
<img
alt="Hyperledger Aries logo"
src="https://raw.githubusercontent.com/hyperledger/aries-framework-javascript/aa31131825e3331dc93694bc58414d955dcb1129/images/aries-logo.png"
height="250px"
/>
</p>
<h1 align="center"><b>Aries Framework JavaScript - Cheqd</b></h1>
<p align="center">
<a
href="https://raw.githubusercontent.com/hyperledger/aries-framework-javascript/main/LICENSE"
><img
alt="License"
src="https://img.shields.io/badge/License-Apache%202.0-blue.svg"
/></a>
<a href="https://www.typescriptlang.org/"
><img
alt="typescript"
src="https://img.shields.io/badge/%3C%2F%3E-TypeScript-%230074c1.svg"
/></a>
<a href="https://www.npmjs.com/package/@aries-framework/cheqd"
><img
alt="@aries-framework/cheqd version"
src="https://img.shields.io/npm/v/@aries-framework/cheqd"
/></a>

</p>
<br />

### Installation

### Quick start

### Example of usage
15 changes: 9 additions & 6 deletions packages/cheqd/tests/cheqd-did-resolver.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@ import { Agent, JsonTransformer } from '@aries-framework/core'

import { getAgentOptions } from '../../core/tests/helpers'
import { getClosestResourceVersion } from '../src/dids/didCheqdUtil'
import { DefaultRPCUrl } from '../src/ledger/CheqdLedgerService'

import { getCheqdModules } from './setupCheqdModule'

const agent = new Agent(getAgentOptions('Indy SDK Sov DID resolver', {}, getCheqdModules()))
export const resolverAgent = new Agent(
getAgentOptions('Cheqd resolver', {}, getCheqdModules(undefined, DefaultRPCUrl.Testnet))
)

describe('Cheqd DID resolver', () => {
beforeAll(async () => {
await agent.initialize()
await resolverAgent.initialize()
})

afterAll(async () => {
await agent.shutdown()
await agent.wallet.delete()
await resolverAgent.shutdown()
await resolverAgent.wallet.delete()
})

it('should resolve a did:cheqd:testnet did', async () => {
const did = await agent.dids.resolve('did:cheqd:testnet:3053e034-8faa-458d-9f01-2e3e1e8b2ab8')
const did = await resolverAgent.dids.resolve('did:cheqd:testnet:3053e034-8faa-458d-9f01-2e3e1e8b2ab8')
expect(JsonTransformer.toJSON(did)).toMatchObject({
didDocument: {
'@context': ['https://www.w3.org/ns/did/v1', 'https://w3id.org/security/suites/ed25519-2020/v1'],
Expand Down Expand Up @@ -46,7 +49,7 @@ describe('Cheqd DID resolver', () => {
})

it('should getClosestResourceVersion', async () => {
const did = await agent.dids.resolve('did:cheqd:testnet:SiVQgrFZ7jFZFrTGstT4ZD')
const did = await resolverAgent.dids.resolve('did:cheqd:testnet:SiVQgrFZ7jFZFrTGstT4ZD')
let resource = getClosestResourceVersion(did.didDocumentMetadata.linkedResourceMetadata, new Date())
expect(resource).toMatchObject({
id: '0b02ebf4-07c4-4df7-9015-e93c21108240',
Expand Down
11 changes: 7 additions & 4 deletions packages/cheqd/tests/cheqd-sdk-anoncreds-registry.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import { Agent, JsonTransformer, TypedArrayEncoder } from '@aries-framework/core
import { agentDependencies, getAgentConfig } from '../../core/tests/helpers'
import { CheqdAnonCredsRegistry } from '../src/anoncreds'

import { resolverAgent } from './cheqd-did-resolver.e2e.test'
import { getCheqdModules } from './setupCheqdModule'

const agentConfig = getAgentConfig('cheqdAnonCredsRegistry')

const agent = new Agent({
config: agentConfig,
dependencies: agentDependencies,
modules: getCheqdModules('000000000000000000000000000cheqd'),
modules: getCheqdModules(
'ugly dirt sorry girl prepare argue door man that manual glow scout bomb pigeon matter library transfer flower clown cat miss pluck drama dizzy'
),
})

const cheqdAnonCredsRegistry = new CheqdAnonCredsRegistry()
Expand Down Expand Up @@ -189,7 +192,7 @@ describe('cheqdAnonCredsRegistry', () => {
test('resolve query based url', async () => {
const schemaResourceId =
'did:cheqd:testnet:d8ac0372-0d4b-413e-8ef5-8e8f07822b2c?resourceName=test - 11&resourceType=anonCredsSchema'
const schemaResponse = await cheqdAnonCredsRegistry.getSchema(agent.context, `${schemaResourceId}`)
const schemaResponse = await cheqdAnonCredsRegistry.getSchema(resolverAgent.context, `${schemaResourceId}`)

expect(schemaResponse).toMatchObject({
schema: {
Expand All @@ -203,7 +206,7 @@ describe('cheqdAnonCredsRegistry', () => {
test('resolve revocation registry definition and statusList', async () => {
const revocationRegistryId = 'did:cheqd:testnet:e42ccb8b-78e8-4e54-9d11-f375153d63f8?resourceName=universityDegree'
const revocationDefinitionResponse = await cheqdAnonCredsRegistry.getRevocationRegistryDefinition(
agent.context,
resolverAgent.context,
revocationRegistryId
)

Expand All @@ -224,7 +227,7 @@ describe('cheqdAnonCredsRegistry', () => {
})

const revocationStatusListResponse = await cheqdAnonCredsRegistry.getRevocationStatusList(
agent.context,
resolverAgent.context,
revocationRegistryId,
1680789403
)
Expand Down
7 changes: 4 additions & 3 deletions packages/cheqd/tests/setupCheqdModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ export const getIndySdkModuleConfig = () =>
indySdk,
})

export const getCheqdModuleConfig = (seed?: string) =>
export const getCheqdModuleConfig = (seed?: string, rpcUrl?: string) =>
({
networks: [
{
rpcUrl: rpcUrl || 'http://localhost:26657',
network: 'testnet',
cosmosPayerSeed:
seed ||
Expand All @@ -23,8 +24,8 @@ export const getCheqdModuleConfig = (seed?: string) =>
],
} satisfies CheqdModuleConfigOptions)

export const getCheqdModules = (seed?: string) => ({
cheqdSdk: new CheqdModule(getCheqdModuleConfig(seed)),
export const getCheqdModules = (seed?: string, rpcUrl?: string) => ({
cheqdSdk: new CheqdModule(getCheqdModuleConfig(seed, rpcUrl)),
dids: new DidsModule({
registrars: [new CheqdDidRegistrar(), new KeyDidRegistrar()],
resolvers: [new CheqdDidResolver(), new KeyDidResolver()],
Expand Down

0 comments on commit 1ffb011

Please sign in to comment.