Skip to content

Commit

Permalink
feat: upgrade to credo 0.5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
genaris committed May 1, 2024
1 parent fb46e35 commit 8b582b0
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 280 deletions.
37 changes: 18 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@
"postinstall": "patch-package"
},
"dependencies": {
"@credo-ts/action-menu": "^0.5.1",
"@credo-ts/anoncreds": "^0.5.1",
"@credo-ts/askar": "^0.5.1",
"@credo-ts/core": "^0.5.1",
"@credo-ts/node": "^0.5.1",
"@credo-ts/question-answer": "^0.5.1",
"@hyperledger/anoncreds-nodejs": "^0.2.1",
"@hyperledger/aries-askar-nodejs": "^0.2.0",
"@credo-ts/action-menu": "^0.5.2",
"@credo-ts/anoncreds": "^0.5.2",
"@credo-ts/askar": "^0.5.2",
"@credo-ts/core": "^0.5.2",
"@credo-ts/node": "^0.5.2",
"@credo-ts/question-answer": "^0.5.2",
"@hyperledger/anoncreds-nodejs": "^0.2.2",
"@hyperledger/aries-askar-nodejs": "^0.2.1",
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
Expand Down Expand Up @@ -120,19 +120,18 @@
"typescript": "^5.3.3"
},
"resolutions": {
"@2060.io/ffi-napi": "4.0.9",
"@types/node": "20.11.19",
"typescript": "5.3.3",
"tsyringe": "4.8.0",
"@hyperledger/anoncreds-nodejs": "0.2.1",
"@hyperledger/aries-askar-nodejs": "0.2.0",
"@hyperledger/anoncreds-shared": "0.2.1",
"@hyperledger/aries-askar-shared": "0.2.0",
"@credo-ts/action-menu": "0.5.1",
"@credo-ts/core": "0.5.1",
"@credo-ts/question-answer": "0.5.1",
"@credo-ts/node": "0.5.1",
"@credo-ts/askar": "0.5.1",
"@credo-ts/anoncreds": "0.5.1"
"@hyperledger/anoncreds-nodejs": "0.2.2",
"@hyperledger/aries-askar-nodejs": "0.2.1",
"@hyperledger/anoncreds-shared": "0.2.2",
"@hyperledger/aries-askar-shared": "0.2.1",
"@credo-ts/action-menu": "0.5.2",
"@credo-ts/core": "0.5.2",
"@credo-ts/question-answer": "0.5.2",
"@credo-ts/node": "0.5.2",
"@credo-ts/askar": "0.5.2",
"@credo-ts/anoncreds": "0.5.2"
}
}
90 changes: 0 additions & 90 deletions patches/@credo-ts+core+0.5.1.patch

This file was deleted.

14 changes: 5 additions & 9 deletions src/controllers/invitation/InvitationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class InvitationController {

const { requestedCredentials } = options

if (requestedCredentials.length === 0) {
if (!requestedCredentials?.length) {
throw Error('You must specify a least a requested credential')
}
const credentialDefinitionId = requestedCredentials[0].credentialDefinitionId
Expand Down Expand Up @@ -85,13 +85,11 @@ export class InvitationController {
},
})

const outOfBandRecord = await agent.oob.createInvitation({ messages: [request.message] })
const { url } = await createInvitation(await this.agentService.getAgent(), [request.message])

return {
id: request.proofRecord.id,
url: outOfBandRecord.outOfBandInvitation.toUrl({
domain: process.env.AGENT_INVITATION_BASE_URL ?? 'https://2060.io/i',
}),
url,
}
}

Expand Down Expand Up @@ -144,13 +142,11 @@ export class InvitationController {
},
})

const outOfBandRecord = await agent.oob.createInvitation({ messages: [request.message] })
const { url } = await createInvitation(await this.agentService.getAgent(), [request.message])

return {
id: request.credentialRecord.id,
url: outOfBandRecord.outOfBandInvitation.toUrl({
domain: process.env.AGENT_INVITATION_BASE_URL ?? 'https://2060.io/i',
}),
url,
}
}
}
11 changes: 0 additions & 11 deletions src/didWebServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'reflect-metadata'
import type { DidWebServerConfig } from './utils/ServerConfig'

import { AnonCredsCredentialDefinitionRepository, AnonCredsSchemaRepository } from '@credo-ts/anoncreds'
import { DidDocumentService } from '@credo-ts/core'
import cors from 'cors'
import express from 'express'
import path from 'path'
Expand Down Expand Up @@ -42,16 +41,6 @@ export const addDidWebRoutes = async (
if (agent.did) {
const [didRecord] = await agent.dids.getCreatedDids({ did: agent.did })
const didDocument = didRecord.didDocument
if (anoncredsBaseUrl) {
didDocument?.service?.push(
new DidDocumentService({
id: `${agent.did}#anoncreds`,
serviceEndpoint: `${anoncredsBaseUrl}/anoncreds/v1`,
type: 'AnonCredsRegistry',
}),
)
}

if (didDocument) {
res.send(didDocument.toJSON())
} else {
Expand Down
31 changes: 31 additions & 0 deletions src/utils/CachedWebDidResolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
AgentContext,
DidResolutionOptions,
DidResolutionResult,
DidsApi,
WebDidResolver,
} from '@credo-ts/core'
import { ParsedDID } from 'did-resolver'

export class CachedWebDidResolver extends WebDidResolver {
public async resolve(
agentContext: AgentContext,
did: string,
parsed: ParsedDID,
didResolutionOptions: DidResolutionOptions,
): Promise<DidResolutionResult> {
// First check within our own public dids, as there is no need to resolve it through HTTPS
const didsApi = agentContext.dependencyManager.resolve(DidsApi)
const [didRecord] = await didsApi.getCreatedDids({ did })

if (didRecord?.didDocument) {
return {
didDocument: didRecord.didDocument,
didDocumentMetadata: {},
didResolutionMetadata: {},
}
}

return super.resolve(agentContext, did, parsed, didResolutionOptions)
}
}
5 changes: 5 additions & 0 deletions src/utils/ServiceAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
AutoAcceptProof,
ConnectionsModule,
CredentialsModule,
DidsModule,
InitConfig,
ProofsModule,
V2CredentialProtocol,
Expand All @@ -27,10 +28,13 @@ import { MediaSharingModule } from 'credo-ts-media-sharing'
import { ReceiptsModule } from 'credo-ts-receipts'
import { UserProfileModule } from 'credo-ts-user-profile'

import { CachedWebDidResolver } from './CachedWebDidResolver'

type ServiceAgentModules = {
askar: AskarModule
anoncreds: AnonCredsModule
actionMenu: ActionMenuModule
dids: DidsModule
connections: ConnectionsModule
credentials: CredentialsModule<
[V2CredentialProtocol<[LegacyIndyCredentialFormatService, AnonCredsCredentialFormatService]>]
Expand Down Expand Up @@ -91,6 +95,7 @@ export const createServiceAgent = (options: ServiceAgentOptions): ServiceAgent =
}),
],
}),
dids: new DidsModule({ resolvers: [new CachedWebDidResolver()] }),
proofs: new ProofsModule({
autoAcceptProofs: AutoAcceptProof.ContentApproved,
proofProtocols: [
Expand Down
9 changes: 5 additions & 4 deletions src/utils/agent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HandshakeProtocol } from '@credo-ts/core'
import { AgentMessage, HandshakeProtocol } from '@credo-ts/core'

import { ServiceAgent } from './ServiceAgent'

Expand All @@ -9,14 +9,15 @@ import { ServiceAgent } from './ServiceAgent'
* @param agent
* @returns
*/
export async function createInvitation(agent: ServiceAgent) {
export async function createInvitation(agent: ServiceAgent, messages?: AgentMessage[]) {
const outOfBandInvitation = (
await agent.oob.createInvitation({
label: agent.config.label,
handshakeProtocols: [HandshakeProtocol.DidExchange, HandshakeProtocol.Connections],
did: agent.did,
multiUseInvitation: true,
invitationDid: agent.did,
multiUseInvitation: !messages,
imageUrl: process.env.AGENT_INVITATION_IMAGE_URL,
messages,
})
).outOfBandInvitation
return {
Expand Down
11 changes: 11 additions & 0 deletions src/utils/setupAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
DidCommV1Service,
DidDocumentBuilder,
DidDocumentRole,
DidDocumentService,
DidExchangeState,
DidRecord,
DidRepository,
Expand Down Expand Up @@ -192,6 +193,16 @@ export const setupAgent = async ({
}),
)
}

if (anoncredsServiceBaseUrl) {
builder.addService(
new DidDocumentService({
id: `${publicDid}#anoncreds`,
serviceEndpoint: `${anoncredsServiceBaseUrl}/anoncreds/v1`,
type: 'AnonCredsRegistry',
}),
)
}
}

const existingRecord = await didRepository.findCreatedDid(agent.context, publicDid)
Expand Down
Loading

0 comments on commit 8b582b0

Please sign in to comment.