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 4 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
6 changes: 3 additions & 3 deletions abi/src/main/java/org/web3j/abi/datatypes/Bytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ public class Bytes extends BytesType {

public static final String TYPE_NAME = "bytes";

protected Bytes(int byteSize, byte[] value) {
public Bytes(int byteSize, byte[] value) {
super(value, TYPE_NAME + value.length);
if (!isValid(byteSize)) {
throw new UnsupportedOperationException(
"Input byte array must be in range 0 < M <= 32 and length must match type");
"Input byte array must be in range 0 < M <= 48 and length must match type");
gtebrean marked this conversation as resolved.
Show resolved Hide resolved
}
}

private boolean isValid(int byteSize) {
int length = getValue().length;
return length > 0 && length <= 32 && length == byteSize;
return length > 0 && length <= 48 && length == byteSize;
NickSneo marked this conversation as resolved.
Show resolved Hide resolved
}
}
4 changes: 4 additions & 0 deletions besu/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ plugins {
}
description 'Besu JSON-RPC API'

repositories {
maven { url "https://artifacts.consensys.net/public/maven/maven/" }
gtebrean marked this conversation as resolved.
Show resolved Hide resolved
}

dependencies {
api project(':eea')
testImplementation project(path: ':core', configuration: 'testArtifacts')
Expand Down
4 changes: 4 additions & 0 deletions codegen/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
description 'web3j project code generators'

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

dependencies {
implementation project(':core'),
"com.squareup:kotlinpoet:$kotlinPoetVersion",
Expand Down
5 changes: 4 additions & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ plugins {
id 'org.ajoberstar.git-publish'
}


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

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

dependencies {
api project(':abi'),
project(':crypto'),
Expand Down
5 changes: 5 additions & 0 deletions crypto/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ plugins {

description 'web3j Ethereum crypto library'

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

dependencies {
api project(':abi'),
project(':rlp'),
project(':utils'),
"org.slf4j:slf4j-api:$slf4jVersion",
"com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
implementation("tech.pegasys:jc-kzg-4844:0.8.0")
}

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.web3j.abi.datatypes.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 = new Bytes(data.length, data);
}

/**
* Get the data of the Blob.
*
* @return the data.
*/
public Bytes getData() {
return data;
}
}
80 changes: 80 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,80 @@
/*
* 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.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

import ethereum.ckzg4844.CKZG4844JNI;

import org.web3j.abi.datatypes.Bytes;

public class BlobUtils {
NickSneo marked this conversation as resolved.
Show resolved Hide resolved
public static boolean libraryLoaded = false;
private static final byte blobCommitmentVersionKZG = 0x01;
private final Blob blobData;

public BlobUtils(Blob blobData) {
if (!libraryLoaded) {
throw new RuntimeException(
"Please load CKZG4844JNI Library by running BlobUtils.loadTrustedSetupFromResource()");
}
NickSneo marked this conversation as resolved.
Show resolved Hide resolved
this.blobData = blobData;
}

public static void loadTrustedSetupFromResource() {
CKZG4844JNI.loadNativeLibrary();
try (InputStream resourceStream =
BlobUtils.class.getClassLoader().getResourceAsStream("trusted_setup.txt")) {
if (resourceStream == null) {
throw new IllegalArgumentException("Resource not found");
}
Path tempFile = Files.createTempFile("trusted_setup", "txt");
Files.copy(resourceStream, tempFile, StandardCopyOption.REPLACE_EXISTING);
CKZG4844JNI.loadTrustedSetup(tempFile.toString());
NickSneo marked this conversation as resolved.
Show resolved Hide resolved
tempFile.toFile().deleteOnExit(); // Delete temp file when JVM exits
} catch (Exception e) {
throw new RuntimeException("Failed to load trusted setup from resource", e);
}
libraryLoaded = true;
}

public Bytes getCommitment() {
return new Bytes(48, CKZG4844JNI.blobToKzgCommitment(blobData.data.getValue()));
}

public Bytes getProof(Bytes commitment) {
return new Bytes(
48,
CKZG4844JNI.computeBlobKzgProof(blobData.data.getValue(), commitment.getValue()));
}

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

public Bytes kzgToVersionedHash(Bytes commitment) {
byte[] hash = Hash.sha256(commitment.getValue());
hash[0] = blobCommitmentVersionKZG;
return new Bytes(32, hash);
}

public static void freeTrustedSetup() {
// the current trusted setup should be freed before a new one is loaded
CKZG4844JNI.freeTrustedSetup();
libraryLoaded = false;
}
}
60 changes: 60 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,12 @@
import java.math.BigInteger;
import java.util.List;

import org.web3j.abi.datatypes.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 +120,64 @@ 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,
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
Loading
Loading