w3c-ccg / did-spec

Please see README.md for latest version being developed by W3C DID WG.
https://w3c.github.io/did-core/
Other
124 stars 45 forks source link

publicKeyJwk, publicKeyHex, publicKeyBase64, publicKeyBase58 missing from context. #152

Closed OR13 closed 4 years ago

OR13 commented 5 years ago

Per the spec, https://w3c-ccg.github.io/did-spec/#public-keys

I was expecting to see publicKeyPem, publicKeyJwk, publicKeyHex, publicKeyBase64 to be present here:

https://w3id.org/did/v1, which resolves to https://w3c-ccg.github.io/did-spec/contexts/did-v1.jsonld

This causes errors when signing DID Documents which contain publicKeys with properties missing from the context, for example:

const https = require("https");
const jsonldSignatures = require("jsonld-signatures");

const get = url => {
  return new Promise((resolve, reject) => {
    https
      .get(url, resp => {
        let data = "";
        resp.on("data", chunk => {
          data += chunk;
        });
        resp.on("end", () => {
          resolve(JSON.parse(data));
        });
      })
      .on("error", err => {
        reject(err);
      });
  });
};

const { EcdsaKoblitzSignature2016 } = jsonldSignatures.suites;
const { PublicKeyProofPurpose } = jsonldSignatures.purposes;

(async () => {
  const didDocument = {
    "@context": "https://w3id.org/did/v1",
    id: "did:example:123",
    publicKey: [
      {
        id:
          "did:example:123#kid=1b96181835f46197512e826e1e5c06d2ce45a3518ee9d975938aab899de4c933",
        type: "EcdsaKoblitzSignature2016",
        owner: "did:example:123",
        publicKeyHex:
          "02234be9bcdf041f7530979b8b88b7dc62dd505a75883c8211f3a8250534f96dc0"
      }
    ],
    authentication: [
      {
        publicKey:
          "did:example:123#kid=1b96181835f46197512e826e1e5c06d2ce45a3518ee9d975938aab899de4c933",
        type: "EcdsaKoblitzSignature2016"
      }
    ]
  };

  const didv1 = await get(
    "https://w3c-ccg.github.io/did-spec/contexts/did-v1.jsonld"
  );
  const testLoader = url => {
    // console.log(url);

    if (url === "https://w3id.org/did/v1") {
      return {
        contextUrl: null,
        document: didv1,
        documentUrl: url
      };
    }

    if (
      url ===
      "ecdsa-koblitz-pubkey:02234be9bcdf041f7530979b8b88b7dc62dd505a75883c8211f3a8250534f96dc0"
    ) {
      return {
        contextUrl: null,
        document: {
          "@context": "https://w3id.org/security/v2",
          id:
            "ecdsa-koblitz-pubkey:02234be9bcdf041f7530979b8b88b7dc62dd505a75883c8211f3a8250534f96dc0",
          type: "CryptographicKey",
          owner: "https://example.com/i/alice",
          publicKeyWif: "16agVsYKpbHL4H4e6NSm4yDkpRN4w84E2Q"
        },
        documentUrl: url
      };
    }
  };

  const controller = {
    "@context": "https://w3id.org/security/v1",
    id: "https://example.com/i/alice",
    publicKey: [
      {
        "@context": "https://w3id.org/security/v1",
        id:
          "ecdsa-koblitz-pubkey:02234be9bcdf041f7530979b8b88b7dc62dd505a75883c8211f3a8250534f96dc0",
        type: "CryptographicKey",
        owner: "https://example.com/i/alice",
        publicKeyWif: "16agVsYKpbHL4H4e6NSm4yDkpRN4w84E2Q"
      }
    ],
    assertionMethod: [
      "ecdsa-koblitz-pubkey:02234be9bcdf041f7530979b8b88b7dc62dd505a75883c8211f3a8250534f96dc0"
    ]
  };

  const signed = await jsonldSignatures.sign(didDocument, {
    documentLoader: testLoader,
    suite: new EcdsaKoblitzSignature2016({
      privateKeyWif: "KzVDttSotdDwhfBBiaU4vPKK3yeUmvfLcWbBPHdWy2k3wNYs2oPE",
      creator: `ecdsa-koblitz-pubkey:02234be9bcdf041f7530979b8b88b7dc62dd505a75883c8211f3a8250534f96dc0`
    }),
    purpose: new PublicKeyProofPurpose()
  });
  const result = await jsonldSignatures.verify(signed, {
    documentLoader: testLoader,
    suite: new EcdsaKoblitzSignature2016({
      creator: `ecdsa-koblitz-pubkey:02234be9bcdf041f7530979b8b88b7dc62dd505a75883c8211f3a8250534f96dc0`,
      date: "2017-03-25T22:01:04Z"
    }),
    purpose: new PublicKeyProofPurpose({
      controller
    })
  });

  console.log("verified? ", result);
})();

The property "publicKeyHex" in the input was not defined in the context.

davidlehn commented 5 years ago

Some of the contexts may be in flux at the moment. Here are some that might help:

Not sure where publicKeyHex comes from. I don't think the plan was to put properties for every key type into the main did context, but perhaps general ones should go there.

rhiaro commented 5 years ago

publicKeyBase58 is in the v0.11 of the DID Context.

For any (the others in the list) not defined in the main context, the ld cryptosuite registry (related issue) needs to be updated with the contexts they can be found in.

I can PR against security/v2 and/or the DID context to add them if someone tells me which ones should go in (maybe all of them until they have another home established? maybe that's a bad idea?).

Also worth mentioning the context URL in the DID spec has changed to https://www.w3.org/2019/did/v1 which currently doesn't resolve to anything and won't for a while probably.

gjgd commented 5 years ago

Bumping this issue.

The fact that publicKeyHex is missing from the did spec as a valid publicKey format is preventing us from adding did:elem to the universal resolver

Could we add it to v0.12?

Edit: publicKeyHex is in the examples of the v0.13 spec: https://w3c-ccg.github.io/did-spec/

dmitrizagidulin commented 5 years ago

@rhiaro @gjgd Yeah, we'll need to add publicKeyHex to the DID context

jandrieu commented 4 years ago

Closing as this has been moved to the DIDWG repo.