@ayanworks/polygon-did-resolver
Version:
The polygon resolver library is used for resolving DID’s in Polygon Method Space. The module is supposed to be used as an integeration to polygon library.
1 lines • 3.22 kB
Source Map (JSON)
{"version":3,"file":"resolver.mjs","names":[],"sources":["../src/resolver.ts"],"sourcesContent":["import DidRegistryContract from '@ayanworks/polygon-did-registry-contract'\nimport type { DIDResolutionResult, DIDResolver } from 'did-resolver'\nimport { Contract, JsonRpcProvider } from 'ethers'\nimport { parseDid, validateDid } from './utils/did'\n\n/**\n * Factory function to create a DID Resolver with custom configuration.\n * @param rpcUrl Optional override for the RPC\n * @param contractAddress Optional override for the Registry Address\n */\nexport function getResolver(rpcUrl?: string, contractAddress?: string): Record<string, DIDResolver> {\n const _rpcUrl = rpcUrl\n const _contractAddress = contractAddress\n\n // The 'resolve' function now matches the DIDResolver type signature\n async function resolve(did: string): Promise<DIDResolutionResult> {\n const isValidDid = validateDid(did)\n if (!isValidDid) {\n throw new Error('invalid did provided')\n }\n\n // We use the overrides passed to getResolver, or fallback to parsing the DID string\n const parsedDid = parseDid(did, { rpcUrl: _rpcUrl, contractAddress: _contractAddress })\n\n const provider = new JsonRpcProvider(parsedDid.networkUrl)\n const registry = new Contract(parsedDid.contractAddress, DidRegistryContract.abi, provider)\n\n // Calling smart contract to get DID Document\n const didDocument = await registry.getDIDDoc(parsedDid.didAddress)\n\n if (!didDocument[0]) {\n return {\n didDocument: null,\n didDocumentMetadata: {},\n didResolutionMetadata: {\n error: `NotFound!`,\n message: `resolver_error: Unable to resolve did '${did}'`,\n },\n }\n }\n const didDocumentJson = JSON.parse(didDocument[0])\n\n if (!didDocumentJson?.verificationMethod) {\n return {\n didDocument: didDocumentJson,\n didDocumentMetadata: {\n linkedResourceMetadata: [],\n deactivated: true,\n },\n didResolutionMetadata: { contentType: 'application/did+ld+json' },\n }\n }\n return {\n didDocument: didDocumentJson,\n didDocumentMetadata: {\n linkedResourceMetadata: didDocument[1].map((element: string) => {\n return JSON.parse(element)\n }),\n },\n didResolutionMetadata: { contentType: 'application/did+ld+json' },\n }\n }\n return { polygon: resolve }\n}\n"],"mappings":";;;;;;;;;;AAUA,SAAgB,YAAY,QAAiB,iBAAuD;CAClG,MAAM,UAAU;CAChB,MAAM,mBAAmB;CAGzB,eAAe,QAAQ,KAA2C;AAEhE,MAAI,CADe,YAAY,IAAI,CAEjC,OAAM,IAAI,MAAM,uBAAuB;EAIzC,MAAM,YAAY,SAAS,KAAK;GAAE,QAAQ;GAAS,iBAAiB;GAAkB,CAAC;EAEvF,MAAM,WAAW,IAAI,gBAAgB,UAAU,WAAW;EAI1D,MAAM,cAAc,MAHH,IAAI,SAAS,UAAU,iBAAiB,oBAAoB,KAAK,SAAS,CAGxD,UAAU,UAAU,WAAW;AAElE,MAAI,CAAC,YAAY,GACf,QAAO;GACL,aAAa;GACb,qBAAqB,EAAE;GACvB,uBAAuB;IACrB,OAAO;IACP,SAAS,0CAA0C,IAAI;IACxD;GACF;EAEH,MAAM,kBAAkB,KAAK,MAAM,YAAY,GAAG;AAElD,MAAI,CAAC,iBAAiB,mBACpB,QAAO;GACL,aAAa;GACb,qBAAqB;IACnB,wBAAwB,EAAE;IAC1B,aAAa;IACd;GACD,uBAAuB,EAAE,aAAa,2BAA2B;GAClE;AAEH,SAAO;GACL,aAAa;GACb,qBAAqB,EACnB,wBAAwB,YAAY,GAAG,KAAK,YAAoB;AAC9D,WAAO,KAAK,MAAM,QAAQ;KAC1B,EACH;GACD,uBAAuB,EAAE,aAAa,2BAA2B;GAClE;;AAEH,QAAO,EAAE,SAAS,SAAS"}