digitalbazaar / jsonld.js

A JSON-LD Processor and API implementation in JavaScript
https://json-ld.org/
Other
1.66k stars 195 forks source link

isBlankNode only checks `@id` and not `id` #413

Closed tplooker closed 3 years ago

tplooker commented 3 years ago

isBlankNode currently doesn't check the node identifier if the id key is being used instead of @id, happy to submit a PR but just checking there is no reason for this function to not being checking id.

davidlehn commented 3 years ago

That shouldn't be needed. Do you have a test case where this is an issue?

tplooker commented 3 years ago

@davidlehn yes I'm attempting to use relabelBlankNodes in jsonld-signatures-bbs with a custom node issuer prior to a framing operation to get around graph isomophism (essentially I need the same node identifiers to persist into the framed result to later prove their integrity to the original signature). Currently when I use relabelBlankNodes with an input graph that contains nodes using the id like

{
      "@context": "https://w3id.org/security/v2",
      "id": "https://issuer.oidp.uscis.gov/credentials/83627465",
      "type": [
        "https://www.w3.org/2018/credentials#VerifiableCredential",
        "https://w3id.org/citizenship#PermanentResidentCard"
      ],
      "http://schema.org/description": "Government of Example Permanent Resident Card.",
      "http://schema.org/identifier": "83627465",
      "http://schema.org/name": "Permanent Resident Card",
      "https://www.w3.org/2018/credentials#credentialSubject": {
        "id": "did:example:b34ca6cd37bbf23",
        "type": [
          "https://w3id.org/citizenship#PermanentResident",
          "http://schema.org/Person"
        ],
        "http://schema.org/birthDate": {
          "type": "xsd:dateTime",
          "@value": "1958-07-17"
        },
        "http://schema.org/familyName": "SMITH",
        "http://schema.org/gender": "Male",
        "http://schema.org/givenName": "JOHN",
        "http://schema.org/image": {
          "id": "data:image/png;base64,iVBORw0KGgokJggg=="
        },
        "https://w3id.org/citizenship#birthCountry": "Bahamas",
        "https://w3id.org/citizenship#commuterClassification": "C1",
        "https://w3id.org/citizenship#lprCategory": "C09",
        "https://w3id.org/citizenship#lprNumber": "999-999-999",
        "https://w3id.org/citizenship#residentSince": {
          "type": "xsd:dateTime",
          "@value": "2015-01-01"
        }
      },
      "https://www.w3.org/2018/credentials#expirationDate": {
        "type": "xsd:dateTime",
        "@value": "2029-12-03T12:19:52Z"
      },
      "https://www.w3.org/2018/credentials#issuanceDate": {
        "type": "xsd:dateTime",
        "@value": "2019-12-03T12:19:52Z"
      },
      "https://www.w3.org/2018/credentials#issuer": {
        "id": "did:example:489398593"
      }
    }

I get the following

{
      "@context": "https://w3id.org/security/v2",
      "id": "https://issuer.oidp.uscis.gov/credentials/83627465",
      "type": [
        "https://www.w3.org/2018/credentials#VerifiableCredential",
        "https://w3id.org/citizenship#PermanentResidentCard"
      ],
      "http://schema.org/description": "Government of Example Permanent Resident Card.",
      "http://schema.org/identifier": "83627465",
      "http://schema.org/name": "Permanent Resident Card",
      "https://www.w3.org/2018/credentials#credentialSubject": {
        "id": "did:example:b34ca6cd37bbf23",
        "type": [
          "https://w3id.org/citizenship#PermanentResident",
          "http://schema.org/Person"
        ],
        "http://schema.org/birthDate": {
          "type": "xsd:dateTime",
          "@value": "1958-07-17"
        },
        "http://schema.org/familyName": "SMITH",
        "http://schema.org/gender": "Male",
        "http://schema.org/givenName": "JOHN",
        "http://schema.org/image": {
          "id": "data:image/png;base64,iVBORw0KGgokJggg==",
          "@id": "urn:bnid:2"
        },
        "https://w3id.org/citizenship#birthCountry": "Bahamas",
        "https://w3id.org/citizenship#commuterClassification": "C1",
        "https://w3id.org/citizenship#lprCategory": "C09",
        "https://w3id.org/citizenship#lprNumber": "999-999-999",
        "https://w3id.org/citizenship#residentSince": {
          "type": "xsd:dateTime",
          "@value": "2015-01-01"
        },
        "@id": "urn:bnid:1"
      },
      "https://www.w3.org/2018/credentials#expirationDate": {
        "type": "xsd:dateTime",
        "@value": "2029-12-03T12:19:52Z"
      },
      "https://www.w3.org/2018/credentials#issuanceDate": {
        "type": "xsd:dateTime",
        "@value": "2019-12-03T12:19:52Z"
      },
      "https://www.w3.org/2018/credentials#issuer": {
        "id": "did:example:489398593",
        "@id": "urn:bnid:3"
      },
      "@id": "urn:bnid:0"
    }

Which then causes a JSON-LD syntax error later on for terms like https://www.w3.org/2018/credentials#issuer where both @id and id are defined.

tplooker commented 3 years ago

@davidlehn any update on this? Im happy to submit a PR with a fix

dlongley commented 3 years ago

@tplooker,

relabelBlankNodes expects to operate on expanded form JSON-LD. Can you just ensure your JSON-LD is in that form before running it (call expand)? I think if you want to do something on compact form based on some presumed @context aliases it would need to be its own custom utility function. It doesn't seem safe (or performant) to, for example, add an option for aliases.

tplooker commented 3 years ago

Ok thanks @dlongley that does clarify will try that