decentralized-identity / did-jwt

Create and verify DID verifiable JWT's in Javascript
Apache License 2.0
331 stars 70 forks source link

[BUG] Error: no_suitable_keys: DID document for did:polygon:testnet:0x00 does not have public keys suitable for ES256K with assertionMethod purpose #286

Closed cakesoft-yash closed 1 year ago

cakesoft-yash commented 1 year ago

Getting this issue while verifyinig the jwt let resolver = await new Resolver({ ...polygonResolver.getResolver() }); let verificationResponse = await verifyJWT(jwt, { resolver, audience: did, proofPurpose: 'assertionMethod' })

mirceanis commented 1 year ago

Hi, what is the response you get from the resolver when you resolve the issuer of the JWT?

cakesoft-yash commented 1 year ago

Hi, its like this Resolver { registry: { polygon: [Function: resolve] }, cache: [Function: noCache] }

cakesoft-yash commented 1 year ago

When I resolve using this I get the response. await resolver.resolve(did1); { didDocument: [ '{"@context":"https://w3id.org/did/v1","id":"did:polygon:testnet:0xBdB838000c0E8e6Af97b2233c9B4F89ace121c29","verificationMethod":[{"id":"did:polygon:testnet:0xBdB838000c0E8e6Af97b2233c9B4F89ace121c29#key-1","type":"EcdsaSecp256k1VerificationKey2019","controller":"did:polygon:testnet:0xBdB838000c0E8e6Af97b2233c9B4F89ace121c29","publicKeyBase58":"7Lnm1ZmVWLRXmqPF4gnaS4i761LjsWFz2o4686CNK5ZpDNCGmzjSvpi57cnF4xS4QwG3TJoURENSfcFjPgSzskwv33vFv"}],"authentication":["did:polygon:testnet:0xBdB838000c0E8e6Af97b2233c9B4F89ace121c29",{"id":"did:polygon:testnet:0xBdB838000c0E8e6Af97b2233c9B4F89ace121c29#key-1","type":"EcdsaSecp256k1VerificationKey2019","controller":"did:polygon:testnet:0xBdB838000c0E8e6Af97b2233c9B4F89ace121c29","publicKeyBase58":"7Lnm1ZmVWLRXmqPF4gnaS4i761LjsWFz2o4686CNK5ZpDNCGmzjSvpi57cnF4xS4QwG3TJoURENSfcFjPgSzskwv33vFv"}],"assertionMethod":["did:polygon:testnet:0xBdB838000c0E8e6Af97b2233c9B4F89ace121c29",{"id":"did:polygon:testnet:0xBdB838000c0E8e6Af97b2233c9B4F89ace121c29#key-1","type":"EcdsaSecp256k1VerificationKey2019","controller":"did:polygon:testnet:0xBdB838000c0E8e6Af97b2233c9B4F89ace121c29","publicKeyBase58":"7Lnm1ZmVWLRXmqPF4gnaS4i761LjsWFz2o4686CNK5ZpDNCGmzjSvpi57cnF4xS4QwG3TJoURENSfcFjPgSzskwv33vFv"}]}' ], didDocumentMetadata: {}, didResolutionMetadata: { contentType: 'application/did+ld+json' } }

mirceanis commented 1 year ago

Ok, I think I see what's going on. It looks like the resolver is not returning a DID document, but what seems to be an array of JSON string encodings of DIDDocuments (in this case with one element). This is a bug in the polygonResolver implementation, but the fix should be relatively easy.

If this is the library you are using as the resolver implementation, then the fix should be something like this inserted after (this statement](https://github.com/ayanworks/polygon-did-resolver/blob/edb42a1cee42961b1977d6a67ccc00317a9f55d2/src/polygon-did-resolver.ts#L59):

try {
  if (Array.isArray(didDocument) && typeof didDocument[0] === 'string') {
    didDocument = JSON.parse(didDocument[0])
  } else if (typeof didDocument === 'string') {
    didDocument = JSON.parse(didDocument)
  }
} catch {
  throw new Error('illegal_argument: the DID document is not a well formatted JSON string')
}
cakesoft-yash commented 1 year ago

So you mean to say it should only return the did document in object not didDocumentMetadata and didResolutionMetadata. Or just change the didDocuement from string to object remaining object will be the same.

mirceanis commented 1 year ago

The full response object must also contain the didDocumentMetadata and the didResolutionMetadata properties. It's just that the didDocument property must be an Object, not a string or array of strings as it seems to be now.

The example you gave earlier should be formatted like this:

{
  "didDocumentMetadata": {},
  "didResolutionMetadata": {
    "contentType": "application/did+ld+json"
  },
  "didDocument": {
    "@context": "https://w3id.org/did/v1",
    "id": "did:polygon:testnet:0xBdB838000c0E8e6Af97b2233c9B4F89ace121c29",
    "verificationMethod": [
      {
        "id": "did:polygon:testnet:0xBdB838000c0E8e6Af97b2233c9B4F89ace121c29#key-1",
        "type": "EcdsaSecp256k1VerificationKey2019",
        "controller": "did:polygon:testnet:0xBdB838000c0E8e6Af97b2233c9B4F89ace121c29",
        "publicKeyBase58": "7Lnm1ZmVWLRXmqPF4gnaS4i761LjsWFz2o4686CNK5ZpDNCGmzjSvpi57cnF4xS4QwG3TJoURENSfcFjPgSzskwv33vFv"
      }
    ],
    "authentication": [
      "did:polygon:testnet:0xBdB838000c0E8e6Af97b2233c9B4F89ace121c29",
      {
        "id": "did:polygon:testnet:0xBdB838000c0E8e6Af97b2233c9B4F89ace121c29#key-1",
        "type": "EcdsaSecp256k1VerificationKey2019",
        "controller": "did:polygon:testnet:0xBdB838000c0E8e6Af97b2233c9B4F89ace121c29",
        "publicKeyBase58": "7Lnm1ZmVWLRXmqPF4gnaS4i761LjsWFz2o4686CNK5ZpDNCGmzjSvpi57cnF4xS4QwG3TJoURENSfcFjPgSzskwv33vFv"
      }
    ],
    "assertionMethod": [
      "did:polygon:testnet:0xBdB838000c0E8e6Af97b2233c9B4F89ace121c29",
      {
        "id": "did:polygon:testnet:0xBdB838000c0E8e6Af97b2233c9B4F89ace121c29#key-1",
        "type": "EcdsaSecp256k1VerificationKey2019",
        "controller": "did:polygon:testnet:0xBdB838000c0E8e6Af97b2233c9B4F89ace121c29",
        "publicKeyBase58": "7Lnm1ZmVWLRXmqPF4gnaS4i761LjsWFz2o4686CNK5ZpDNCGmzjSvpi57cnF4xS4QwG3TJoURENSfcFjPgSzskwv33vFv"
      }
    ]
  }
}
cakesoft-yash commented 1 year ago

Hi, So above issue is resolve now but getting this new error now Error: invalid_signature: Signature invalid for JWT

cakesoft-yash commented 1 year ago

Hi, The ethereumAddress key must be added to the verification method to resolve the above issue. Thanks for the help @mirceanis

mirceanis commented 1 year ago

Hi, So above issue is resolve now but getting this new error now Error: invalid_signature: Signature invalid for JWT

Do you have a sample JWT that fails verification with that error?

cakesoft-yash commented 1 year ago

It's working now I was able to fix the issue from the resolver side. Do you still need the sample jwt?

mirceanis commented 1 year ago

It's working now I was able to fix the issue from the resolver side. Do you still need the sample jwt?

If it is working, then the sample is not needed anymore. Feel free to close the issue.

But you are not the first person to report this problem with the polygon resolver from ayanworks. Is there a public fix that other people can use?

cakesoft-yash commented 1 year ago

Okay I will close the issue. Thank you for the help. About the ayanworks issue cannot say because how everyone use that package. If they are using that package as a resolver with did-jwt package then the solution can be useful.