Skip to content

Commit

Permalink
fix: check passed did has been created/imported first
Browse files Browse the repository at this point in the history
Signed-off-by: Ariel Gentile <gentilester@gmail.com>
  • Loading branch information
genaris committed Dec 4, 2023
1 parent 1c1d12e commit 335ba53
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
1 change: 0 additions & 1 deletion packages/core/src/modules/connections/ConnectionsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { ConnectionType } from './models'
import type { ConnectionRecord } from './repository/ConnectionRecord'
import type { Routing } from './services'
import type { Query } from '../../storage/StorageService'
import type { PeerDidNumAlgo } from '../dids'
import type { OutOfBandRecord } from '../oob/repository'

import { AgentContext } from '../../agent'
Expand Down
16 changes: 13 additions & 3 deletions packages/core/src/modules/connections/DidExchangeProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
getNumAlgoFromPeerDid,
PeerDidNumAlgo,
DidsApi,
isValidPeerDid,
} from '../dids'
import { getKeyFromVerificationMethod } from '../dids/domain/key-type'
import { tryParseDid } from '../dids/domain/parse'
Expand Down Expand Up @@ -83,7 +84,6 @@ export class DidExchangeProtocol {
outOfBandRecord,
params,
})
const didsApi = agentContext.dependencyManager.resolve(DidsApi)
const config = agentContext.dependencyManager.resolve(ConnectionsModuleConfig)

const { outOfBandInvitation } = outOfBandRecord
Expand All @@ -110,8 +110,9 @@ export class DidExchangeProtocol {
// Create message
const label = params.label ?? agentContext.config.label

// If our did is specified, make sure we have all key material for it
const didDocument = did
? await didsApi.resolveDidDocument(did)
? await this.getDidDocumentForCreatedDid(agentContext, did)
: await this.createPeerDidDoc(
agentContext,
this.routingToServices(routing),
Expand All @@ -122,7 +123,7 @@ export class DidExchangeProtocol {
const message = new DidExchangeRequestMessage({ label, parentThreadId, did: didDocument.id, goal, goalCode })

// Create sign attachment containing didDoc
if (getNumAlgoFromPeerDid(didDocument.id) === PeerDidNumAlgo.GenesisDoc) {
if (isValidPeerDid(didDocument.id) && getNumAlgoFromPeerDid(didDocument.id) === PeerDidNumAlgo.GenesisDoc) {
const didDocAttach = await this.createSignedAttachment(agentContext, didDocument, [
routing.recipientKey.publicKeyBase58,
])
Expand Down Expand Up @@ -485,6 +486,15 @@ export class DidExchangeProtocol {
return result.didState.didDocument
}

private async getDidDocumentForCreatedDid(agentContext: AgentContext, did: string) {
const didRecord = await this.didRepository.findCreatedDid(agentContext, did)

if (!didRecord?.didDocument) {
throw new AriesFrameworkError(`Could not get DidDocument for created did ${did}`)
}
return didRecord.didDocument
}

private async createSignedAttachment(agentContext: AgentContext, didDoc: DidDocument, verkeys: string[]) {
const didDocAttach = new Attachment({
mimeType: 'application/json',
Expand Down

0 comments on commit 335ba53

Please sign in to comment.