From fa098a9f79847809e8e36b7715048ad328e65397 Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Laptop)" Date: Thu, 22 Feb 2024 17:10:57 -0700 Subject: [PATCH 1/7] feat: Add did:web support Signed-off-by: Colton Wolkins (Laptop) --- src/constants.ts | 2 +- src/lib/didcomm.ts | 65 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index f9f8099..094855d 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,5 +1,5 @@ export const DEFAULT_MEDIATOR = - "did:peer:2.Ez6LStkZg14oG5LCxja3RhotWB7m94afER4EiBLhYpUSokbyR.Vz6MkgSYBM63iHNeiT2VSQu7bbtXhGYCQrPJ8uEGurbfGbbgE.SW3sidCI6ImRtIiwicyI6Imh0dHBzOi8vdXMtZWFzdC5wdWJsaWMubWVkaWF0b3IuaW5kaWNpb3RlY2guaW8vbWVzc2FnZSIsInIiOltdLCJhIjpbImRpZGNvbW0vdjIiLCJkaWRjb21tL2FpcDI7ZW52PXJmYzE5Il19LHsidCI6ImRtIiwicyI6IndzczovL3dzLnVzLWVhc3QucHVibGljLm1lZGlhdG9yLmluZGljaW90ZWNoLmlvL3dzIiwiciI6W10sImEiOlsiZGlkY29tbS92MiIsImRpZGNvbW0vYWlwMjtlbnY9cmZjMTkiXX1d" + "did:web:dev.cloudmediator.indiciotech.io" export const ROOTS_MEDIATOR = "did:peer:2.Ez6LSms555YhFthn1WV8ciDBpZm86hK9tp83WojJUmxPGk1hZ.Vz6MkmdBjMyB4TS5UbbQw54szm8yvMMf1ftGV2sQVYAxaeWhE.SeyJpZCI6Im5ldy1pZCIsInQiOiJkbSIsInMiOiJodHRwczovL21lZGlhdG9yLnJvb3RzaWQuY2xvdWQiLCJhIjpbImRpZGNvbW0vdjIiXX0" diff --git a/src/lib/didcomm.ts b/src/lib/didcomm.ts index 977362f..aa6e75a 100644 --- a/src/lib/didcomm.ts +++ b/src/lib/didcomm.ts @@ -98,6 +98,67 @@ export class DIDPeerResolver implements DIDResolver { } } +var did_web_cache: Record = {}; + +export class DIDWebResolver implements DIDResolver { + async resolve(did: DID): Promise { + if(did in did_web_cache) + return did_web_cache[did]; + + var path = did.slice(8); + path += "/.well-known/did.json"; + const raw_doc = await fetch(`https://${path}`); + var doc = await raw_doc.json(); + console.log("doc?", doc); + var new_methods = [] + for(const method of doc["verificationMethod"]) { + var t = "MultiKey"; + if (doc["authentication"].includes(method["id"])) + t = "Ed25519VerificationKey2020"; + if (doc["keyAgreement"].includes(method["id"])) + t = "X25519KeyAgreementKey2020"; + var new_method = { + ...method, + type: t, + } + if(new_method.id.startsWith("#")) + new_method.id = new_method.controller + new_method.id + new_methods.push(new_method); + } + doc["verificationMethod"] = new_methods; + doc["keyAgreement"].forEach((value: string, index: number, arr: Array) => { + if(value.startsWith("#")) + arr[index] = did + value + }); + doc["authentication"].forEach((value: string, index: number, arr: Array) => { + if(value.startsWith("#")) + arr[index] = did + value + }); + did_web_cache[did] = doc; + return doc + } +} + +type ResolverMap = { + [key: string]: DIDResolver; +} + +export class PrefixResolver implements DIDResolver { + resolver_map: ResolverMap = {} + constructor() { + this.resolver_map = { + "did:peer:2": new DIDPeerResolver() as DIDResolver, + "did:web:": new DIDWebResolver() as DIDResolver, + } + } + + async resolve(did: DID): Promise { + var result = Object.keys(this.resolver_map).filter(resolver => did.startsWith(resolver)); + const resolved_doc = await this.resolver_map[result[0] as keyof typeof this.resolver_map].resolve(did); + return resolved_doc; + } +} + export interface SecretsManager extends SecretsResolver { store_secret: (secret: Secret) => void } @@ -211,11 +272,11 @@ export interface DIDCommMessage { } export class DIDComm { - private readonly resolver: DIDPeerResolver + private readonly resolver: DIDResolver private readonly secretsResolver: SecretsManager constructor() { - this.resolver = new DIDPeerResolver() + this.resolver = new PrefixResolver() this.secretsResolver = new EphemeralSecretsResolver() } From a31e3fb369727475f937fd4a69d301e2e4ab423e Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Laptop)" Date: Thu, 22 Feb 2024 17:12:00 -0700 Subject: [PATCH 2/7] fix: did:peer:2 resolve & generation Signed-off-by: Colton Wolkins (Laptop) --- src/lib/didcomm.ts | 13 +++++++++---- src/lib/peer2.ts | 11 +++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/lib/didcomm.ts b/src/lib/didcomm.ts index aa6e75a..dd528d1 100644 --- a/src/lib/didcomm.ts +++ b/src/lib/didcomm.ts @@ -26,7 +26,8 @@ function x25519ToSecret( x25519KeyPriv: Uint8Array, x25519Key: Uint8Array ): Secret { - const encIdent = DIDPeer.keyToIdent(x25519Key, "x25519-pub") + //const encIdent = DIDPeer.keyToIdent(x25519Key, "x25519-pub") + const encIdent = "key-2" const secretEnc: Secret = { id: `${did}#${encIdent}`, type: "X25519KeyAgreementKey2020", @@ -40,7 +41,8 @@ function ed25519ToSecret( ed25519KeyPriv: Uint8Array, ed25519Key: Uint8Array ): Secret { - const verIdent = DIDPeer.keyToIdent(ed25519Key, "ed25519-pub") + //const verIdent = DIDPeer.keyToIdent(ed25519Key, "ed25519-pub") + const verIdent = "key-1" const secretVer: Secret = { id: `${did}#${verIdent}`, type: "Ed25519VerificationKey2020", @@ -56,8 +58,11 @@ export function generateDidForMediator() { const enckey = edwardsToMontgomeryPub(verkey) const service = { type: "DIDCommMessaging", - serviceEndpoint: "", - accept: ["didcomm/v2"], + serviceEndpoint: { + uri: "didcomm:transport/queue", + accept: ["didcomm/v2"], + routingKeys: [] as string[], + }, } const did = DIDPeer.generate([verkey], [enckey], service) diff --git a/src/lib/peer2.ts b/src/lib/peer2.ts index eb0bada..da734a8 100644 --- a/src/lib/peer2.ts +++ b/src/lib/peer2.ts @@ -159,6 +159,7 @@ export default class DIDPeer { id: did, } let serviceIndex = 0; + let keyIndex = 1; elements.forEach(element => { const purposeCode = element.charAt(0) @@ -175,10 +176,7 @@ export default class DIDPeer { if (!doc.verificationMethod) { doc.verificationMethod = [] } - let ident = `${did}#${DIDPeer.keyToIdent( - decodedSigningKey, - "ed25519-pub" - )}` + let ident = `${did}#key-${keyIndex++}` doc.verificationMethod.push({ id: ident, controller: did, @@ -202,10 +200,7 @@ export default class DIDPeer { if (!doc.verificationMethod) { doc.verificationMethod = [] } - let ident = `${did}#${DIDPeer.keyToIdent( - decodedEncryptionKey, - "x25519-pub" - )}` + let ident = `${did}#key-${keyIndex++}` doc.verificationMethod.push({ id: ident, controller: did, From b2644c264036cf4f405ffa2b965073c169d88c23 Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Laptop)" Date: Tue, 5 Mar 2024 11:55:15 -0700 Subject: [PATCH 3/7] chore: update indicio mediator did Signed-off-by: Colton Wolkins (Laptop) --- src/constants.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/constants.ts b/src/constants.ts index 094855d..c2a5e3e 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,5 +1,5 @@ export const DEFAULT_MEDIATOR = - "did:web:dev.cloudmediator.indiciotech.io" + "did:web:us-east.public.mediator.indiciotech.io" export const ROOTS_MEDIATOR = "did:peer:2.Ez6LSms555YhFthn1WV8ciDBpZm86hK9tp83WojJUmxPGk1hZ.Vz6MkmdBjMyB4TS5UbbQw54szm8yvMMf1ftGV2sQVYAxaeWhE.SeyJpZCI6Im5ldy1pZCIsInQiOiJkbSIsInMiOiJodHRwczovL21lZGlhdG9yLnJvb3RzaWQuY2xvdWQiLCJhIjpbImRpZGNvbW0vdjIiXX0" From d03d48810ebe444d55f381c0648ae7e9dde0081c Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Laptop)" Date: Fri, 8 Mar 2024 14:26:36 -0700 Subject: [PATCH 4/7] fix: Workaround didcomm-rust crash Signed-off-by: Colton Wolkins (Laptop) --- src/lib/peer2.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib/peer2.ts b/src/lib/peer2.ts index da734a8..18a634b 100644 --- a/src/lib/peer2.ts +++ b/src/lib/peer2.ts @@ -231,6 +231,7 @@ export default class DIDPeer { return service }) .map(DIDPeer.transformOldServiceStyleToNew) + services = services.filter((service: any) => service.type == "DIDCommMessaging") if (!Array.isArray(doc.service)) { doc.service = []; From 7c4b02284f0b3da4744a2363f2eeebf82a2652eb Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Laptop)" Date: Mon, 18 Mar 2024 14:48:28 -0600 Subject: [PATCH 5/7] feat: allow did:web to parse subdirectories Signed-off-by: Colton Wolkins (Laptop) --- src/lib/didcomm.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/lib/didcomm.ts b/src/lib/didcomm.ts index dd528d1..6e84a7b 100644 --- a/src/lib/didcomm.ts +++ b/src/lib/didcomm.ts @@ -110,8 +110,25 @@ export class DIDWebResolver implements DIDResolver { if(did in did_web_cache) return did_web_cache[did]; + // Remove did:web: from the start var path = did.slice(8); - path += "/.well-known/did.json"; + + // Split by : to build the path + var paths = path.split(":") + + // Decode %3A to a : + paths[0] = paths[0].replaceAll(/%3[aA]/g, ":") + + if(paths.length == 1) { + // If there's only one elemenet in the path, fetch the well known + path = `${paths[0]}/.well-known/did.json`; + } else { + // Otherwise, join and fetch the ./did.json + path = paths.join("/"); + path += "/did.json"; + } + + // Fetch the did_doc const raw_doc = await fetch(`https://${path}`); var doc = await raw_doc.json(); console.log("doc?", doc); From d09969c3e719b3dd047de7f29f0e25ec30cea4ad Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Laptop)" Date: Mon, 18 Mar 2024 14:49:21 -0600 Subject: [PATCH 6/7] fix: Filter out service types that we can't handle Signed-off-by: Colton Wolkins (Laptop) --- src/lib/didcomm.ts | 1 + src/lib/peer2.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/src/lib/didcomm.ts b/src/lib/didcomm.ts index 6e84a7b..df84ed6 100644 --- a/src/lib/didcomm.ts +++ b/src/lib/didcomm.ts @@ -156,6 +156,7 @@ export class DIDWebResolver implements DIDResolver { if(value.startsWith("#")) arr[index] = did + value }); + doc["service"] = doc["service"].filter((s: any) => s.type == "DIDCommMessaging"); did_web_cache[did] = doc; return doc } diff --git a/src/lib/peer2.ts b/src/lib/peer2.ts index 18a634b..6c187ea 100644 --- a/src/lib/peer2.ts +++ b/src/lib/peer2.ts @@ -231,6 +231,7 @@ export default class DIDPeer { return service }) .map(DIDPeer.transformOldServiceStyleToNew) + .filter((service: any) => {return service.type == "DIDCommMessaging"}) services = services.filter((service: any) => service.type == "DIDCommMessaging") if (!Array.isArray(doc.service)) { From 91b87c1aba267b16ed1fb7c59e591e6de184fd2d Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Laptop)" Date: Wed, 12 Jun 2024 21:57:46 -0600 Subject: [PATCH 7/7] feat: update Indicio mediator did Signed-off-by: Colton Wolkins (Laptop) --- src/constants.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/constants.ts b/src/constants.ts index c2a5e3e..893f88b 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,5 +1,5 @@ export const DEFAULT_MEDIATOR = - "did:web:us-east.public.mediator.indiciotech.io" + "did:web:us-east2.public.mediator.indiciotech.io" export const ROOTS_MEDIATOR = "did:peer:2.Ez6LSms555YhFthn1WV8ciDBpZm86hK9tp83WojJUmxPGk1hZ.Vz6MkmdBjMyB4TS5UbbQw54szm8yvMMf1ftGV2sQVYAxaeWhE.SeyJpZCI6Im5ldy1pZCIsInQiOiJkbSIsInMiOiJodHRwczovL21lZGlhdG9yLnJvb3RzaWQuY2xvdWQiLCJhIjpbImRpZGNvbW0vdjIiXX0"