D
Deno

help

Abusing prototype CryptoKey in `node:crypto`

Aabi6/11/2023
Is there any way that I can set the prototype of an object to specifically CryptoKey as defined by node:crypto? I am trying to get around some nasty instanceof check in a third-party library which is getting in the way.
import * as krypto from "node:crypto";
Object.setPrototypeOf(foo, krypto.webcrypto.CryptoKey.prototype);
import * as krypto from "node:crypto";
Object.setPrototypeOf(foo, krypto.webcrypto.CryptoKey.prototype);
The above gives me this error:
error: Uncaught TypeError: Cannot read properties of undefined (reading 'prototype')
error: Uncaught TypeError: Cannot read properties of undefined (reading 'prototype')
I also tried just ..., krypto.webcrypto.CryptoKey) but it's still undefined obviously and i'm not quite sure why I'm basically trying to get the type as found in [npm]/@types/node/18.11.18/crypto.d.ts:
interface Crypto {
// ...
CryptoKey: CryptoKeyConstructor;
}

// This constructor throws ILLEGAL_CONSTRUCTOR so it should not be newable.
interface CryptoKeyConstructor {
/** Illegal constructor */
(_: { readonly _: unique symbol }): never; // Allows instanceof to work but not be callable by the user.
readonly length: 0;
readonly name: 'CryptoKey';
readonly prototype: CryptoKey;
}
/**
* @since v15.0.0
*/
interface CryptoKey {

interface Crypto {
// ...
CryptoKey: CryptoKeyConstructor;
}

// This constructor throws ILLEGAL_CONSTRUCTOR so it should not be newable.
interface CryptoKeyConstructor {
/** Illegal constructor */
(_: { readonly _: unique symbol }): never; // Allows instanceof to work but not be callable by the user.
readonly length: 0;
readonly name: 'CryptoKey';
readonly prototype: CryptoKey;
}
/**
* @since v15.0.0
*/
interface CryptoKey {

Actually, looking back at how I even got here makes me question why I'm even doing it, because I'm calling krypto.webcrypto.subtle.importKey and then the return value of that should already be krypto.webcrypto.CryptoKey but this third-party library code does instanceof CryptoKey and says it's "a different" CryptoKey And so does krypto.KeyObject.from:
error: Uncaught TypeError: The "key" argument must be an instance of CryptoKey. Received an instance of CryptoKey
const keyObject = krypto.KeyObject.from(privateCryptoKey);
error: Uncaught TypeError: The "key" argument must be an instance of CryptoKey. Received an instance of CryptoKey
const keyObject = krypto.KeyObject.from(privateCryptoKey);
Mmmastrac6/12/2023
In this case, I suspect we're not actually exporting the CryptoKey prototype from the node:crypto polyfill which is probably a bug. We do export CryptoKey in the global namespace -- does this work?
Object.setPrototypeOf(foo, CryptoKey.prototype);
Object.setPrototypeOf(foo, CryptoKey.prototype);
Aabi6/13/2023
oof, sorry, didn't see your message until now! the whole problem is no more tbh, not exactly sure why, but if it pops back up, i will make sure to give that a try. (but i'm like 90% sure i tried it before and it failed!)

Looking for more? Join the community!

Recommended Posts
Uncaught (in promise) TypeError: cannot read headers: request closedWhat is this? How to fix it?Async OP that returns value instead of promiseIn `deno_runtime` is possible to have async op that returns the resolved value instead of promise? KFresh Logging MiddlewareHow would you go about writing a duration logger? Using 2 middlewares, I assumed they would behave VS-Code auto import local modulesHey is there a way to enable some sort of auto import for local modules/files in VS-Code? Currently In memory Deno KvHi there, I am exploring with [Deno Kv](https://deno.com/deploy/docs/kv) and it looks great! But myDeno Language Server Issue - Attempting to Use KV in Existing ProjectHi! I'm attempting to create a branch of an existing project (utilizing Fresh) and add KV as an expeTwitter APIIs there a easy way to fetch Tweets using Twitter API or something else? (since they put their API cIn browser video chat client & serverHello! How might people go about architecting an in-browser video chat client & server, specificallynpm:pusher working but http://esm.sh/pusher notAny ideas? deno run -A test.js //test.js import Pusher from "pusher" // const Pusher = require('pFile Permissions on Deno.FileInfoHello. As a programming exercise I'm trying to convert a Decimal number into Octal and that Octal reDeno Fresh - Different POST handlers for different <form>s ?what's the idiomatic way of handling different POST requests on the same page?Deno.open() questionDoes, when the process terminate, deno close the files if they were opened with `Deno.open()`?Can't figure out complicated type incompatibilityAnyone know what's going on here? `T` should have resolved to `[number, number, number]` https://wwManually edit existing KV tableHello! another day, another question. Is there a way to manually edit KV database to modify an entr