{"version":3,"file":"peerDidNumAlgo4.mjs","names":[],"sources":["../../../../../src/modules/dids/methods/peer/peerDidNumAlgo4.ts"],"sourcesContent":["import { CredoError } from '../../../../error'\nimport {\n  JsonEncoder,\n  JsonTransformer,\n  MultiBaseEncoder,\n  MultiHashEncoder,\n  TypedArrayEncoder,\n  VarintEncoder,\n} from '../../../../utils'\nimport { DidDocument } from '../../domain'\nimport { parseDid } from '../../domain/parse'\n\nconst LONG_RE = /^did:peer:4(z[1-9a-km-zA-HJ-NP-Z]{46}):(z[1-9a-km-zA-HJ-NP-Z]{6,})$/\nconst SHORT_RE = /^did:peer:4(z[1-9a-km-zA-HJ-NP-Z]{46})$/\nconst JSON_MULTICODEC_VARINT = 0x0200\n\nexport const isShortFormDidPeer4 = (did: string) => SHORT_RE.test(did)\nexport const isLongFormDidPeer4 = (did: string) => LONG_RE.test(did)\n\nconst hashEncodedDocument = (encodedDocument: string) =>\n  MultiBaseEncoder.encode(\n    MultiHashEncoder.encode(TypedArrayEncoder.fromUtf8String(encodedDocument), 'sha-256'),\n    'base58btc'\n  )\n\nexport function getAlternativeDidsForNumAlgo4Did(did: string) {\n  const match = did.match(LONG_RE)\n  if (!match) return\n  const [, hash] = match\n  return [`did:peer:4${hash}`]\n}\n\nexport function didToNumAlgo4DidDocument(did: string) {\n  const parsed = parseDid(did)\n\n  const match = parsed.did.match(LONG_RE)\n  if (!match) {\n    throw new CredoError(`Invalid long form algo 4 did:peer: ${parsed.did}`)\n  }\n  const [, hash, encodedDocument] = match\n  if (hash !== hashEncodedDocument(encodedDocument)) {\n    throw new CredoError(`Hash is invalid for did: ${did}`)\n  }\n\n  const { data } = MultiBaseEncoder.decode(encodedDocument)\n  const [multiCodecValue] = VarintEncoder.decode(data.subarray(0, 2))\n  if (multiCodecValue !== JSON_MULTICODEC_VARINT) {\n    throw new CredoError('Not a JSON multicodec data')\n  }\n  const didDocumentJson = JsonEncoder.fromUint8Array(data.subarray(2))\n\n  didDocumentJson.id = parsed.did\n  didDocumentJson.alsoKnownAs = [parsed.did.slice(0, did.lastIndexOf(':'))]\n\n  // Populate all verification methods without controller\n  const addControllerIfNotPresent = (item: unknown) => {\n    if (Array.isArray(item)) item.forEach(addControllerIfNotPresent)\n\n    if (item && typeof item === 'object' && (item as Record<string, unknown>).controller === undefined) {\n      ;(item as Record<string, unknown>).controller = parsed.did\n    }\n  }\n\n  addControllerIfNotPresent(didDocumentJson.verificationMethod)\n  addControllerIfNotPresent(didDocumentJson.authentication)\n  addControllerIfNotPresent(didDocumentJson.assertionMethod)\n  addControllerIfNotPresent(didDocumentJson.keyAgreement)\n  addControllerIfNotPresent(didDocumentJson.capabilityDelegation)\n  addControllerIfNotPresent(didDocumentJson.capabilityInvocation)\n\n  const didDocument = JsonTransformer.fromJSON(didDocumentJson, DidDocument)\n  return didDocument\n}\n\nexport function didDocumentToNumAlgo4Did(didDocument: DidDocument) {\n  const didDocumentJson = didDocument.toJSON()\n\n  // Build input document based on did document, without any\n  // reference to controller\n  const deleteControllerIfPresent = (item: unknown) => {\n    if (Array.isArray(item)) {\n      for (const method of item) {\n        if (method.controller === '#id' || method.controller === didDocument.id) method.controller = undefined\n      }\n    }\n  }\n  didDocumentJson.id = undefined\n  didDocumentJson.alsoKnownAs = undefined\n  deleteControllerIfPresent(didDocumentJson.verificationMethod)\n  deleteControllerIfPresent(didDocumentJson.authentication)\n  deleteControllerIfPresent(didDocumentJson.assertionMethod)\n  deleteControllerIfPresent(didDocumentJson.keyAgreement)\n  deleteControllerIfPresent(didDocumentJson.capabilityDelegation)\n  deleteControllerIfPresent(didDocumentJson.capabilityInvocation)\n\n  // Construct encoded document by prefixing did document with multicodec prefix for JSON\n  const buffer = TypedArrayEncoder.concat([\n    VarintEncoder.encode(JSON_MULTICODEC_VARINT),\n    TypedArrayEncoder.fromUtf8String(JSON.stringify(didDocumentJson)),\n  ])\n\n  const encodedDocument = MultiBaseEncoder.encode(buffer, 'base58btc')\n\n  const shortFormDid = `did:peer:4${hashEncodedDocument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