Adding magic bytes to network parameters, doing real validation for bitcoin addresses now

This commit is contained in:
Chris Stewart 2015-12-23 16:49:04 -06:00
parent 7bf2d3c6e2
commit f29d7e6f42
4 changed files with 57 additions and 17 deletions

View File

@ -0,0 +1,38 @@
package org.scalacoin.config
import org.bitcoinj.core.NetworkParameters
import org.bitcoinj.params.{MainNetParams, RegTestParams, TestNet3Params}
/**
* Created by chris on 7/27/15.
*/
trait NetworkParametersWrapper {
def network : NetworkParameters
def p2pkhNetworkByte : Byte
def p2shNetworkByte : Byte
}
trait MainNet extends NetworkParametersWrapper {
override def network = MainNetParams.get
override def p2pkhNetworkByte = 0x00
override def p2shNetworkByte = 0x05
}
object MainNet extends MainNet
trait TestNet3 extends NetworkParametersWrapper {
override def network = TestNet3Params.get
override def p2pkhNetworkByte = 0x6F
override def p2shNetworkByte = 196.toByte
}
object TestNet3 extends TestNet3
trait RegTest extends NetworkParametersWrapper {
override def network = RegTestParams.get
override def p2pkhNetworkByte = TestNet3.p2pkhNetworkByte
override def p2shNetworkByte = TestNet3.p2shNetworkByte
}
object RegTest extends RegTest

View File

@ -1,6 +1,7 @@
package org.scalacoin.protocol
import org.bitcoinj.core.{VersionedChecksummedBytes, Base58, Utils}
import org.scalacoin.config.{RegTest, TestNet3, MainNet}
case class AddressInfo(bitcoinAddress: BitcoinAddress, n_tx: Long, total_received: Long, total_sent: Long,
final_balance: Long)
@ -43,7 +44,14 @@ object BitcoinAddress {
* @return
*/
def p2shAddress(address : String) : Boolean = {
address.charAt(0) == '3' || address.charAt(0) == '2'
try {
val base58decodeChecked : Array[Byte] = Base58.decodeChecked(address)
val firstByte = base58decodeChecked(0)
((firstByte == MainNet.p2shNetworkByte || firstByte == TestNet3.p2shNetworkByte) && base58decodeChecked.size == 21)
} catch {
case _ : Throwable => false
}
}
/**
@ -59,8 +67,16 @@ object BitcoinAddress {
* @return
*/
def p2pkh(address : String) : Boolean = {
val firstChar = address.charAt(0)
firstChar == '1' || firstChar == 'm' || firstChar == 'n'
try {
val base58decodeChecked : Array[Byte] = Base58.decodeChecked(address)
val firstByte = base58decodeChecked(0)
((firstByte == MainNet.p2pkhNetworkByte || firstByte == TestNet3.p2pkhNetworkByte ||
firstByte == RegTest.p2pkhNetworkByte) && base58decodeChecked.size == 21)
} catch {
case _ : Throwable => false
}
}
/**

View File

@ -20,7 +20,6 @@ trait VarInt {
case class NetworkVarInt( serialization : String) extends VarInt with ScalacoinUtil {
override def length : BigInt = {
val slice = hexToBigInt(serialization.slice(0,2))
println(slice)
if (slice == 0xFD) hexToBigInt(serialization.slice(2,6))
else if (slice == 0xFE) hexToBigInt(serialization.slice(2,10))
else if (slice == 0xFF) hexToBigInt(serialization.slice(2,18))

View File

@ -41,19 +41,6 @@ class BitcoinAddressTest extends FlatSpec with MustMatchers {
}
}
"3J98t1WpEZ73CNmQviecrnyiWr (26 characters) " must "be a valid bitcoin address" in {
val address = "3J98t1WpEZ73CNmQviecrnyiWr"
BitcoinAddress(address).value must be(address)
}
"3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLyy (35 characters)" must "be a valid bitcoin address" in {
val address = "3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLyy"
BitcoinAddress(address).value must be(address)
}
"akJsoCcyh34FGPotxfEoSXGwFPCNAkyCgTA" must "be a valid asset address" in {
val assetAddress = AssetAddress("akJsoCcyh34FGPotxfEoSXGwFPCNAkyCgTA")
assetAddress.value must be ("akJsoCcyh34FGPotxfEoSXGwFPCNAkyCgTA")