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

EIP-4844 support #2000

Merged
merged 10 commits into from
Feb 13, 2024
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
8 changes: 7 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ ext {
jnr_unixsocketVersion = '0.38.17'
okhttpVersion = '4.9.0'
rxjavaVersion = '2.2.2'
slf4jVersion = '1.7.30'
slf4jVersion = '2.0.11'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's move to 2.0.12, is the latest one

javaWebSocketVersion = '1.5.3'
picocliVersion = '3.0.0'
ensAdraffyVersion = '0.1.2'
kzg4844Version = '0.8.0'
tuweniVersion = '2.4.2'
// test dependencies
equalsverifierVersion = '3.14.1'
junitVersion = '5.5.2'
Expand Down Expand Up @@ -72,6 +74,10 @@ allprojects {
useJUnitPlatform()
}

repositories {
maven { url "https://artifacts.consensys.net/public/maven/maven/" }
}

dependencies {
testImplementation "org.junit.jupiter:junit-jupiter:$junitVersion"
testImplementation "org.mockito:mockito-junit-jupiter:$mockitoJunitVersion"
Expand Down
1 change: 0 additions & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ plugins {
id 'org.ajoberstar.git-publish'
}


description 'web3j is a lightweight Java library for integration with Ethereum clients'

dependencies {
Expand Down
3 changes: 3 additions & 0 deletions crypto/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ dependencies {
project(':utils'),
"org.slf4j:slf4j-api:$slf4jVersion",
"com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
implementation("tech.pegasys:jc-kzg-4844:$kzg4844Version")
implementation("io.tmio:tuweni-bytes:$tuweniVersion")
implementation("io.tmio:tuweni-units:$tuweniVersion")
}

configurations { testArtifacts.extendsFrom testRuntime }
Expand Down
67 changes: 67 additions & 0 deletions crypto/src/main/java/org/web3j/crypto/Blob.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2024 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;

import java.util.Objects;

import org.apache.tuweni.bytes.Bytes;

public class Blob {
NickSneo marked this conversation as resolved.
Show resolved Hide resolved

final Bytes data;

/**
* Create a new Blob.
*
* @param data that represents the blob in Bytes.
*/
public Blob(final Bytes data) {
this.data = data;
}

Check warning on line 30 in crypto/src/main/java/org/web3j/crypto/Blob.java

View check run for this annotation

Codecov / codecov/patch

crypto/src/main/java/org/web3j/crypto/Blob.java#L28-L30

Added lines #L28 - L30 were not covered by tests

/**
* Create a new Blob.
*
* @param data that represents the blob in byte[].
*/
public Blob(final byte[] data) {
this.data = Bytes.wrap(data);
}

/**
* Get the data of the Blob.
*
* @return the data.
*/
public Bytes getData() {
return data;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Blob blob = (Blob) o;
return Objects.equals(data, blob.data);

Check warning on line 55 in crypto/src/main/java/org/web3j/crypto/Blob.java

View check run for this annotation

Codecov / codecov/patch

crypto/src/main/java/org/web3j/crypto/Blob.java#L54-L55

Added lines #L54 - L55 were not covered by tests
}

@Override
public int hashCode() {
return data != null ? data.hashCode() : 0;
}

@Override
public String toString() {
return "Blob{" + "data=" + data + '}';

Check warning on line 65 in crypto/src/main/java/org/web3j/crypto/Blob.java

View check run for this annotation

Codecov / codecov/patch

crypto/src/main/java/org/web3j/crypto/Blob.java#L65

Added line #L65 was not covered by tests
}
}
51 changes: 51 additions & 0 deletions crypto/src/main/java/org/web3j/crypto/BlobUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2024 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;

import ethereum.ckzg4844.CKZG4844JNI;
import org.apache.tuweni.bytes.Bytes;

public class BlobUtils {

Check warning on line 18 in crypto/src/main/java/org/web3j/crypto/BlobUtils.java

View check run for this annotation

Codecov / codecov/patch

crypto/src/main/java/org/web3j/crypto/BlobUtils.java#L18

Added line #L18 was not covered by tests
NickSneo marked this conversation as resolved.
Show resolved Hide resolved

private static final byte blobCommitmentVersionKZG = 0x01;
private static final String trustedSetupFilePath = "/trusted_setup.txt";

static {
CKZG4844JNI.loadNativeLibrary();
loadTrustedSetupParameters();
}

private static void loadTrustedSetupParameters() {
CKZG4844JNI.loadTrustedSetupFromResource(BlobUtils.trustedSetupFilePath, BlobUtils.class);
}

public static Bytes getCommitment(Blob blobData) {
return Bytes.wrap(CKZG4844JNI.blobToKzgCommitment(blobData.data.toArray()));
}

public static Bytes getProof(Blob blobData, Bytes commitment) {
return Bytes.wrap(
CKZG4844JNI.computeBlobKzgProof(blobData.data.toArray(), commitment.toArray()));
}

public static boolean checkProofValidity(Blob blobData, Bytes commitment, Bytes proof) {
return CKZG4844JNI.verifyBlobKzgProof(
blobData.data.toArray(), commitment.toArray(), proof.toArray());
}

public static Bytes kzgToVersionedHash(Bytes commitment) {
byte[] hash = Hash.sha256(commitment.toArray());
hash[0] = blobCommitmentVersionKZG;
return Bytes.wrap(hash);
}
NickSneo marked this conversation as resolved.
Show resolved Hide resolved
}
87 changes: 87 additions & 0 deletions crypto/src/main/java/org/web3j/crypto/RawTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
import java.math.BigInteger;
import java.util.List;

import org.apache.tuweni.bytes.Bytes;

import org.web3j.crypto.transaction.type.ITransaction;
import org.web3j.crypto.transaction.type.LegacyTransaction;
import org.web3j.crypto.transaction.type.Transaction1559;
import org.web3j.crypto.transaction.type.Transaction2930;
import org.web3j.crypto.transaction.type.Transaction4844;
import org.web3j.crypto.transaction.type.TransactionType;

/**
Expand Down Expand Up @@ -118,6 +121,90 @@
maxFeePerGas));
}

public static RawTransaction createTransaction(
long chainId,
BigInteger nonce,
BigInteger maxPriorityFeePerGas,
BigInteger maxFeePerGas,
BigInteger gasLimit,
String to,
BigInteger value,
String data,
BigInteger maxFeePerBlobGas,
List<Bytes> versionedHashes) {

return new RawTransaction(
Transaction4844.createTransaction(

Check warning on line 137 in crypto/src/main/java/org/web3j/crypto/RawTransaction.java

View check run for this annotation

Codecov / codecov/patch

crypto/src/main/java/org/web3j/crypto/RawTransaction.java#L136-L137

Added lines #L136 - L137 were not covered by tests
chainId,
nonce,
maxPriorityFeePerGas,
maxFeePerGas,
gasLimit,
to,
value,
data,
maxFeePerBlobGas,
versionedHashes));
}

public static RawTransaction createTransaction(
List<Blob> blobs,
long chainId,
BigInteger nonce,
BigInteger maxPriorityFeePerGas,
BigInteger maxFeePerGas,
BigInteger gasLimit,
String to,
BigInteger value,
String data,
BigInteger maxFeePerBlobGas) {

return new RawTransaction(
Transaction4844.createTransaction(
blobs,
chainId,
nonce,
maxPriorityFeePerGas,
maxFeePerGas,
gasLimit,
to,
value,
data,
maxFeePerBlobGas));
}

public static RawTransaction createTransaction(
List<Blob> blobs,
List<Bytes> kzgCommitments,
List<Bytes> kzgProofs,
long chainId,
BigInteger nonce,
BigInteger maxPriorityFeePerGas,
BigInteger maxFeePerGas,
BigInteger gasLimit,
String to,
BigInteger value,
String data,
BigInteger maxFeePerBlobGas,
List<Bytes> versionedHashes) {
NickSneo marked this conversation as resolved.
Show resolved Hide resolved

return new RawTransaction(
Transaction4844.createTransaction(
blobs,
kzgCommitments,
kzgProofs,
chainId,
nonce,
maxPriorityFeePerGas,
maxFeePerGas,
gasLimit,
to,
value,
data,
maxFeePerBlobGas,
versionedHashes));
}

public static RawTransaction createTransaction(
long chainId,
BigInteger nonce,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public Sign.SignatureData getSignatureData() {

@Override
public byte[] getEncodedTransaction(Long chainId) {
if (null == chainId) {
if (this.getTransaction().getType().isEip4844()) {
return TransactionEncoder.encode4844(this);
} else if (null == chainId) {
return TransactionEncoder.encode(this);
} else {
return TransactionEncoder.encode(this, chainId);
Expand Down
Loading
Loading