Skip to content

Commit

Permalink
Merge pull request #72 from frostyfrog/feat/did-web
Browse files Browse the repository at this point in the history
Feature: Add did:web support
  • Loading branch information
dbluhm authored Jun 14, 2024
2 parents 198658c + 91b87c1 commit f5383e1
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const DEFAULT_MEDIATOR =
"did:peer:2.Ez6LStkZg14oG5LCxja3RhotWB7m94afER4EiBLhYpUSokbyR.Vz6MkgSYBM63iHNeiT2VSQu7bbtXhGYCQrPJ8uEGurbfGbbgE.SW3sidCI6ImRtIiwicyI6Imh0dHBzOi8vdXMtZWFzdC5wdWJsaWMubWVkaWF0b3IuaW5kaWNpb3RlY2guaW8vbWVzc2FnZSIsInIiOltdLCJhIjpbImRpZGNvbW0vdjIiLCJkaWRjb21tL2FpcDI7ZW52PXJmYzE5Il19LHsidCI6ImRtIiwicyI6IndzczovL3dzLnVzLWVhc3QucHVibGljLm1lZGlhdG9yLmluZGljaW90ZWNoLmlvL3dzIiwiciI6W10sImEiOlsiZGlkY29tbS92MiIsImRpZGNvbW0vYWlwMjtlbnY9cmZjMTkiXX1d"
"did:web:us-east2.public.mediator.indiciotech.io"

export const ROOTS_MEDIATOR =
"did:peer:2.Ez6LSms555YhFthn1WV8ciDBpZm86hK9tp83WojJUmxPGk1hZ.Vz6MkmdBjMyB4TS5UbbQw54szm8yvMMf1ftGV2sQVYAxaeWhE.SeyJpZCI6Im5ldy1pZCIsInQiOiJkbSIsInMiOiJodHRwczovL21lZGlhdG9yLnJvb3RzaWQuY2xvdWQiLCJhIjpbImRpZGNvbW0vdjIiXX0"
Expand Down
96 changes: 90 additions & 6 deletions src/lib/didcomm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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)

Expand Down Expand Up @@ -98,6 +103,85 @@ export class DIDPeerResolver implements DIDResolver {
}
}

var did_web_cache: Record<DID, any> = {};

export class DIDWebResolver implements DIDResolver {
async resolve(did: DID): Promise<DIDDoc | null> {
if(did in did_web_cache)
return did_web_cache[did];

// Remove did:web: from the start
var path = did.slice(8);

// 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);
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<string>) => {
if(value.startsWith("#"))
arr[index] = did + value
});
doc["authentication"].forEach((value: string, index: number, arr: Array<string>) => {
if(value.startsWith("#"))
arr[index] = did + value
});
doc["service"] = doc["service"].filter((s: any) => s.type == "DIDCommMessaging");
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<DIDDoc | null> {
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
}
Expand Down Expand Up @@ -211,11 +295,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()
}

Expand Down
13 changes: 5 additions & 8 deletions src/lib/peer2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export default class DIDPeer {
id: did,
}
let serviceIndex = 0;
let keyIndex = 1;

elements.forEach(element => {
const purposeCode = element.charAt(0)
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -236,6 +231,8 @@ 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)) {
doc.service = [];
Expand Down

0 comments on commit f5383e1

Please sign in to comment.