forked from webview-crypto/react-native-webview-crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
166 lines (148 loc) · 4.28 KB
/
index.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const React = require("react");
const { StyleSheet, View, Platform } = require("react-native");
const { WebView } = require("react-native-webview");
const { MainWorker, webViewWorkerString } = require("webview-crypto");
const styles = StyleSheet.create({
hide: {
display: "none",
position: "absolute",
width: 0,
height: 0,
flexGrow: 0,
flexShrink: 1
}
});
const internalLibrary = `
(function () {
function postMessage (message) {
if (window.ReactNativeWebView.postMessage === undefined) {
setTimeout(postMessage, 200, message)
} else {
window.ReactNativeWebView.postMessage(message)
}
}
var wvw = new WebViewWorker(postMessage)
//for android
window.document.addEventListener('message', function (e) {wvw.onMainMessage(e.data);})
//for ios
window.addEventListener('message', function (e) {wvw.onMainMessage(e.data);})
}())
`;
let resolveWorker;
let workerPromise = new Promise(resolve => {
resolveWorker = resolve;
});
function sendToWorker(message) {
workerPromise.then(worker => worker.onWebViewMessage(message));
}
const subtle = {
fake: true,
decrypt(...args) {
return workerPromise.then(worker => worker.crypto.subtle.decrypt(...args));
},
deriveBits(...args) {
return workerPromise.then(worker =>
worker.crypto.subtle.deriveBits(...args)
);
},
deriveKey(...args) {
return workerPromise.then(worker =>
worker.crypto.subtle.deriveKey(...args)
);
},
digest(...args) {
return workerPromise.then(worker => worker.crypto.subtle.digest(...args));
},
encrypt(...args) {
return workerPromise.then(worker => worker.crypto.subtle.encrypt(...args));
},
exportKey(...args) {
return workerPromise.then(worker =>
worker.crypto.subtle.exportKey(...args)
);
},
generateKey(...args) {
return workerPromise.then(worker =>
worker.crypto.subtle.generateKey(...args)
);
},
importKey(...args) {
return workerPromise.then(worker =>
worker.crypto.subtle.importKey(...args)
);
},
sign(...args) {
return workerPromise.then(worker => worker.crypto.subtle.sign(...args));
},
unwrapKey(...args) {
return workerPromise.then(worker =>
worker.crypto.subtle.unwrapKey(...args)
);
},
verify(...args) {
return workerPromise.then(worker => worker.crypto.subtle.verify(...args));
},
wrapKey(...args) {
return workerPromise.then(worker => worker.crypto.subtle.wrapKey(...args));
}
};
class PolyfillCrypto extends React.Component {
constructor(props) {
super(props);
this.props = props;
this.webViewRef = React.createRef();
}
shouldComponentUpdate() {
return false;
}
//reset promise so it can be resolved on next re-mount
componentWillUnmount() {
resolveWorker = undefined;
workerPromise = new Promise((resolve) => {
resolveWorker = resolve;
});
}
componentDidMount() {
const webView = this.webViewRef.current;
resolveWorker(
new MainWorker(msg => {
webView.postMessage(msg);
}, this.props.debug)
);
}
render() {
const code = `((function () {${webViewWorkerString};${internalLibrary}})())`;
const html = `<html><body><script language='javascript'>${code}</script></body></html>`
// The uri 'about:blank' doesn't have access to crypto.subtle on android
// const uri = "file:///android_asset/blank.html";
// Base64 dance is to work around https://github.com/facebook/react-native/issues/20365
// const source = Platform.select({
// android: { source: { uri } },
// ios: undefined
// });
return (
<View style={styles.hide}>
<WebView
javaScriptEnabled={true}
onError={a =>
console.error(Object.keys(a), a.type, a.nativeEvent.description)
}
onMessage={ev => sendToWorker(ev.nativeEvent.data)}
ref={this.webViewRef}
originWhitelist={["*"]}
source={{ html: html, baseUrl: 'https://localhost' }}
/>
</View>
);
}
}
if (typeof global.crypto !== "object") {
global.crypto = {};
}
//required for webview-crypto serializer fromObject
global.crypto.fake = true;
if (typeof global.crypto.subtle !== "object") {
global.crypto.subtle = subtle;
}
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = PolyfillCrypto;