Skip to content

Commit

Permalink
Synchronise updates form openwallet-foundation/credo-ts-ext#222
Browse files Browse the repository at this point in the history
Ports changes from openwallet-foundation/credo-ts-ext#222 to this repo. This includes commits:

* 039948d9849f0082bb057eb1885b35595b098233
* 2e2638903f5510acd559026057140819bf74c54f
  • Loading branch information
mattdean-digicatapult committed Aug 24, 2023
1 parent ffbb2bb commit a4c2a1c
Show file tree
Hide file tree
Showing 16 changed files with 1,575 additions and 568 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@digicatapult/veritable-cloudagent",
"version": "0.3.1",
"version": "0.4.0",
"main": "build/index",
"types": "build/index",
"files": [
Expand Down Expand Up @@ -47,13 +47,11 @@
"@aries-framework/anoncreds-rs": "^0.4.0",
"@aries-framework/askar": "^0.4.0",
"@aries-framework/core": "^0.4.0",

"@aries-framework/node": "^0.4.0",
"@hyperledger/anoncreds-nodejs": "^0.1.0",
"@hyperledger/anoncreds-shared": "^0.1.0",
"@hyperledger/aries-askar-nodejs": "^0.1.0",
"@hyperledger/aries-askar-shared": "^0.1.0",

"@tsoa/runtime": "^4.1.3",
"@types/ws": "^7.4.7",
"body-parser": "^1.20.0",
Expand Down
64 changes: 20 additions & 44 deletions src/cliAgent.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
import type { InitConfig } from '@aries-framework/core'
import type { WalletConfig } from '@aries-framework/core/build/types'
import type { InitConfig, WalletConfig } from '@aries-framework/core'

import { AnonCredsCredentialFormatService, AnonCredsModule } from '@aries-framework/anoncreds'
import { AnonCredsRsModule } from '@aries-framework/anoncreds-rs'
import { AskarModule } from '@aries-framework/askar'
import {
HttpOutboundTransport,
WsOutboundTransport,
LogLevel,
Agent,
ConnectionsModule,
ProofsModule,
CredentialsModule,
AutoAcceptCredential,
AutoAcceptProof,
MediatorModule,
V2CredentialProtocol,
} from '@aries-framework/core'

import { agentDependencies, HttpInboundTransport, WsInboundTransport } from '@aries-framework/node'
import { anoncreds } from '@hyperledger/anoncreds-nodejs'
import { ariesAskar } from '@hyperledger/aries-askar-nodejs'
import { readFile } from 'fs/promises'

import { setupServer } from './server'
import { getAgentModules, RestAgent } from './utils/agent'
import { TsLogger } from './utils/logger'
import VeritableAnonCredsRegistry from './anoncreds'
import Ipfs from './ipfs'

export type Transports = 'ws' | 'http'
export type InboundTransport = {
Expand Down Expand Up @@ -94,37 +81,18 @@ export async function runRestAgent(restConfig: AriesRestConfig) {
logger,
}

const agent = new Agent({
const modules = getAgentModules({
autoAcceptConnections,
autoAcceptProofs,
autoAcceptCredentials,
autoAcceptMediationRequests,
ipfsOrigin,
})

const agent: RestAgent = new Agent({
config: agentConfig,
dependencies: agentDependencies,
modules: {
connections: new ConnectionsModule({
autoAcceptConnections,
}),
proofs: new ProofsModule({
autoAcceptProofs,
}),
credentials: new CredentialsModule<[V2CredentialProtocol<[AnonCredsCredentialFormatService]>]>({
autoAcceptCredentials,
credentialProtocols: [
new V2CredentialProtocol({
credentialFormats: [new AnonCredsCredentialFormatService()],
}),
],
}),
anoncreds: new AnonCredsModule({
registries: [new VeritableAnonCredsRegistry(new Ipfs(ipfsOrigin))],
}),
anoncredsRs: new AnonCredsRsModule({
anoncreds,
}),
askar: new AskarModule({
ariesAskar,
}),
mediator: new MediatorModule({
autoAcceptMediationRequests,
}),
},
modules,
})

// Register outbound transports
Expand All @@ -140,6 +108,14 @@ export async function runRestAgent(restConfig: AriesRestConfig) {
}

await agent.initialize()

const existingSecrets = await agent.modules.anoncreds.getLinkSecretIds()
if (existingSecrets.length === 0) {
await agent.modules.anoncreds.createLinkSecret({
setAsDefault: true,
})
}

const app = await setupServer(agent, {
webhookUrl,
port: adminPort,
Expand Down
57 changes: 53 additions & 4 deletions src/controllers/did/DidController.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { DidResolutionResultProps } from '../types'
import type { DidCreateOptions, DidCreateResult, DidResolutionResultProps } from '../types'

import { Agent } from '@aries-framework/core'
import { Controller, Example, Get, Path, Route, Tags } from 'tsoa'
import { Agent, AriesFrameworkError, ImportDidOptions } from '@aries-framework/core'
import { Body, Controller, Example, Get, Path, Post, Res, Route, Tags, TsoaResponse } from 'tsoa'
import { injectable } from 'tsyringe'

import { Did, DidRecordExample } from '../examples'
import { Did, DidRecordExample, DidStateExample } from '../examples'

@Tags('Dids')
@Route('/dids')
Expand Down Expand Up @@ -34,4 +34,53 @@ export class DidController extends Controller {

return { ...resolveResult, didDocument: resolveResult.didDocument.toJSON() }
}

/**
* Import a Did to the Agent and return the did resolution result
*
* @param options
* @returns DidResolutionResultProps
*/
@Example<DidResolutionResultProps>(DidRecordExample)
@Post('/import')
public async importDid(
@Body() options: ImportDidOptions,
@Res() badRequestError: TsoaResponse<400, { reason: string }>,
@Res() internalServerError: TsoaResponse<500, { message: string }>
) {
try {
await this.agent.dids.import(options)
return this.getDidRecordByDid(options.did)
} catch (error) {
if (error instanceof AriesFrameworkError) {
return badRequestError(400, {
reason: `Error importing Did - ${error.message}`,
})
}
return internalServerError(500, { message: `something went wrong: ${error}` })
}
}

/**
* Create a Did and return the did resolution result
*
* @param options
* @returns DidResolutionResultProps
*/
@Example<DidCreateResult>(DidStateExample)
@Post('/create')
public async createDid(
@Body() options: DidCreateOptions,
@Res() internalServerError: TsoaResponse<500, { message: string }>
) {
const { didState } = await this.agent.dids.create(options)

if (didState.state === 'failed') {
return internalServerError(500, {
message: `Error creating Did - ${didState.reason}`,
})
}

return didState
}
}
46 changes: 44 additions & 2 deletions src/controllers/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,48 @@ export const DidRecordExample = {
},
}

export const DidStateExample = {
state: 'finished' as const,
did: 'did:key:z6MkpGuzuD38tpgZKPfmLmmD8R6gihP9KJhuopMuVvfGzLmc',
didDocument: {
'@context': [
'https://w3id.org/did/v1',
'https://w3id.org/security/suites/ed25519-2018/v1',
'https://w3id.org/security/suites/x25519-2019/v1',
],
id: 'did:key:z6MkpGuzuD38tpgZKPfmLmmD8R6gihP9KJhuopMuVvfGzLmc',
verificationMethod: [
{
id: 'did:key:z6MkpGuzuD38tpgZKPfmLmmD8R6gihP9KJhuopMuVvfGzLmc#z6MkpGuzuD38tpgZKPfmLmmD8R6gihP9KJhuopMuVvfGzLmc',
type: 'Ed25519VerificationKey2018',
controller: 'did:key:z6MkpGuzuD38tpgZKPfmLmmD8R6gihP9KJhuopMuVvfGzLmc',
publicKeyBase58: 'ApexJxnhZHC6Ctq4fCoNHKYgu87HuRTZ7oSyfehG57zE',
},
],
authentication: [
'did:key:z6MkpGuzuD38tpgZKPfmLmmD8R6gihP9KJhuopMuVvfGzLmc#z6MkpGuzuD38tpgZKPfmLmmD8R6gihP9KJhuopMuVvfGzLmc',
],
assertionMethod: [
'did:key:z6MkpGuzuD38tpgZKPfmLmmD8R6gihP9KJhuopMuVvfGzLmc#z6MkpGuzuD38tpgZKPfmLmmD8R6gihP9KJhuopMuVvfGzLmc',
],
keyAgreement: [
{
id: 'did:key:z6MkpGuzuD38tpgZKPfmLmmD8R6gihP9KJhuopMuVvfGzLmc#z6LSm5B4fB9NA55xB7PSeMYTMS9sf8uboJvyZBaDLLSZ7Ryd',
type: 'X25519KeyAgreementKey2019',
controller: 'did:key:z6MkpGuzuD38tpgZKPfmLmmD8R6gihP9KJhuopMuVvfGzLmc',
publicKeyBase58: 'APzu8sLW4cND5j1g7i2W2qwPozNV6hkpgCrXqso2Q4Cs',
},
],
capabilityInvocation: [
'did:key:z6MkpGuzuD38tpgZKPfmLmmD8R6gihP9KJhuopMuVvfGzLmc#z6MkpGuzuD38tpgZKPfmLmmD8R6gihP9KJhuopMuVvfGzLmc',
],
capabilityDelegation: [
'did:key:z6MkpGuzuD38tpgZKPfmLmmD8R6gihP9KJhuopMuVvfGzLmc#z6MkpGuzuD38tpgZKPfmLmmD8R6gihP9KJhuopMuVvfGzLmc',
],
},
secret: {},
}

type OutOfBandRecordProperties = Omit<OutOfBandRecordProps, 'outOfBandInvitation'>
export type OutOfBandInvitationProps = Omit<
OutOfBandInvitationOptions,
Expand All @@ -131,7 +173,7 @@ export interface OutOfBandRecordWithInvitationProps extends OutOfBandRecordPrope
}

export const outOfBandInvitationExample = {
'@type': 'https://didcomm.org/out-of-band/2.0/invitation',
'@type': 'did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/out-of-band/1.1/invitation',
'@id': 'd6472943-e5d0-4d95-8b48-790ed5a41931',
label: 'Aries Test Agent',
accept: ['didcomm/aip1', 'didcomm/aip2;env=rfc19'],
Expand Down Expand Up @@ -170,7 +212,7 @@ export const CredentialExchangeRecordExample = {
connectionId: 'ac6d0fdd-0db8-4f52-8a3d-de7ff8ddc14b',
},
metadata: {
'_internal/anoncredsCredential': {
'_internal/indyCredential': {
credentialDefinitionId: 'q7ATwTYbQDgiigVijUAej:3:CL:318187:latest',
schemaId: 'q7ATwTYbQDgiigVijUAej:2:Employee Badge:1.0',
},
Expand Down
Loading

0 comments on commit a4c2a1c

Please sign in to comment.