mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-03-15 20:30:17 +01:00
Adding magic bytes to network parameters, doing real validation for bitcoin addresses now
This commit is contained in:
parent
75d57084e7
commit
8483fd83b6
4 changed files with 57 additions and 17 deletions
|
@ -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
|
|
@ -1,6 +1,7 @@
|
||||||
package org.scalacoin.protocol
|
package org.scalacoin.protocol
|
||||||
|
|
||||||
import org.bitcoinj.core.{VersionedChecksummedBytes, Base58, Utils}
|
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,
|
case class AddressInfo(bitcoinAddress: BitcoinAddress, n_tx: Long, total_received: Long, total_sent: Long,
|
||||||
final_balance: Long)
|
final_balance: Long)
|
||||||
|
@ -43,7 +44,14 @@ object BitcoinAddress {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
def p2shAddress(address : String) : Boolean = {
|
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
|
* @return
|
||||||
*/
|
*/
|
||||||
def p2pkh(address : String) : Boolean = {
|
def p2pkh(address : String) : Boolean = {
|
||||||
val firstChar = address.charAt(0)
|
try {
|
||||||
firstChar == '1' || firstChar == 'm' || firstChar == 'n'
|
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
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -20,7 +20,6 @@ trait VarInt {
|
||||||
case class NetworkVarInt( serialization : String) extends VarInt with ScalacoinUtil {
|
case class NetworkVarInt( serialization : String) extends VarInt with ScalacoinUtil {
|
||||||
override def length : BigInt = {
|
override def length : BigInt = {
|
||||||
val slice = hexToBigInt(serialization.slice(0,2))
|
val slice = hexToBigInt(serialization.slice(0,2))
|
||||||
println(slice)
|
|
||||||
if (slice == 0xFD) hexToBigInt(serialization.slice(2,6))
|
if (slice == 0xFD) hexToBigInt(serialization.slice(2,6))
|
||||||
else if (slice == 0xFE) hexToBigInt(serialization.slice(2,10))
|
else if (slice == 0xFE) hexToBigInt(serialization.slice(2,10))
|
||||||
else if (slice == 0xFF) hexToBigInt(serialization.slice(2,18))
|
else if (slice == 0xFF) hexToBigInt(serialization.slice(2,18))
|
||||||
|
|
|
@ -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 {
|
"akJsoCcyh34FGPotxfEoSXGwFPCNAkyCgTA" must "be a valid asset address" in {
|
||||||
val assetAddress = AssetAddress("akJsoCcyh34FGPotxfEoSXGwFPCNAkyCgTA")
|
val assetAddress = AssetAddress("akJsoCcyh34FGPotxfEoSXGwFPCNAkyCgTA")
|
||||||
assetAddress.value must be ("akJsoCcyh34FGPotxfEoSXGwFPCNAkyCgTA")
|
assetAddress.value must be ("akJsoCcyh34FGPotxfEoSXGwFPCNAkyCgTA")
|
||||||
|
|
Loading…
Add table
Reference in a new issue