UNPKG

@credo-ts/core

Version:

<p align="center"> <br /> <img alt="Credo Logo" src="https://github.com/openwallet-foundation/credo-ts/blob/c7886cb8377ceb8ee4efe8d264211e561a75072d/images/credo-logo.png" height="250px" /> </p> <h1 align="center"><b>Credo - Core</b></h1

1 lines • 6.12 kB
{"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(encodedDocument)}`\n const longFormDid = `${shortFormDid}:${encodedDocument}`\n\n return { shortFormDid, longFormDid }\n}\n"],"mappings":";;;;;;;;;;;;;AAYA,MAAM,UAAU;AAChB,MAAM,WAAW;AACjB,MAAM,yBAAyB;AAE/B,MAAa,uBAAuB,QAAgB,SAAS,KAAK,GAAG;AAGrE,MAAM,uBAAuB,oBAC3B,iBAAiB,OACf,iBAAiB,OAAO,kBAAkB,eAAe,eAAe,GAAG,SAAS,GACpF,WACF;AAEF,SAAgB,iCAAiC,KAAa;CAC5D,MAAM,QAAQ,IAAI,MAAM,OAAO;CAC/B,IAAI,CAAC,OAAO;CACZ,MAAM,GAAG,QAAQ;CACjB,OAAO,CAAC,aAAa,MAAM;AAC7B;AAEA,SAAgB,yBAAyB,KAAa;CACpD,MAAM,SAAS,SAAS,GAAG;CAE3B,MAAM,QAAQ,OAAO,IAAI,MAAM,OAAO;CACtC,IAAI,CAAC,OACH,MAAM,IAAI,WAAW,sCAAsC,OAAO,KAAK;CAEzE,MAAM,GAAG,MAAM,mBAAmB;CAClC,IAAI,SAAS,oBAAoB,eAAe,GAC9C,MAAM,IAAI,WAAW,4BAA4B,KAAK;CAGxD,MAAM,EAAE,SAAS,iBAAiB,OAAO,eAAe;CACxD,MAAM,CAAC,mBAAmB,cAAc,OAAO,KAAK,SAAS,GAAG,CAAC,CAAC;CAClE,IAAI,oBAAoB,wBACtB,MAAM,IAAI,WAAW,4BAA4B;CAEnD,MAAM,kBAAkB,YAAY,eAAe,KAAK,SAAS,CAAC,CAAC;CAEnE,gBAAgB,KAAK,OAAO;CAC5B,gBAAgB,cAAc,CAAC,OAAO,IAAI,MAAM,GAAG,IAAI,YAAY,GAAG,CAAC,CAAC;CAGxE,MAAM,6BAA6B,SAAkB;EACnD,IAAI,MAAM,QAAQ,IAAI,GAAG,KAAK,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