diff --git a/src/main/scala/com/fluency03/blockchain/Crypto.scala b/src/main/scala/com/fluency03/blockchain/Crypto.scala index 1e41186..098abde 100644 --- a/src/main/scala/com/fluency03/blockchain/Crypto.scala +++ b/src/main/scala/com/fluency03/blockchain/Crypto.scala @@ -17,7 +17,7 @@ object Crypto { val ecSpec: ECParameterSpec = ECNamedCurveTable.getParameterSpec(SPECP256K1) - def sign(data: Array[Byte], privateKey: Array[Byte]): Array[Byte] = { + def sign(data: Bytes, privateKey: Bytes): Bytes = { val keySpec: PKCS8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey) val keyFactory: KeyFactory = KeyFactory.getInstance(KEY_ALGORITHM) val key: PrivateKey = keyFactory.generatePrivate(keySpec) @@ -28,7 +28,7 @@ object Crypto { sig.sign() } - def verify(data: Array[Byte], publicKey: Array[Byte], signature: Array[Byte]): Boolean = { + def verify(data: Bytes, publicKey: Bytes, signature: Bytes): Boolean = { val keySpec: X509EncodedKeySpec = new X509EncodedKeySpec(publicKey) val keyFactory: KeyFactory = KeyFactory.getInstance(KEY_ALGORITHM) val key: PublicKey = keyFactory.generatePublic(keySpec) diff --git a/src/main/scala/com/fluency03/blockchain/Util.scala b/src/main/scala/com/fluency03/blockchain/Util.scala index a960e72..67e57d2 100644 --- a/src/main/scala/com/fluency03/blockchain/Util.scala +++ b/src/main/scala/com/fluency03/blockchain/Util.scala @@ -17,7 +17,7 @@ object Util { * Generate digest from a input String. * https://gist.github.com/navicore/6234040bbfce3aa58f866db314c07c15 */ - def digestOf(text: String): Array[Byte] = + def digestOf(text: String): Bytes = MessageDigest.getInstance("SHA-256").digest(text.getBytes("UTF-8")) /** @@ -53,7 +53,7 @@ object Util { /** * Encode an Array of Bytes String to Base64. */ - def base64Of(data: Array[Byte]): String = Base64.toBase64String(data) + def base64Of(data: Bytes): String = Base64.toBase64String(data) /** * Decode a Base64 to String. diff --git a/src/main/scala/com/fluency03/blockchain/core/Transaction.scala b/src/main/scala/com/fluency03/blockchain/core/Transaction.scala index ff327b7..2014cbc 100644 --- a/src/main/scala/com/fluency03/blockchain/core/Transaction.scala +++ b/src/main/scala/com/fluency03/blockchain/core/Transaction.scala @@ -64,7 +64,7 @@ object Transaction { ) def signTxIn( - txId: Array[Byte], + txId: Bytes, txIn: TxIn, keyPair: KeyPair, unspentTxOuts: mutable.Map[Outpoint, TxOut] @@ -78,7 +78,7 @@ object Transaction { def validateTxIn(txIn: TxIn, txId: String, unspentTxOuts: mutable.Map[Outpoint, TxOut]): Boolean = validateTxIn(txIn, txId.hex2Bytes, unspentTxOuts) - def validateTxIn(txIn: TxIn, txId: Array[Byte], unspentTxOuts: mutable.Map[Outpoint, TxOut]): Boolean = + def validateTxIn(txIn: TxIn, txId: Bytes, unspentTxOuts: mutable.Map[Outpoint, TxOut]): Boolean = unspentTxOuts.get(txIn.previousOut) match { case Some(txOut) => Crypto.verify(txId, txOut.address.hex2Bytes, txIn.signature.hex2Bytes) case None => false diff --git a/src/test/scala/com/fluency03/blockchain/UtilTest.scala b/src/test/scala/com/fluency03/blockchain/UtilTest.scala index 564e34c..4468ea5 100644 --- a/src/test/scala/com/fluency03/blockchain/UtilTest.scala +++ b/src/test/scala/com/fluency03/blockchain/UtilTest.scala @@ -57,4 +57,22 @@ class UtilTest extends FlatSpec with Matchers with MockFactory { an[NumberFormatException] should be thrownBy binaryOfHex("g") } + "StringImplicit" should "convert String to corresponding type." in { + fromBase64("open sesame".toBase64) shouldEqual "open sesame" + "open sesame".toSha256 shouldEqual "41ef4bb0b23661e66301aac36066912dac037827b4ae63a7b1165a5aa93ed4eb" + "".toSha256 shouldEqual "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + "a".hex2Binary shouldEqual "1010" + "ab".hex2Binary shouldEqual "10101011" + } + + "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==" + } + + "genesisTimestamp" should "be the Epoch time of 2018-04-11T18:52:01Z ." in { + genesisTimestamp shouldEqual 1523472721 + } + }