{"version":3,"file":"PeerDidResolver.mjs","names":[],"sources":["../../../../../src/modules/dids/methods/peer/PeerDidResolver.ts"],"sourcesContent":["import type { AgentContext } from '../../../../agent'\nimport { CredoError } from '../../../../error'\nimport type { DidDocument } from '../../domain'\nimport type { DidResolver } from '../../domain/DidResolver'\nimport { DidRepository } from '../../repository'\nimport type { DidResolutionResult } from '../../types'\n\nimport { getNumAlgoFromPeerDid, isValidPeerDid, PeerDidNumAlgo } from './didPeer'\nimport { didToNumAlgo0DidDocument } from './peerDidNumAlgo0'\nimport { didToNumAlgo2DidDocument } from './peerDidNumAlgo2'\nimport { didToNumAlgo4DidDocument, isShortFormDidPeer4 } from './peerDidNumAlgo4'\n\nexport class PeerDidResolver implements DidResolver {\n  public readonly supportedMethods = ['peer']\n\n  /**\n   * No remote resolving done, did document is fetched from storage. To not pollute the cache we don't allow caching\n   */\n  public readonly allowsCaching = false\n\n  /**\n   * Did peer records are often server from local did doucment, but it's easier to handle it in\n   * the peer did resolver.\n   */\n  public readonly allowsLocalDidRecord = false\n\n  public async resolve(agentContext: AgentContext, did: string): Promise<DidResolutionResult> {\n    const didRepository = agentContext.dependencyManager.resolve(DidRepository)\n\n    const didDocumentMetadata = {}\n\n    try {\n      let didDocument: DidDocument\n\n      if (!isValidPeerDid(did)) {\n        throw new CredoError(`did ${did} is not a valid peer did`)\n      }\n\n      const numAlgo = getNumAlgoFromPeerDid(did)\n\n      // For method 0, generate from did\n      if (numAlgo === PeerDidNumAlgo.InceptionKeyWithoutDoc) {\n        didDocument = didToNumAlgo0DidDocument(did)\n      }\n      // For Method 1, retrieve from storage\n      else if (numAlgo === PeerDidNumAlgo.GenesisDoc) {\n        // We can have multiple did document records stored for a single did (one created and one received). In this case it\n        // doesn't matter which one we use, and they should be identical. So we just take the first one.\n        const [didDocumentRecord] = await didRepository.findAllByDid(agentContext, did)\n\n        if (!didDocumentRecord) {\n          throw new CredoError(`No did record found for peer did ${did}.`)\n        }\n\n        if (!didDocumentRecord.didDocument) {\n          throw new CredoError(`Found did record for method 1 peer did (${did}), but no did document.`)\n        }\n\n        didDocument = didDocumentRecord.didDocument\n      }\n      // For Method 2, generate from did\n      else if (numAlgo === PeerDidNumAlgo.MultipleInceptionKeyWithoutDoc) {\n        didDocument = didToNumAlgo2DidDocument(did)\n      }\n      // For Method 4, if short form is received, attempt to get the didDocument from stored record\n      else {\n        if (isShortFormDidPeer4(did)) {\n          const [didRecord] = await didRepository.findAllByDid(agentContext, did)\n\n          if (!didRecord) {\n            throw new CredoError(`No did record found for peer did ${did}.`)\n          }\n          didDocument = didToNumAlgo4DidDocument(didRecord.did)\n        } else {\n          didDocument = didToNumAlgo4DidDocument(did)\n        }\n      }\n\n      return {\n        didDocument,\n        didDocumentMetadata,\n        didResolutionMetadata: { contentType: 'application/did+ld+json' },\n      }\n    } catch (error) {\n      agentContext.config.logger.error(`Error resolving did '${did}'`, {\n        error,\n      })\n\n      return {\n        didDocument: null,\n        didDocumentMetadata,\n        didResolutionMetadata: {\n          error: 'notFound',\n          message: `resolver_error: Unable to resolve did '${did}': ${error}`,\n        },\n      }\n    }\n  }\n}\n"],"mappings":";;;;;;;;;;;;AAYA,IAAa,kBAAb,MAAoD;;OAClC,mBAAmB,CAAC,OAAO;OAK3B,gBAAgB;OAMhB,uBAAuB;;CAEvC,MAAa,QAAQ,cAA4B,KAA2C;EAC1F,MAAM,gBAAgB,aAAa,kBAAkB,QAAQ,cAAc;EAE3E,MAAM,sBAAsB,EAAE;AAE9B,MAAI;GACF,IAAI;AAEJ,OAAI,CAAC,eAAe,IAAI,CACtB,OAAM,IAAI,WAAW,OAAO,IAAI,0BAA0B;GAG5D,MAAM,UAAU,sBAAsB,IAAI;AAG1C,OAAI,YAAY,eAAe,uBAC7B,eAAc,yBAAyB,IAAI;YAGpC,YAAY,eAAe,YAAY;IAG9C,MAAM,CAAC,qBAAqB,MAAM,cAAc,aAAa,cAAc,IAAI;AAE/E,QAAI,CAAC,kBACH,OAAM,IAAI,WAAW,oCAAoC,IAAI,GAAG;AAGlE,QAAI,CAAC,kBAAkB,YACrB,OAAM,IAAI,WAAW,2CAA2C,IAAI,yBAAyB;AAG/F,kBAAc,kBAAkB;cAGzB,YAAY,eAAe,+BAClC,eAAc,yBAAyB,IAAI;YAIvC,oBAAoB,IAAI,EAAE;IAC5B,MAAM,CAAC,aAAa,MAAM,cAAc,aAAa,cAAc,IAAI;AAEvE,QAAI,CAAC,UACH,OAAM,IAAI,WAAW,oCAAoC,IAAI,GAAG;AAElE,kBAAc,yBAAyB,UAAU,IAAI;SAErD,eAAc,yBAAyB,IAAI;AAI/C,UAAO;IACL;IACA;IACA,uBAAuB,EAAE,aAAa,2BAA2B;IAClE;WACM,OAAO;AACd,gBAAa,OAAO,OAAO,MAAM,wBAAwB,IAAI,IAAI,EAC/D,OACD,CAAC;AAEF,UAAO;IACL,aAAa;IACb;IACA,uBAAuB;KACrB,OAAO;KACP,SAAS,0CAA0C,IAAI,KAAK;KAC7D;IACF"}