From 5c981b60fc1b02067d67813f357bba3166efe278 Mon Sep 17 00:00:00 2001 From: Pascal Marco Caversaccio Date: Tue, 10 May 2022 00:02:17 +0200 Subject: [PATCH 01/12] feat: add address to hex conversion Signed-off-by: Pascal Marco Caversaccio --- CHANGELOG.md | 1 + contracts/mocks/StringsMock.sol | 4 ++++ contracts/utils/Strings.sol | 7 +++++++ test/utils/Strings.test.js | 7 +++++++ 4 files changed, 19 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 263f869ff81..06428a5c9a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 that converts an `address` to its ASCII `string` hexadecimal representation. ([#TBD]()) ## 4.6.0 (2022-04-26) diff --git a/contracts/mocks/StringsMock.sol b/contracts/mocks/StringsMock.sol index f257734e76f..a7cf47a8e94 100644 --- a/contracts/mocks/StringsMock.sol +++ b/contracts/mocks/StringsMock.sol @@ -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); + } } diff --git a/contracts/utils/Strings.sol b/contracts/utils/Strings.sol index d38bbe8267e..76c91684d08 100644 --- a/contracts/utils/Strings.sol +++ b/contracts/utils/Strings.sol @@ -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) { + return toHexString(uint256(uint160(addr))); + } } diff --git a/test/utils/Strings.test.js b/test/utils/Strings.test.js index 5128ce577dc..0be392c239d 100644 --- a/test/utils/Strings.test.js +++ b/test/utils/Strings.test.js @@ -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()); + }); + }); }); From 80a233a8a341a1160b00bfb564320caf4f290418 Mon Sep 17 00:00:00 2001 From: Pascal Marco Caversaccio Date: Tue, 10 May 2022 00:14:26 +0200 Subject: [PATCH 02/12] add PR number to CHANGELOG Signed-off-by: Pascal Marco Caversaccio --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 06428a5c9a5..04a4de1136b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +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 that converts an `address` to its ASCII `string` hexadecimal representation. ([#TBD]()) + * `Strings`: add a new function 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) From 70434230ee938e2a704d3610f7967230b8646615 Mon Sep 17 00:00:00 2001 From: Pascal Marco Caversaccio Date: Tue, 10 May 2022 00:16:09 +0200 Subject: [PATCH 03/12] adjust CHANGELOG message Signed-off-by: Pascal Marco Caversaccio --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 04a4de1136b..4023562449e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +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 that converts an `address` to its ASCII `string` hexadecimal representation. ([#3403](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3403)) + * `Strings`: add a new function `fromAddressHex` 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) From 2bc6fe5ea88f8ccff48062ef6bd5704cbd0c6d96 Mon Sep 17 00:00:00 2001 From: Pascal Marco Caversaccio Date: Tue, 10 May 2022 00:16:47 +0200 Subject: [PATCH 04/12] typo Signed-off-by: Pascal Marco Caversaccio --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4023562449e..2196165919e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +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 `fromAddressHex` that converts an `address` to its ASCII `string` hexadecimal representation. ([#3403](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3403)) + * `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) From ac902373e13ad6118c20a905a70e87a174e43111 Mon Sep 17 00:00:00 2001 From: Pascal Marco Caversaccio Date: Thu, 12 May 2022 10:08:47 +0200 Subject: [PATCH 05/12] fixed length case Signed-off-by: Pascal Marco Caversaccio --- contracts/utils/Strings.sol | 5 +++-- test/utils/Strings.test.js | 8 ++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/contracts/utils/Strings.sol b/contracts/utils/Strings.sol index 76c91684d08..30143dbcd4c 100644 --- a/contracts/utils/Strings.sol +++ b/contracts/utils/Strings.sol @@ -8,6 +8,7 @@ pragma solidity ^0.8.0; */ library Strings { bytes16 private constant _HEX_SYMBOLS = "0123456789abcdef"; + uint8 private constant _ADDRESS_LENGTH = 20; /** * @dev Converts a `uint256` to its ASCII `string` decimal representation. @@ -66,9 +67,9 @@ library Strings { } /** - * @dev Converts an `address` to its ASCII `string` hexadecimal representation. + * @dev Converts an `address` to its ASCII `string` hexadecimal representation with fixed length of 20 bytes. */ function addressToHexString(address addr) internal pure returns (string memory) { - return toHexString(uint256(uint160(addr))); + return toHexString(uint256(uint160(addr)), _ADDRESS_LENGTH); } } diff --git a/test/utils/Strings.test.js b/test/utils/Strings.test.js index 0be392c239d..e4b259dbd3f 100644 --- a/test/utils/Strings.test.js +++ b/test/utils/Strings.test.js @@ -57,10 +57,14 @@ contract('Strings', function (accounts) { }); }); - describe('from address - hex format', function () { - it('converts an address', async function () { + describe('from address - fixed 20 bytes hex format', function () { + it('converts a random address', async function () { expect(web3.utils.toChecksumAddress(await this.strings.fromAddressHex(this.strings.address))) .to.equal(this.strings.address.toString()); }); + it('converts an address with leading zeros', async function () { + const addr = '0x0000E0Ca771e21bD00057F54A68C30D400000000'; + expect(web3.utils.toChecksumAddress(await this.strings.fromAddressHex(addr))).to.equal(addr); + }); }); }); From 4eff9debff652207b6def1e7a97fa46c06477b5c Mon Sep 17 00:00:00 2001 From: Pascal Marco Caversaccio Date: Thu, 12 May 2022 10:32:39 +0200 Subject: [PATCH 06/12] update CHANGELOG Signed-off-by: Pascal Marco Caversaccio --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2196165919e..61de351b12f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +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)) + * `Strings`: add a new function `addressToHexString` that converts an `address` to its ASCII `string` hexadecimal representation with fixed length of 20 bytes. ([#3403](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3403)) ## 4.6.0 (2022-04-26) From fb77a6ec55437cef0d7dc89224faa9c0d2c08ad2 Mon Sep 17 00:00:00 2001 From: Pascal Marco Caversaccio Date: Thu, 12 May 2022 10:39:19 +0200 Subject: [PATCH 07/12] adjust wording Signed-off-by: Pascal Marco Caversaccio --- CHANGELOG.md | 2 +- contracts/utils/Strings.sol | 2 +- test/utils/Strings.test.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61de351b12f..b4307d20ef9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +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 with fixed length of 20 bytes. ([#3403](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3403)) + * `Strings`: add a new function `addressToHexString` that converts an `address` with fixed length of 20 bytes to its ASCII `string` hexadecimal representation. ([#3403](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3403)) ## 4.6.0 (2022-04-26) diff --git a/contracts/utils/Strings.sol b/contracts/utils/Strings.sol index 30143dbcd4c..f8ef96b89a1 100644 --- a/contracts/utils/Strings.sol +++ b/contracts/utils/Strings.sol @@ -67,7 +67,7 @@ library Strings { } /** - * @dev Converts an `address` to its ASCII `string` hexadecimal representation with fixed length of 20 bytes. + * @dev Converts an `address` with fixed length of 20 bytes to its ASCII `string` hexadecimal representation. */ function addressToHexString(address addr) internal pure returns (string memory) { return toHexString(uint256(uint160(addr)), _ADDRESS_LENGTH); diff --git a/test/utils/Strings.test.js b/test/utils/Strings.test.js index e4b259dbd3f..b4ccb0e36a7 100644 --- a/test/utils/Strings.test.js +++ b/test/utils/Strings.test.js @@ -57,7 +57,7 @@ contract('Strings', function (accounts) { }); }); - describe('from address - fixed 20 bytes hex format', function () { + describe('from address - hex format', function () { it('converts a random address', async function () { expect(web3.utils.toChecksumAddress(await this.strings.fromAddressHex(this.strings.address))) .to.equal(this.strings.address.toString()); From f13655eda9d48ea5c617ab8c32d7b49a4103bb59 Mon Sep 17 00:00:00 2001 From: Pascal Marco Caversaccio Date: Thu, 12 May 2022 17:23:07 +0200 Subject: [PATCH 08/12] add comment on checksum Signed-off-by: Pascal Marco Caversaccio --- CHANGELOG.md | 2 +- contracts/utils/Strings.sol | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b4307d20ef9..246c68a829a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +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` with fixed length of 20 bytes to its ASCII `string` hexadecimal representation. ([#3403](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3403)) + * `Strings`: add a new function `addressToHexString` that converts an `address` with fixed length of 20 bytes to its not checksummed ASCII `string` hexadecimal representation. ([#3403](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3403)) ## 4.6.0 (2022-04-26) diff --git a/contracts/utils/Strings.sol b/contracts/utils/Strings.sol index f8ef96b89a1..275c1acdd7b 100644 --- a/contracts/utils/Strings.sol +++ b/contracts/utils/Strings.sol @@ -67,7 +67,7 @@ library Strings { } /** - * @dev Converts an `address` with fixed length of 20 bytes to its ASCII `string` hexadecimal representation. + * @dev Converts an `address` with fixed length of 20 bytes to its not checksummed ASCII `string` hexadecimal representation. */ function addressToHexString(address addr) internal pure returns (string memory) { return toHexString(uint256(uint160(addr)), _ADDRESS_LENGTH); From f4916184bdb2ae8493457208b5001fda5034ab1f Mon Sep 17 00:00:00 2001 From: Pascal Marco Caversaccio Date: Fri, 13 May 2022 16:49:31 +0200 Subject: [PATCH 09/12] refactor for function overloading Signed-off-by: Pascal Marco Caversaccio --- CHANGELOG.md | 2 +- contracts/mocks/StringsMock.sol | 4 ++-- contracts/utils/Strings.sol | 2 +- test/utils/Strings.test.js | 6 +++--- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07bebe084b6..007688e535c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ * `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)) * `ERC20FlashMint`: Add customizable flash fee receiver. ([#3327](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3327)) - * `Strings`: add a new function `addressToHexString` that converts an `address` with fixed length of 20 bytes to its not checksummed ASCII `string` hexadecimal representation. ([#3403](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3403)) + * `Strings`: add a new overloaded function `toHexString` that converts an `address` with fixed length of 20 bytes to its not checksummed ASCII `string` hexadecimal representation. ([#3403](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3403)) ## 4.6.0 (2022-04-26) diff --git a/contracts/mocks/StringsMock.sol b/contracts/mocks/StringsMock.sol index a7cf47a8e94..b8622680bab 100644 --- a/contracts/mocks/StringsMock.sol +++ b/contracts/mocks/StringsMock.sol @@ -17,7 +17,7 @@ contract StringsMock { return Strings.toHexString(value, length); } - function fromAddressHex(address addr) public pure returns (string memory) { - return Strings.addressToHexString(addr); + function fromAddressHexFixed(address addr) public pure returns (string memory) { + return Strings.toHexString(addr); } } diff --git a/contracts/utils/Strings.sol b/contracts/utils/Strings.sol index 275c1acdd7b..5b3e12d135c 100644 --- a/contracts/utils/Strings.sol +++ b/contracts/utils/Strings.sol @@ -69,7 +69,7 @@ library Strings { /** * @dev Converts an `address` with fixed length of 20 bytes to its not checksummed ASCII `string` hexadecimal representation. */ - function addressToHexString(address addr) internal pure returns (string memory) { + function toHexString(address addr) internal pure returns (string memory) { return toHexString(uint256(uint160(addr)), _ADDRESS_LENGTH); } } diff --git a/test/utils/Strings.test.js b/test/utils/Strings.test.js index b4ccb0e36a7..899d55da4cb 100644 --- a/test/utils/Strings.test.js +++ b/test/utils/Strings.test.js @@ -57,14 +57,14 @@ contract('Strings', function (accounts) { }); }); - describe('from address - hex format', function () { + describe('from address - fixed hex format', function () { it('converts a random address', async function () { - expect(web3.utils.toChecksumAddress(await this.strings.fromAddressHex(this.strings.address))) + expect(web3.utils.toChecksumAddress(await this.strings.fromAddressHexFixed(this.strings.address))) .to.equal(this.strings.address.toString()); }); it('converts an address with leading zeros', async function () { const addr = '0x0000E0Ca771e21bD00057F54A68C30D400000000'; - expect(web3.utils.toChecksumAddress(await this.strings.fromAddressHex(addr))).to.equal(addr); + expect(web3.utils.toChecksumAddress(await this.strings.fromAddressHexFixed(addr))).to.equal(addr); }); }); }); From 09e83ffab7b9737b5a446581ccda19e77a517a0e Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Fri, 13 May 2022 15:32:42 -0300 Subject: [PATCH 10/12] use deterministic address for test --- test/utils/Strings.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/utils/Strings.test.js b/test/utils/Strings.test.js index 899d55da4cb..0f34bf57622 100644 --- a/test/utils/Strings.test.js +++ b/test/utils/Strings.test.js @@ -59,8 +59,8 @@ contract('Strings', function (accounts) { describe('from address - fixed hex format', function () { it('converts a random address', async function () { - expect(web3.utils.toChecksumAddress(await this.strings.fromAddressHexFixed(this.strings.address))) - .to.equal(this.strings.address.toString()); + const addr = '0xa9036907dccae6a1e0033479b12e837e5cf5a02f'; + expect(await this.strings.fromAddressHexFixed(addr)).to.equal(addr); }); it('converts an address with leading zeros', async function () { const addr = '0x0000E0Ca771e21bD00057F54A68C30D400000000'; From a9335a4f29bbc7a1357fb65efaf793f7fd55221f Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Fri, 13 May 2022 15:33:03 -0300 Subject: [PATCH 11/12] test against lowercase address instead of checksummed --- test/utils/Strings.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/utils/Strings.test.js b/test/utils/Strings.test.js index 0f34bf57622..e7dfc9f4982 100644 --- a/test/utils/Strings.test.js +++ b/test/utils/Strings.test.js @@ -63,8 +63,8 @@ contract('Strings', function (accounts) { expect(await this.strings.fromAddressHexFixed(addr)).to.equal(addr); }); it('converts an address with leading zeros', async function () { - const addr = '0x0000E0Ca771e21bD00057F54A68C30D400000000'; - expect(web3.utils.toChecksumAddress(await this.strings.fromAddressHexFixed(addr))).to.equal(addr); + const addr = '0x0000e0ca771e21bd00057f54a68c30d400000000'; + expect(await this.strings.fromAddressHexFixed(addr)).to.equal(addr); }); }); }); From 277ae92178899324cd771dee4a4fe93760a17a78 Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Fri, 13 May 2022 15:33:13 -0300 Subject: [PATCH 12/12] whitespace --- test/utils/Strings.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/utils/Strings.test.js b/test/utils/Strings.test.js index e7dfc9f4982..8dda829ea96 100644 --- a/test/utils/Strings.test.js +++ b/test/utils/Strings.test.js @@ -62,6 +62,7 @@ contract('Strings', function (accounts) { const addr = '0xa9036907dccae6a1e0033479b12e837e5cf5a02f'; expect(await this.strings.fromAddressHexFixed(addr)).to.equal(addr); }); + it('converts an address with leading zeros', async function () { const addr = '0x0000e0ca771e21bd00057f54a68c30d400000000'; expect(await this.strings.fromAddressHexFixed(addr)).to.equal(addr);