Skip to content

Commit

Permalink
remove etherjar dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
a10zn8 committed Feb 14, 2024
1 parent 31ed51a commit f4aae5b
Show file tree
Hide file tree
Showing 91 changed files with 1,467 additions and 117 deletions.
2 changes: 0 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ dependencies {
}
implementation libs.brave.instrumentation.grpc

implementation libs.bundles.etherjar

implementation libs.logstash.encoder

implementation libs.janino
Expand Down
11 changes: 0 additions & 11 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[versions]
detekt = "1.23.1"
etherjar = "0.11.1"
groovy = "4.0.15"
protoc = "3.21.7"
jackson = "2.11.0"
Expand Down Expand Up @@ -37,15 +36,6 @@ detekt-formatting = { module = "io.gitlab.arturbosch.detekt:detekt-formatting",

equals-verifier = "nl.jqno.equalsverifier:equalsverifier:3.10.1"

etherjar-domain = { module = "io.emeraldpay.etherjar:etherjar-domain", version.ref = "etherjar" }
etherjar-hex = { module = "io.emeraldpay.etherjar:etherjar-hex", version.ref = "etherjar" }
etherjar-rpc-api = { module = "io.emeraldpay.etherjar:etherjar-rpc-api", version.ref = "etherjar" }
etherjar-rpc-http = { module = "io.emeraldpay.etherjar:etherjar-rpc-http", version.ref = "etherjar" }
etherjar-rpc-ws = { module = "io.emeraldpay.etherjar:etherjar-rpc-ws", version.ref = "etherjar" }
etherjar-tx = { module = "io.emeraldpay.etherjar:etherjar-tx", version.ref = "etherjar" }
etherjar-contract = { module = "io.emeraldpay.etherjar:etherjar-contract", version.ref = "etherjar" }
etherjar-erc20 = { module = "io.emeraldpay.etherjar:etherjar-erc20", version.ref = "etherjar" }

groovy = { module = "org.apache.groovy:groovy", version.ref = "groovy" }

grpc-proto-util = { module = "com.google.protobuf:protobuf-java-util", version.ref = "protoc" }
Expand Down Expand Up @@ -132,7 +122,6 @@ mockito-inline = "org.mockito:mockito-inline:4.0.0"

[bundles]
apache-commons = ["commons-io", "apache-commons-lang3", "apache-commons-collections4"]
etherjar = ["etherjar-domain", "etherjar-hex", "etherjar-rpc-api", "etherjar-rpc-http", "etherjar-rpc-ws", "etherjar-tx", "etherjar-contract", "etherjar-erc20"]
grpc = ["grpc-protobuf", "grpc-stub", "grpc-netty", "grpc-proto-util", "grpc-services"]
httpcomponents = ["httpcomponents-httpmime", "httpcomponents-httpclient"]
jackson = ["jackson-core", "jackson-databind", "jackson-datatype-jdk8", "jackson-datatype-jsr310", "jackson-module-kotlin"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

package io.emeraldpay.dshackle.upstream.ethereum.domain;

import io.emeraldpay.etherjar.hex.Hex32;
import io.emeraldpay.etherjar.hex.HexData;
import io.emeraldpay.dshackle.upstream.ethereum.hex.Hex32;
import io.emeraldpay.dshackle.upstream.ethereum.hex.HexData;
import org.bouncycastle.jcajce.provider.digest.Keccak;
import org.bouncycastle.util.encoders.Hex;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package io.emeraldpay.dshackle.upstream.ethereum.domain;

import io.emeraldpay.etherjar.hex.HexData;
import io.emeraldpay.dshackle.upstream.ethereum.hex.HexData;

public class BlockHash extends HexData {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.emeraldpay.dshackle.upstream.ethereum.domain;

import io.emeraldpay.etherjar.hex.HexData;
import io.emeraldpay.dshackle.upstream.ethereum.hex.HexData;
import org.bouncycastle.jcajce.provider.digest.Keccak;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package io.emeraldpay.dshackle.upstream.ethereum.domain;

import io.emeraldpay.etherjar.hex.Hex32;
import io.emeraldpay.etherjar.hex.HexData;
import io.emeraldpay.dshackle.upstream.ethereum.hex.Hex32;
import io.emeraldpay.dshackle.upstream.ethereum.hex.HexData;
import org.bouncycastle.jcajce.provider.digest.Keccak;

import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package io.emeraldpay.dshackle.upstream.ethereum.domain;

import io.emeraldpay.etherjar.hex.HexData;
import io.emeraldpay.dshackle.upstream.ethereum.hex.HexData;

/**
* An address, followed by a function selector.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package io.emeraldpay.dshackle.upstream.ethereum.domain;

import io.emeraldpay.etherjar.hex.HexData;
import io.emeraldpay.dshackle.upstream.ethereum.hex.HexData;
import org.bouncycastle.jcajce.provider.digest.Keccak;

import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package io.emeraldpay.dshackle.upstream.ethereum.domain;


import io.emeraldpay.etherjar.hex.HexData;
import io.emeraldpay.dshackle.upstream.ethereum.hex.HexData;

public class Nonce extends HexData {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package io.emeraldpay.dshackle.upstream.ethereum.domain;

import io.emeraldpay.etherjar.hex.HexData;
import io.emeraldpay.dshackle.upstream.ethereum.hex.HexData;

/**
* Transaction Hash value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package io.emeraldpay.dshackle.upstream.ethereum.domain;

import io.emeraldpay.etherjar.hex.HexData;
import io.emeraldpay.dshackle.upstream.ethereum.hex.HexData;

import java.util.Objects;

Expand Down
150 changes: 150 additions & 0 deletions src/main/java/io/emeraldpay/dshackle/upstream/ethereum/hex/Hex32.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* Copyright (c) 2020 EmeraldPay Inc, All Rights Reserved.
* Copyright (c) 2016-2017 Infinitape Inc, All Rights Reserved.
*
* 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 io.emeraldpay.dshackle.upstream.ethereum.hex;

import java.math.BigInteger;

/**
* Fixed-size 32-bytes hex value.
*/
public class Hex32 extends HexData {

// 0x1000 > MAX_NUMBER
private static final BigInteger NUMBER_LIMIT = BigInteger.valueOf(2).pow(256);
// 0xffff.... as for 256-bit value
private static final BigInteger MAX_NUMBER = NUMBER_LIMIT.subtract(BigInteger.ONE);

public static final int SIZE_BYTES = 32;
public static final int SIZE_HEX = 2 + SIZE_BYTES * 2;

/**
* Use {@link Hex32#empty()}
*/
@Deprecated
public static final Hex32 EMPTY =
Hex32.from("0x0000000000000000000000000000000000000000000000000000000000000000");

public static Hex32 from(HexData data) {
if (data instanceof Hex32)
return (Hex32) data;

if (data.getSize() != Hex32.SIZE_BYTES)
throw new IllegalArgumentException(
String.format("Data length is not %d: %d", Hex32.SIZE_BYTES, data.getSize()));

return from(data.getBytes());
}

public Hex32(byte[] value) {
super(value, SIZE_BYTES);
}

public static Hex32 from(byte[] value) {
if (value == null) {
throw new IllegalArgumentException("Null Hex32");
}
if (value.length != SIZE_BYTES) {
throw new IllegalArgumentException("Invalid Hex32 length: " + value.length);
}
return new Hex32(value);
}

public static Hex32 from(String value) {
if (value == null) {
throw new IllegalArgumentException("Null Hex32");
}
if (value.length() != SIZE_HEX) {
throw new IllegalArgumentException("Invalid Hex32 length: " + value.length());
}
return new Hex32(HexData.from(value).getBytes());
}

public static Hex32 empty() {
return new Hex32(new byte[SIZE_BYTES]);
}

public static Hex32 extendFrom(HexData value) {
return extendFrom(value.getBytes());
}

public static Hex32 extendFrom(HexQuantity value) {
if (value.getValue().signum() < 0) {
// use .add() because it's already negative
BigInteger asNegative = MAX_NUMBER.add(value.getValue()).add(BigInteger.ONE);

byte[] bytesAll = asNegative.toByteArray();
// usually it have a 0-byte prefix and we need to remove it
if (bytesAll[0] == 0) {
byte[] bytesClean = new byte[bytesAll.length - 1];
System.arraycopy(bytesAll, 1, bytesClean, 0, bytesClean.length);

return extendFrom(bytesClean);
} else {
return extendFrom(bytesAll);
}
}
return extendFrom(value.asData());
}

public static Hex32 extendFrom(Long value) {
return extendFrom(HexQuantity.from(value));
}

public static Hex32 extendFrom(BigInteger value) {
return extendFrom(HexQuantity.from(value));
}

public static Hex32 extendFrom(byte[] value) {
if (value.length > SIZE_BYTES) {
throw new IllegalArgumentException("Data is too long: " + value.length);
}
if (value.length == SIZE_BYTES) {
return new Hex32(value);
}
if (value.length == 0) {
return empty();
}
byte[] base = new byte[SIZE_BYTES];
System.arraycopy(value, 0, base, base.length - value.length, value.length);
return new Hex32(base);
}

/**
* Convert bytes to an unsigned number. I.e., <code>uint</code> -&gt; <code>BigInteger</code>
* @return number
*/
public BigInteger asUInt() {
return new BigInteger(1, value);
}

/**
* Convert bytes to a signed number. I.e., <code>int</code> -&gt; <code>BigInteger</code>
* @return number
*/
public BigInteger asInt() {
BigInteger number = new BigInteger(1, value);
// for negative value the highest bit is set to 1
if (value[0] >= 0) {
// just positive number, return as is
return number;
} else {
// calculate signed part
return NUMBER_LIMIT.subtract(number).negate();
}
}
}
Loading

0 comments on commit f4aae5b

Please sign in to comment.