UNPKG

@sphereon/ssi-sdk-ext.did-resolver-key

Version:

Sphereon did:key resolver with BBS+/BLS and EBSI support.

527 lines (516 loc) • 15.4 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { DID_JSON: () => DID_JSON, DID_LD_JSON: () => DID_LD_JSON, default: () => index_default, getResolver: () => getResolver }); module.exports = __toCommonJS(index_exports); var import_varint = __toESM(require("varint"), 1); var import_base58 = require("multiformats/bases/base58"); // src/drivers/ed25519.ts var u8a = __toESM(require("uint8arrays"), 1); var import_ed25519 = require("@stablelib/ed25519"); // src/types.ts var DID_LD_JSON = "application/did+ld+json"; var DID_JSON = "application/did+json"; // src/drivers/ed25519.ts var { toString } = u8a; function encodeKey(key, encodeKey2) { const bytes = new Uint8Array(key.length + 2); bytes[0] = encodeKey2 ?? 236; bytes[1] = 1; bytes.set(key, 2); return `z${toString(bytes, "base58btc")}`; } __name(encodeKey, "encodeKey"); var keyToDidDoc = /* @__PURE__ */ __name((args) => { const { options } = args; if (!options?.publicKeyFormat) { return keyToDidDoc2020(args); } switch (options.publicKeyFormat) { case "Ed25519VerificationKey2018": case "X25519KeyAgreementKey2019": return keyToDidDoc2018_2019(args); case "Ed25519VerificationKey2020": case "X25519KeyAgreementKey2020": case "Multikey": return keyToDidDoc2020(args); default: throw Error(`${options.publicKeyFormat} not supported yet for the ed25519 driver`); } }, "keyToDidDoc"); var keyToDidDoc2018_2019 = /* @__PURE__ */ __name(({ pubKeyBytes, fingerprint, contentType }) => { const did = `did:key:${fingerprint}`; const keyId = `${did}#${fingerprint}`; const x25519PubBytes = (0, import_ed25519.convertPublicKeyToX25519)(pubKeyBytes); const x25519KeyId = `${did}#${encodeKey(x25519PubBytes)}`; return { ...contentType === DID_LD_JSON && { "@context": [ "https://www.w3.org/ns/did/v1", "https://w3id.org/security/suites/ed25519-2018/v1", "https://w3id.org/security/suites/x25519-2019/v1" ] }, id: did, verificationMethod: [ { id: keyId, type: "Ed25519VerificationKey2018", controller: did, publicKeyBase58: toString(pubKeyBytes, "base58btc") }, { id: x25519KeyId, type: "X25519KeyAgreementKey2019", controller: did, publicKeyBase58: toString(x25519PubBytes, "base58btc") } ], authentication: [ keyId ], assertionMethod: [ keyId ], capabilityDelegation: [ keyId ], capabilityInvocation: [ keyId ], keyAgreement: [ x25519KeyId ] }; }, "keyToDidDoc2018_2019"); var keyToDidDoc2020 = /* @__PURE__ */ __name(({ pubKeyBytes, fingerprint, contentType }) => { const did = `did:key:${fingerprint}`; const keyId = `${did}#${fingerprint}`; const x25519PubBytes = (0, import_ed25519.convertPublicKeyToX25519)(pubKeyBytes); const x25519KeyId = `${did}#${encodeKey(x25519PubBytes)}`; return { ...contentType === DID_LD_JSON && { "@context": [ "https://www.w3.org/ns/did/v1", "https://w3id.org/security/suites/ed25519-2020/v1", "https://w3id.org/security/suites/x25519-2020/v1" ] }, id: did, verificationMethod: [ { id: keyId, type: "Ed25519VerificationKey2020", controller: did, publicKeyMultibase: encodeKey(pubKeyBytes, 237) } ], authentication: [ keyId ], assertionMethod: [ keyId ], capabilityDelegation: [ keyId ], capabilityInvocation: [ keyId ], keyAgreement: [ { id: x25519KeyId, type: "X25519KeyAgreementKey2020", controller: did, publicKeyMultibase: encodeKey(x25519PubBytes, 236) } ] }; }, "keyToDidDoc2020"); var ed25519_default = { keyToDidDoc }; // src/drivers/bls12381g2.ts var u8a2 = __toESM(require("uint8arrays"), 1); var { toString: toString2 } = u8a2; var keyToDidDoc2 = /* @__PURE__ */ __name(({ pubKeyBytes, fingerprint }) => { const did = `did:key:${fingerprint}`; const keyId = `${did}#${fingerprint}`; return { id: did, verificationMethod: [ { id: keyId, type: "Bls12381G2Key2020", controller: did, publicKeyBase58: toString2(pubKeyBytes, "base58btc") } ], authentication: [ keyId ], assertionMethod: [ keyId ], capabilityDelegation: [ keyId ], capabilityInvocation: [ keyId ] }; }, "keyToDidDoc"); var bls12381g2_default = { keyToDidDoc: keyToDidDoc2 }; // src/drivers/secp256k1.ts var u8a3 = __toESM(require("uint8arrays"), 1); var { toString: toString3 } = u8a3; var keyToDidDoc3 = /* @__PURE__ */ __name(({ pubKeyBytes, fingerprint }) => { const did = `did:key:${fingerprint}`; const keyId = `${did}#${fingerprint}`; return { id: did, verificationMethod: [ { id: keyId, type: "Secp256k1VerificationKey2018", controller: did, publicKeyBase58: toString3(pubKeyBytes, "base58btc") } ], authentication: [ keyId ], assertionMethod: [ keyId ], capabilityDelegation: [ keyId ], capabilityInvocation: [ keyId ] }; }, "keyToDidDoc"); var secp256k1_default = { keyToDidDoc: keyToDidDoc3 }; // src/drivers/secp256r1.ts var nist_weierstrauss = __toESM(require("nist-weierstrauss"), 1); var u8a4 = __toESM(require("uint8arrays"), 1); var { fromString } = u8a4; function keyToDidDoc4({ pubKeyBytes, fingerprint }) { const did = `did:key:${fingerprint}`; const keyId = `${did}#${fingerprint}`; const key = pubKeyBytesToXY(pubKeyBytes); return { id: did, verificationMethod: [ { id: keyId, type: "JsonWebKey2020", controller: did, publicKeyJwk: { kty: "EC", crv: "P-256", x: key.xm, y: key.ym } } ], authentication: [ keyId ], assertionMethod: [ keyId ], capabilityDelegation: [ keyId ], capabilityInvocation: [ keyId ] }; } __name(keyToDidDoc4, "keyToDidDoc"); function pubKeyBytesToXY(pubKeyBytes) { if (!nist_weierstrauss.nist_weierstrauss_common.testUint8Array(pubKeyBytes)) { throw new TypeError("input must be a Uint8Array"); } const publicKeyHex = nist_weierstrauss.nist_weierstrauss_common.pubKeyBytesToHex(pubKeyBytes); const bytesCount = publicKeyHex.length / 2; if (bytesCount == 64) { return nist_weierstrauss.nist_weierstrauss_common.publicKeyToXY(publicKeyHex); } if (bytesCount == 65) { if (publicKeyHex.slice(0, 2) == "04") { const publicKey = publicKeyHex.slice(2); return nist_weierstrauss.nist_weierstrauss_common.publicKeyToXY(publicKey); } } if (bytesCount == 33) { if (publicKeyHex.slice(0, 2) == "03" || publicKeyHex.slice(0, 2) == "02") { const publicKey = fromString(publicKeyHex, "base16"); const point = nist_weierstrauss.secp256r1.ECPointDecompress(publicKey); return nist_weierstrauss.nist_weierstrauss_common.publicKeyIntToXY(point); } } throw new Error("Unexpected pubKeyBytes"); } __name(pubKeyBytesToXY, "pubKeyBytesToXY"); var secp256r1_default = { keyToDidDoc: keyToDidDoc4 }; // src/drivers/secp384r1.ts var u8a5 = __toESM(require("uint8arrays"), 1); var nist_weierstrauss2 = __toESM(require("nist-weierstrauss"), 1); var { fromString: fromString2 } = u8a5; function keyToDidDoc5({ pubKeyBytes, fingerprint }) { const did = `did:key:${fingerprint}`; const keyId = `${did}#${fingerprint}`; const key = pubKeyBytesToXY2(pubKeyBytes); return { id: did, verificationMethod: [ { id: keyId, type: "JsonWebKey2020", controller: did, publicKeyJwk: { kty: "EC", crv: "P-384", x: key.xm, y: key.ym } } ], authentication: [ keyId ], assertionMethod: [ keyId ], capabilityDelegation: [ keyId ], capabilityInvocation: [ keyId ] }; } __name(keyToDidDoc5, "keyToDidDoc"); function pubKeyBytesToXY2(pubKeyBytes) { if (!nist_weierstrauss2.nist_weierstrauss_common.testUint8Array(pubKeyBytes)) { throw new TypeError("input must be a Uint8Array"); } const publicKeyHex = nist_weierstrauss2.nist_weierstrauss_common.pubKeyBytesToHex(pubKeyBytes); const bytesCount = publicKeyHex.length / 2; if (bytesCount == 96) { return nist_weierstrauss2.nist_weierstrauss_common.publicKeyToXY(publicKeyHex); } if (bytesCount == 97) { if (publicKeyHex.slice(0, 2) == "04") { const publicKey = publicKeyHex.slice(2); return nist_weierstrauss2.nist_weierstrauss_common.publicKeyToXY(publicKey); } } if (bytesCount == 49) { if (publicKeyHex.slice(0, 2) == "03" || publicKeyHex.slice(0, 2) == "02") { const publicKey = fromString2(publicKeyHex, "base16"); const point = nist_weierstrauss2.secp384r1.ECPointDecompress(publicKey); return nist_weierstrauss2.nist_weierstrauss_common.publicKeyIntToXY(point); } } throw new Error("Unexpected pubKeyBytes"); } __name(pubKeyBytesToXY2, "pubKeyBytesToXY"); var secp384r1_default = { keyToDidDoc: keyToDidDoc5 }; // src/drivers/secp521r1.ts var u8a6 = __toESM(require("uint8arrays"), 1); var nist_weierstrauss3 = __toESM(require("nist-weierstrauss"), 1); var { fromString: fromString3 } = u8a6; function keyToDidDoc6({ pubKeyBytes, fingerprint }) { const did = `did:key:${fingerprint}`; const keyId = `${did}#${fingerprint}`; const key = pubKeyBytesToXY3(pubKeyBytes); return { id: did, verificationMethod: [ { id: keyId, type: "JsonWebKey2020", controller: did, publicKeyJwk: { kty: "EC", crv: "P-521", x: key.xm, y: key.ym } } ], authentication: [ keyId ], assertionMethod: [ keyId ], capabilityDelegation: [ keyId ], capabilityInvocation: [ keyId ] }; } __name(keyToDidDoc6, "keyToDidDoc"); function pubKeyBytesToXY3(pubKeyBytes) { if (!nist_weierstrauss3.nist_weierstrauss_common.testUint8Array(pubKeyBytes)) { throw new TypeError("input must be a Uint8Array"); } const publicKeyHex = nist_weierstrauss3.nist_weierstrauss_common.pubKeyBytesToHex(pubKeyBytes); if (132 <= publicKeyHex.length && publicKeyHex.length <= 134) { if (publicKeyHex.slice(0, 2) == "03" || publicKeyHex.slice(0, 2) == "02") { const publicKey = fromString3(publicKeyHex, "base16"); const point = nist_weierstrauss3.secp521r1.ECPointDecompress(publicKey); return nist_weierstrauss3.nist_weierstrauss_common.publicKeyIntToXY(point); } } throw new Error("Unexpected pubKeyBytes"); } __name(pubKeyBytesToXY3, "pubKeyBytesToXY"); var secp521r1_default = { keyToDidDoc: keyToDidDoc6 }; // src/drivers/jwk.jcs.ts var import_ssi_sdk_ext = require("@sphereon/ssi-sdk-ext.key-utils"); var keyToDidDoc7 = /* @__PURE__ */ __name(({ pubKeyBytes, fingerprint, contentType }) => { const did = `did:key:${fingerprint}`; const keyId = `${did}#${fingerprint}`; const publicKeyJwk = (0, import_ssi_sdk_ext.jwkJcsDecode)(pubKeyBytes); return { ...contentType === DID_LD_JSON && { "@context": [ "https://www.w3.org/ns/did/v1", "https://w3id.org/security/suites/jws-2020/v1" ] }, id: did, verificationMethod: [ { id: keyId, type: "JsonWebKey2020", controller: did, publicKeyJwk } ], authentication: [ keyId ], assertionMethod: [ keyId ], capabilityDelegation: [ keyId ], capabilityInvocation: [ keyId ] }; }, "keyToDidDoc"); var jwk_jcs_default = { keyToDidDoc: keyToDidDoc7 }; // src/index.ts var { decode } = import_varint.default; var prefixToDriverMap = { 231: secp256k1_default, 237: ed25519_default, 4608: secp256r1_default, 4609: secp384r1_default, 4610: secp521r1_default, 235: bls12381g2_default, 60241: jwk_jcs_default }; var getResolver = /* @__PURE__ */ __name(() => { return { key: /* @__PURE__ */ __name(async (did, parsed, r, options) => { const contentType = options.accept || DID_LD_JSON; const response = { didResolutionMetadata: { contentType }, didDocument: null, didDocumentMetadata: {} }; try { const multicodecPubKey = import_base58.base58btc.decode(parsed.id); const keyType = decode(multicodecPubKey); const pubKeyBytes = multicodecPubKey.slice(decode.bytes); const args = { pubKeyBytes, fingerprint: parsed.id, contentType, options }; const doc = await prefixToDriverMap[keyType].keyToDidDoc(args); if (contentType === DID_LD_JSON) { if (!doc["@context"]) { doc["@context"] = "https://w3id.org/did/v1"; } else if (Array.isArray(doc["@context"]) && !doc["@context"].includes("https://w3id.org/did/v1") && !doc["@context"].includes("https://www.w3.org/ns/did/v1")) { doc["@context"].push("https://w3id.org/did/v1"); } response.didDocument = doc; } else if (contentType === DID_JSON) { response.didDocument = doc; } else { delete response.didResolutionMetadata.contentType; response.didResolutionMetadata.error = "representationNotSupported"; } } catch (e) { response.didResolutionMetadata.error = "invalidDid"; response.didResolutionMetadata.message = e.toString(); } return response; }, "key") }; }, "getResolver"); var index_default = { getResolver }; //# sourceMappingURL=index.cjs.map