1

I have a public key string as follows let pk_str = "public key strig here" and I am using the library jose to verify a JWS

            (async() => {
                const decoder = new TextDecoder();
                const jws = vc_proof_value;
                const { payload, protectedHeader } = await compactVerify(jws, pk_str);
    
                console.log(protectedHeader)
                console.log(decoder.decode(payload))
              })();

I am getting the following error when trying to run the script

(node:75986) UnhandledPromiseRejectionWarning: TypeError: Key must be one of type KeyObject, CryptoKey, or Uint8Array. Received type string

Is there a way to construct the key ?

not 0x12
  • 19,360
  • 22
  • 67
  • 133

2 Answers2

1

In NodeJS (I refer to NodeJS, since you have tagged this), the public key is passed as KeyObject wich is created with crypto.createPublicKey(). You didn't say much about the key, presumably it's PEM encoded (since it's a string). In this case, you simply have to pass the PEM encoded key:

var key = crypto.createPublicKey(pk_str);

If in the compactVerify() call pk_str is replaced by key, verification works.

In addition to PEM keys (default), JWK and DER keys (X.509/SPKI or PKCS#1) are also supported.

Topaco
  • 40,594
  • 4
  • 35
  • 62
1

Here is the documentation for the key argument of all applicable functions.