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 8 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
47 changes: 47 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,47 @@
/*
* 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 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;
}

/**
* 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;
}
}
82 changes: 82 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,82 @@
/*
* 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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

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

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

static {
CKZG4844JNI.loadNativeLibrary();
}

private static final byte blobCommitmentVersionKZG = 0x01;

public static void loadTrustedSetupParameters() {
NickSneo marked this conversation as resolved.
Show resolved Hide resolved
try (InputStream inputStream =
BlobUtils.class.getClassLoader().getResourceAsStream("trusted_setup.txt")) {
assert inputStream != null;
final BufferedReader reader =
new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
final int g1Count = Integer.parseInt(reader.readLine());
final int g2Count = Integer.parseInt(reader.readLine());

final ByteBuffer g1 = ByteBuffer.allocate(g1Count * CKZG4844JNI.BYTES_PER_G1);
final ByteBuffer g2 = ByteBuffer.allocate(g2Count * CKZG4844JNI.BYTES_PER_G2);

for (int i = 0; i < g1Count; i++) {
g1.put(Bytes.fromHexString(reader.readLine()).toArray());
}
for (int i = 0; i < g2Count; i++) {
g2.put(Bytes.fromHexString(reader.readLine()).toArray());
}
CKZG4844JNI.loadTrustedSetup(g1.array(), g1Count, g2.array(), g2Count);
cfelde marked this conversation as resolved.
Show resolved Hide resolved
} catch (final IOException ex) {
throw new UncheckedIOException(ex);
}
}

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

public static void freeTrustedSetup() {
// the current trusted setup should be freed before a new one is loaded
CKZG4844JNI.freeTrustedSetup();
}
}
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 @@ public static RawTransaction createTransaction(
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(
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