Search code examples
javascriptsubtlecrypto

Error on importing a public key 'SubtleCrypto': The provided value cannot be converted to a sequence


I'm trying to import a public key into crypto.subtle and I got a cryptic (pun intended) error:

Uncaught (in promise) TypeError: Failed to execute 'importKey' on 'SubtleCrypto': The provided value cannot be converted to a sequence.

Here's my key:

jPublicJWK = {
  "crv":"P-256",
  "key_ops":"verify",
  "kty":"EC",
  "x":"QcQI-5wvczyuzU0SWl91tdUWbG5RMYFdNrOCNHen-08",
  "y":"sYVMwVF4ZO2-u0xPMyOXff7VoOQo6kdBv0IeEcnrYno",
  "use":"sig"
}

And here's how I'm importing:

let jwkCryptoKey = await crypto.subtle.importKey(
    "jwk",
    jPublicJWK, 
    {
        name: "ECDSA",
        namedCurve: "P-256",
    },
    true,
    ["verify"]
);

What am I doing wrong?


Solution

  • According to the the rfc, key_ops is an array. "Its value is an array of key operation values." Tricky little sentence to miss.

    Since key_ops is optional, you can delete it all together or put it in an array:

    jPublicJWK = {
        "crv": "P-256",
        "kty": "EC",
        "key_ops": ["verify"],
        "x": "NDeK5mTx7Tse16x4ipl1m6NYDkwLO-YBU_BUqEtOuIM",
        "y": "9Q3XDebaKId0pmrPqVTTR__UnpBn2oxqY8nAlrTw_qU",
        "use": "sig"
    };