Skip to content

Commit

Permalink
fix: native crypto polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
sidmorizon committed Oct 7, 2023
1 parent e751a73 commit 391f9a3
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"bech32": "^2.0.0",
"bignumber.js": "^9.0.1",
"bip32": "^4.0.0",
"bip39": "^3.0.4",
"bip39": "^3.1.0",
"bitcoinforkjs": "git+https://github.com/OneKeyHQ/bitcoinjs-lib.git#feat/remove-npm-lock",
"bitcoinjs-message": "^2.2.0",
"bs58check": "^2.1.2",
Expand Down
19 changes: 13 additions & 6 deletions packages/shared/src/modules3rdParty/cross-crypto/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
const crypto = require('crypto-browserify');

const nativeCrypto = global.crypto;
if (nativeCrypto) {
nativeCrypto.randomBytes = nativeCrypto.randomBytes || crypto.randomBytes;
// **** for test only
// if (process.env.NODE_ENV !== 'production') {
// const getRandomValuesOld = global.crypto.getRandomValues;
// global.crypto.getRandomValues = function (...args) {
// return getRandomValuesOld.apply(global.crypto, args);
// };
// }

if (global.crypto) {
global.crypto.randomBytes = global.crypto.randomBytes || crypto.randomBytes;
crypto.getRandomValues =
crypto.getRandomValues || nativeCrypto.getRandomValues;
crypto.getRandomValues || global.crypto.getRandomValues;
}
crypto.$$isOneKeyShim = true;
nativeCrypto.$$isOneKeyShim = true;
global.crypto.$$isOneKeyShim = true;

if (process.env.NODE_ENV !== 'production') {
console.log('crypto-browserify polyfilled', crypto, nativeCrypto);
console.log('crypto-browserify polyfilled', crypto, global.crypto);
}

module.exports = crypto;
29 changes: 27 additions & 2 deletions packages/shared/src/modules3rdParty/cross-crypto/index.native.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
// require.resolve('react-native-crypto')
// react-native-crypto
// react-native-quick-crypto
// react-native-get-random-values
// react-native-randombytes
// react-native-randombytes (deprecated)

if (global.crypto && global.crypto.getRandomValues) {
delete global.crypto.getRandomValues;
}
// shim global.crypto.getRandomValues
require('react-native-get-random-values');

const crypto = require('react-native-crypto');

const { randomBytes } = require('@noble/hashes/utils');

// re-assign randomBytes from global.crypto.getRandomValues
crypto.randomBytes = randomBytes;
crypto.getRandomValues =
crypto.getRandomValues || global.crypto.getRandomValues;
global.crypto.randomBytes = global.crypto.randomBytes || crypto.randomBytes;

crypto.$$isOneKeyShim = true;
global.crypto.$$isOneKeyShim = true;

if (process.env.NODE_ENV !== 'production') {
console.log('react-native-crypto polyfilled', crypto, global.crypto);
}

module.exports = crypto;
14 changes: 7 additions & 7 deletions packages/shared/src/modules3rdParty/cross-crypto/verify.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import assert from 'assert';
import * as crypto from 'crypto';

const nativeCrypto = global.crypto;
const globalCrypto = global.crypto;

// @ts-ignore
assert.ok(nativeCrypto.$$isOneKeyShim, 'nativeCrypto is not polyfilled');
assert.ok(globalCrypto?.$$isOneKeyShim, 'nativeCrypto is not polyfilled');
// @ts-ignore
assert.ok(crypto.$$isOneKeyShim, 'crypto is not polyfilled');
assert.ok(crypto?.$$isOneKeyShim, 'crypto is not polyfilled');

assert.equal(
// eslint-disable-next-line @typescript-eslint/unbound-method
nativeCrypto.getRandomValues,
globalCrypto.getRandomValues,
// @ts-ignore
crypto.getRandomValues,
'getRandomValues is matched',
'getRandomValues is not matched',
);

assert.equal(
// @ts-ignore
nativeCrypto.randomBytes,
globalCrypto.randomBytes,
crypto.randomBytes,
'randomBytes is matched',
'randomBytes is not matched',
);

if (process.env.NODE_ENV !== 'production') {
Expand Down
11 changes: 10 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6597,7 +6597,7 @@ __metadata:
bech32: ^2.0.0
bignumber.js: ^9.0.1
bip32: ^4.0.0
bip39: ^3.0.4
bip39: ^3.1.0
bitcoinforkjs: "git+https://github.com/OneKeyHQ/bitcoinjs-lib.git#feat/remove-npm-lock"
bitcoinjs-message: ^2.2.0
bs58check: ^2.1.2
Expand Down Expand Up @@ -13288,6 +13288,15 @@ __metadata:
languageName: node
linkType: hard

"bip39@npm:^3.1.0":
version: 3.1.0
resolution: "bip39@npm:3.1.0"
dependencies:
"@noble/hashes": ^1.2.0
checksum: 1224e763ffc6b097052ed8abd57f0e521ad6d31f1645be0d0a15f4417c13f8461f00e47e9cf7c8c784bd533f4fb1ee3ab020f258c7df45ee5dc722b4b0336cfc
languageName: node
linkType: hard

"bip66@npm:^1.1.0, bip66@npm:^1.1.5":
version: 1.1.5
resolution: "bip66@npm:1.1.5"
Expand Down

0 comments on commit 391f9a3

Please sign in to comment.