'subtle crypto with ECDSA : Cannot create a key using the specified key usages
I wanted to import an ECDSA private key in Chrome to sign some data, tried yet with crypto.subtle.importKey
: feeded the importKey
with a derivated private key using secp256k1
.
When trying to use the lib, I got stuck with the following error:
Cannot create a key using the specified key usages.
The code:
const browserKey = await crypto.subtle.importKey(
'raw',
derivatedNode.privateKey, // ArrayBuffer
{
name: 'ECDSA',
namedCurve: 'P-256' // Haven't found better fit for secp256k1?
},
false,
['sign']
);
Double-checked my private key, got Uint8Array(32)
which seems to be usually OK with importKey
.
Any help appreciated.
Solution 1:[1]
Web Cryptography API does not support the secp256k1
curve. It will also not support it in the future.
Solution 2:[2]
Web Crypto API doesn't support secp256k1 but you can use two workarounds:
Use a Node.js library to store the private key, and issue from the browser requests to sign some payloads: https://www.npmjs.com/package/ecdh-crypto/v/1.0.5
Use ethers.js library to do it in pure Javascript, but you won't be able to have non-extractable keys, this is good if you're building a browser extension with static bundle files, otherwise you have to trust your own servers not to get compromised: https://docs.ethers.io/v4/api-wallet.html#signing
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Filip Skokan |
Solution 2 | Gregory Magarshak |