fix: crypto polyfill (#3862)

This commit is contained in:
morizon
2023-11-24 11:21:26 +08:00
committed by GitHub
parent 954f9cf308
commit 48a226bc8b
3 changed files with 29 additions and 20 deletions

View File

@@ -1,7 +1,12 @@
console.log('================ cross-crypto (web)');
if (process.env.NODE_ENV !== 'production') {
const getRandomValuesOld = global.crypto.getRandomValues;
global.crypto.getRandomValues = function (...args) {
console.log('------------ call global.crypto.getRandomValues (web)');
console.log(
'------------ call global.crypto.getRandomValues (web)',
getRandomValuesOld,
);
return getRandomValuesOld.apply(global.crypto, args);
};
}

View File

@@ -2,6 +2,7 @@
// react-native-quick-crypto
// react-native-get-random-values
// (react-native-crypto depend on) react-native-randombytes (deprecated)
console.log('================ cross-crypto (native)');
if (global.crypto && global.crypto.getRandomValues) {
delete global.crypto.getRandomValues;
@@ -12,7 +13,10 @@ require('react-native-get-random-values');
if (process.env.NODE_ENV !== 'production') {
const getRandomValuesOld = global.crypto.getRandomValues;
global.crypto.getRandomValues = function (...args) {
console.log('------------ call global.crypto.getRandomValues (native)');
console.log(
'------------ call global.crypto.getRandomValues (native)',
getRandomValuesOld,
);
return getRandomValuesOld.apply(global.crypto, args);
};
}

View File

@@ -1,15 +1,15 @@
/* eslint-disable global-require, no-restricted-syntax, import/no-unresolved */
import 'setimmediate';
import './intlShim';
import 'react-native-url-polyfill/auto';
import platformEnv from '@onekeyhq/shared/src/platformEnv';
require('setimmediate');
require('./intlShim');
require('react-native-url-polyfill/auto');
const platformEnv = require('@onekeyhq/shared/src/platformEnv');
const shimsLog = (str) => console.log(`Shims Injected: ${str}`);
const shimsInjectedLog = (str) => console.log(`Shims Injected log: ${str}`);
if (typeof __dirname === 'undefined') global.__dirname = '/';
if (typeof __filename === 'undefined') global.__filename = '';
if (typeof process === 'undefined') {
shimsLog('process');
shimsInjectedLog('process');
global.process = require('process');
} else {
const bProcess = require('process');
@@ -23,18 +23,18 @@ if (typeof process === 'undefined') {
// TextEncoder and TextDecoder polyfill for starcoin
if (typeof TextDecoder === 'undefined') {
shimsLog('TextDecoder');
shimsInjectedLog('TextDecoder');
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
global.TextDecoder = require('text-encoding').TextDecoder;
}
if (typeof TextEncoder === 'undefined') {
shimsLog('TextEncoder');
shimsInjectedLog('TextEncoder');
global.TextEncoder = require('text-encoding').TextEncoder;
}
// Buffer polyfill
if (typeof Buffer === 'undefined') {
shimsLog('Buffer');
shimsInjectedLog('Buffer');
// global.Buffer = require('@craftzdog/react-native-buffer').Buffer;
global.Buffer = require('buffer').Buffer;
}
@@ -43,9 +43,9 @@ if (typeof Buffer === 'undefined') {
if (typeof crypto === 'undefined') {
try {
shimsLog('crypto');
shimsInjectedLog('crypto');
// eslint-disable-next-line no-const-assign
global.crypto = require('crypto');
global.crypto = require('crypto'); // cross-crypto/index.native.js
} catch (error) {
console.error(error);
}
@@ -53,7 +53,6 @@ if (typeof crypto === 'undefined') {
// https://docs.ethers.io/v5/cookbook/react-native/
// Import the crypto getRandomValues shim (**BEFORE** the shims)
import 'react-native-get-random-values';
// Import the the ethers shims (**BEFORE** ethers)
/*
Shims Injected:
@@ -64,19 +63,20 @@ Shims Injected:
*/
// Shim atob and btoa
// js-base64 lib cannot import by `require` function in React Native 0.72.
import Base64 from 'js-base64';
const { Base64 } = require('js-base64');
if (!global.atob) {
shimsLog('atob');
shimsInjectedLog('atob');
global.atob = Base64.atob;
}
if (!global.btoa) {
shimsLog('btoa');
shimsInjectedLog('btoa');
global.btoa = Base64.btoa;
}
// Shim nextTick
if (!global.nextTick) {
shimsLog('nextTick');
shimsInjectedLog('nextTick');
global.nextTick = function (callback) {
setTimeout(callback, 0);
};
@@ -91,7 +91,7 @@ try {
try {
fr.readAsArrayBuffer(new Blob(['hello'], { type: 'text/plain' }));
} catch (error) {
shimsLog('FileReader.prototype.readAsArrayBuffer');
shimsInjectedLog('FileReader.prototype.readAsArrayBuffer');
FileReader.prototype.readAsArrayBuffer = function (blob) {
if (this.readyState === this.LOADING) {
throw new Error('InvalidStateError');
@@ -146,4 +146,4 @@ if (platformEnv.isNativeIOS) {
Error.captureStackTrace = () => {};
}
console.log('polyfillsPlatform.native shim loaded');
console.log('polyfillsPlatform.native shim loaded');