-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Add utility function for converting an address to checksummed string #5067
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
Changes from 1 commit
215452f
3ef22ec
db76d54
e05ab88
b0967a8
719978b
f2ce027
ac713f0
8f11b32
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,17 +64,15 @@ library Strings { | |
| * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length. | ||
| */ | ||
| function toHexString(uint256 value, uint256 length) internal pure returns (string memory) { | ||
| uint256 localValue = value; | ||
| if (length < Math.log256(value) + 1) { | ||
|
||
| revert StringsInsufficientHexLength(value, length); | ||
| } | ||
|
|
||
| bytes memory buffer = new bytes(2 * length + 2); | ||
| buffer[0] = "0"; | ||
| buffer[1] = "x"; | ||
| for (uint256 i = 2 * length + 1; i > 1; --i) { | ||
| buffer[i] = HEX_DIGITS[localValue & 0xf]; | ||
| localValue >>= 4; | ||
| } | ||
| if (localValue != 0) { | ||
| revert StringsInsufficientHexLength(value, length); | ||
| } | ||
| _setHexString(buffer, 2, value); | ||
|
|
||
| return string(buffer); | ||
| } | ||
|
|
||
|
|
@@ -91,12 +89,8 @@ library Strings { | |
| * representation, according to EIP-55. | ||
| */ | ||
| function toChecksumHexString(address addr) internal pure returns (string memory) { | ||
| uint160 localValue = uint160(addr); | ||
| bytes memory lowercase = new bytes(40); | ||
| for (uint256 i = 40; i > 0; --i) { | ||
| lowercase[i - 1] = HEX_DIGITS[localValue & 0xf]; | ||
| localValue >>= 4; | ||
| } | ||
| _setHexString(lowercase, 0, uint160(addr)); | ||
cairoeth marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| bytes32 hashedAddr = keccak256(abi.encodePacked(lowercase)); | ||
|
||
|
|
||
| bytes memory buffer = new bytes(42); | ||
|
|
@@ -122,4 +116,14 @@ library Strings { | |
| function equal(string memory a, string memory b) internal pure returns (bool) { | ||
| return bytes(a).length == bytes(b).length && keccak256(bytes(a)) == keccak256(bytes(b)); | ||
| } | ||
|
|
||
| /** | ||
| * @dev Sets the hexadecimal representation of a value in the specified buffer starting from the given offset. | ||
| */ | ||
| function _setHexString(bytes memory buffer, uint256 offset, uint256 value) private pure { | ||
ernestognw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for (uint256 i = buffer.length; i > offset; --i) { | ||
| buffer[i - 1] = HEX_DIGITS[value & 0xf]; | ||
Amxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| value >>= 4; | ||
| } | ||
| } | ||
| } | ||
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.
Redesigned
toChecksumHexStringto avoid double allocation._unsafeSetHexStringHEX_DIGITS_UPPERCASEThere 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.
Right this is extremely cleaner. Thanks!
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.
great changes, thxs!