From f048cc5c34560254dfe8a522a605a215fb3c4092 Mon Sep 17 00:00:00 2001 From: kevlened Date: Sun, 8 Sep 2019 23:06:05 -0400 Subject: [PATCH] Remove mitt dependency --- package-lock.json | 14 +++++++----- package.json | 1 - src/react-native.js | 55 ++++++++++++++++----------------------------- 3 files changed, 28 insertions(+), 42 deletions(-) diff --git a/package-lock.json b/package-lock.json index 15b12e1..458a2af 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3688,6 +3688,15 @@ "jest-regex-util": "^24.9.0" } }, + "expo-random": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/expo-random/-/expo-random-4.0.0.tgz", + "integrity": "sha512-z5T6lV39wAGjfBr9UEOKwDBlrc57YIx2TkyTyQh13XOndin9xQ/pdycS4QBYqdHGlbOjDPbIKwKP5Pk+PJgVcg==", + "optional": true, + "requires": { + "base64-js": "^1.3.0" + } + }, "extend": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz", @@ -7821,11 +7830,6 @@ "through2": "^2.0.0" } }, - "mitt": { - "version": "1.1.3", - "resolved": "https://registry.npmjs.org/mitt/-/mitt-1.1.3.tgz", - "integrity": "sha512-mUDCnVNsAi+eD6qA0HkRkwYczbLHJ49z17BGe2PYRhZL4wpZUFZGJHU7/5tmvohoma+Hdn0Vh/oJTiPEmgSruA==" - }, "mixin-deep": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/mixin-deep/-/mixin-deep-1.3.2.tgz", diff --git a/package.json b/package.json index 1661e17..55fd1b2 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,6 @@ "dependencies": { "b64-lite": "^1.3.1", "b64u-lite": "^1.0.1", - "mitt": "^1.1.3", "msrcrypto": "^1.5.4", "node-webcrypto-ossl": "^1.0.48", "str2buf": "^1.3.0", diff --git a/src/react-native.js b/src/react-native.js index 29b7410..0634ea7 100644 --- a/src/react-native.js +++ b/src/react-native.js @@ -12,13 +12,8 @@ if (require.getModules) { if (!generateSecureRandom) { console.log(` - isomorphic-webcrypto cannot ensure the security of some operations. - Please eject and run: - - npm install react-native-securerandom --save - react-native link - - Or install and configure expo-random: https://www.npmjs.com/package/expo-random + isomorphic-webcrypto cannot ensure the security of some operations! + Install and configure expo-random or react-native-securerandom `); generateSecureRandom = function(length) { const uint8Array = new Uint8Array(length); @@ -29,39 +24,28 @@ if (!generateSecureRandom) { } } -const EventEmitter = require('mitt'); -const b64u = require('b64u-lite'); const str2buf = require('str2buf'); +const b64u = require('b64u-lite'); const b64 = require('b64-lite'); global.atob = typeof atob === 'undefined' ? b64.atob : atob; global.btoa = typeof btoa === 'undefined' ? b64.btoa : btoa; global.msrCryptoPermanentForceSync = true; const crypto = require('msrcrypto'); -const secureWatch = new EventEmitter() - -let secured = !crypto.initPrng -let secureRandomError -if (!secured) { - generateSecureRandom(48) - .then(byteArray => { - crypto.initPrng(Array.from(byteArray)) - secured = true - secureWatch.emit('secure') - }) - .catch(err => { - secureRandomError = err - secureWatch.emit('secureRandomError') - }) -} - -crypto.ensureSecure = () => new Promise((resolve, reject) => { - if (secured) return resolve(); - if (secureRandomError) return reject(secureRandomError) - secureWatch.on('secure', () => resolve()) - secureWatch.on('secureRandomError', () => reject(secureRandomError)) +let isSecured = false; +const secured = new Promise((resolve, reject) => { + if (!crypto.initPrng) return resolve(false); + return generateSecureRandom(48) + .then(byteArray => { + crypto.initPrng(Array.from(byteArray)) + isSecured = true; + resolve(true); + }) + .catch(err => reject(err)); }); +crypto.ensureSecure = () => secured; + function standardizeAlgoName(algo) { const upper = algo.toUpperCase(); return upper === 'RSASSA-PKCS1-V1_5' ? 'RSASSA-PKCS1-v1_5' : upper; @@ -78,13 +62,12 @@ function ensureUint8Array(buffer) { const originalGetRandomValues = crypto.getRandomValues; crypto.getRandomValues = function getRandomValues() { - if (!secured) { + if (!isSecured) { throw new Error(` You must wait until the library is secure to call this method: - crypto.ensureSecure(err => { - if (err) throw err; - const safeValues = crypto.getRandomValues(); - }); + + await crypto.ensureSecure(); + const safeValues = crypto.getRandomValues(); `); } return originalGetRandomValues.apply(crypto, arguments);