Skip to content

Commit

Permalink
fix: browser compatible webcrypto import
Browse files Browse the repository at this point in the history
  • Loading branch information
adiwajshing committed Jan 8, 2025
1 parent 31cb348 commit bde72e1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion js/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { ChaCha20Poly1305 } from '@stablelib/chacha20poly1305'
import { subtle } from 'crypto'
import { AlgorithmConfig, EncryptionAlgorithm } from './types'
import { bitsToUint8Array, bitsToUintArray, toUint8Array, toUintArray, uint8ArrayToBits, uintArrayToBits } from './utils'
import { webcrypto } from './webcrypto'

const { subtle } = webcrypto

// commit hash for this repo
export const GIT_COMMIT_HASH = 'c9df4f9eef53566536a0afec5444b9544630fa0c'
Expand Down
14 changes: 14 additions & 0 deletions js/src/webcrypto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const webcrypto = (() => {
// if we're in node, we need to use
// webcrypto provided by the crypto module
if(typeof window !== 'undefined') {
return window.crypto
}

if(typeof self !== 'undefined' && self.crypto) {
return self.crypto
}

const { webcrypto } = require('crypto')
return webcrypto as Crypto
})()

0 comments on commit bde72e1

Please sign in to comment.