{"version":3,"file":"createPeerDidDocumentFromServices.mjs","names":[],"sources":["../../../../../src/modules/dids/methods/peer/createPeerDidDocumentFromServices.ts"],"sourcesContent":["import { convertPublicKeyToX25519 } from '@stablelib/ed25519'\nimport type { ResolvedDidCommService } from '../../../../types'\n\nimport { PublicJwk } from '../../../kms'\nimport type { DidDocumentKey } from '../../DidsApiOptions'\nimport { DidDocument, getEd25519VerificationKey2018, getX25519KeyAgreementKey2019 } from '../../domain'\nimport { DidDocumentBuilder } from '../../domain/DidDocumentBuilder'\nimport { DidCommV1Service } from '../../domain/service/DidCommV1Service'\nimport { DidKey } from '../key'\n\nexport function createPeerDidDocumentFromServices<WithKeys extends boolean>(\n  services: ResolvedDidCommService[],\n  withKeys: WithKeys\n): { didDocument: DidDocument; keys: WithKeys extends true ? DidDocumentKey[] : undefined } {\n  const didDocumentBuilder = new DidDocumentBuilder('')\n\n  // Keep track of all added key id based on the fingerprint so we can add them to the recipientKeys as references\n  const recipientKeyIdMapping: { [fingerprint: string]: string } = {}\n\n  const keys: DidDocumentKey[] = []\n\n  let keyIndex = 1\n  services.forEach((service, index) => {\n    // Get the local key reference for each of the recipient keys\n    const recipientKeys = service.recipientKeys.map((recipientKey) => {\n      // Key already added to the did document\n      if (recipientKeyIdMapping[recipientKey.fingerprint]) return recipientKeyIdMapping[recipientKey.fingerprint]\n\n      const x25519Key = PublicJwk.fromPublicKey({\n        crv: 'X25519',\n        kty: 'OKP',\n        publicKey: convertPublicKeyToX25519(recipientKey.publicKey.publicKey),\n      })\n\n      // key ids follow the #key-N pattern to comply with did:peer:2 spec\n      const ed25519RelativeVerificationMethodId = `#key-${keyIndex++}`\n      const ed25519VerificationMethod = getEd25519VerificationKey2018({\n        id: ed25519RelativeVerificationMethodId,\n        publicJwk: recipientKey,\n        controller: '#id',\n      })\n      const x25519RelativeVerificationMethodId = `#key-${keyIndex++}`\n      const x25519VerificationMethod = getX25519KeyAgreementKey2019({\n        id: x25519RelativeVerificationMethodId,\n        publicJwk: x25519Key,\n        controller: '#id',\n      })\n\n      recipientKeyIdMapping[recipientKey.fingerprint] = ed25519VerificationMethod.id\n\n      // NOTE: both use the same key id as the x25519 key is derived from the ed25519 key\n      // This is special for DIDComm v1 and any kms that wants to support DIDComm v1 will have\n      // to support both Ed25519 and X25519 operations on a Ed25519 key\n      if (withKeys) {\n        keys.push({\n          didDocumentRelativeKeyId: ed25519RelativeVerificationMethodId,\n          kmsKeyId: recipientKey.keyId,\n        })\n        keys.push({\n          didDocumentRelativeKeyId: x25519RelativeVerificationMethodId,\n          kmsKeyId: recipientKey.keyId,\n        })\n      }\n\n      // We should not add duplicated keys for services\n      didDocumentBuilder.addAuthentication(ed25519VerificationMethod).addKeyAgreement(x25519VerificationMethod)\n\n      return recipientKeyIdMapping[recipientKey.fingerprint]\n    })\n\n    // Transform all routing keys into did:key:xxx#key-id references. This will probably change for didcomm v2\n    const routingKeys = service.routingKeys?.map((key) => {\n      const didKey = new DidKey(key)\n\n      return `${didKey.did}#${key.fingerprint}`\n    })\n\n    didDocumentBuilder.addService(\n      new DidCommV1Service({\n        id: service.id,\n        priority: index,\n        serviceEndpoint: service.serviceEndpoint,\n        recipientKeys,\n        routingKeys,\n      })\n    )\n  })\n\n  return {\n    didDocument: didDocumentBuilder.build(),\n    keys: (withKeys ? keys : undefined) as WithKeys extends true ? DidDocumentKey[] : undefined,\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;AAUA,SAAgB,kCACd,UACA,UAC0F;CAC1F,MAAM,qBAAqB,IAAI,mBAAmB,GAAG;CAGrD,MAAM,wBAA2D,EAAE;CAEnE,MAAM,OAAyB,EAAE;CAEjC,IAAI,WAAW;AACf,UAAS,SAAS,SAAS,UAAU;EAEnC,MAAM,gBAAgB,QAAQ,cAAc,KAAK,iBAAiB;AAEhE,OAAI,sBAAsB,aAAa,aAAc,QAAO,sBAAsB,aAAa;GAE/F,MAAM,YAAY,UAAU,cAAc;IACxC,KAAK;IACL,KAAK;IACL,WAAW,yBAAyB,a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