From 76fc41076ca180691163f1f10c52da8f33a8030e Mon Sep 17 00:00:00 2001 From: Benjamin Bollen Date: Thu, 2 May 2024 18:48:19 +0200 Subject: [PATCH] (NameRegistry): short names should always be padded to 12 characters --- src/names/NameRegistry.sol | 2 +- test/names/MockNameRegistry.sol | 4 ++++ test/names/NameRegistry.t.sol | 10 ++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/names/NameRegistry.sol b/src/names/NameRegistry.sol index e7dddf8..7200332 100644 --- a/src/names/NameRegistry.sol +++ b/src/names/NameRegistry.sol @@ -311,7 +311,7 @@ contract NameRegistry is Base58Converter, INameRegistry, INameRegistryErrors, IC string memory base58FullAddress = _toBase58(uint256(uint160(_avatar))); return string(abi.encodePacked(DEFAULT_CIRCLES_NAME_PREFIX, base58FullAddress)); } - string memory base58ShortName = _toBase58(uint256(shortName)); + string memory base58ShortName = _toBase58WithPadding(uint256(shortName)); return string(abi.encodePacked(DEFAULT_CIRCLES_NAME_PREFIX, base58ShortName)); } diff --git a/test/names/MockNameRegistry.sol b/test/names/MockNameRegistry.sol index cecc2af..3b27ac2 100644 --- a/test/names/MockNameRegistry.sol +++ b/test/names/MockNameRegistry.sol @@ -55,4 +55,8 @@ contract MockNameRegistry is NameRegistry { function toBase58WithPadding(uint256 _data) external pure returns (string memory) { return _toBase58WithPadding(_data); } + + function storeShortName(address _avatar, uint72 _shortName) external { + _storeShortName(_avatar, _shortName, 0); + } } diff --git a/test/names/NameRegistry.t.sol b/test/names/NameRegistry.t.sol index 05f0e02..ce4ae3e 100644 --- a/test/names/NameRegistry.t.sol +++ b/test/names/NameRegistry.t.sol @@ -59,6 +59,16 @@ contract NamesTest is Test, HumanRegistration, Base58Decode { assertEq(shortName, "Rings-uNJGyf6sN6vY"); } + function testShortNameWithPadding() public { + // 42 converts to "j" in base58 + assertEq(mockNameRegistry.toBase58(42), "j"); + + // but as a short name it shold be padded to 12 characters + mockNameRegistry.storeShortName(addresses[0], 42); + string memory shortName = mockNameRegistry.getShortOrLongName(addresses[0]); + assertEq(shortName, "Rings-11111111111j"); + } + function testBase58Conversion() public { assertEq(mockNameRegistry.toBase58(0), "1"); assertEq(mockNameRegistry.toBase58(mockNameRegistry.MAX_SHORT_NAME()), "zzzzzzzzzzzz");