PolyFillCrypto.js
2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import * as React from 'react';
import { Platform, StyleSheet, View } from 'react-native';
import WebViewBridge from 'react-native-webview-bridge';
import { MainWorker, webViewWorkerString } from '../webview-crypto';
import encodeUtf8 from 'encode-utf8';
import encodeBase64 from 'fast-base64-encode';
const base64EncodeString = (input) => {
return encodeBase64(new Uint8Array(encodeUtf8(input)));
};
const internalLibIOS = `
${webViewWorkerString}
(function () {
var wvw = new WebViewWorker(WebViewBridge.send.bind(WebViewBridge));
WebViewBridge.onMessage = wvw.onMainMessage.bind(wvw);
}());
`;
const intermediateLib = `
${webViewWorkerString}
(function () {
var wvw = new WebViewWorker(WebViewBridge.send.bind(WebViewBridge));
WebViewBridge.onMessage = wvw.onMainMessage.bind(wvw);
}());
`;
const internalLibAndroid = `eval(window.atob('${base64EncodeString(intermediateLib)}'))`;
export default class PolyfillCrypto extends React.Component {
shouldComponentUpdate() {
return false;
}
render() {
let worker;
const uri = 'file:///android_asset/html/blank.html';
return (
<View style={styles.hidden}>
<WebViewBridge
ref={(c) => {
if (c && !worker) {
worker = new MainWorker(c.sendToBridge, this.props.debug);
if (window.crypto) {
// we are in chrome debugger
// this means overridng the crypto object itself won't
// work, so we have to override all of it's methods
for (const name in worker.crypto.subtle) {
window.crypto.subtle[name] = worker.crypto.subtle[name];
}
window.crypto.fake = true;
} else {
window.crypto = worker.crypto;
}
window.crypto.loaded = true;
console.log('*** poly injected', window.crypto);
}
}}
onBridgeMessage={
// can't refer to this.state.onBridgeMessage directly
// because it is not defined when this component is first
// started, only set in `ref`
(message) => {
worker.onWebViewMessage(message);
}
}
injectedJavaScript={
Platform.OS === 'android' ? internalLibAndroid : internalLibIOS
}
onError={(error) => {
console.warn('Error creating webview: ', error);
}}
javaScriptEnabled={true}
source={{
uri: Platform.OS === 'android' ? uri : 'about:blank',
}}
/>
</View>
);
}
}
const styles = StyleSheet.create({
hidden: {
height: 0,
opacity: 0,
},
});