-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: crypto random generator polyfill (#3609)
* fix: bip39 generate mnemonic strength to 256 * fix: web crypto polyfill * fix: native crypto polyfill * fix: lint * fix: lint
- Loading branch information
1 parent
c5afc83
commit 01331e5
Showing
13 changed files
with
120 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
if (process.env.NODE_ENV !== 'production') { | ||
const getRandomValuesOld = global.crypto.getRandomValues; | ||
global.crypto.getRandomValues = function (...args) { | ||
console.log('------------ call global.crypto.getRandomValues (web)'); | ||
return getRandomValuesOld.apply(global.crypto, args); | ||
}; | ||
} | ||
|
||
const crypto = require('crypto-browserify'); | ||
|
||
if (global.crypto) { | ||
global.crypto.randomBytes = global.crypto.randomBytes || crypto.randomBytes; | ||
crypto.getRandomValues = | ||
crypto.getRandomValues || global.crypto.getRandomValues; | ||
} | ||
crypto.$$isOneKeyShim = true; | ||
global.crypto.$$isOneKeyShim = true; | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
console.log('crypto-browserify polyfilled', crypto, global.crypto); | ||
} | ||
|
||
module.exports = crypto; |
37 changes: 37 additions & 0 deletions
37
packages/shared/src/modules3rdParty/cross-crypto/index.native.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// react-native-crypto | ||
// react-native-quick-crypto | ||
// react-native-get-random-values | ||
// react-native-randombytes (deprecated) | ||
|
||
if (global.crypto && global.crypto.getRandomValues) { | ||
delete global.crypto.getRandomValues; | ||
} | ||
// shim global.crypto.getRandomValues | ||
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)'); | ||
return getRandomValuesOld.apply(global.crypto, args); | ||
}; | ||
} | ||
|
||
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; |
28 changes: 28 additions & 0 deletions
28
packages/shared/src/modules3rdParty/cross-crypto/verify.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import assert from 'assert'; | ||
import * as crypto from 'crypto'; | ||
|
||
const globalCrypto = global.crypto; | ||
|
||
// @ts-ignore | ||
assert.ok(globalCrypto?.$$isOneKeyShim, 'global crypto is not polyfilled'); | ||
// @ts-ignore | ||
assert.ok(crypto?.$$isOneKeyShim, 'crypto is not polyfilled'); | ||
|
||
assert.equal( | ||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
globalCrypto.getRandomValues, | ||
// @ts-ignore | ||
crypto.getRandomValues, | ||
'getRandomValues is not matched', | ||
); | ||
|
||
assert.equal( | ||
// @ts-ignore | ||
globalCrypto.randomBytes, | ||
crypto.randomBytes, | ||
'randomBytes is not matched', | ||
); | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
console.log('cross-crypto verify success!'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters