@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.
52 lines (50 loc) • 1.77 kB
JavaScript
import { parseDid, validateDid } from "./utils/did.mjs";
import DidRegistryContract from "@ayanworks/polygon-did-registry-contract";
import { Contract, JsonRpcProvider } from "ethers";
//#region src/resolver.ts
/**
* Factory function to create a DID Resolver with custom configuration.
* @param rpcUrl Optional override for the RPC
* @param contractAddress Optional override for the Registry Address
*/
function getResolver(rpcUrl, contractAddress) {
const _rpcUrl = rpcUrl;
const _contractAddress = contractAddress;
async function resolve(did) {
if (!validateDid(did)) throw new Error("invalid did provided");
const parsedDid = parseDid(did, {
rpcUrl: _rpcUrl,
contractAddress: _contractAddress
});
const provider = new JsonRpcProvider(parsedDid.networkUrl);
const didDocument = await new Contract(parsedDid.contractAddress, DidRegistryContract.abi, provider).getDIDDoc(parsedDid.didAddress);
if (!didDocument[0]) return {
didDocument: null,
didDocumentMetadata: {},
didResolutionMetadata: {
error: `NotFound!`,
message: `resolver_error: Unable to resolve did '${did}'`
}
};
const didDocumentJson = JSON.parse(didDocument[0]);
if (!didDocumentJson?.verificationMethod) return {
didDocument: didDocumentJson,
didDocumentMetadata: {
linkedResourceMetadata: [],
deactivated: true
},
didResolutionMetadata: { contentType: "application/did+ld+json" }
};
return {
didDocument: didDocumentJson,
didDocumentMetadata: { linkedResourceMetadata: didDocument[1].map((element) => {
return JSON.parse(element);
}) },
didResolutionMetadata: { contentType: "application/did+ld+json" }
};
}
return { polygon: resolve };
}
//#endregion
export { getResolver };
//# sourceMappingURL=resolver.mjs.map