-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfix-modules.js
67 lines (58 loc) · 1.92 KB
/
fix-modules.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
const fs = require('fs');
const fixWindowError = () => {
const file = './node_modules/bitcore-lib/lib/crypto/random.js';
let fileData = fs.readFileSync(file).toString();
fileData = fileData.replace(
`Random.getRandomBufferBrowser = function(size) {
if (!window.crypto && !window.msCrypto)
throw new Error('window.crypto not available');
if (window.crypto && window.crypto.getRandomValues)
var crypto = window.crypto;
else if (window.msCrypto && window.msCrypto.getRandomValues) //internet explorer
var crypto = window.msCrypto;
else
throw new Error('window.crypto.getRandomValues not available');
var bbuf = new Uint8Array(size);
crypto.getRandomValues(bbuf);
var buf = Buffer.from(bbuf);
return buf;
};`,
`Random.getRandomBufferBrowser = function(size) {
var bbuf = new Uint8Array(size);
crypto.getRandomValues(bbuf);
var buf = Buffer.from(bbuf);
return buf;
};`
);
fs.writeFileSync(file, fileData);
};
const fixWindowError2 = () => {
const file = './node_modules/tiny-secp256k1/lib/rand.browser.js';
let fileData = fs.readFileSync(file).toString();
fileData = fileData.replace('window.crypto', 'crypto');
fs.writeFileSync(file, fileData);
};
const fixDocumentError = () => {
const file = './dist/chrome/background.js';
let fileData = fs.readFileSync(file).toString();
fileData = fileData.replace('document.baseURI || self.location.href', 'self.location.href');
fileData = fileData.replace('document.baseURI||self.location.href', 'self.location.href');
fs.writeFileSync(file, fileData);
};
const run = async () => {
let success = true;
try {
// fixWindowError();
// fixWindowError2();
// fixWindowError3();
// fixBufferError();
// fixWalletSdkError();
fixDocumentError();
} catch (e) {
console.error('error:', e.message);
success = false;
} finally {
console.log('Fix modules result: ', success ? 'success' : 'failed');
}
};
run();