universal-resolver icon indicating copy to clipboard operation
universal-resolver copied to clipboard

Add JavaScript SDK for the Universal Resolver

Open thomas-tran opened this issue 6 years ago • 7 comments

As noted in the comments, there are SDKs for python and java here

thomas-tran avatar Dec 06 '18 09:12 thomas-tran

Can you be more specific? This repo contains docker examples, are you looking for a node-js sdk for the universal resolver http interface?

OR13 avatar Aug 12 '19 16:08 OR13

@peacekeeper I'd be happy to provide a js sdk for the public http interface if that would help resolve this issue, its something I would find very useful generally.

OR13 avatar Aug 12 '19 16:08 OR13

@OR13 yes that would be great! This could be added here in the directory structure: https://github.com/decentralized-identity/universal-resolver/tree/master/resolver

The idea has always been for DIF to develop DID Resolvers and clients in multiple languages.

peacekeeper avatar Aug 12 '19 18:08 peacekeeper

I will open a PR, it might be rough, but it will work in both the browser and nodejs... it will be based on: https://github.com/transmute-industries/PROPOSAL-OpenPgpSignature2019/blob/master/src/universalResolver.js

OR13 avatar Aug 14 '19 17:08 OR13

We revisited this on the 23 Jun 2021 Universal Resolver call.

Even though there hasn't been any activity on this issue for a while, this still sounds useful; is anyone aware of a JS library (NPM package?) that resolves DIDs via the Universal Resolver HTTPS interface? @thomas-tran @OR13 ?

peacekeeper avatar Jun 23 '21 17:06 peacekeeper

I think this would be helpful, particularly to help test resolution and dereferencing.

We would need code that maps a DID_URL and RESOLUTION_OPTIONS to an http request...

We've avoided this so far, by just using JSON-LD Document Loaders....

You can consider the use of a document loader to be equivalent to RESOLUTION_OPTIONS defaulted to application/did+ld+json:

import { contexts } from "./contexts";

import axios from "axios";

import * as ed25519 from "@transmute/did-key-ed25519";
import * as bls12381 from "@transmute/did-key-bls12381";

export const documentLoader = async (iri) => {
  if (contexts[iri]) {
    return {
      documentUrl: iri,
      document: contexts[iri],
    };
  }

  if (iri.includes("did:web:")) {
    let url = `https://did-web.web.app/api/v1/identifiers/${iri}`;
    const resp = await axios.get(url);
    return {
      documentUrl: iri,
      document: resp.data,
    };
  }

  if (iri.startsWith("did:key:z6M")) {
    const { didDocument } = await ed25519.driver.resolve(iri, {
      accept: "application/did+ld+json",
    });
    return {
      documentUrl: iri,
      document: didDocument,
    };
  }

  if (iri.startsWith("did:key:zUC")) {
    const { didDocument } = await bls12381.driver.resolve(iri, {
      accept: "application/did+ld+json",
    });
    return {
      documentUrl: iri,
      document: didDocument,
    };
  }

  if (
    iri.startsWith(
      "https://w3c-ccg.github.io/vc-http-api/fixtures/revocationList.json"
    )
  ) {
    const resp = await axios.get(iri);

    return {
      documentUrl: iri,
      document: resp.data,
    };
  }

  if (iri.startsWith("did:trustbloc")) {
    const url = `https://resolver.sandbox.trustbloc.dev/1.0/identifiers/${iri}`;
    const resp = await axios.get(url);
    return {
      documentUrl: iri,
      document: resp.data.didDocument,
    };
  }

  if (iri.startsWith("https://issuer.sandbox.trustbloc.dev/status/1")) {
    const resp = await axios.get(iri);
    return {
      documentUrl: iri,
      document: resp.data,
    };
  }

  if (iri.includes("did:")) {
    const url = `https://dev.uniresolver.io/1.0/identifiers/${iri}`;
    const resp = await axios.get(url);
    return {
      documentUrl: iri,
      document: resp.data.didDocument,
    };
  }

  try {
    const resp = await axios({
      method: "get",
      url: iri,
      headers: {
        // Authorization: `Bearer ${token}`,
        accept: "application/json",
      },
    });
    return {
      documentUrl: iri,
      document: resp.data,
    };
  } catch (e) {
    console.error(e);
    // error will be thrown
  }

  console.error("Unsupporteed iri " + iri);
  throw new Error("Unsupporteed iri " + iri);
};

There remain questions about whether document loaders should do "resolution" or "dereferencing" which would impact the return type that the universal resolver would send, or would require "framing" on the client...

See https://github.com/digitalbazaar/jsonld-signatures/issues/141

OR13 avatar Jun 23 '21 18:06 OR13

We discussed this on the 29 Sep 2021 Universal Resolver work item call. A JavaScript client DID resolver would definitely be useful, and it is also clearly useful for JSON-LD document loaders. Not sure however if it makes sense to keep this issue here open.

If anyone is interested in working on such a JavaScript library as a standalone module, then a new repository and work item should be created for this, and this issue here should probably be closed.

peacekeeper avatar Sep 29 '21 13:09 peacekeeper