Skip to content

Commit a4d8788

Browse files
Merge pull request #103 from tronprotocol/release_v2.2.6
Release v2.2.6 merge to main
2 parents ac3fe8b + 5dd3ba7 commit a4d8788

18 files changed

Lines changed: 2384 additions & 60 deletions

README.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,32 @@ libp2p is a p2p network SDK implemented in java language. The functional modules
55
* publish nodes on dns domain and node discovery by dns,
66
* support compressed message transmission among nodes
77

8+
# Note
9+
Starting from version 2.2.6, `libp2p` has removed the logback component and adopted the logger facade. If logging is required, you need to introduce a logging framework manually.
10+
Here’s how to include logback in your project using Gradle:
11+
12+
1. Add the following dependencies to your build.gradle file
13+
```
14+
dependencies {
15+
implementation group: 'ch.qos.logback', name: 'logback-classic', version: 'x.x.xx'
16+
}
17+
```
18+
2. Create or edit logback.xml in src/main/resources to configure logging. Here is an example of logback.xml:
19+
```xml
20+
<?xml version="1.0" encoding="UTF-8"?>
21+
<configuration>
22+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
23+
<encoder>
24+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
25+
</encoder>
26+
</appender>
27+
<root level="INFO">
28+
<appender-ref ref="STDOUT" />
29+
</root>
30+
</configuration>
31+
```
32+
33+
834
# Build
935
Building libp2p requires `git` and `Oracle JDK 1.8` to be installed, other JDK versions are not supported yet. Make sure you operate on `Linux` and `MacOS` operating systems.
1036

@@ -43,7 +69,7 @@ repositories {
4369
Then add the required packages as dependencies. Please add dependencies locally.
4470
```bash
4571
dependencies {
46-
implementation group: 'io.github.tronprotocol', name: 'libp2p', version: '2.2.5'
72+
implementation group: 'io.github.tronprotocol', name: 'libp2p', version: '2.2.6'
4773
}
4874
```
4975
Or if you are using the jar files as your dependencies:
@@ -66,7 +92,7 @@ dependencies {
6692
<dependency>
6793
<groupId>io.github.tronprotocol</groupId>
6894
<artifactId>libp2p</artifactId>
69-
<version>2.2.5</version>
95+
<version>2.2.6</version>
7096
</dependency>
7197
```
7298

build.gradle

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
group 'io.github.tronprotocol'
2-
version '2.2.5'
2+
version '2.2.6'
33

44
buildscript {
55
repositories {
@@ -49,20 +49,18 @@ buildscript {
4949

5050
dependencies {
5151
classpath 'com.github.jengelman.gradle.plugins:shadow:5.2.0'
52-
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.12'
52+
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.9.1'
5353
}
5454
}
5555

5656
dependencies {
57-
testImplementation group: 'junit', name: 'junit', version: '4.12'
57+
testImplementation group: 'junit', name: 'junit', version: '4.13.2'
5858

5959
implementation group: 'org.xerial.snappy', name: 'snappy-java', version: '1.1.10.5'
60-
implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.13'
61-
implementation group: 'ch.qos.logback', name: 'logback-core', version: '1.2.13'
6260
implementation group: 'com.google.protobuf', name: 'protobuf-java', version: protobufVersion
6361
implementation group: 'com.google.protobuf', name: 'protobuf-java-util', version: protobufVersion
6462
implementation group: 'io.grpc', name: 'grpc-netty', version: grpcVersion
65-
implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
63+
implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.36'
6664
implementation group: 'org.bouncycastle', name: 'bcprov-jdk18on', version: '1.79'
6765
implementation group: 'org.bouncycastle', name: 'bcpkix-jdk18on', version: '1.79'
6866

@@ -74,9 +72,6 @@ dependencies {
7472
annotationProcessor group: 'org.projectlombok', name: 'lombok', version: '1.18.12'
7573

7674
implementation group: 'dnsjava', name: 'dnsjava', version: '3.6.2'
77-
implementation group: 'org.web3j', name: 'crypto', version: '5.0.0',{
78-
exclude group: 'org.bouncycastle', module: 'bcprov-jdk15on'
79-
}
8075
implementation('software.amazon.awssdk:route53:2.18.41', {
8176
exclude group: 'io.netty', module: 'netty-codec-http2'
8277
exclude group: 'io.netty', module: 'netty-codec-http'
@@ -151,6 +146,8 @@ compileTestJava {
151146
task sourcesJar(type: Jar) {
152147
archiveClassifier.set('sources')
153148
from sourceSets.main.allJava
149+
duplicatesStrategy = DuplicatesStrategy.INCLUDE
150+
dependsOn(generateProto) // explicit_dependency
154151
}
155152

156153
task javadocJar(type: Jar) {
@@ -178,4 +175,4 @@ artifacts {
178175
archives javadocJar, sourcesJar
179176
}
180177

181-
178+
processResources.dependsOn(generateProto) // explicit_dependency

gradle/verification-metadata.xml

Lines changed: 1287 additions & 0 deletions
Large diffs are not rendered by default.

gradle/wrapper/gradle-wrapper.jar

-216 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.4-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

gradlew

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gradlew.bat

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/org/tron/p2p/base/Constant.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class Constant {
77

88
public static final int NODE_ID_LEN = 64;
99
public static final List<String> ipV4Urls = Arrays.asList(
10-
"http://checkip.amazonaws.com", "https://ifconfig.me/", "https://4.ipw.cn/");
10+
"http://checkip.amazonaws.com", "https://ifconfig.me/ip", "https://4.ipw.cn/");
1111
public static final List<String> ipV6Urls = Arrays.asList(
1212
"https://v6.ident.me", "http://6.ipw.cn/");
1313
public static final String ipV4Hex = "00000000"; //32 bit
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2019 Web3 Labs Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package org.web3j.crypto;
14+
15+
import java.math.BigInteger;
16+
17+
/** An ECDSA Signature. */
18+
public class ECDSASignature {
19+
public final BigInteger r;
20+
public final BigInteger s;
21+
22+
public ECDSASignature(BigInteger r, BigInteger s) {
23+
this.r = r;
24+
this.s = s;
25+
}
26+
27+
/**
28+
* @return true if the S component is "low", that means it is below {@link
29+
* Sign#HALF_CURVE_ORDER}. See <a
30+
* href="https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki#Low_S_values_in_signatures">
31+
* BIP62</a>.
32+
*/
33+
public boolean isCanonical() {
34+
return s.compareTo(Sign.HALF_CURVE_ORDER) <= 0;
35+
}
36+
37+
/**
38+
* Will automatically adjust the S component to be less than or equal to half the curve order,
39+
* if necessary. This is required because for every signature (r,s) the signature (r, -s (mod
40+
* N)) is a valid signature of the same message. However, we dislike the ability to modify the
41+
* bits of a Bitcoin transaction after it's been signed, as that violates various assumed
42+
* invariants. Thus in future only one of those forms will be considered legal and the other
43+
* will be banned.
44+
*
45+
* @return the signature in a canonicalised form.
46+
*/
47+
public ECDSASignature toCanonicalised() {
48+
if (!isCanonical()) {
49+
// The order of the curve is the number of valid points that exist on that curve.
50+
// If S is in the upper half of the number of valid points, then bring it back to
51+
// the lower half. Otherwise, imagine that
52+
// N = 10
53+
// s = 8, so (-8 % 10 == 2) thus both (r, 8) and (r, 2) are valid solutions.
54+
// 10 - 8 == 2, giving us always the latter solution, which is canonical.
55+
return new ECDSASignature(r, Sign.CURVE.getN().subtract(s));
56+
} else {
57+
return this;
58+
}
59+
}
60+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright 2019 Web3 Labs Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package org.web3j.crypto;
14+
15+
import java.math.BigInteger;
16+
import java.security.KeyPair;
17+
import java.util.Arrays;
18+
19+
import org.bouncycastle.crypto.digests.SHA256Digest;
20+
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
21+
import org.bouncycastle.crypto.signers.ECDSASigner;
22+
import org.bouncycastle.crypto.signers.HMacDSAKCalculator;
23+
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;
24+
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;
25+
26+
import org.web3j.utils.Numeric;
27+
28+
/** Elliptic Curve SECP-256k1 generated key pair. */
29+
public class ECKeyPair {
30+
private final BigInteger privateKey;
31+
private final BigInteger publicKey;
32+
33+
public ECKeyPair(BigInteger privateKey, BigInteger publicKey) {
34+
this.privateKey = privateKey;
35+
this.publicKey = publicKey;
36+
}
37+
38+
public BigInteger getPrivateKey() {
39+
return privateKey;
40+
}
41+
42+
public BigInteger getPublicKey() {
43+
return publicKey;
44+
}
45+
46+
/**
47+
* Sign a hash with the private key of this key pair.
48+
*
49+
* @param transactionHash the hash to sign
50+
* @return An {@link ECDSASignature} of the hash
51+
*/
52+
public ECDSASignature sign(byte[] transactionHash) {
53+
ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
54+
55+
ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(privateKey, Sign.CURVE);
56+
signer.init(true, privKey);
57+
BigInteger[] components = signer.generateSignature(transactionHash);
58+
59+
return new ECDSASignature(components[0], components[1]).toCanonicalised();
60+
}
61+
62+
public static ECKeyPair create(KeyPair keyPair) {
63+
BCECPrivateKey privateKey = (BCECPrivateKey) keyPair.getPrivate();
64+
BCECPublicKey publicKey = (BCECPublicKey) keyPair.getPublic();
65+
66+
BigInteger privateKeyValue = privateKey.getD();
67+
68+
// Ethereum does not use encoded public keys like bitcoin - see
69+
// https://en.bitcoin.it/wiki/Elliptic_Curve_Digital_Signature_Algorithm for details
70+
// Additionally, as the first bit is a constant prefix (0x04) we ignore this value
71+
byte[] publicKeyBytes = publicKey.getQ().getEncoded(false);
72+
BigInteger publicKeyValue =
73+
new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length));
74+
75+
return new ECKeyPair(privateKeyValue, publicKeyValue);
76+
}
77+
78+
public static ECKeyPair create(BigInteger privateKey) {
79+
return new ECKeyPair(privateKey, Sign.publicKeyFromPrivate(privateKey));
80+
}
81+
82+
public static ECKeyPair create(byte[] privateKey) {
83+
return create(Numeric.toBigInt(privateKey));
84+
}
85+
86+
@Override
87+
public boolean equals(Object o) {
88+
if (this == o) {
89+
return true;
90+
}
91+
if (o == null || getClass() != o.getClass()) {
92+
return false;
93+
}
94+
95+
ECKeyPair ecKeyPair = (ECKeyPair) o;
96+
97+
if (privateKey != null
98+
? !privateKey.equals(ecKeyPair.privateKey)
99+
: ecKeyPair.privateKey != null) {
100+
return false;
101+
}
102+
103+
return publicKey != null
104+
? publicKey.equals(ecKeyPair.publicKey)
105+
: ecKeyPair.publicKey == null;
106+
}
107+
108+
@Override
109+
public int hashCode() {
110+
int result = privateKey != null ? privateKey.hashCode() : 0;
111+
result = 31 * result + (publicKey != null ? publicKey.hashCode() : 0);
112+
return result;
113+
}
114+
}

0 commit comments

Comments
 (0)