UNPKG

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

Version:

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

498 lines (487 loc) • 13.7 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); // src/index.ts import varint from "varint"; import { base58btc } from "multiformats/bases/base58"; // src/drivers/ed25519.ts import * as u8a from "uint8arrays"; import { convertPublicKeyToX25519 } from "@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 = 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 = 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 import * as u8a2 from "uint8arrays"; 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 import * as u8a3 from "uint8arrays"; 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 import * as nist_weierstrauss from "nist-weierstrauss"; import * as u8a4 from "uint8arrays"; 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 import * as u8a5 from "uint8arrays"; import * as nist_weierstrauss2 from "nist-weierstrauss"; 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 import * as u8a6 from "uint8arrays"; import * as nist_weierstrauss3 from "nist-weierstrauss"; 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 import { jwkJcsDecode } from "@sphereon/ssi-sdk-ext.key-utils"; var keyToDidDoc7 = /* @__PURE__ */ __name(({ pubKeyBytes, fingerprint, contentType }) => { const did = `did:key:${fingerprint}`; const keyId = `${did}#${fingerprint}`; const publicKeyJwk = 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 } = varint; 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 = 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 }; export { DID_JSON, DID_LD_JSON, index_default as default, getResolver }; //# sourceMappingURL=index.js.map