Skip to content

Commit

Permalink
public key to address implicit
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed May 9, 2018
1 parent 62d441d commit 642bc2c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/main/resources/address
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1DU8Hi1sbHTpEP9vViBEkEw6noeUrgKkJH
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,10 @@ object Secp256k1 {
def publicKeyToAddress(publicKey: PublicKey, networkBytes: String = "00"): String =
Base58.checkEncode(networkBytes.hex2Bytes ++ publicKeyToBytes(publicKey).toHash160Digest)

def addressToHash160(address: String, networkBytes: String = "00"): (String, String) = {
val decoded: Bytes = Base58.decode(address)
val (preBytes, fourBytes) = decoded.splitAt(decoded.length - 4)
preBytes.toHex.splitAt(2)
}

}
14 changes: 10 additions & 4 deletions src/main/scala/com/github/fluency03/blockchain/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.time.Instant

import com.github.fluency03.blockchain.crypto.Secp256k1.{privateKeyToHex, publicKeyToHex}
import com.github.fluency03.blockchain.core.{Peer, PeerSimple, TxIn, TxOut}
import com.github.fluency03.blockchain.crypto.{RIPEMD160, SHA256}
import com.github.fluency03.blockchain.crypto.{RIPEMD160, SHA256, Secp256k1}
import org.bouncycastle.util.encoders.{Base64, Hex}
import org.json4s.native.Serialization
import org.json4s.{Formats, NoTypeHints}
Expand All @@ -22,13 +22,18 @@ package object blockchain {

implicit val defaultCharset: Charset = Charset.forName("UTF-8")

val GENESIS_MINER_PUBLIC_KEY_RESOURCE: String = "public-key"
val GENESIS_MINER_PRIVATE_KEY_RESOURCE: String = "private-key"
val GENESIS_MINER_ADDRESS_RESOURCE: String = "address"

def getResource(re: String): String = Source.fromResource(re).getLines.mkString

val ZERO64: String = "0000000000000000000000000000000000000000000000000000000000000000"

lazy val genesisTimestamp: Long = Instant.parse(genesisTime).getEpochSecond
val genesisTimestamp: Long = Instant.parse(genesisTime).getEpochSecond

val genesisTime: String = "2018-04-11T18:52:01Z"

val genesisMiner: String = Source.fromResource("public-key").getLines.mkString
val genesisMiner: String = getResource(GENESIS_MINER_PUBLIC_KEY_RESOURCE)

val SLOGAN: String = "Welcome to Blockchain in Scala!"

Expand Down Expand Up @@ -56,6 +61,7 @@ package object blockchain {

implicit class PublicKeyImplicit(val publicKey: PublicKey) {
def toHex: String = publicKeyToHex(publicKey)
def address: String = Secp256k1.publicKeyToAddress(publicKey)
}

implicit class PrivateKeyImplicit(val privateKey: PrivateKey) {
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/address
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1DU8Hi1sbHTpEP9vViBEkEw6noeUrgKkJH
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ class Secp256k1Test extends FlatSpec with Matchers {
"4A2541129368D7D9BB05CD8AFBDF7705A6540D98028236965553F91BF1C5B4F70073F55B55D")) shouldEqual
"1DU8Hi1sbHTpEP9vViBEkEw6noeUrgKkJH"

recoverPublicKey("04B4D653FCBB4B96000C99343F23B08A44FA306031E0587F9E657AB" +
"4A2541129368D7D9BB05CD8AFBDF7705A6540D98028236965553F91BF1C5B4F70073F55B55D").address shouldEqual
"1DU8Hi1sbHTpEP9vViBEkEw6noeUrgKkJH"

Secp256k1.addressToHash160("1DU8Hi1sbHTpEP9vViBEkEw6noeUrgKkJH") shouldEqual
("00", "88C2D2FA846282C870A76CADECBE45C4ACD72BB6".toLowerCase)

Secp256k1.privateKeyToBytes(pair.getPrivate).toBigInt shouldEqual Secp256k1.privateKeyToHex(pair.getPrivate).hex2BigInt

}
Expand Down

0 comments on commit 642bc2c

Please sign in to comment.