{"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;;EACpD,KAAgB,mBAAmB,CAAC,MAAM;;CAE1C,MAAa,OACX,cACA,SAK0B;EAC1B,MAAM,MAAM,aAAa,kBAAkB,QAAQ,gBAAgB;EACnE,MAAM,gBAAgB,aAAa,kBAAkB,QAAQ,aAAa;EAE1E,IAAI;EACJ,IAAI;EAEJ,IAAI;EAEJ,IAAI;GACF,IAAI,+BAA+B,OAAO,GAAG;IAC3C,IAAI;IAEJ,IAAI,QAAQ,QAAQ,WAAW;KAC7B,MAAM,kBAAkB,MAAM,IAAI,UAAU,QAAQ,QAAQ,SAAS;KACrE,YAAY,UAAU,cAAc,gBAAgB,SAAS;KAC7D,OAAO,CACL;MACE,0BAA0B,IAAI,UAAU;MACxC,UAAU,gBAAgB;KAC5B,CACF;IACF,OAAO;KACL,MAAM,aAAa,MAAM,IAAI,aAAa,EACxC,OAAO,QAAQ,QAAQ,MACzB,CAAC;KAED,IAAI,CAAC,YACH,OAAO;MACL,qBAAqB,CAAC;MACtB,yBAAyB,CAAC;MAC1B,UAAU;OACR,OAAO;OACP,QAAQ,8BAA8B,QAAQ,QAAQ,MAAM;MAC9D;KACF;KAGF,IAAI,WAAW,QAAQ,OACrB,OAAO;MACL,qBAAqB,CAAC;MACtB,yBAAyB,CAAC;MAC1B,UAAU;OACR,OAAO;OACP,QAAQ,8BAA8B,QAAQ,QAAQ,MAAM;MA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