Skip to content

Commit e903bb1

Browse files
chore: auto format code (#169)
1 parent 9913d05 commit e903bb1

22 files changed

Lines changed: 498 additions & 393 deletions

.github/workflows/test.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,34 @@ on:
99
types: [ ready_for_review, synchronize, opened ]
1010

1111
jobs:
12+
format:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v2
18+
with:
19+
ref: ${{ github.head_ref }}
20+
21+
- name: Merge Conflict finder
22+
uses: olivernybroe/action-conflict-finder@v1.1
23+
24+
- name: Use Java Version ${{ matrix.java }}
25+
uses: actions/setup-java@v2
26+
with:
27+
distribution: 'adopt'
28+
java-version: 8
29+
cache: 'gradle'
30+
31+
- name: Format code
32+
run: gradle format
33+
34+
- name: Commit fixed code
35+
uses: stefanzweifel/git-auto-commit-action@v4
36+
with:
37+
commit_message: "style: resolve style guide violations"
38+
branch: ${{ github.head_ref }}
39+
1240
unit:
1341
runs-on: ubuntu-latest
1442
strategy:

src/main/java/org/arkecosystem/crypto/Schnorr.java

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package org.arkecosystem.crypto;
22

3-
import org.bitcoinj.core.Sha256Hash;
4-
53
import java.math.BigInteger;
4+
import org.bitcoinj.core.Sha256Hash;
65

7-
/**
8-
* Heavily inspired in https://github.com/miketwk/bip-schnorr-java/blob/master/Schnorr.java
9-
*/
6+
/** Heavily inspired in https://github.com/miketwk/bip-schnorr-java/blob/master/Schnorr.java */
107
public class Schnorr {
11-
public static final BigInteger p = new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16);
12-
public static final BigInteger n = new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16);
8+
public static final BigInteger p =
9+
new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16);
10+
public static final BigInteger n =
11+
new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16);
1312
public static final BigInteger[] G = {
1413
new BigInteger("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 16),
1514
new BigInteger("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", 16)
@@ -18,27 +17,32 @@ public class Schnorr {
1817
public static final BigInteger TWO = BigInteger.valueOf(2);
1918
public static final BigInteger THREE = BigInteger.valueOf(3);
2019

21-
private final static char[] hexArray = "0123456789ABCDEF".toCharArray();
20+
private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
2221

2322
public static BigInteger[] addPoint(BigInteger[] p1, BigInteger[] p2) {
24-
if (p1 == null || p1.length != 2)
25-
return p2;
23+
if (p1 == null || p1.length != 2) return p2;
2624

27-
if (p2 == null || p2.length != 2)
28-
return p1;
25+
if (p2 == null || p2.length != 2) return p1;
2926

30-
if (p1[0].compareTo(p2[0]) == 0 && p1[1].compareTo(p2[1]) != 0)
31-
return null;
27+
if (p1[0].compareTo(p2[0]) == 0 && p1[1].compareTo(p2[1]) != 0) return null;
3228

3329
BigInteger lam;
3430
if (p1[0].compareTo(p2[0]) == 0 && p1[1].compareTo(p2[1]) == 0)
35-
lam = (THREE.multiply(p1[0]).multiply(p1[0]).multiply(TWO.multiply(p1[1]).modPow(p.subtract(TWO), p))).mod(p);
31+
lam =
32+
(THREE.multiply(p1[0])
33+
.multiply(p1[0])
34+
.multiply(TWO.multiply(p1[1]).modPow(p.subtract(TWO), p)))
35+
.mod(p);
3636
else
37-
lam = (p2[1].subtract(p1[1]).multiply(p2[0].subtract(p1[0]).modPow(p.subtract(TWO), p))).mod(p);
37+
lam =
38+
(p2[1]
39+
.subtract(p1[1])
40+
.multiply(p2[0].subtract(p1[0]).modPow(p.subtract(TWO), p)))
41+
.mod(p);
3842

3943
BigInteger x3 = (lam.multiply(lam).subtract(p1[0]).subtract(p2[0])).mod(p);
4044

41-
return new BigInteger[]{x3, lam.multiply(p1[0].subtract(x3)).subtract(p1[1]).mod(p)};
45+
return new BigInteger[] {x3, lam.multiply(p1[0].subtract(x3)).subtract(p1[1]).mod(p)};
4246
}
4347

4448
public static BigInteger[] multiplyPoint(BigInteger[] P, BigInteger n) {
@@ -56,27 +60,24 @@ public static BigInteger jacobi(BigInteger x) {
5660
}
5761

5862
public static BigInteger[] bytesToPoint(byte[] b) {
59-
if (b[0] != 2 && b[0] != 3)
60-
return null;
63+
if (b[0] != 2 && b[0] != 3) return null;
6164

6265
BigInteger odd = b[0] == 3 ? BigInteger.ONE : BigInteger.ZERO;
6366
BigInteger x = toBigInteger(b, 1, 32);
6467
BigInteger y_sq = x.modPow(THREE, p).add(BigInteger.valueOf(7)).mod(p);
6568
BigInteger y0 = y_sq.modPow(p.add(BigInteger.ONE).divide(BigInteger.valueOf(4)), p);
66-
if (y_sq.compareTo(y0.modPow(TWO, p)) != 0)
67-
return null;
69+
if (y_sq.compareTo(y0.modPow(TWO, p)) != 0) return null;
6870

6971
BigInteger y = y0.and(BigInteger.ONE).compareTo(odd) != 0 ? p.subtract(y0) : y0;
7072

71-
return new BigInteger[]{x, y};
73+
return new BigInteger[] {x, y};
7274
}
7375

7476
public static byte[] to32BytesData(BigInteger num) {
7577
String hexNum = num.toString(16);
7678
if (hexNum.length() < 64) {
7779
StringBuilder sb = new StringBuilder();
78-
for (int i = 0; i < 64 - hexNum.length(); i++)
79-
sb.append("0");
80+
for (int i = 0; i < 64 - hexNum.length(); i++) sb.append("0");
8081

8182
hexNum = sb.append(hexNum).toString();
8283
}
@@ -93,16 +94,19 @@ public static BigInteger toBigInteger(byte[] data) {
9394

9495
public static byte[] pointToBytes(BigInteger[] point) {
9596
byte[] res = new byte[33];
96-
res[0] = BigInteger.ONE.compareTo(point[1].and(BigInteger.ONE)) == 0 ? (byte) 0x03 : (byte) 0x02;
97+
res[0] =
98+
BigInteger.ONE.compareTo(point[1].and(BigInteger.ONE)) == 0
99+
? (byte) 0x03
100+
: (byte) 0x02;
97101
System.arraycopy(to32BytesData(point[0]), 0, res, 1, 32);
98102
return res;
99103
}
100104

101105
public static byte[] schnorrSign(byte[] msg, BigInteger seckey) {
102-
if (msg.length != 32)
103-
throw new RuntimeException("The message must be a 32-byte array.");
106+
if (msg.length != 32) throw new RuntimeException("The message must be a 32-byte array.");
104107

105-
if (BigInteger.ZERO.compareTo(seckey) > 0 || seckey.compareTo(n.subtract(BigInteger.ONE)) > 0)
108+
if (BigInteger.ZERO.compareTo(seckey) > 0
109+
|| seckey.compareTo(n.subtract(BigInteger.ONE)) > 0)
106110
throw new RuntimeException("The secret key must be an integer in the range 1..n-1.");
107111

108112
byte[] resultData = new byte[32 + msg.length];
@@ -132,24 +136,20 @@ public static byte[] schnorrSign(byte[] msg, BigInteger seckey) {
132136
}
133137

134138
public static boolean schnorrVerify(byte[] msg, byte[] pubkey, byte[] sig) {
135-
if (msg.length != 32)
136-
throw new RuntimeException("The message must be a 32-byte array.");
139+
if (msg.length != 32) throw new RuntimeException("The message must be a 32-byte array.");
137140

138141
if (pubkey.length != 33)
139142
throw new RuntimeException("The public key must be a 33-byte array.");
140143

141-
if (sig.length != 64)
142-
throw new RuntimeException("The signature must be a 64-byte array.");
144+
if (sig.length != 64) throw new RuntimeException("The signature must be a 64-byte array.");
143145

144146
BigInteger[] P = bytesToPoint(pubkey);
145-
if (P == null)
146-
return false;
147+
if (P == null) return false;
147148

148149
BigInteger r = toBigInteger(sig, 0, 32);
149150
BigInteger s = toBigInteger(sig, 32, 32);
150151

151-
if (r.compareTo(p) >= 0 || s.compareTo(n) >= 0)
152-
return false;
152+
if (r.compareTo(p) >= 0 || s.compareTo(n) >= 0) return false;
153153

154154
byte[] eData = new byte[32 + 33 + 32];
155155
System.arraycopy(sig, 0, eData, 0, 32);
@@ -167,8 +167,10 @@ public static byte[] hexStringToByteArray(String s) {
167167
int len = s.length();
168168
byte[] data = new byte[len / 2];
169169
for (int i = 0; i < len; i += 2) {
170-
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
171-
+ Character.digit(s.charAt(i + 1), 16));
170+
data[i / 2] =
171+
(byte)
172+
((Character.digit(s.charAt(i), 16) << 4)
173+
+ Character.digit(s.charAt(i + 1), 16));
172174
}
173175
return data;
174176
}

src/main/java/org/arkecosystem/crypto/signature/ECDSASigner.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
public class ECDSASigner implements Signer {
77
@Override
88
public byte[] sign(byte[] message, ECKey privateKey) {
9-
return privateKey
10-
.sign(Sha256Hash.wrap(message))
11-
.encodeToDER();
9+
return privateKey.sign(Sha256Hash.wrap(message)).encodeToDER();
1210
}
1311
}

src/main/java/org/arkecosystem/crypto/signature/SchnorrSigner.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.arkecosystem.crypto.Schnorr;
44
import org.bitcoinj.core.ECKey;
55

6-
76
public class SchnorrSigner implements Signer {
87
@Override
98
public byte[] sign(byte[] message, ECKey privateKey) {

src/main/java/org/arkecosystem/crypto/signature/SchnorrVerifier.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.arkecosystem.crypto.Schnorr;
44
import org.bitcoinj.core.ECKey;
55

6-
76
public class SchnorrVerifier implements Verifier {
87
@Override
98
public boolean verify(byte[] hash, ECKey keys, byte[] signature) {
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package org.arkecosystem.crypto.signature;
22

3-
43
import org.bitcoinj.core.ECKey;
54

65
public interface Signer {
76

87
byte[] sign(byte[] message, ECKey key);
9-
108
}
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package org.arkecosystem.crypto.signature;
22

3-
43
import org.bitcoinj.core.ECKey;
54

65
public interface Verifier {
76

87
boolean verify(byte[] hash, ECKey key, byte[] signature);
9-
108
}

src/main/java/org/arkecosystem/crypto/transactions/Deserializer.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package org.arkecosystem.crypto.transactions;
22

3+
import java.nio.ByteBuffer;
4+
import java.nio.ByteOrder;
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.HashSet;
8+
import java.util.Map;
9+
import java.util.Set;
310
import org.arkecosystem.crypto.encoding.Hex;
411
import org.arkecosystem.crypto.enums.CoreTransactionTypes;
512
import org.arkecosystem.crypto.enums.TransactionTypeGroup;
@@ -16,14 +23,6 @@
1623
import org.arkecosystem.crypto.transactions.types.Transfer;
1724
import org.arkecosystem.crypto.transactions.types.Vote;
1825

19-
import java.nio.ByteBuffer;
20-
import java.nio.ByteOrder;
21-
import java.util.ArrayList;
22-
import java.util.HashMap;
23-
import java.util.HashSet;
24-
import java.util.Map;
25-
import java.util.Set;
26-
2726
public class Deserializer {
2827

2928
private final ByteBuffer buffer;
@@ -35,18 +34,18 @@ public Deserializer(String serialized) {
3534
Map<Integer, Transaction> coreTransactionTypes = new HashMap<>();
3635
coreTransactionTypes.put(CoreTransactionTypes.TRANSFER.getValue(), new Transfer());
3736
coreTransactionTypes.put(
38-
CoreTransactionTypes.SECOND_SIGNATURE_REGISTRATION.getValue(),
39-
new SecondSignatureRegistration());
37+
CoreTransactionTypes.SECOND_SIGNATURE_REGISTRATION.getValue(),
38+
new SecondSignatureRegistration());
4039
coreTransactionTypes.put(
41-
CoreTransactionTypes.DELEGATE_REGISTRATION.getValue(), new DelegateRegistration());
40+
CoreTransactionTypes.DELEGATE_REGISTRATION.getValue(), new DelegateRegistration());
4241
coreTransactionTypes.put(CoreTransactionTypes.VOTE.getValue(), new Vote());
4342
coreTransactionTypes.put(
44-
CoreTransactionTypes.MULTI_SIGNATURE_REGISTRATION.getValue(),
45-
new MultiSignatureRegistration());
43+
CoreTransactionTypes.MULTI_SIGNATURE_REGISTRATION.getValue(),
44+
new MultiSignatureRegistration());
4645
coreTransactionTypes.put(CoreTransactionTypes.IPFS.getValue(), new Ipfs());
4746
coreTransactionTypes.put(CoreTransactionTypes.MULTI_PAYMENT.getValue(), new MultiPayment());
4847
coreTransactionTypes.put(
49-
CoreTransactionTypes.DELEGATE_RESIGNATION.getValue(), new DelegateResignation());
48+
CoreTransactionTypes.DELEGATE_RESIGNATION.getValue(), new DelegateResignation());
5049
coreTransactionTypes.put(CoreTransactionTypes.HTLC_LOCK.getValue(), new HtlcLock());
5150
coreTransactionTypes.put(CoreTransactionTypes.HTLC_CLAIM.getValue(), new HtlcClaim());
5251
coreTransactionTypes.put(CoreTransactionTypes.HTLC_REFUND.getValue(), new HtlcRefund());
@@ -131,7 +130,8 @@ private void deserializeEcdsa() {
131130
}
132131

133132
private boolean canReadNonMultiSignature() {
134-
return buffer.hasRemaining() && (buffer.remaining() % 64 == 0 || buffer.remaining() % 65 != 0);
133+
return buffer.hasRemaining()
134+
&& (buffer.remaining() % 64 == 0 || buffer.remaining() % 65 != 0);
135135
}
136136

137137
private void deserializeSchnorr() {
@@ -206,8 +206,8 @@ private int currentSignatureLength() {
206206
public void setNewTransactionType(Transaction transaction) {
207207
if (this.transactionGroups.containsKey(transaction.getTransactionTypeGroup())) {
208208
this.transactionGroups
209-
.get(transaction.getTransactionTypeGroup())
210-
.put(transaction.getTransactionType(), transaction);
209+
.get(transaction.getTransactionTypeGroup())
210+
.put(transaction.getTransactionType(), transaction);
211211
} else {
212212
Map<Integer, Transaction> newTransactionGroup = new HashMap<>();
213213
newTransactionGroup.put(transaction.getTransactionType(), transaction);

src/main/java/org/arkecosystem/crypto/transactions/Serializer.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package org.arkecosystem.crypto.transactions;
22

3+
import java.nio.ByteBuffer;
4+
import java.nio.ByteOrder;
35
import org.arkecosystem.crypto.configuration.Network;
46
import org.arkecosystem.crypto.encoding.Hex;
57
import org.arkecosystem.crypto.transactions.types.Transaction;
68

7-
import java.nio.ByteBuffer;
8-
import java.nio.ByteOrder;
9-
109
public class Serializer {
1110

1211
private final Transaction transaction;
@@ -19,18 +18,25 @@ public static byte[] serialize(Transaction transaction) {
1918
return serialize(transaction, false, false, false);
2019
}
2120

22-
public static byte[] serialize(Transaction transaction, boolean skipSignature, boolean skipSecondSignature, boolean skipMultiSignature) {
23-
return new Serializer(transaction).serialize(skipSignature, skipSecondSignature, skipMultiSignature);
21+
public static byte[] serialize(
22+
Transaction transaction,
23+
boolean skipSignature,
24+
boolean skipSecondSignature,
25+
boolean skipMultiSignature) {
26+
return new Serializer(transaction)
27+
.serialize(skipSignature, skipSecondSignature, skipMultiSignature);
2428
}
2529

26-
public byte[] serialize(boolean skipSignature, boolean skipSecondSignature, boolean skipMultiSignature) {
30+
public byte[] serialize(
31+
boolean skipSignature, boolean skipSecondSignature, boolean skipMultiSignature) {
2732

2833
byte[] common = serializeCommon();
2934
byte[] vendorField = serializeVendorField();
3035

3136
byte[] typeBuffer = this.transaction.serialize();
3237

33-
byte[] signatures = serializeSignatures(skipSignature, skipSecondSignature, skipMultiSignature);
38+
byte[] signatures =
39+
serializeSignatures(skipSignature, skipSecondSignature, skipMultiSignature);
3440

3541
ByteBuffer buffer =
3642
ByteBuffer.allocate(
@@ -99,7 +105,8 @@ private byte[] serializeVendorField() {
99105
return buffer.array();
100106
}
101107

102-
private byte[] serializeSignatures(boolean skipSignature, boolean skipSecondSignature, boolean skipMultiSignature) {
108+
private byte[] serializeSignatures(
109+
boolean skipSignature, boolean skipSecondSignature, boolean skipMultiSignature) {
103110
ByteBuffer buffer = ByteBuffer.allocate(16 * 65);
104111
buffer.order(ByteOrder.LITTLE_ENDIAN);
105112

src/main/java/org/arkecosystem/crypto/transactions/TransactionAsset.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package org.arkecosystem.crypto.transactions;
22

3-
import org.arkecosystem.crypto.enums.HtlcLockExpirationType;
4-
53
import java.util.ArrayList;
64
import java.util.HashMap;
75
import java.util.List;
6+
import org.arkecosystem.crypto.enums.HtlcLockExpirationType;
87

98
public class TransactionAsset {
109
public Signature signature = new Signature();

0 commit comments

Comments
 (0)