Skip to content

Commit 337bfd5

Browse files
authored
Add utility function for converting an address to checksummed string (#5067)
1 parent 8a890ff commit 337bfd5

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

.changeset/forty-dodos-visit.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openzeppelin-solidity': minor
3+
---
4+
5+
`Strings`: Added a utility function for converting an address to checksummed string.

contracts/utils/Strings.sol

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,30 @@ library Strings {
8585
return toHexString(uint256(uint160(addr)), ADDRESS_LENGTH);
8686
}
8787

88+
/**
89+
* @dev Converts an `address` with fixed length of 20 bytes to its checksummed ASCII `string` hexadecimal
90+
* representation, according to EIP-55.
91+
*/
92+
function toChecksumHexString(address addr) internal pure returns (string memory) {
93+
bytes memory buffer = bytes(toHexString(addr));
94+
95+
// hash the hex part of buffer (skip length + 2 bytes, length 40)
96+
uint256 hashValue;
97+
assembly ("memory-safe") {
98+
hashValue := shr(96, keccak256(add(buffer, 0x22), 40))
99+
}
100+
101+
for (uint256 i = 41; i > 1; --i) {
102+
// possible values for buffer[i] are 48 (0) to 57 (9) and 97 (a) to 102 (f)
103+
if (hashValue & 0xf > 7 && uint8(buffer[i]) > 96) {
104+
// case shift by xoring with 0x20
105+
buffer[i] ^= 0x20;
106+
}
107+
hashValue >>= 4;
108+
}
109+
return string(buffer);
110+
}
111+
88112
/**
89113
* @dev Returns true if the two strings are equal.
90114
*/

test/utils/Strings.test.js

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,42 @@ describe('Strings', function () {
108108
});
109109
});
110110

111-
describe('toHexString address', function () {
112-
it('converts a random address', async function () {
113-
const addr = '0xa9036907dccae6a1e0033479b12e837e5cf5a02f';
114-
expect(await this.mock.getFunction('$toHexString(address)')(addr)).to.equal(addr);
111+
describe('addresses', function () {
112+
const addresses = [
113+
'0xa9036907dccae6a1e0033479b12e837e5cf5a02f', // Random address
114+
'0x0000e0ca771e21bd00057f54a68c30d400000000', // Leading and trailing zeros
115+
// EIP-55 reference
116+
'0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed',
117+
'0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359',
118+
'0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB',
119+
'0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb',
120+
'0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359',
121+
'0x52908400098527886E0F7030069857D2E4169EE7',
122+
'0x8617E340B3D01FA5F11F306F4090FD50E238070D',
123+
'0xde709f2102306220921060314715629080e2fb77',
124+
'0x27b1fdb04752bbc536007a920d24acb045561c26',
125+
'0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed',
126+
'0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359',
127+
'0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB',
128+
'0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb',
129+
];
130+
131+
describe('toHexString', function () {
132+
for (const addr of addresses) {
133+
it(`converts ${addr}`, async function () {
134+
expect(await this.mock.getFunction('$toHexString(address)')(addr)).to.equal(addr.toLowerCase());
135+
});
136+
}
115137
});
116138

117-
it('converts an address with leading zeros', async function () {
118-
const addr = '0x0000e0ca771e21bd00057f54a68c30d400000000';
119-
expect(await this.mock.getFunction('$toHexString(address)')(addr)).to.equal(addr);
139+
describe('toChecksumHexString', function () {
140+
for (const addr of addresses) {
141+
it(`converts ${addr}`, async function () {
142+
expect(await this.mock.getFunction('$toChecksumHexString(address)')(addr)).to.equal(
143+
ethers.getAddress(addr.toLowerCase()),
144+
);
145+
});
146+
}
120147
});
121148
});
122149

0 commit comments

Comments
 (0)