diff --git a/src/main/scala/com/github/fluency03/blockchain/api/routes/GenericRoutes.scala b/src/main/scala/com/github/fluency03/blockchain/api/routes/GenericRoutes.scala index dfea470..c477da8 100644 --- a/src/main/scala/com/github/fluency03/blockchain/api/routes/GenericRoutes.scala +++ b/src/main/scala/com/github/fluency03/blockchain/api/routes/GenericRoutes.scala @@ -23,7 +23,7 @@ trait GenericRoutes extends RoutesSupport { path(TO_SHA256) { post { entity(as[Input]) { in => - complete(failsafeResp(in.content.toSha256)) + complete(failsafeResp(in.content.sha256)) } } } ~ diff --git a/src/main/scala/com/github/fluency03/blockchain/crypto/Base58.scala b/src/main/scala/com/github/fluency03/blockchain/crypto/Base58.scala index d2abd48..3bc4447 100644 --- a/src/main/scala/com/github/fluency03/blockchain/crypto/Base58.scala +++ b/src/main/scala/com/github/fluency03/blockchain/crypto/Base58.scala @@ -47,7 +47,7 @@ object Base58 { def decodeToHex(str: Base58): HexString = new String(decode(str)) def checkEncode(bytes: Bytes): Base58 = - encode(bytes ++ bytes.toSha256Digest.toSha256Digest.slice(0, 4)) + encode(bytes ++ bytes.sha256Digest.sha256Digest.slice(0, 4)) } \ No newline at end of file diff --git a/src/main/scala/com/github/fluency03/blockchain/crypto/SHA256.scala b/src/main/scala/com/github/fluency03/blockchain/crypto/SHA256.scala index 353a26d..a31eae2 100644 --- a/src/main/scala/com/github/fluency03/blockchain/crypto/SHA256.scala +++ b/src/main/scala/com/github/fluency03/blockchain/crypto/SHA256.scala @@ -21,6 +21,6 @@ object SHA256 { def hash256(bytes: Bytes): HexString = hash(hashDigest(bytes)) - def hash256ToDigest(bytes: Bytes): Bytes = hashDigest(hashDigest(bytes)) + def hash256Digest(bytes: Bytes): Bytes = hashDigest(hashDigest(bytes)) } diff --git a/src/main/scala/com/github/fluency03/blockchain/crypto/Secp256k1.scala b/src/main/scala/com/github/fluency03/blockchain/crypto/Secp256k1.scala index 9c8a44d..9474271 100644 --- a/src/main/scala/com/github/fluency03/blockchain/crypto/Secp256k1.scala +++ b/src/main/scala/com/github/fluency03/blockchain/crypto/Secp256k1.scala @@ -72,7 +72,7 @@ object Secp256k1 { privateKey.asInstanceOf[ECPrivateKey].getD.toByteArray def publicKeyHexToAddress(publicKey: HexString, networkBytes: String = "00"): String = - Base58.checkEncode(networkBytes.hex2Bytes ++ publicKey.hex2Bytes.toHash160Digest) + Base58.checkEncode(networkBytes.hex2Bytes ++ publicKey.hex2Bytes.hash160Digest) /** * Convert a Public Key to its Base58 Address. @@ -88,7 +88,7 @@ object Secp256k1 { * 9 - Base58 encoding of 8 */ def publicKeyToAddress(publicKey: PublicKey, networkBytes: String = "00"): Base58 = - Base58.checkEncode(networkBytes.hex2Bytes ++ publicKeyToBytes(publicKey).toHash160Digest) + Base58.checkEncode(networkBytes.hex2Bytes ++ publicKeyToBytes(publicKey).hash160Digest) def hash160ToAddress(hash160: HexString, networkBytes: String = "00"): Base58 = Base58.checkEncode(networkBytes.hex2Bytes ++ hash160.hex2Bytes) diff --git a/src/main/scala/com/github/fluency03/blockchain/package.scala b/src/main/scala/com/github/fluency03/blockchain/package.scala index 2f6890a..6ae1a72 100644 --- a/src/main/scala/com/github/fluency03/blockchain/package.scala +++ b/src/main/scala/com/github/fluency03/blockchain/package.scala @@ -46,26 +46,26 @@ package object blockchain { def hex2Bytes: Bytes = Hex.decode(str) def hex2Binary: BinaryString = binaryOfHex(str) def toBase64: Base64 = base64Of(str.getBytes) - def toSha256: HexString = SHA256.hash(str) - def toRipemd160: HexString = RIPEMD160.hash(str) + def sha256: HexString = SHA256.hash(str) + def ripemd160: HexString = RIPEMD160.hash(str) } implicit class BytesImplicit(val bytes: Bytes) { def toHex: HexString = Hex.toHexString(bytes) def toBigInt: BigInt = BigInt(bytes) def toBase64: Base64 = base64Of(bytes) - def toSha256: HexString = SHA256.hash(bytes) - def toSha256Digest: Bytes = SHA256.hashDigest(bytes) - def toRipemd160: HexString = RIPEMD160.hash(bytes) - def toRipemd160ODigest: Bytes = RIPEMD160.hashDigest(bytes) - def toHash160: HexString = RIPEMD160.hash160(bytes) - def toHash160Digest: Bytes = RIPEMD160.hash160Digest(bytes) + def sha256: HexString = SHA256.hash(bytes) + def sha256Digest: Bytes = SHA256.hashDigest(bytes) + def ripemd160: HexString = RIPEMD160.hash(bytes) + def ripemd160ODigest: Bytes = RIPEMD160.hashDigest(bytes) + def hash160: HexString = RIPEMD160.hash160(bytes) + def hash160Digest: Bytes = RIPEMD160.hash160Digest(bytes) } implicit class PublicKeyImplicit(val publicKey: PublicKey) { def toHex: HexString = Secp256k1.publicKeyToHex(publicKey) def toBytes: Bytes = Secp256k1.publicKeyToBytes(publicKey) - def toHash160: HexString = Secp256k1.publicKeyToBytes(publicKey).toHash160 + def hash160: HexString = Secp256k1.publicKeyToBytes(publicKey).hash160 def address: Base58 = Secp256k1.publicKeyToAddress(publicKey) } diff --git a/src/test/scala/com/github/fluency03/blockchain/BlockchainPackageTest.scala b/src/test/scala/com/github/fluency03/blockchain/BlockchainPackageTest.scala index 45bbceb..a2015f7 100644 --- a/src/test/scala/com/github/fluency03/blockchain/BlockchainPackageTest.scala +++ b/src/test/scala/com/github/fluency03/blockchain/BlockchainPackageTest.scala @@ -51,36 +51,36 @@ class BlockchainPackageTest extends FlatSpec with Matchers with MockFactory { "StringImplicit" should "convert String to corresponding type." in { fromBase64("open sesame".toBase64) shouldEqual "open sesame" - "open sesame".toSha256 shouldEqual "41ef4bb0b23661e66301aac36066912dac037827b4ae63a7b1165a5aa93ed4eb" - "".toSha256 shouldEqual "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + "open sesame".sha256 shouldEqual "41ef4bb0b23661e66301aac36066912dac037827b4ae63a7b1165a5aa93ed4eb" + "".sha256 shouldEqual "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" "a".hex2Binary shouldEqual "1010" "a".hex2BigInt shouldEqual BigInt(10) "ab".hex2Binary shouldEqual "10101011" "ab".hex2BigInt shouldEqual BigInt(171) - "bitcoin".toRipemd160 shouldEqual "5891bf40b0b0e8e19f524bdc2e842d012264624b" - "blockchain".toRipemd160 shouldEqual "5c403af45cae136a79eea3c7e9f79c3dd049776b" + "bitcoin".ripemd160 shouldEqual "5891bf40b0b0e8e19f524bdc2e842d012264624b" + "blockchain".ripemd160 shouldEqual "5c403af45cae136a79eea3c7e9f79c3dd049776b" } "BytesImplicit" should "convert Array of Byte to corresponding type." in { val bytes: Bytes = Array(192.toByte, 168.toByte, 1, 9) bytes.toHex shouldEqual "c0a80109" bytes.toBase64 shouldEqual "wKgBCQ==" - "open sesame".getBytes.toSha256 shouldEqual "41ef4bb0b23661e66301aac36066912dac037827b4ae63a7b1165a5aa93ed4eb" - "open sesame".getBytes.toSha256Digest shouldEqual ("41ef4bb0b23661e66301aac36066912da" + + "open sesame".getBytes.sha256 shouldEqual "41ef4bb0b23661e66301aac36066912dac037827b4ae63a7b1165a5aa93ed4eb" + "open sesame".getBytes.sha256Digest shouldEqual ("41ef4bb0b23661e66301aac36066912da" + "c037827b4ae63a7b1165a5aa93ed4eb").hex2Bytes - "173BDED8F2A2069C193E63EA30DC8FD20E815EC3642B9C24AD7002C03D1BFB9B".hex2Bytes.toRipemd160 shouldEqual + "173BDED8F2A2069C193E63EA30DC8FD20E815EC3642B9C24AD7002C03D1BFB9B".hex2Bytes.ripemd160 shouldEqual "88C2D2FA846282C870A76CADECBE45C4ACD72BB6".toLowerCase - "173BDED8F2A2069C193E63EA30DC8FD20E815EC3642B9C24AD7002C03D1BFB9B".hex2Bytes.toRipemd160ODigest shouldEqual + "173BDED8F2A2069C193E63EA30DC8FD20E815EC3642B9C24AD7002C03D1BFB9B".hex2Bytes.ripemd160ODigest shouldEqual "88C2D2FA846282C870A76CADECBE45C4ACD72BB6".hex2Bytes ("04b4d653fcbb4b96000c99343f23b08a44fa306031e0587f9e657ab4a25411" + - "29368d7d9bb05cd8afbdf7705a6540d98028236965553f91bf1c5b4f70073f55b55d").hex2Bytes.toHash160Digest shouldEqual + "29368d7d9bb05cd8afbdf7705a6540d98028236965553f91bf1c5b4f70073f55b55d").hex2Bytes.hash160Digest shouldEqual "88C2D2FA846282C870A76CADECBE45C4ACD72BB6".hex2Bytes ("04b4d653fcbb4b96000c99343f23b08a44fa306031e0587f9e657ab4a25411" + - "29368d7d9bb05cd8afbdf7705a6540d98028236965553f91bf1c5b4f70073f55b55d").hex2Bytes.toHash160 shouldEqual + "29368d7d9bb05cd8afbdf7705a6540d98028236965553f91bf1c5b4f70073f55b55d").hex2Bytes.hash160 shouldEqual "88C2D2FA846282C870A76CADECBE45C4ACD72BB6".toLowerCase } diff --git a/src/test/scala/com/github/fluency03/blockchain/core/KeyContainerTest.scala b/src/test/scala/com/github/fluency03/blockchain/core/KeyContainerTest.scala index e28624b..aaaa4bb 100644 --- a/src/test/scala/com/github/fluency03/blockchain/core/KeyContainerTest.scala +++ b/src/test/scala/com/github/fluency03/blockchain/core/KeyContainerTest.scala @@ -25,7 +25,7 @@ class KeyContainerTest extends FlatSpec with Matchers { "KeyContainer" should "be able to sign a TxIn." in { val kc = KeyContainer() - val id = "".toSha256 + val id = "".sha256 val txIn = TxIn(Outpoint("def0", 0), "abc") val uTxOs: mutable.Map[Outpoint, TxOut] = mutable.Map.empty[Outpoint, TxOut] diff --git a/src/test/scala/com/github/fluency03/blockchain/core/TransactionTest.scala b/src/test/scala/com/github/fluency03/blockchain/core/TransactionTest.scala index 3f89cb0..8dc7fc7 100644 --- a/src/test/scala/com/github/fluency03/blockchain/core/TransactionTest.scala +++ b/src/test/scala/com/github/fluency03/blockchain/core/TransactionTest.scala @@ -181,7 +181,7 @@ class TransactionTest extends FlatSpec with Matchers { val address1 = pair1.getPublic.toHex val pair2 = Secp256k1.generateKeyPair() val address2 = pair2.getPublic.toHex - val randHash = "".toSha256 + val randHash = "".sha256 val tx: Transaction = Transaction( Seq(TxIn(Outpoint(randHash, 0), ""), TxIn(Outpoint(randHash, 1), "")), diff --git a/src/test/scala/com/github/fluency03/blockchain/crypto/Secp256k1Test.scala b/src/test/scala/com/github/fluency03/blockchain/crypto/Secp256k1Test.scala index d699afc..8cc79f1 100644 --- a/src/test/scala/com/github/fluency03/blockchain/crypto/Secp256k1Test.scala +++ b/src/test/scala/com/github/fluency03/blockchain/crypto/Secp256k1Test.scala @@ -18,7 +18,11 @@ class Secp256k1Test extends FlatSpec with Matchers { recoverPublicKey(publicKeyToHex(pair.getPublic)) shouldEqual pair.getPublic recoverPrivateKey(privateKeyToHex(pair.getPrivate)) shouldEqual pair.getPrivate recoverPublicKey(pair.getPublic.toHex) shouldEqual pair.getPublic + recoverPublicKey(pair.getPublic.toBytes.toHex) shouldEqual pair.getPublic + pair.getPublic.toBytes.toHex shouldEqual pair.getPublic.toHex + pair.getPublic.hash160 shouldEqual RIPEMD160.hash(SHA256.hashDigest(pair.getPublic.toBytes)) recoverPrivateKey(pair.getPrivate.toHex) shouldEqual pair.getPrivate + recoverPrivateKey(pair.getPrivate.toBytes.toHex) shouldEqual pair.getPrivate Secp256k1.publicKeyHexToAddress("04B4D653FCBB4B96000C99343F23B08A44FA306031E0587F9E657AB" + "4A2541129368D7D9BB05CD8AFBDF7705A6540D98028236965553F91BF1C5B4F70073F55B55D") shouldEqual diff --git a/src/test/scala/com/github/fluency03/blockchain/misc/SerdeTest.scala b/src/test/scala/com/github/fluency03/blockchain/misc/SerdeTest.scala index 5d10155..cd98ed2 100644 --- a/src/test/scala/com/github/fluency03/blockchain/misc/SerdeTest.scala +++ b/src/test/scala/com/github/fluency03/blockchain/misc/SerdeTest.scala @@ -1,9 +1,24 @@ package com.github.fluency03.blockchain.misc +import java.io.NotSerializableException + import org.scalatest.{FlatSpec, Matchers} -class SerdeTest extends FlatSpec with Matchers { +case class Obj1() extends Serializable +case class Obj2() +class Obj3() extends Serializable +class Obj4() + +class SerdeTest extends FlatSpec with Matchers { + "Serde" should "serialize an object and deserialize it." in { + Serde.deserialize[String](Serde.serialize("abc")) shouldEqual "abc" + Serde.deserialize[Int](Serde.serialize(123)) shouldEqual 123 + Serde.deserialize[Obj1](Serde.serialize(Obj1())) shouldEqual Obj1() + Serde.deserialize[Obj2](Serde.serialize(Obj2())) shouldEqual Obj2() + Serde.deserialize[Obj3](Serde.serialize(new Obj3)) shouldBe an[Obj3] + a[NotSerializableException] should be thrownBy Serde.serialize(new Obj4()) + } }