Skip to content

Commit

Permalink
feat: ourDid parameter for accepting invitations
Browse files Browse the repository at this point in the history
Signed-off-by: Ariel Gentile <gentilester@gmail.com>
  • Loading branch information
genaris committed Sep 22, 2023
1 parent 05191e9 commit da6b379
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
6 changes: 3 additions & 3 deletions packages/core/src/modules/connections/ConnectionsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ export class ConnectionsApi {
imageUrl?: string
protocol: HandshakeProtocol
routing?: Routing
peerDidNumAlgo?: PeerDidNumAlgo
ourDid?: string
}
) {
const { protocol, label, alias, imageUrl, autoAcceptConnection, peerDidNumAlgo } = config
const { protocol, label, alias, imageUrl, autoAcceptConnection, ourDid } = config

const routing =
config.routing ||
Expand All @@ -108,7 +108,7 @@ export class ConnectionsApi {
alias,
routing,
autoAcceptConnection,
peerNumAlgo: peerDidNumAlgo ?? this.config.peerNumAlgoForDidExchangeRequests,
ourDid,
})
} else if (protocol === HandshakeProtocol.Connections) {
result = await this.connectionService.createRequest(this.agentContext, outOfBandRecord, {
Expand Down
25 changes: 17 additions & 8 deletions packages/core/src/modules/connections/DidExchangeProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import { JsonTransformer } from '../../utils/JsonTransformer'
import { base64ToBase64URL } from '../../utils/base64'
import {
DidDocument,
DidRegistrarService,
DidDocumentRole,
createPeerDidDocumentFromServices,
DidKey,
getNumAlgoFromPeerDid,
PeerDidNumAlgo,
DidsApi,
} from '../dids'
import { getKeyFromVerificationMethod } from '../dids/domain/key-type'
import { tryParseDid } from '../dids/domain/parse'
Expand All @@ -36,6 +36,7 @@ import { DidRecord, DidRepository } from '../dids/repository'
import { OutOfBandRole } from '../oob/domain/OutOfBandRole'
import { OutOfBandState } from '../oob/domain/OutOfBandState'

import { ConnectionsModuleConfig } from './ConnectionsModuleConfig'
import { DidExchangeStateMachine } from './DidExchangeStateMachine'
import { DidExchangeProblemReportError, DidExchangeProblemReportReason } from './errors'
import { DidExchangeCompleteMessage } from './messages/DidExchangeCompleteMessage'
Expand All @@ -51,26 +52,23 @@ interface DidExchangeRequestParams {
goalCode?: string
routing: Routing
autoAcceptConnection?: boolean
peerNumAlgo: PeerDidNumAlgo
ourDid?: string
}

@injectable()
export class DidExchangeProtocol {
private connectionService: ConnectionService
private didRegistrarService: DidRegistrarService
private jwsService: JwsService
private didRepository: DidRepository
private logger: Logger

public constructor(
connectionService: ConnectionService,
didRegistrarService: DidRegistrarService,
didRepository: DidRepository,
jwsService: JwsService,
@inject(InjectionSymbols.Logger) logger: Logger
) {
this.connectionService = connectionService
this.didRegistrarService = didRegistrarService
this.didRepository = didRepository
this.jwsService = jwsService
this.logger = logger
Expand All @@ -85,9 +83,11 @@ export class DidExchangeProtocol {
outOfBandRecord,
params,
})
const didsApi = agentContext.dependencyManager.resolve(DidsApi)
const config = agentContext.dependencyManager.resolve(ConnectionsModuleConfig)

const { outOfBandInvitation } = outOfBandRecord
const { alias, goal, goalCode, routing, autoAcceptConnection, peerNumAlgo } = params
const { alias, goal, goalCode, routing, autoAcceptConnection, ourDid: did } = params
// TODO: We should store only one did that we'll use to send the request message with success.
// We take just the first one for now.
const [invitationDid] = outOfBandInvitation.invitationDids
Expand All @@ -110,7 +110,13 @@ export class DidExchangeProtocol {
// Create message
const label = params.label ?? agentContext.config.label

const didDocument = await this.createPeerDidDoc(agentContext, this.routingToServices(routing), peerNumAlgo)
const didDocument = did
? await didsApi.resolveDidDocument(did)
: await this.createPeerDidDoc(
agentContext,
this.routingToServices(routing),
config.peerNumAlgoForDidExchangeRequests
)
const parentThreadId = outOfBandRecord.outOfBandInvitation.id

const message = new DidExchangeRequestMessage({ label, parentThreadId, did: didDocument.id, goal, goalCode })
Expand Down Expand Up @@ -453,10 +459,13 @@ export class DidExchangeProtocol {
services: ResolvedDidCommService[],
numAlgo: PeerDidNumAlgo
) {
const didsApi = agentContext.dependencyManager.resolve(DidsApi)

// Create did document without the id property
const didDocument = createPeerDidDocumentFromServices(services)
// Register did:peer document. This will generate the id property and save it to a did record
const result = await this.didRegistrarService.create(agentContext, {

const result = await didsApi.create({
method: 'peer',
didDocument,
options: {
Expand Down
11 changes: 5 additions & 6 deletions packages/core/src/modules/oob/OutOfBandApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type { Attachment } from '../../decorators/attachment/Attachment'
import type { Query } from '../../storage/StorageService'
import type { PlaintextMessage } from '../../types'
import type { ConnectionInvitationMessage, ConnectionRecord, Routing } from '../connections'
import type { PeerDidNumAlgo } from '../dids'

import { catchError, EmptyError, first, firstValueFrom, map, of, timeout } from 'rxjs'

Expand Down Expand Up @@ -78,7 +77,7 @@ interface BaseReceiveOutOfBandInvitationConfig {
routing?: Routing
acceptInvitationTimeoutMs?: number
isImplicit?: boolean
peerDidNumAlgo?: PeerDidNumAlgo
ourDid?: string
}

export type ReceiveOutOfBandInvitationConfig = Omit<BaseReceiveOutOfBandInvitationConfig, 'isImplicit'>
Expand Down Expand Up @@ -462,7 +461,7 @@ export class OutOfBandApi {
reuseConnection,
routing,
timeoutMs: config.acceptInvitationTimeoutMs,
peerDidNumAlgo: config.peerDidNumAlgo,
ourDid: config.ourDid,
})
}

Expand Down Expand Up @@ -498,13 +497,13 @@ export class OutOfBandApi {
*/
routing?: Routing
timeoutMs?: number
peerDidNumAlgo?: PeerDidNumAlgo
ourDid?: string
}
) {
const outOfBandRecord = await this.outOfBandService.getById(this.agentContext, outOfBandId)

const { outOfBandInvitation } = outOfBandRecord
const { label, alias, imageUrl, autoAcceptConnection, reuseConnection, peerDidNumAlgo } = config
const { label, alias, imageUrl, autoAcceptConnection, reuseConnection, ourDid } = config
const services = outOfBandInvitation.getServices()
const messages = outOfBandInvitation.getRequests()
const timeoutMs = config.timeoutMs ?? 20000
Expand Down Expand Up @@ -570,7 +569,7 @@ export class OutOfBandApi {
autoAcceptConnection,
protocol: handshakeProtocol,
routing,
peerDidNumAlgo,
ourDid,
})
}

Expand Down

0 comments on commit da6b379

Please sign in to comment.