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 ?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.