spruceid / didkit

A cross-platform toolkit for decentralized identity.
https://www.spruceid.dev/didkit/didkit
Apache License 2.0
274 stars 76 forks source link

Could not add additional field into "credentialSubject" when issuing credential #385

Closed Lisbet111111 closed 6 months ago

Lisbet111111 commented 7 months ago

Test code(refer to lib/java/test/com/spruceid/DIDKitTest.java) as below:

String jwk = DIDKit.generateEd25519Key();
String method = "key";
String did = DIDKit.keyToDID(method, jwk);
String verificationMethod = DIDKit.keyToVerificationMethod(method, jwk);
String hash = "9c71f81d0375bca3ebb3b2a234f8ba27af968b8f362725a40c4a49ea1de26b56";
String credential = "{"
  + "   \"@context\": \"https://www.w3.org/2018/credentials/v1\","
  + "   \"id\": \"http://example.org/credentials/3731\","
  + "   \"type\": [\"VerifiableCredential\"],"
  + "   \"issuer\": \"" + did + "\","
  + "   \"issuanceDate\": \"2020-08-19T21:41:50Z\","
  + "   \"credentialSubject\": {"
  + "       \"id\": \"did:example:d23dd687a7dc6787646f2eb98d0\","
  + "       \"hash\": \"" + hash + "\""
  + "   }"
  + "}";

String vcOptions = "{"
  + "  \"proofPurpose\": \"assertionMethod\","
  + "  \"verificationMethod\": \"" + verificationMethod + "\""
  + "}";
String vc = DIDKit.issueCredential(credential, vcOptions, jwk);

=============================================================

Expect to add additional field "hash" and some other custom fields into "credentialSubject", but met below exception, QA.1: could you have a look about how to fix this exception and issue credential successfully? and confirmed it's ok to issue credential after delete the "hash" field from "credentialSubject". QA.2: Above exception message can NOT be found in the code from "https://github.com/spruceid/ssi", why?

DIDKitException: Expansion failed: Expansion failed: Key expansion failed


Thanks a lot!!

sbihel commented 6 months ago

All fields in a Verifiable Credential must be defined in the context. Here's an (untested) example:

{
  "@context": [
    "https://www.w3.org/2018/credentials/v1",
    {
      "hash": "https://example.com#hash"
    }
  ]
}