Skip to content

Commit

Permalink
add tests for implicit class in package object
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed Apr 24, 2018
1 parent 29aa1c4 commit 93f5c1b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/main/scala/com/fluency03/blockchain/Crypto.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/com/fluency03/blockchain/Util.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

/**
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ object Transaction {
)

def signTxIn(
txId: Array[Byte],
txId: Bytes,
txIn: TxIn,
keyPair: KeyPair,
unspentTxOuts: mutable.Map[Outpoint, TxOut]
Expand All @@ -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
Expand Down
18 changes: 18 additions & 0 deletions src/test/scala/com/fluency03/blockchain/UtilTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}

0 comments on commit 93f5c1b

Please sign in to comment.