Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix address encoding #15

Merged
merged 3 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.quarkchain</groupId>
<artifactId>web3j</artifactId>
<version>0.0.1</version>
<version>0.0.2</version>
<inceptionYear>2019</inceptionYear>
<licenses>
<license>
Expand Down Expand Up @@ -39,10 +39,16 @@
<scope>compile</scope>
</dependency>
<dependency>
<!-- used by sample -->
<!-- used by sample -->
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import java.util.ArrayList;
import java.util.List;

import org.quarkchain.web3j.crypto.Sign.SignatureData;
import org.quarkchain.web3j.protocol.core.request.EvmTransaction;
import org.quarkchain.web3j.protocol.core.request.TxData;
import org.quarkchain.web3j.protocol.core.request.EvmTransaction.TxDataUnsigned;
import org.quarkchain.web3j.protocol.core.request.TxData;
import org.quarkchain.web3j.rlp.RlpEncoder;
import org.quarkchain.web3j.rlp.RlpList;
import org.quarkchain.web3j.rlp.RlpString;
Expand Down Expand Up @@ -73,8 +72,8 @@ private static List<RlpType> asRlpValues(EvmTransaction evmTransaction) {
// an empty to address (contract creation) should not be encoded as a numeric 0
// value
String to = txData.getTo();
if (to.length() > 0) {
result.add(RlpString.create(Numeric.toBigInt(to)));
if (to != null && to.length() > 0) {
result.add(RlpString.create(Numeric.hexStringToByteArray(to)));
} else {
result.add(RlpString.create(""));
}
Expand All @@ -91,4 +90,8 @@ private static List<RlpType> asRlpValues(EvmTransaction evmTransaction) {
result.add(RlpString.create(txData.getS()));
return result;
}

public static List<RlpType> asRlpValuesPub(EvmTransaction evmTransaction) {
return asRlpValues(evmTransaction);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.quarkchain.web3j.crypto;

import java.math.BigInteger;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.quarkchain.web3j.protocol.core.request.EvmTransaction;
import org.quarkchain.web3j.protocol.core.request.TxData;
import org.quarkchain.web3j.rlp.RlpString;
import org.quarkchain.web3j.rlp.RlpType;
import org.quarkchain.web3j.utils.Numeric;

import static org.junit.jupiter.api.Assertions.*;

public class TransactionEncoderTest {

@Test
public void testAsRlpValues() {
String toAddress = "0x00aab646f0aad5afac5ed6724434c4439904829400000000";

TxData txData = new TxData(
BigInteger.ONE,
BigInteger.valueOf(10_000_000_000L),
BigInteger.valueOf(21000),
toAddress, // to
BigInteger.valueOf(1_000_000_000L),
"0x",
"1",
"0x9148d72cc71f3d8a8ae93a1e28f22449bd9fc70f00000000",
"0x00aab646f0aad5afac5ed6724434c4439904829400000000",
"0x",
"0x",
BigInteger.valueOf(27),
new BigInteger("48eafed96f1f9dc716b377260070eb188f12a3a899b69772972c9f7094d453ef", 16),
new BigInteger("5c534aa35c576ede5d914e381cf4e5ef8f8f19ebdacdf165aa7b6420ac735bde", 16)
);

txData.setVersion(BigInteger.ZERO);

EvmTransaction evmTransaction = new EvmTransaction();
evmTransaction.setData(txData);

List<RlpType> result = TransactionEncoder.asRlpValuesPub(evmTransaction);
assertEquals(15, result.size(), "RLP size is 15");
assertEquals(RlpString.create(BigInteger.ONE), result.get(0),
"Expected RlpType does not match the actual result.");
assertEquals(RlpString.create(BigInteger.valueOf(10_000_000_000L)), result.get(1),
"Expected gasPrice as BigInteger");
assertEquals(RlpString.create(BigInteger.valueOf(21000)), result.get(2), "Expected gas as BigInteger");

// to
RlpString rlpString = (RlpString) result.get(3);
String actualHex = Numeric.toHexString(rlpString.getBytes());
System.out.println("Actual content (HEX) = " + actualHex);
assertEquals(toAddress, actualHex, "To address mismatch");
}
}