diff --git a/src/main/java/io/zksync/utils/WalletUtils.java b/src/main/java/io/zksync/utils/WalletUtils.java index 4f0a002..4b028ba 100644 --- a/src/main/java/io/zksync/utils/WalletUtils.java +++ b/src/main/java/io/zksync/utils/WalletUtils.java @@ -130,8 +130,15 @@ public static BigInteger estimateCustomBridgeDepositL2Gas(String l1BridgeAddress } public static String applyL1ToL2Alias(String address){ - String a = Numeric.toHexStringWithPrefix(Numeric.toBigInt(address).add(Numeric.toBigInt("0x1111000000000000000000000000000000001111")).mod(BigInteger.valueOf(2).pow(160))); - return Numeric.toHexStringWithPrefix(Numeric.toBigInt(address).add(Numeric.toBigInt("0x1111000000000000000000000000000000001111")).mod(BigInteger.valueOf(2).pow(160))); + return Numeric.toHexStringWithPrefixZeroPadded(Numeric.toBigInt(address).add(Numeric.toBigInt("0x1111000000000000000000000000000000001111")).mod(BigInteger.valueOf(2).pow(160)), 40); + } + + public static String undoL1ToL2Alias(String address){ + BigInteger result = Numeric.toBigInt(address).subtract(Numeric.toBigInt("0x1111000000000000000000000000000000001111")); + if (result.compareTo(BigInteger.ZERO) < 0){ + result.add(BigInteger.valueOf(2).pow(160)); + } + return Numeric.toHexStringWithPrefixZeroPadded(result, 40); } public static String getERC20DefaultBridgeData(String l1TokenAddress, Web3j provider, Credentials credentials, ContractGasProvider gasProvider){ diff --git a/src/test/java/io/zksync/utils/ContractDeployerTest.java b/src/test/java/io/zksync/utils/ContractDeployerTest.java index 86bfb56..a7dc819 100644 --- a/src/test/java/io/zksync/utils/ContractDeployerTest.java +++ b/src/test/java/io/zksync/utils/ContractDeployerTest.java @@ -20,6 +20,18 @@ public void computeL2Create2AddressActual() { Assertions.assertEquals(expected, result.getValue()); } + @Test + public void testApplyAlias(){ + String nonZeroPadded = WalletUtils.applyL1ToL2Alias("0x702942B8205E5dEdCD3374E5f4419843adA76Eeb"); + Assertions.assertEquals("0x813A42B8205E5DedCd3374e5f4419843ADa77FFC".toLowerCase(), nonZeroPadded.toLowerCase()); + } + + @Test + public void testUndoAlias(){ + String nonZeroPadded = WalletUtils.applyL1ToL2Alias("0x702942B8205E5dEdCD3374E5f4419843adA76Eeb"); + Assertions.assertEquals("0x813A42B8205E5DedCd3374e5f4419843ADa77FFC".toLowerCase(), nonZeroPadded.toLowerCase()); + } + @Test public void computeL2CreateAddressActual() { String expected = "0x5107b7154dfc1d3b7f1c4e19b5087e1d3393bcf4";