Skip to content

Commit e6e4e08

Browse files
committed
fix crypto shim
1 parent 48f57b1 commit e6e4e08

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

shim.js

+19-10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ if (typeof localStorage !== 'undefined') {
2222
}
2323

2424
if (require('./package.json').dependencies['react-native-crypto']) {
25+
// important that this comes before require('crypto')
2526
const algos = require('browserify-sign/algos')
2627
if (!algos.sha256) {
2728
algos.sha256 = {
@@ -31,17 +32,25 @@ if (require('./package.json').dependencies['react-native-crypto']) {
3132
}
3233
}
3334

34-
const randomBytes = require('react-native-randombytes').randomBytes
35-
35+
let crypto
3636
if (typeof window === 'object') {
37-
const wCrypto = window.crypto = window.crypto || {}
38-
if (!wCrypto.getRandomValues) {
39-
wCrypto.getRandomValues = function getRandomValues (arr) {
40-
const bytes = randomBytes(arr.length)
41-
for (var i = 0; i < bytes.length; i++) {
42-
arr[i] = bytes[i]
43-
}
44-
}
37+
crypto = window.crypto = window.crypto || {}
38+
} else {
39+
crypto = require('crypto')
40+
}
41+
42+
if (!crypto.getRandomValues) {
43+
crypto.getRandomValues = getRandomValues
44+
}
45+
46+
let randomBytes
47+
48+
function getRandomValues (arr) {
49+
if (!randomBytes) randomBytes = require('react-native-randombytes').randomBytes
50+
51+
const bytes = randomBytes(arr.length)
52+
for (var i = 0; i < bytes.length; i++) {
53+
arr[i] = bytes[i]
4554
}
4655
}
4756
}

0 commit comments

Comments
 (0)