-
Notifications
You must be signed in to change notification settings - Fork 12k
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 all commits
215452f
3ef22ec
db76d54
e05ab88
b0967a8
719978b
f2ce027
ac713f0
8f11b32
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'openzeppelin-solidity': minor | ||
--- | ||
|
||
`Strings`: Added a utility function for converting an address to checksummed string. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,6 +85,30 @@ library Strings { | |
return toHexString(uint256(uint160(addr)), ADDRESS_LENGTH); | ||
} | ||
|
||
/** | ||
* @dev Converts an `address` with fixed length of 20 bytes to its checksummed ASCII `string` hexadecimal | ||
* representation, according to EIP-55. | ||
*/ | ||
function toChecksumHexString(address addr) internal pure returns (string memory) { | ||
bytes memory buffer = bytes(toHexString(addr)); | ||
|
||
// hash the hex part of buffer (skip length + 2 bytes, length 40) | ||
uint256 hashValue; | ||
assembly ("memory-safe") { | ||
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. note: this is safe because we know buffer is 42 bytes long. |
||
hashValue := shr(96, keccak256(add(buffer, 0x22), 40)) | ||
} | ||
|
||
for (uint256 i = 41; i > 1; --i) { | ||
// possible values for buffer[i] are 48 (0) to 57 (9) and 97 (a) to 102 (f) | ||
if (hashValue & 0xf > 7 && uint8(buffer[i]) > 96) { | ||
// case shift by xoring with 0x20 | ||
buffer[i] ^= 0x20; | ||
} | ||
hashValue >>= 4; | ||
} | ||
return string(buffer); | ||
} | ||
|
||
/** | ||
* @dev Returns true if the two strings are equal. | ||
*/ | ||
|
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. There's a rust implementation of this checksum algorithm in Foundry (as seen in cast), so it should be relatively trivial to make a PR and request for it to be exposed through VM.sol as with Base64. With that, we can fuzz the implementation, which would be extremely valuable. 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. totally -- fuzzing should be used in some of the other utils as well. 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. Agree, feel free to open PRs adding fuzzing or Halmos FV to those utils you consider make sense |
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
toChecksumHexString
to avoid double allocation._unsafeSetHexString
HEX_DIGITS_UPPERCASE
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.
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!