SyntaxError: Cannot create a key using the specified key usages

In my real task, I pass a private key from one Web page to another in URL string (yes, I know that this is not secure).
Here is a simplified code that prints in browser console an error that I don't understand:
SyntaxError: Cannot create a key using the specified key usages.
<html>
<head>
<script>
async function main() {
const pair = await window.crypto.subtle.generateKey(
{name: 'ECDSA', namedCurve: 'P-256'/*secp256k1*/}, true, ['sign', 'verify']
);
const frontendTweakPrivKey = await window.crypto.subtle.exportKey("pkcs8", pair.privateKey);
const privKeyUsable = await window.crypto.subtle.importKey(
"pkcs8", frontendTweakPrivKey, {name: 'ECDSA', namedCurve: 'P-256'/*secp256k1*/}, false, ["sign", "verify"]
);
}
(async () => main())();
</script>
</head>
</html>
(In my real code) the key is generated on the first Web page, encoded base64, and then decoded on another Web page it signs a message.
What does the error mean and how to fix it?
Remark: The key is generated on the first page and passed to the second one in order to "confirm" that the first page and the second page were browsed by the same user, not a hacker that simulates the user on the second page.
Answer
The error was that ECDSA private keys only sign (verification is done by public keys).
So, need just to remove verify
.
Enjoyed this question?
Check out more content on our blog or follow us on social media.
Browse more questions