{"version":3,"file":"PeerDidRegistrar.mjs","names":[],"sources":["../../../../../src/modules/dids/methods/peer/PeerDidRegistrar.ts"],"sourcesContent":["import type { AgentContext } from '../../../../agent'\nimport type { XOR } from '../../../../types'\nimport { JsonTransformer } from '../../../../utils'\nimport { KeyManagementApi, type KmsCreateKeyOptions, type KmsCreateKeyTypeAsymmetric, PublicJwk } from '../../../kms'\nimport type { DidDocumentKey } from '../../DidsApiOptions'\nimport { DidDocument } from '../../domain'\nimport { DidDocumentRole } from '../../domain/DidDocumentRole'\nimport type { DidRegistrar } from '../../domain/DidRegistrar'\nimport { DidRecord, DidRepository } from '../../repository'\nimport type { DidCreateOptions, DidCreateResult, DidDeactivateResult, DidUpdateResult } from '../../types'\nimport { getAlternativeDidsForPeerDid, PeerDidNumAlgo } from './didPeer'\nimport { publicJwkToNumAlgo0DidDocument } from './peerDidNumAlgo0'\nimport { didDocumentJsonToNumAlgo1Did } from './peerDidNumAlgo1'\nimport { didDocumentToNumAlgo2Did } from './peerDidNumAlgo2'\nimport { didDocumentToNumAlgo4Did } from './peerDidNumAlgo4'\n\nexport class PeerDidRegistrar implements DidRegistrar {\n  public readonly supportedMethods = ['peer']\n\n  public async create(\n    agentContext: AgentContext,\n    options:\n      | PeerDidNumAlgo0CreateOptions\n      | PeerDidNumAlgo1CreateOptions\n      | PeerDidNumAlgo2CreateOptions\n      | PeerDidNumAlgo4CreateOptions\n  ): Promise<DidCreateResult> {\n    const kms = agentContext.dependencyManager.resolve(KeyManagementApi)\n    const didRepository = agentContext.dependencyManager.resolve(DidRepository)\n\n    let did: string\n    let didDocument: DidDocument\n\n    let keys: DidDocumentKey[]\n\n    try {\n      if (isPeerDidNumAlgo0CreateOptions(options)) {\n        let publicJwk: PublicJwk\n\n        if (options.options.createKey) {\n          const createKeyResult = await kms.createKey(options.options.createKey)\n          publicJwk = PublicJwk.fromPublicJwk(createKeyResult.publicJwk)\n          keys = [\n            {\n              didDocumentRelativeKeyId: `#${publicJwk.fingerprint}`,\n              kmsKeyId: createKeyResult.keyId,\n            },\n          ]\n        } else {\n          const _publicJwk = await kms.getPublicKey({\n            keyId: options.options.keyId,\n          })\n\n          if (!_publicJwk) {\n            return {\n              didDocumentMetadata: {},\n              didRegistrationMetadata: {},\n              didState: {\n                state: 'failed',\n                reason: `notFound: key with key id '${options.options.keyId}' not found`,\n              },\n            }\n          }\n\n          if (_publicJwk.kty === 'oct') {\n            return {\n              didDocumentMetadata: {},\n              didRegistrationMetadata: {},\n              didState: {\n                state: 'failed',\n                reason: `notFound: key with key id '${options.options.keyId}' uses unsupported kty 'oct' for did:key`,\n              },\n            }\n          }\n\n          publicJwk = PublicJwk.fromPublicJwk(_publicJwk)\n          keys = [\n            {\n              didDocumentRelativeKeyId: `#${publicJwk.fingerprint}`,\n              kmsKeyId: options.options.keyId,\n            },\n          ]\n        }\n\n        didDocument = publicJwkToNumAlgo0DidDocument(publicJwk)\n        did = didDocument.id\n      } else if (isPeerDidNumAlgo1CreateOptions(options)) {\n        const didDocumentJson = options.didDocument.toJSON()\n        did = didDocumentJsonToNumAlgo1Did(didDocumentJson)\n        keys = options.options.keys\n\n        didDocument = JsonTransformer.fromJSON({ ...didDocumentJson, id: did }, DidDocument)\n      } else if (isPeerDidNumAlgo2CreateOptions(options)) {\n        const didDocumentJson = options.didDocument.toJSON()\n        did = didDocumentToNumAlgo2Did(options.didDocument)\n        keys = options.options.keys\n\n        didDocument = JsonTransformer.fromJSON({ ...didDocumentJson, id: did }, DidDocument)\n      } else if (isPeerDidNumAlgo4CreateOptions(options)) {\n        const didDocumentJson = options.didDocument.toJSON()\n        keys = options.options.keys\n\n        const { longFormDid, shortFormDid } = didDocumentToNumAlgo4Did(options.didDocument)\n\n        did = longFormDid\n        didDocument = JsonTransformer.fromJSON(\n          { ...didDocumentJson, id: longFormDid, alsoKnownAs: [shortFormDid] },\n          DidDocument\n        )\n      } else {\n        return {\n          didDocumentMetadata: {},\n          didRegistrationMetadata: {},\n          didState: {\n            state: 'failed',\n            reason: 'Missing or incorrect numAlgo provided',\n          },\n        }\n      }\n\n      if (!keys || keys.length === 0) {\n        return {\n          didDocumentMetadata: {},\n          didRegistrationMetadata: {},\n          didState: {\n            state: 'failed',\n            reason: `Missing required 'keys' linking did document verification method id to the kms key id. Provide at least one key in the create options`,\n          },\n        }\n      }\n\n      // Save the did so we know we created it and can use it for didcomm\n      const didRecord = new DidRecord({\n        did,\n        role: DidDocumentRole.Created,\n        didDocument: isPeerDidNumAlgo1CreateOptions(options) ? didDocument : undefined,\n        keys,\n        tags: {\n          // We need to save the recipientKeys, so we can find the associated did\n          // of a key when we receive a message from another connection.\n          recipientKeyFingerprints: didDocument.recipientKeys.map((key) => key.fingerprint),\n          alternativeDids: getAlternativeDidsForPeerDid(did),\n        },\n      })\n      await didRepository.save(agentContext, didRecord)\n\n      return {\n        didDocumentMetadata: {},\n        didRegistrationMetadata: {},\n        didState: {\n          state: 'finished',\n          did: didDocument.id,\n          didDocument,\n        },\n      }\n    } catch (error) {\n      return {\n        didDocumentMetadata: {},\n        didRegistrationMetadata: {},\n        didState: {\n          state: 'failed',\n          reason: `unknownError: ${error.message}`,\n        },\n      }\n    }\n  }\n\n  public async update(): Promise<DidUpdateResult> {\n    return {\n      didDocumentMetadata: {},\n      didRegistrationMetadata: {},\n      didState: {\n        state: 'failed',\n        reason: 'notImplemented: updating did:peer not implemented yet',\n      },\n    }\n  }\n\n  public async deactivate(): Promise<DidDeactivateResult> {\n    return {\n      didDocumentMetadata: {},\n      didRegistrationMetadata: {},\n      didState: {\n        state: 'failed',\n        reason: 'notImplemented: deactivating did:peer not implemented yet',\n      },\n    }\n  }\n}\n\nfunction isPeerDidNumAlgo1CreateOptions(options: PeerDidCreateOptions): options is PeerDidNumAlgo1CreateOptions {\n  return options.options.numAlgo === PeerDidNumAlgo.GenesisDoc\n}\n\nfunction isPeerDidNumAlgo0CreateOptions(options: PeerDidCreateOptions): options is PeerDidNumAlgo0CreateOptions {\n  return options.options.numAlgo === PeerDidNumAlgo.InceptionKeyWithoutDoc\n}\n\nfunction isPeerDidNumAlgo2CreateOptions(options: PeerDidCreateOptions): options is PeerDidNumAlgo2CreateOptions {\n  return options.options.numAlgo === PeerDidNumAlgo.MultipleInceptionKeyWithoutDoc\n}\n\nfunction isPeerDidNumAlgo4CreateOptions(options: PeerDidCreateOptions): options is PeerDidNumAlgo4CreateOptions {\n  return options.options.numAlgo === PeerDidNumAlgo.ShortFormAndLongForm\n}\n\nexport type PeerDidCreateOptions =\n  | PeerDidNumAlgo0CreateOptions\n  | PeerDidNumAlgo1CreateOptions\n  | PeerDidNumAlgo2CreateOptions\n  | PeerDidNumAlgo4CreateOptions\n\nexport interface PeerDidNumAlgo0CreateOptions extends DidCreateOptions {\n  method: 'peer'\n  did?: never\n  didDocument?: never\n  options: {\n    numAlgo: PeerDidNumAlgo.InceptionKeyWithoutDoc\n  } & XOR<{ createKey: KmsCreateKeyOptions<KmsCreateKeyTypeAsymmetric> }, { keyId: string }>\n  secret?: never\n}\n\nexport interface PeerDidNumAlgo1CreateOptions extends DidCreateOptions {\n  method: 'peer'\n  did?: never\n  didDocument: DidDocument\n  options: {\n    numAlgo: PeerDidNumAlgo.GenesisDoc\n\n    /**\n     * The linking between the did document keys and the kms keys. If you want to use\n     * the DID within Credo you MUST add the key here. All keys must be present in the did\n     * document, but not all did document keys must be present in this array, to allow for keys\n     * that are not controleld by this agent.\n     */\n    keys: DidDocumentKey[]\n  }\n  secret?: never\n}\n\nexport interface PeerDidNumAlgo2CreateOptions extends DidCreateOptions {\n  method: 'peer'\n  did?: never\n  didDocument: DidDocument\n  options: {\n    numAlgo: PeerDidNumAlgo.MultipleInceptionKeyWithoutDoc\n\n    /**\n     * The linking between the did document keys and the kms keys. If you want to use\n     * the DID within Credo you MUST add the key here. All keys must be present in the did\n     * document, but not all did document keys must be present in this array, to allow for keys\n     * that are not controleld by this agent.\n     */\n    keys: DidDocumentKey[]\n  }\n  secret?: never\n}\n\nexport interface PeerDidNumAlgo4CreateOptions extends DidCreateOptions {\n  method: 'peer'\n  did?: never\n  didDocument: DidDocument\n  options: {\n    numAlgo: PeerDidNumAlgo.ShortFormAndLongForm\n\n    /**\n     * The linking between the did document keys and the kms keys. If you want to use\n     * the DID within Credo you MUST add the key here. All keys must be present in the did\n     * document, but not all did document keys must be present in this array, to allow for keys\n     * that are not controleld by this agent.\n     */\n    keys: DidDocumentKey[]\n  }\n  secret?: never\n}\n\n// Update and Deactivate not supported for did:peer\nexport type PeerDidUpdateOptions = never\nexport type PeerDidDeactivateOptions = never\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAgBA,IAAa,mBAAb,MAAsD;;OACpC,mBAAmB,CAAC,OAAO;;CAE3C,MAAa,OACX,cACA,SAK0B;EAC1B,MAAM,MAAM,aAAa,kBAAkB,QAAQ,iBAAiB;EACpE,MAAM,gBAAgB,aAAa,kBAAkB,QAAQ,cAAc;EAE3E,IAAI;EACJ,IAAI;EAEJ,IAAI;AAEJ,MAAI;AACF,OAAI,+BAA+B,QAAQ,EAAE;IAC3C,IAAI;AAEJ,QAAI,QAAQ,QAAQ,WAAW;KAC7B,MAAM,kBAAkB,MAAM,IAAI,UAAU,QAAQ,QAAQ,UAAU;AACtE,iBAAY,UAAU,cAAc,gBAAgB,UAAU;AAC9D,YAAO,CACL;MACE,0BAA0B,IAAI,UAAU;MACxC,UAAU,gBAAgB;MAC3B,CACF;WACI;KACL,MAAM,aAAa,MAAM,IAAI,aAAa,EACxC,OAAO,QAAQ,QAAQ,OACxB,CAAC;AAEF,SAAI,CAAC,WACH,QAAO;MACL,qBAAqB,EAAE;MACvB,yBAAyB,EAAE;MAC3B,UAAU;OACR,OAAO;OACP,QAAQ,8BAA8B,QAAQ,QAAQ,MAAM;OAC7D;MACF;AAGH,SAAI,WAAW,QAAQ,MACrB,QAAO;MACL,qBAAqB,EAAE;MACvB,yBAAyB,EAAE;MAC3B,UAAU;OACR,OAAO;OACP,QAAQ,8BAA8B,QAAQ,QAAQ,MAAM;OAC7D;M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