decentralized-identity / universal-resolver

Universal Resolver implementation and drivers.
https://uniresolver.io/
Apache License 2.0
529 stars 233 forks source link

Add JavaScript SDK for the Universal Resolver #28

Open thomas-tran opened 5 years ago

thomas-tran commented 5 years ago

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

OR13 commented 4 years ago

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 commented 4 years ago

@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.

peacekeeper commented 4 years ago

@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.

OR13 commented 4 years ago

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

peacekeeper commented 3 years ago

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 ?

OR13 commented 3 years ago

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

peacekeeper commented 2 years ago

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.