Skip to content
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

Add address to string conversion #3403

Merged
merged 13 commits into from May 13, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
* `Clones`: optimize clone creation ([#3329](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3329))
* `TimelockController`: Migrate `_call` to `_execute` and allow inheritance and overriding similar to `Governor`. ([#3317](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3317))
* `CrossChainEnabledPolygonChild`: replace the `require` statement with the custom error `NotCrossChainCall`. ([#3380](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3380))
* `Strings`: add a new function `addressToHexString` that converts an `address` to its ASCII `string` hexadecimal representation. ([#3403](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3403))

## 4.6.0 (2022-04-26)

Expand Down
4 changes: 4 additions & 0 deletions contracts/mocks/StringsMock.sol
Expand Up @@ -16,4 +16,8 @@ contract StringsMock {
function fromUint256HexFixed(uint256 value, uint256 length) public pure returns (string memory) {
return Strings.toHexString(value, length);
}

function fromAddressHex(address addr) public pure returns (string memory) {
return Strings.addressToHexString(addr);
}
}
7 changes: 7 additions & 0 deletions contracts/utils/Strings.sol
Expand Up @@ -64,4 +64,11 @@ library Strings {
require(value == 0, "Strings: hex length insufficient");
return string(buffer);
}

/**
* @dev Converts an `address` to its ASCII `string` hexadecimal representation.
*/
function addressToHexString(address addr) internal pure returns (string memory) {
pcaversaccio marked this conversation as resolved.
Show resolved Hide resolved
return toHexString(uint256(uint160(addr)));
pcaversaccio marked this conversation as resolved.
Show resolved Hide resolved
}
}
7 changes: 7 additions & 0 deletions test/utils/Strings.test.js
Expand Up @@ -56,4 +56,11 @@ contract('Strings', function (accounts) {
.to.equal(web3.utils.toHex(constants.MAX_UINT256));
});
});

describe('from address - hex format', function () {
it('converts an address', async function () {
expect(web3.utils.toChecksumAddress(await this.strings.fromAddressHex(this.strings.address)))
.to.equal(this.strings.address.toString());
});
});
});