Skip to content

Commit

Permalink
Merge pull request #2000 from web3j/nicks/eip-4844
Browse files Browse the repository at this point in the history
EIP-4844 support
  • Loading branch information
NickSneo authored Feb 13, 2024
2 parents 01c003f + e199d7f commit a3679af
Show file tree
Hide file tree
Showing 22 changed files with 5,146 additions and 11 deletions.
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'
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 {

final Bytes data;

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

/**
* 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);
}

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

@Override
public String toString() {
return "Blob{" + "data=" + data + '}';
}
}
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 {

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);
}
}
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 @@ -16,10 +16,13 @@
import java.util.Collections;
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 @@ -144,6 +147,90 @@ public static RawTransaction createTransaction(
accessList));
}

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(
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) {

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

0 comments on commit a3679af

Please sign in to comment.