transmute-industries / verifiable-data

Open Source Decentralized Identifiers and Verifiable Credentials Infrastructure and Tooling
https://transmute-industries.github.io/verifiable-data/smoke-test-react/
Apache License 2.0
51 stars 21 forks source link

[packages: vc.js + bbs-bls12381-signature-2020] TypeError: Cannot read properties of undefined (reading 'replace') #196

Closed lukasz8888 closed 1 year ago

lukasz8888 commented 2 years ago

When you try to create a sign (verifiable) credential with Bls12381G2KeyPair / BbsBlsSignature2020 (rather than Ed25519VerificationKey2018 + Ed25519Signature2018) then it fails during verification process. I needed to use BbsBlsSignature2020 in my case as it allows deriving proofs and do selective disclosure in general.

In ProofSet.ts, line 143 tries to do const matchFound = s.type.replace("sec:", "") === proof.type; while s.type is undefined.

Steps to reproduce:

  const key = await Bls12381G2KeyPair.generate({
    secureRandom: () => {
      return Buffer.from('4e61bc1918ea6a47ae3307331be7798196a1a8e7cfe4b6e8f7c9a5f36017d929', 'hex');
    }
  });

  const exportedKey = await key.export({ type: 'Bls12381G2Key2020' });

  const getDidDocument = (iri) => {
    return {
      documentUrl: iri,
      document: {
        '@context': ['https://www.w3.org/ns/did/v1', 'https://w3id.org/security/suites/bls12381-2020/v1'],
        id: key.controller,
        assertionMethod: [
          {
            id: exportedKey.id,
            type: exportedKey.type,
            controller: exportedKey.controller,
            publicKeyBase58: exportedKey.publicKeyBase58
          }
        ]
      }
    };
  };

  const result = await verifiable.credential.create({
    credential: {
      '@context': [
        'https://www.w3.org/2018/credentials/v1',
        'https://w3id.org/security/suites/bls12381-2020/v1',
        {
          alsoKnownAs: 'https://www.w3.org/ns/activitystreams#alsoKnownAs'
        }
      ],
      id: 'http://example.edu/credentials/3732',
      type: ['VerifiableCredential'],
      issuer: {
        id: key.controller
      },
      issuanceDate: '2010-01-01T19:23:24Z',
      credentialSubject: {
        alsoKnownAs: 'did:example:ebfeb1f712ebc6f1c276e12ec21'
      }
    },
    format: ['vc'],
    documentLoader: mockDocumentLoader,
    suite: new BbsBlsSignature2020({
      key
    })
  });

  const result2 = await verifiable.credential.verify({
    credential: result.items[0],
    format: ['vc'],
    documentLoader: (iri) => {
      if (iri.startsWith(key.controller)) {
        return getDidDocument(iri);
      }
      return mockDocumentLoader(iri);
    },
    suite: [new BbsBlsSignature2020()]
  });

Then the result2 variable contains the following object:

{
  verified: false,
  error: VerificationError: Verification error(s).
      at /test-project/node_modules/@transmute/linked-data-proof/dist/linked-data-proof.cjs.development.js:912:26
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at _testBls (/test-project/transmute.js:322:19)
      at /test-project/transmute.js:144:3 {
    errors: [
      TypeError: Cannot read properties of undefined (reading 'replace')
          at map (/test-project/node_modules/@transmute/linked-data-proof/src/ProofSet.ts:146:15)
          at Array.map (<anonymous>)
          at /test-project/node_modules/@transmute/linked-data-proof/src/ProofSet.ts:135:17
          at processTicksAndRejections (node:internal/process/task_queues:96:5)
          at _testBls (/test-project/transmute.js:322:19)
          at /test-project/transmute.js:144:3
    ]
  }
}

Interestingly, when I use json-ld package to verify result1 I can successfully verify it:

  const jsigs = require('jsonld-signatures');
  const verifiedResult1 = await jsigs.verify(result.items[0], {
    suite: [new BbsBlsSignature2020()],
    purpose: new jsigs.purposes.AssertionProofPurpose(),
    documentLoader: (iri) => {
      if (iri.startsWith(key.controller)) {
        return getDidDocument(iri);
      }
      return mockDocumentLoader(iri);
    }
  });

In that case the verifiedResult variable is:

{
  verified: true,
  results: [
    {
      proof: [Object],
      verified: true,
      verificationMethod: [Object],
      error: undefined,
      purposeResult: [Object]
    }
  ]
}
OR13 commented 1 year ago

Thanks for reporting this, we are waiting for https://github.com/w3c/vc-di-bbs to publish a new version