-
-
Notifications
You must be signed in to change notification settings - Fork 255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve random int #14828
Improve random int #14828
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,8 @@ | |
"dependencies": { | ||
"@trezor/connect": "workspace:*", | ||
"@trezor/connect-common": "workspace:*", | ||
"@trezor/utils": "workspace:*" | ||
"@trezor/utils": "workspace:*", | ||
"crypto-browserify": "^3.12.0" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dependency or devDependency ? 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It depends... 🤣 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it should, imho, be only There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. connect-web itself only imports scheduleAction, createDeferred and createDeferredManager |
||
}, | ||
"devDependencies": { | ||
"@babel/preset-typescript": "^7.24.7", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,7 @@ const config: webpack.Configuration = { | |
src: path.resolve(__dirname, '../../suite/src/'), | ||
}, | ||
fallback: { | ||
// Polyfills crypto API for NodeJS libraries in the browser. 'crypto' does not run without 'stream' | ||
// Polyfills crypto API for Node.js libraries in the browser. 'crypto' does not run without 'stream' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I received a notice, that this is the official™ spelling is |
||
crypto: require.resolve('crypto-browserify'), | ||
stream: require.resolve('stream-browserify'), | ||
// Not required | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
"test:e2e:new-bridge:hw": "USE_HW=true USE_NODE_BRIDGE=true yarn test:e2e:bridge", | ||
"test:e2e:new-bridge:emu": "USE_HW=false USE_NODE_BRIDGE=true yarn test:e2e:bridge", | ||
"build:e2e:api:node": "yarn esbuild ./e2e/api/api.test.ts --bundle --outfile=./e2e/dist/api.test.node.js --platform=node --target=node18 --external:usb", | ||
"build:e2e:api:browser": "yarn esbuild ./e2e/api/api.test.ts --bundle --outfile=./e2e/dist/api.test.browser.js --platform=browser --external:usb && cp e2e/ui/api.test.html e2e/dist/index.html", | ||
"build:e2e:api:browser": "yarn esbuild ./e2e/api/api.test.ts --bundle --alias:crypto=crypto-browserify --alias:stream=stream-browserify --outfile=./e2e/dist/api.test.browser.js --platform=browser --external:usb && cp e2e/ui/api.test.html e2e/dist/index.html", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Took me a while to figure this out: https://esbuild.github.io/api/#alias |
||
"test:e2e:api:node:hw": "yarn build:e2e:api:node && node ./e2e/dist/api.test.node.js", | ||
"test:e2e:api:browser:hw": "yarn build:e2e:api:browser && npx http-serve ./e2e/dist" | ||
}, | ||
|
@@ -25,6 +25,7 @@ | |
"@trezor/trezor-user-env-link": "workspace:^", | ||
"@trezor/utils": "workspace:*", | ||
"buffer": "^6.0.3", | ||
"crypto-browserify": "^3.12.0", | ||
"esbuild": "^0.23.1", | ||
"jest": "^29.7.0", | ||
"jest-extended": "^4.0.2", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { randomBytes } from 'crypto'; | ||
|
||
/** | ||
* Crypto.randomInt() function is not implemented by polyfill 'crypto-browserify' | ||
* @see https://github.com/browserify/crypto-browserify/issues/224 | ||
*/ | ||
export const getRandomInt = (min: number, max: number) => { | ||
if (min >= max) { | ||
throw new RangeError( | ||
`The value of "max" is out of range. It must be greater than the value of "min" (${min}). Received ${max}`, | ||
); | ||
} | ||
|
||
const randomValue = parseInt(randomBytes(4).toString('hex'), 16); | ||
|
||
return min + (randomValue % (max - min)); | ||
}; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* @deprecated Consider using `getRandomInt` which is cryptographically secure. | ||
*/ | ||
export const getWeakRandomNumberInRange = (min: number, max: number) => | ||
Math.floor(Math.random() * (max - min + 1)) + min; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and what about
stream-browserify
. this is transient?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, I used the same approach as was in
suite
package