Skip to content

Commit

Permalink
use node's webcrypto when available (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
panva authored May 9, 2023
1 parent d2a2f60 commit a373606
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-geckos-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@edge-runtime/primitives': minor
---

use node's webcrypto when available
36 changes: 29 additions & 7 deletions packages/primitives/src/primitives/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @ts-check

const path = require('path')
const nodeCrypto = require('crypto')

function load() {
/** @type {Record<string, any>} */
Expand Down Expand Up @@ -104,13 +105,34 @@ function load() {
WebSocket: fetchImpl.WebSocket,
})

const cryptoImpl = require('./crypto')
Object.assign(context, {
crypto: cryptoImpl.crypto,
Crypto: cryptoImpl.Crypto,
CryptoKey: cryptoImpl.CryptoKey,
SubtleCrypto: cryptoImpl.SubtleCrypto,
})
if (typeof SubtleCrypto !== 'undefined') {
Object.assign(context, {
crypto: globalThis.crypto,
Crypto: globalThis.Crypto,
CryptoKey: globalThis.CryptoKey,
SubtleCrypto: globalThis.SubtleCrypto,
})
// @ts-ignore
} else if (nodeCrypto.webcrypto) {
Object.assign(context, {
// @ts-ignore
crypto: nodeCrypto.webcrypto,
// @ts-ignore
Crypto: nodeCrypto.webcrypto.constructor,
// @ts-ignore
CryptoKey: nodeCrypto.webcrypto.CryptoKey,
// @ts-ignore
SubtleCrypto: nodeCrypto.webcrypto.subtle.constructor,
})
} else {
const cryptoImpl = require('./crypto')
Object.assign(context, {
crypto: cryptoImpl.crypto,
Crypto: cryptoImpl.Crypto,
CryptoKey: cryptoImpl.CryptoKey,
SubtleCrypto: cryptoImpl.SubtleCrypto,
})
}

return context
}
Expand Down
27 changes: 27 additions & 0 deletions packages/vm/tests/integration/crypto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,30 @@ function toHex(buffer: ArrayBuffer) {
.map((b) => b.toString(16).padStart(2, '0'))
.join('')
}

const nodeMajorVersion = parseInt(process.versions.node.split('.')[0])
if (nodeMajorVersion >= 16) {
test('Ed25519', async () => {
const vm = new EdgeVM()

function fn() {
return crypto.subtle.generateKey('Ed25519', false, ['sign', 'verify'])
}

const kp = await vm.evaluate(`(${fn})()`)
expect(kp).toHaveProperty('privateKey')
expect(kp).toHaveProperty('publicKey')
})

test('X25519', async () => {
const vm = new EdgeVM()

function fn() {
return crypto.subtle.generateKey('X25519', false, ['deriveBits', 'deriveKey'])
}

const kp = await vm.evaluate(`(${fn})()`)
expect(kp).toHaveProperty('privateKey')
expect(kp).toHaveProperty('publicKey')
})
}

1 comment on commit a373606

@vercel
Copy link

@vercel vercel bot commented on a373606 May 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

edge-runtime – ./

edge-runtime.vercel.app
edge-runtime.vercel.sh
edge-runtime-git-main.vercel.sh

Please sign in to comment.