Skip to content

Commit

Permalink
Fix/1564 change exception message fix eip 2544 (#1612)
Browse files Browse the repository at this point in the history
* small refactoring Eip-1559 related functionality

* fix name validation according to eip-2544, add GOERLI testnet chain Id, change the exception message on more meaningful

* optimize imports

* fix check string on Empty
  • Loading branch information
andrii-kl authored Jan 31, 2022
1 parent d4979ca commit f64e7af
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 16 deletions.
3 changes: 3 additions & 0 deletions core/src/main/java/org/web3j/ens/Contracts.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class Contracts {
public static final String MAINNET = "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e";
public static final String ROPSTEN = "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e";
public static final String RINKEBY = "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e";
public static final String GOERLI = "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e";

public static String resolveRegistryContract(String chainId) {
final Long chainIdLong = Long.parseLong(chainId);
Expand All @@ -29,6 +30,8 @@ public static String resolveRegistryContract(String chainId) {
return ROPSTEN;
} else if (chainIdLong.equals(ChainIdLong.RINKEBY)) {
return RINKEBY;
} else if (chainIdLong.equals(ChainIdLong.GOERLI)) {
return GOERLI;
} else {
throw new EnsResolutionException(
"Unable to resolve ENS registry contract for network id: " + chainId);
Expand Down
25 changes: 16 additions & 9 deletions core/src/main/java/org/web3j/ens/EnsResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
import org.web3j.tx.TransactionManager;
import org.web3j.tx.gas.DefaultGasProvider;
import org.web3j.utils.Numeric;
import org.web3j.utils.Strings;

/** Resolution logic for contract addresses. */
/** Resolution logic for contract addresses. According to https://eips.ethereum.org/EIPS/eip-2544 */
public class EnsResolver {

public static final long DEFAULT_SYNC_THRESHOLD = 1000 * 60 * 3;
Expand Down Expand Up @@ -83,25 +84,31 @@ protected PublicResolver obtainPublicResolver(String ensName) {
}
}

public String resolve(String contractId) {
if (isValidEnsName(contractId, addressLength)) {
PublicResolver resolver = obtainPublicResolver(contractId);
public String resolve(String ensName) {

byte[] nameHash = NameHash.nameHashAsBytes(contractId);
String contractAddress = null;
if (Strings.isBlank(ensName) || (ensName.trim().length() == 1 && ensName.contains("."))) {
return null;
}

if (isValidEnsName(ensName, addressLength)) {
PublicResolver resolver = obtainPublicResolver(ensName);

byte[] nameHash = NameHash.nameHashAsBytes(ensName);
String contractAddress;
try {
contractAddress = resolver.addr(nameHash).send();
} catch (Exception e) {
throw new RuntimeException("Unable to execute Ethereum request", e);
throw new RuntimeException(
"ENS resolver exception, unable to execute request: ", e);
}

if (!WalletUtils.isValidAddress(contractAddress)) {
throw new RuntimeException("Unable to resolve address for name: " + contractId);
throw new RuntimeException("Unable to resolve address for name: " + ensName);
} else {
return contractAddress;
}
} else {
return contractId;
return ensName;
}
}

Expand Down
8 changes: 5 additions & 3 deletions core/src/main/java/org/web3j/service/TxHSMSignService.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ public byte[] sign(RawTransaction rawTransaction, long chainId) {
byte[] finalBytes;
byte[] encodedTransaction;
Sign.SignatureData signatureData;
boolean isNewTx =
// Legacy tx is tx before Eip1559, should have chainId as an additional parameter.
// After Eip1559 chainId is a part of tx.
boolean isLegacy =
chainId > ChainId.NONE && rawTransaction.getType().equals(TransactionType.LEGACY);

if (isNewTx) {
if (isLegacy) {
encodedTransaction = encode(rawTransaction, chainId);
} else {
encodedTransaction = encode(rawTransaction);
Expand All @@ -51,7 +53,7 @@ public byte[] sign(RawTransaction rawTransaction, long chainId) {

signatureData = hsmRequestProcessor.callHSM(messageHash, hsmPass);

if (isNewTx) {
if (isLegacy) {
signatureData = createEip155SignatureData(signatureData, chainId);
}

Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/org/web3j/tx/ChainIdLong.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class ChainIdLong {
public static final long EXPANSE_MAINNET = 2;
public static final long ROPSTEN = 3;
public static final long RINKEBY = 4;
public static final long GOERLI = 5;
public static final long ROOTSTOCK_MAINNET = 30;
public static final long ROOTSTOCK_TESTNET = 31;
public static final long KOVAN = 42;
Expand Down
10 changes: 10 additions & 0 deletions core/src/test/java/org/web3j/ens/EnsResolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
Expand Down Expand Up @@ -82,6 +83,14 @@ public void testResolve() throws Exception {
ensResolver.resolve("web3j.eth"), ("0x19e03255f667bdfd50a32722df860b1eeaf4d635"));
}

@Test
public void testResolveEnsNameEmptyOrDot() throws Exception {
assertNull(ensResolver.resolve(" "));
assertNull(ensResolver.resolve(""));
assertNull(ensResolver.resolve("."));
assertNull(ensResolver.resolve(" . "));
}

@Test
public void testReverseResolve() throws Exception {
configureSyncing(false);
Expand Down Expand Up @@ -165,5 +174,6 @@ public void testIsEnsName() {
assertFalse(isValidEnsName("19e03255f667bdfd50a32722df860b1eeaf4d635"));

assertTrue(isValidEnsName(""));
assertTrue(isValidEnsName("."));
}
}
28 changes: 25 additions & 3 deletions crypto/src/main/java/org/web3j/crypto/TransactionEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.nio.ByteBuffer;
import java.util.List;

import org.web3j.crypto.transaction.type.TransactionType;
import org.web3j.crypto.exception.CryptoWeb3jException;
import org.web3j.rlp.RlpEncoder;
import org.web3j.rlp.RlpList;
import org.web3j.rlp.RlpType;
Expand All @@ -31,6 +31,12 @@ public class TransactionEncoder {
private static final int CHAIN_ID_INC = 35;
private static final int LOWER_REAL_V = 27;

/**
* Use for new transactions Eip1559 (this txs has a new field chainId) or an old one before
* Eip155
*
* @return signature
*/
public static byte[] signMessage(RawTransaction rawTransaction, Credentials credentials) {
byte[] encodedTransaction = encode(rawTransaction);
Sign.SignatureData signatureData =
Expand All @@ -39,10 +45,16 @@ public static byte[] signMessage(RawTransaction rawTransaction, Credentials cred
return encode(rawTransaction, signatureData);
}

/**
* Use for legacy txs (after Eip155 before Eip1559)
*
* @return signature
*/
public static byte[] signMessage(
RawTransaction rawTransaction, long chainId, Credentials credentials) {

if (!rawTransaction.getType().equals(TransactionType.LEGACY)) {
// Eip1559: Tx has ChainId inside
if (rawTransaction.getType().isEip1559()) {
return signMessage(rawTransaction, credentials);
}

Expand Down Expand Up @@ -80,7 +92,16 @@ public static byte[] encode(RawTransaction rawTransaction) {
return encode(rawTransaction, null);
}

/**
* Encode transaction with chainId together, it make sense only for Legacy transactions
*
* @return encoded bytes
*/
public static byte[] encode(RawTransaction rawTransaction, long chainId) {
if (!rawTransaction.getType().isLegacy()) {
throw new CryptoWeb3jException("Incorrect transaction type. Tx type should be Legacy.");
}

Sign.SignatureData signatureData =
new Sign.SignatureData(longToBytes(chainId), new byte[] {}, new byte[] {});
return encode(rawTransaction, signatureData);
Expand All @@ -95,7 +116,8 @@ public static byte[] encode(RawTransaction rawTransaction, Sign.SignatureData si
List<RlpType> values = asRlpValues(rawTransaction, signatureData);
RlpList rlpList = new RlpList(values);
byte[] encoded = RlpEncoder.encode(rlpList);
if (!rawTransaction.getType().equals(TransactionType.LEGACY)) {

if (rawTransaction.getType().isEip1559()) {
return ByteBuffer.allocate(encoded.length + 1)
.put(rawTransaction.getType().getRlpType())
.put(encoded)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2022 Web3 Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.web3j.crypto.exception;

public class CryptoWeb3jException extends RuntimeException {

public CryptoWeb3jException() {}

public CryptoWeb3jException(String message) {
super(message);
}

public CryptoWeb3jException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public LegacyTransaction(
this(LEGACY, nonce, gasPrice, gasLimit, to, value, data);
}

// LegacyTransaction can have only one tx type. Use another constructor.
@Deprecated
public LegacyTransaction(
TransactionType type,
BigInteger nonce,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ public enum TransactionType {
public Byte getRlpType() {
return type;
}

public boolean isLegacy() {
return this.equals(TransactionType.LEGACY);
}

public boolean isEip1559() {
return this.equals(TransactionType.EIP1559);
}
}
18 changes: 17 additions & 1 deletion utils/src/main/java/org/web3j/utils/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,23 @@ public static String repeat(char value, int n) {
return new String(new char[n]).replace("\0", String.valueOf(value));
}

/**
* Returns true if the string is empty, otherwise false.
*
* @param s String value
* @return is given string is Empty or not
*/
public static boolean isEmpty(String s) {
return s == null || s.length() == 0;
return s == null || s.isEmpty();
}

/**
* Returns true if the string is empty or contains only white space codepoints, otherwise false.
*
* @param s String value
* @return is given string is Blank or not
*/
public static boolean isBlank(String s) {
return s == null || s.trim().isEmpty();
}
}

0 comments on commit f64e7af

Please sign in to comment.