Skip to content

Commit

Permalink
add tests for Serde
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed May 12, 2018
1 parent 331186d commit 3100d56
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
} ~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))


}
Original file line number Diff line number Diff line change
Expand Up @@ -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))

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down
18 changes: 9 additions & 9 deletions src/main/scala/com/github/fluency03/blockchain/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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), "")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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())
}

}

0 comments on commit 3100d56

Please sign in to comment.