From 371ca5aad621f1192d08028a96337ef090cd596e Mon Sep 17 00:00:00 2001 From: fluency03 Date: Sun, 6 May 2018 15:39:32 +0200 Subject: [PATCH] add tests for Secp256k1 --- .../github/fluency03/blockchain/crypto/Secp256k1.scala | 6 +++--- .../fluency03/blockchain/crypto/Secp256k1Test.scala | 9 ++++++++- 2 files changed, 11 insertions(+), 4 deletions(-) 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 9e5d904..3b89328 100644 --- a/src/main/scala/com/github/fluency03/blockchain/crypto/Secp256k1.scala +++ b/src/main/scala/com/github/fluency03/blockchain/crypto/Secp256k1.scala @@ -71,10 +71,10 @@ object Secp256k1 { def privateKeyToBytes(privateKey: PrivateKey): Bytes = privateKey.asInstanceOf[ECPrivateKey].getD.toByteArray - def publicKeyToAddress(publicKey: String, networkBytes: String = "00"): String = + def publicKeyHexToAddress(publicKey: String, networkBytes: String = "00"): String = Base58.checkEncode(networkBytes.hex2Bytes ++ publicKey.hex2Bytes.toHash160Digest) - - + def publicKeyToAddress(publicKey: PublicKey, networkBytes: String = "00"): String = + Base58.checkEncode(networkBytes.hex2Bytes ++ publicKeyToBytes(publicKey).toHash160Digest) } 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 880a17c..6c7657b 100644 --- a/src/test/scala/com/github/fluency03/blockchain/crypto/Secp256k1Test.scala +++ b/src/test/scala/com/github/fluency03/blockchain/crypto/Secp256k1Test.scala @@ -20,9 +20,16 @@ class Secp256k1Test extends FlatSpec with Matchers { recoverPublicKey(pair.getPublic.toHex) shouldEqual pair.getPublic recoverPrivateKey(pair.getPrivate.toHex) shouldEqual pair.getPrivate - Secp256k1.publicKeyToAddress("04B4D653FCBB4B96000C99343F23B08A44FA306031E0587F9E657AB" + + Secp256k1.publicKeyHexToAddress("04B4D653FCBB4B96000C99343F23B08A44FA306031E0587F9E657AB" + "4A2541129368D7D9BB05CD8AFBDF7705A6540D98028236965553F91BF1C5B4F70073F55B55D") shouldEqual "1DU8Hi1sbHTpEP9vViBEkEw6noeUrgKkJH" + + Secp256k1.publicKeyToAddress(recoverPublicKey("04B4D653FCBB4B96000C99343F23B08A44FA306031E0587F9E657AB" + + "4A2541129368D7D9BB05CD8AFBDF7705A6540D98028236965553F91BF1C5B4F70073F55B55D")) shouldEqual + "1DU8Hi1sbHTpEP9vViBEkEw6noeUrgKkJH" + + Secp256k1.privateKeyToBytes(pair.getPrivate).toBigInt shouldEqual Secp256k1.privateKeyToHex(pair.getPrivate).hex2BigInt + }