-
Notifications
You must be signed in to change notification settings - Fork 5k
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
Create custom addHexPrefix function #9306
Merged
rekmarks
merged 14 commits into
MetaMask:develop
from
PatrykLucka:create-custom-addHexPrefix
Nov 6, 2020
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9a3d164
create custom addHexPrefix function
PatrykLucka b9a01b7
switch to custom addHexPrefix
PatrykLucka 6cc1d3a
Update app/scripts/lib/util.js
PatrykLucka 6b66f6a
fix jsDoc and gasPrice comparison
PatrykLucka 25fbdc3
fix lint error
PatrykLucka 98fff60
Update util.js
PatrykLucka 2ba32e8
fix non string case
PatrykLucka eb720e0
Update addHexPrefix
rekmarks d8a6d82
Update app/scripts/lib/util.js
rekmarks 0af5619
Update app/scripts/lib/util.js
rekmarks a11a616
Update app/scripts/lib/util.js
rekmarks 75da02e
Update app/scripts/lib/util.js
rekmarks 89a6720
Update ui/app/hooks/useCancelTransaction.js
rekmarks 336a2d3
Lint
rekmarks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,17 +103,6 @@ function sufficientBalance(txParams, hexBalance) { | |
return balance.gte(maxCost) | ||
} | ||
|
||
/** | ||
* Converts a BN object to a hex string with a '0x' prefix | ||
* | ||
* @param {BN} inputBn - The BN to convert to a hex string | ||
* @returns {string} - A '0x' prefixed hex string | ||
* | ||
*/ | ||
function bnToHex(inputBn) { | ||
return ethUtil.addHexPrefix(inputBn.toString(16)) | ||
} | ||
|
||
/** | ||
* Converts a hex string to a BN object | ||
* | ||
|
@@ -173,13 +162,47 @@ function isPrefixedFormattedHexString(value) { | |
return /^0x[1-9a-f]+[0-9a-f]*$/iu.test(value) | ||
} | ||
|
||
/** | ||
* Prefixes a hex string with '0x' or '-0x' and returns it. Idempotent. | ||
* | ||
* @param {string} str - The string to prefix. | ||
* @returns {string} The prefixed string. | ||
*/ | ||
const addHexPrefix = (str) => { | ||
if (typeof str !== 'string' || str.match(/^-?0x/u)) { | ||
return str | ||
rekmarks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if (str.match(/^-?0X/u)) { | ||
return str.replace('0X', '0x') | ||
} | ||
|
||
if (str.startsWith('-')) { | ||
return str.replace('-', '-0x') | ||
} | ||
|
||
return `0x${str}` | ||
} | ||
Comment on lines
+165
to
+185
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 changed this implementation. Comparison: https://github.com/MetaMask/metamask-extension/compare/2ba32e896f545ed432488d3d04a91ee42eacd9c3...89a6720?expand=1 |
||
|
||
/** | ||
* Converts a BN object to a hex string with a '0x' prefix | ||
* | ||
* @param {BN} inputBn - The BN to convert to a hex string | ||
* @returns {string} - A '0x' prefixed hex string | ||
* | ||
*/ | ||
function bnToHex(inputBn) { | ||
return addHexPrefix(inputBn.toString(16)) | ||
} | ||
|
||
export { | ||
getPlatform, | ||
getEnvironmentType, | ||
sufficientBalance, | ||
hexToBn, | ||
bnToHex, | ||
BnMultiplyByFraction, | ||
checkForError, | ||
isPrefixedFormattedHexString, | ||
addHexPrefix, | ||
bnToHex, | ||
} |
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
2 changes: 1 addition & 1 deletion
2
...ents/app/gas-customization/gas-modal-page-container/gas-modal-page-container.container.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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Very nice use of the word "Idempotent" 🙌