Skip to content

Commit 119c260

Browse files
committed
[ETCM-522][ETCM-523] Apply scalafix and scalafmt
1 parent 12db9a5 commit 119c260

File tree

607 files changed

+9664
-8043
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

607 files changed

+9664
-8043
lines changed

bytes/src/main/scala/io/iohk/ethereum/utils/ByteStringUtils.scala

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ object ByteStringUtils {
1414
ByteString(Hex.decode(hash))
1515

1616
implicit class Padding(val bs: ByteString) extends AnyVal {
17-
def padToByteString(length: Int, b: Byte): ByteString = {
17+
def padToByteString(length: Int, b: Byte): ByteString =
1818
if (length <= bs.length) bs
1919
else {
2020
val len = Math.max(bs.length, length)
@@ -27,7 +27,6 @@ object ByteStringUtils {
2727
}
2828
ByteString.fromArray(result)
2929
}
30-
}
3130
}
3231

3332
implicit class ByteStringOps(val bytes: ByteString) extends AnyVal {
@@ -54,9 +53,8 @@ object ByteStringUtils {
5453
def asByteArray: Array[Byte] = Array(b)
5554
}
5655

57-
implicit val byteStringOrdering: Ordering[ByteString] = {
56+
implicit val byteStringOrdering: Ordering[ByteString] =
5857
Ordering.by[ByteString, Seq[Byte]](_.toSeq)
59-
}
6058

6159
def concatByteStrings(head: ByteStringElement, tail: ByteStringElement*): ByteString = {
6260
val it = Iterator.single(head) ++ tail.iterator

bytes/src/main/scala/io/iohk/ethereum/utils/ByteUtils.scala

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package io.iohk.ethereum.utils
22

33
import java.math.BigInteger
4-
import java.nio.{ByteBuffer, ByteOrder}
4+
import java.nio.ByteBuffer
5+
import java.nio.ByteOrder
56

67
import akka.util.ByteString
78

89
import scala.util.Random
910

1011
object ByteUtils {
1112

12-
/**
13-
* Calculates number of matching bytes from the beginning of both arrays.
13+
/** Calculates number of matching bytes from the beginning of both arrays.
1414
* Due to performance reasons needs to be as fast as possible which means usage of while loops and var's.
1515
*
1616
* @param a - first array of bytes to check
@@ -19,9 +19,8 @@ object ByteUtils {
1919
*/
2020
def matchingLength(a: Array[Byte], b: Array[Byte]): Int = {
2121
var prefixLen = 0
22-
while (prefixLen < a.length && prefixLen < b.length && a(prefixLen) == b(prefixLen)) {
22+
while (prefixLen < a.length && prefixLen < b.length && a(prefixLen) == b(prefixLen))
2323
prefixLen = prefixLen + 1
24-
}
2524
prefixLen
2625
}
2726

@@ -38,16 +37,15 @@ object ByteUtils {
3837
bigIntegerToBytes(b.bigInteger, numBytes)
3938

4039
def toBigInt(bytes: ByteString): BigInt =
41-
bytes.foldLeft(BigInt(0)) { (n, b) => (n << 8) + (b & 0xff) }
40+
bytes.foldLeft(BigInt(0))((n, b) => (n << 8) + (b & 0xff))
4241

4342
def bigIntToUnsignedByteArray(i: BigInt): Array[Byte] = {
4443
val asByteArray = i.toByteArray
4544
if (asByteArray.head == 0) asByteArray.tail
4645
else asByteArray
4746
}
4847

49-
/**
50-
* Calculates xor distance between two byte arrays. Due to performance reasons needs to be as fast as possible
48+
/** Calculates xor distance between two byte arrays. Due to performance reasons needs to be as fast as possible
5149
* which means usage of while loops and var's.
5250
*
5351
* @param a - array of bytes to xor
@@ -108,9 +106,8 @@ object ByteUtils {
108106
data
109107
}
110108

111-
def compactPickledBytes(buffer: ByteBuffer): ByteString = {
109+
def compactPickledBytes(buffer: ByteBuffer): ByteString =
112110
ByteString(compactPickledBytesToArray(buffer))
113-
}
114111

115112
def byteSequenceToBuffer(bytes: IndexedSeq[Byte]): ByteBuffer =
116113
ByteBuffer.wrap(bytes.toArray)
@@ -127,12 +124,10 @@ object ByteUtils {
127124
ret
128125
}
129126

130-
def getIntFromWord(arr: Array[Byte]): Int = {
127+
def getIntFromWord(arr: Array[Byte]): Int =
131128
ByteBuffer.wrap(arr, 0, 4).order(ByteOrder.LITTLE_ENDIAN).getInt
132-
}
133129

134-
/**
135-
* Converts array of Int to corresponding array of bytes. Due to performance reasons needs to be as fast as possible
130+
/** Converts array of Int to corresponding array of bytes. Due to performance reasons needs to be as fast as possible
136131
* which means usage of while loops and var's.
137132
*
138133
* @param arr - array of int's to convert
@@ -141,7 +136,7 @@ object ByteUtils {
141136
* @param bigEndian - param specifying which int representation should be used.
142137
* @return Unit
143138
*/
144-
def intsToBytesMut(arr: Array[Int], b: Array[Byte], bigEndian: Boolean): Unit = {
139+
def intsToBytesMut(arr: Array[Int], b: Array[Byte], bigEndian: Boolean): Unit =
145140
if (!bigEndian) {
146141
var off = 0
147142
var i = 0
@@ -175,10 +170,8 @@ object ByteUtils {
175170
i = i + 1
176171
}
177172
}
178-
}
179173

180-
/**
181-
* Converts array of bytes to corresponding array of ints. Due to performance reasons needs to be as fast as possible
174+
/** Converts array of bytes to corresponding array of ints. Due to performance reasons needs to be as fast as possible
182175
* which means usage of while loops and var's.
183176
*
184177
* @param b - array of bytes to convert
@@ -187,7 +180,7 @@ object ByteUtils {
187180
* @param bigEndian - param specifying which int representation should be used.
188181
* @return Unit
189182
*/
190-
def bytesToIntsMut(b: Array[Byte], arr: Array[Int], bigEndian: Boolean): Unit = {
183+
def bytesToIntsMut(b: Array[Byte], arr: Array[Int], bigEndian: Boolean): Unit =
191184
if (!bigEndian) {
192185
var off = 0
193186
var i = 0
@@ -222,6 +215,5 @@ object ByteUtils {
222215
i = i + 1
223216
}
224217
}
225-
}
226218

227219
}

bytes/src/test/scala/io/iohk/ethereum/utils/ByteStringUtilsTest.scala

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package io.iohk.ethereum.utils
22

33
import akka.util.ByteString
4-
import org.scalatest.wordspec.AnyWordSpec
4+
5+
import scala.collection.immutable.ArraySeq
6+
import scala.util.Failure
7+
import scala.util.Success
8+
import scala.util.Try
9+
510
import org.scalatest.matchers.should.Matchers
11+
import org.scalatest.wordspec.AnyWordSpec
12+
613
import ByteStringUtils._
7-
import scala.collection.immutable.ArraySeq
8-
import scala.util.{Try, Success, Failure}
914

1015
class ByteStringUtilsTest extends AnyWordSpec with Matchers {
1116

@@ -39,7 +44,7 @@ class ByteStringUtilsTest extends AnyWordSpec with Matchers {
3944
val bs3: Byte = 2
4045
val bs4 = Array[Byte](3, 3)
4146
val bs5 = Array[Byte](4, 4)
42-
val summarized: ByteString = bs1 ++ bs2
47+
bs1 ++ bs2
4348
val concatenated: ByteString = ByteStringUtils.concatByteStrings(bs1, bs2, bs3, bs4, bs5)
4449
concatenated shouldEqual string2hash("0000FFFF0203030404")
4550
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package io.iohk.ethereum.utils
22

3-
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
3+
import org.scalacheck.Arbitrary
4+
import org.scalacheck.Gen
45
import org.scalatest.funsuite.AnyFunSuite
5-
import org.scalacheck.{Arbitrary, Gen}
6+
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
67

78
class ByteUtilsSpec extends AnyFunSuite with ScalaCheckPropertyChecks {
89
def byteArrayOfNItemsGen(n: Int): Gen[Array[Byte]] =
@@ -12,15 +13,15 @@ class ByteUtilsSpec extends AnyFunSuite with ScalaCheckPropertyChecks {
1213
forAll(byteArrayOfNItemsGen(32)) { bytes =>
1314
val toInts = ByteUtils.bytesToInts(bytes, bigEndian = false)
1415
val asBytes = ByteUtils.intsToBytes(toInts, bigEndian = false)
15-
assert(asBytes sameElements bytes)
16+
assert(asBytes.sameElements(bytes))
1617
}
1718
}
1819

1920
test("Convert Bytes to Int in big endian") {
2021
forAll(byteArrayOfNItemsGen(32)) { bytes =>
2122
val toInts = ByteUtils.bytesToInts(bytes, bigEndian = true)
2223
val asBytes = ByteUtils.intsToBytes(toInts, bigEndian = true)
23-
assert(asBytes sameElements bytes)
24+
assert(asBytes.sameElements(bytes))
2425
}
2526
}
2627
}

crypto/src/main/scala/io/iohk/ethereum/crypto/ConcatKDFBytesGenerator.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
package io.iohk.ethereum.crypto
22

33
import akka.util.ByteString
4+
45
import org.bouncycastle.crypto.Digest
56
import org.bouncycastle.util.Pack
67

7-
/**
8-
* Basic KDF generator for derived keys and ivs as defined by NIST SP 800-56A.
8+
/** Basic KDF generator for derived keys and ivs as defined by NIST SP 800-56A.
99
* @param digest for source of derived keys
1010
*/
1111
class ConcatKDFBytesGenerator(digest: Digest) {
1212
val digestSize: Int = digest.getDigestSize
1313

14-
/**
15-
* @param outputLength length of output that will be produced by this method,
14+
/** @param outputLength length of output that will be produced by this method,
1615
* maximum value is (digest output size in bits) * (2^32 - 1) but it should not be a problem
1716
* because we are using Int
1817
* @throws scala.IllegalArgumentException ("Output length too large") when outputLength is greater than (digest output size in bits) * (2^32 - 1)

crypto/src/main/scala/io/iohk/ethereum/crypto/ECDSASignature.scala

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package io.iohk.ethereum.crypto
22

33
import akka.util.ByteString
4-
import io.iohk.ethereum.utils.ByteUtils
4+
5+
import scala.util.Try
6+
57
import org.bouncycastle.asn1.x9.X9IntegerConverter
68
import org.bouncycastle.crypto.AsymmetricCipherKeyPair
79
import org.bouncycastle.crypto.digests.SHA256Digest
810
import org.bouncycastle.crypto.params.ECPublicKeyParameters
9-
import org.bouncycastle.crypto.signers.{ECDSASigner, HMacDSAKCalculator}
10-
import org.bouncycastle.math.ec.{ECCurve, ECPoint}
11+
import org.bouncycastle.crypto.signers.ECDSASigner
12+
import org.bouncycastle.crypto.signers.HMacDSAKCalculator
13+
import org.bouncycastle.math.ec.ECCurve
14+
import org.bouncycastle.math.ec.ECPoint
1115

12-
import scala.util.Try
16+
import io.iohk.ethereum.utils.ByteUtils
1317

1418
object ECDSASignature {
1519

@@ -29,18 +33,16 @@ object ECDSASignature {
2933
val positivePointSign: Byte = 28
3034
val newPositivePointSign: Byte = 36
3135

32-
val allowedPointSigns = Set(negativePointSign, positivePointSign)
36+
val allowedPointSigns: Set[Byte] = Set(negativePointSign, positivePointSign)
3337

34-
def apply(r: ByteString, s: ByteString, v: Byte): ECDSASignature = {
38+
def apply(r: ByteString, s: ByteString, v: Byte): ECDSASignature =
3539
ECDSASignature(BigInt(1, r.toArray), BigInt(1, s.toArray), v)
36-
}
3740

38-
def fromBytes(bytes65: ByteString): Option[ECDSASignature] = {
41+
def fromBytes(bytes65: ByteString): Option[ECDSASignature] =
3942
if (bytes65.length == EncodedLength)
4043
Some(apply(bytes65.take(RLength), bytes65.drop(RLength).take(SLength), bytes65.last))
4144
else
4245
None
43-
}
4446

4547
def sign(messageHash: ByteString, prvKey: ByteString): ECDSASignature =
4648
sign(messageHash.toArray, keyPairFromPrvKey(prvKey.toArray), None)
@@ -63,18 +65,17 @@ object ECDSASignature {
6365
val pointSign = chainId match {
6466
case Some(id) if v == negativePointSign => (id * 2 + newNegativePointSign).toByte
6567
case Some(id) if v == positivePointSign => (id * 2 + newPositivePointSign).toByte
66-
case None => v
67-
case _ => throw new IllegalStateException(s"Unexpected pointSign. ChainId: ${chainId}, v: ${v}")
68+
case None => v
69+
case _ => throw new IllegalStateException(s"Unexpected pointSign. ChainId: ${chainId}, v: ${v}")
6870
}
6971

7072
ECDSASignature(r, s, pointSign)
7173
}
7274

73-
/**
74-
* new formula for calculating point sign post EIP 155 adoption
75+
/** new formula for calculating point sign post EIP 155 adoption
7576
* v = CHAIN_ID * 2 + 35 or v = CHAIN_ID * 2 + 36
7677
*/
77-
private def getRecoveredPointSign(pointSign: Byte, chainId: Option[Byte]): Option[Byte] = {
78+
private def getRecoveredPointSign(pointSign: Byte, chainId: Option[Byte]): Option[Byte] =
7879
(chainId match {
7980
case Some(id) =>
8081
if (pointSign == negativePointSign || pointSign == (id * 2 + newNegativePointSign).toByte) {
@@ -86,7 +87,6 @@ object ECDSASignature {
8687
}
8788
case None => Some(pointSign)
8889
}).filter(pointSign => allowedPointSigns.contains(pointSign))
89-
}
9090

9191
private def canonicalise(s: BigInt): BigInt = {
9292
val halfCurveOrder: BigInt = curveParams.getN.shiftRight(1)
@@ -109,7 +109,7 @@ object ECDSASignature {
109109
recId: Byte,
110110
chainId: Option[Byte],
111111
messageHash: Array[Byte]
112-
): Option[Array[Byte]] = {
112+
): Option[Array[Byte]] =
113113
Try {
114114
val order = curve.getCurve.getOrder
115115
//ignore case when x = r + order because it is negligibly improbable
@@ -132,7 +132,6 @@ object ECDSASignature {
132132
} else None
133133
}
134134
}.toOption.flatten
135-
}
136135

137136
private def constructPoint(xCoordinate: BigInt, recId: Int): ECPoint = {
138137
val x9 = new X9IntegerConverter
@@ -142,8 +141,7 @@ object ECDSASignature {
142141
}
143142
}
144143

145-
/**
146-
* ECDSASignature r and s are same as in documentation where signature is represented by tuple (r, s)
144+
/** ECDSASignature r and s are same as in documentation where signature is represented by tuple (r, s)
147145
*
148146
* The `publicKey` method is also the way to verify the signature: if the key can be retrieved based
149147
* on the signed message, the signature is correct, otherwise it isn't.
@@ -154,17 +152,15 @@ object ECDSASignature {
154152
*/
155153
case class ECDSASignature(r: BigInt, s: BigInt, v: Byte) {
156154

157-
/**
158-
* returns ECC point encoded with on compression and without leading byte indicating compression
155+
/** returns ECC point encoded with on compression and without leading byte indicating compression
159156
* @param messageHash message to be signed; should be a hash of the actual data.
160157
* @param chainId optional value if you want new signing schema with recovery id calculated with chain id
161158
* @return
162159
*/
163160
def publicKey(messageHash: Array[Byte], chainId: Option[Byte] = None): Option[Array[Byte]] =
164161
ECDSASignature.recoverPubBytes(r, s, v, chainId, messageHash)
165162

166-
/**
167-
* returns ECC point encoded with on compression and without leading byte indicating compression
163+
/** returns ECC point encoded with on compression and without leading byte indicating compression
168164
* @param messageHash message to be signed; should be a hash of the actual data.
169165
* @return
170166
*/

crypto/src/main/scala/io/iohk/ethereum/crypto/ECIESCoder.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
package io.iohk.ethereum.crypto
22

3-
import java.io.{ByteArrayInputStream, IOException}
3+
import java.io.ByteArrayInputStream
4+
import java.io.IOException
45
import java.math.BigInteger
56
import java.security.SecureRandom
67

8+
import org.bouncycastle.crypto.BufferedBlockCipher
9+
import org.bouncycastle.crypto.InvalidCipherTextException
710
import org.bouncycastle.crypto.digests.SHA256Digest
811
import org.bouncycastle.crypto.engines.AESEngine
912
import org.bouncycastle.crypto.generators.ECKeyPairGenerator
1013
import org.bouncycastle.crypto.macs.HMac
1114
import org.bouncycastle.crypto.modes.SICBlockCipher
1215
import org.bouncycastle.crypto.params._
13-
import org.bouncycastle.crypto.{BufferedBlockCipher, InvalidCipherTextException}
1416
import org.bouncycastle.math.ec.ECPoint
1517

1618
object ECIESCoder {
1719

1820
val KeySize = 128
1921
val PublicKeyOverheadSize = 65
2022
val MacOverheadSize = 32
21-
val OverheadSize = PublicKeyOverheadSize + KeySize / 8 + MacOverheadSize
23+
val OverheadSize: Int = PublicKeyOverheadSize + KeySize / 8 + MacOverheadSize
2224

2325
@throws[IOException]
2426
@throws[InvalidCipherTextException]

0 commit comments

Comments
 (0)