diff --git a/hedera-node/hapi-utils/src/main/java/com/hedera/node/app/hapi/utils/ethereum/EthTxData.java b/hedera-node/hapi-utils/src/main/java/com/hedera/node/app/hapi/utils/ethereum/EthTxData.java index 13025f545903..99546d2e2fd1 100644 --- a/hedera-node/hapi-utils/src/main/java/com/hedera/node/app/hapi/utils/ethereum/EthTxData.java +++ b/hedera-node/hapi-utils/src/main/java/com/hedera/node/app/hapi/utils/ethereum/EthTxData.java @@ -21,7 +21,6 @@ import java.util.Objects; import org.apache.tuweni.bytes.Bytes; import org.apache.tuweni.bytes.Bytes32; -import org.bouncycastle.util.BigIntegers; public record EthTxData( byte[] rawTx, @@ -111,6 +110,20 @@ public static int getTransactionType(byte[] data) { } } + /// Returns an unsigned byte[] representation of a BigInteger, dropping the leading zero byte + /// if present. Adopted from org.bouncycastle.util.BigIntegers.asUnsignedByteArray(). + public static byte[] asUnsignedByteArray(final BigInteger value) { + final byte[] bytes = value.toByteArray(); + + if (bytes[0] == 0 && bytes.length != 1) { + final byte[] tmp = new byte[bytes.length - 1]; + System.arraycopy(bytes, 1, tmp, 0, tmp.length); + return tmp; + } + + return bytes; + } + public EthTxData replaceCallData(final byte[] newCallData) { return new EthTxData( null, @@ -770,9 +783,9 @@ private static byte[] deriveChainId(@NonNull final BigInteger vBI) { // after EIP155 the chain id is equal to CHAIN_ID = (v - {0,1} - 35) / 2. // asUnsignedByteArray avoids the leading sign byte BigInteger.toByteArray adds when the // top bit is set — see https://github.com/hashgraph/hedera-services/issues/15953 - return BigIntegers.asUnsignedByteArray(BigInteger.valueOf((v - 35) >> 1)); + return EthTxData.asUnsignedByteArray(BigInteger.valueOf((v - 35) >> 1)); } - return BigIntegers.asUnsignedByteArray( + return EthTxData.asUnsignedByteArray( vBI.subtract(BigInteger.valueOf(35)).shiftRight(1)); } diff --git a/hedera-node/hapi-utils/src/test/java/com/hedera/node/app/hapi/utils/ethereum/EthTxDataTest.java b/hedera-node/hapi-utils/src/test/java/com/hedera/node/app/hapi/utils/ethereum/EthTxDataTest.java index b81b1248d945..cbede788248b 100644 --- a/hedera-node/hapi-utils/src/test/java/com/hedera/node/app/hapi/utils/ethereum/EthTxDataTest.java +++ b/hedera-node/hapi-utils/src/test/java/com/hedera/node/app/hapi/utils/ethereum/EthTxDataTest.java @@ -25,7 +25,6 @@ import java.util.HexFormat; import java.util.List; import org.bouncycastle.asn1.sec.SECNamedCurves; -import org.bouncycastle.util.BigIntegers; import org.hiero.base.utility.CommonUtils; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -792,7 +791,7 @@ void bigPositiveValueWithDifferentTypes(final EthTransactionType type) { @Test void populateEthTxDataComparedToUnsignedByteArrayNoExtraByteAdded() { final var subject = EthTxData.populateEthTxData(HexFormat.of().parseHex(RAW_TX_TYPE_0_WITH_CHAIN_ID_11155111)); - final byte[] passingChainId = BigIntegers.asUnsignedByteArray(BigInteger.valueOf(11155111L)); + final byte[] passingChainId = EthTxData.asUnsignedByteArray(BigInteger.valueOf(11155111L)); assertNotNull(subject); assertEquals(HexFormat.of().formatHex(subject.chainId()), HexFormat.of().formatHex(passingChainId)); } diff --git a/hedera-node/hedera-app/src/test/java/com/hedera/node/app/blocks/impl/BlockStreamManagerImplTest.java b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/blocks/impl/BlockStreamManagerImplTest.java index 50e1a449893a..58a2f4448c2a 100644 --- a/hedera-node/hedera-app/src/test/java/com/hedera/node/app/blocks/impl/BlockStreamManagerImplTest.java +++ b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/blocks/impl/BlockStreamManagerImplTest.java @@ -104,7 +104,6 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; import java.util.function.Function; -import org.bouncycastle.util.Arrays; import org.hiero.base.crypto.Hash; import org.hiero.base.crypto.test.fixtures.CryptoRandomUtils; import org.hiero.consensus.model.event.ConsensusEvent; @@ -2185,7 +2184,7 @@ private PlatformState platformStateWithFreezeTime(@Nullable final Instant freeze private void mockRound(Instant timestamp, long roundNum) { given(round.getRoundNum()).willReturn(roundNum); - lenient().when(round.iterator()).thenReturn(new Arrays.Iterator<>(new ConsensusEvent[] {mockEvent})); + lenient().when(round.iterator()).thenReturn(List.of(mockEvent).iterator()); lenient().when(round.getConsensusTimestamp()).thenReturn(timestamp); } @@ -2217,7 +2216,7 @@ private void mockRoundWithTxnTimestamp(Instant timestamp, long roundNum) { txn.setConsensusTimestamp(timestamp); lenient() .when(mockEvent.consensusTransactionIterator()) - .thenReturn(new Arrays.Iterator<>(new ConsensusTransaction[] {txn})); + .thenReturn(List.of(txn).iterator()); } @Test