mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-02-23 06:45:21 +01:00
commit
5750da8fb4
11 changed files with 88 additions and 10 deletions
|
@ -21,4 +21,12 @@ class NetworkParametersTest extends BitcoinSUnitTest {
|
|||
it must "create the correct magic network bytes for regtest" in {
|
||||
BitcoinSUtil.encodeHex(RegTest.magicBytes) must be("fabfb5da")
|
||||
}
|
||||
|
||||
it must "get the correct Network from string" in {
|
||||
assert(Networks.fromString("mainnet").contains(MainNet))
|
||||
assert(Networks.fromString("testnet").contains(TestNet3))
|
||||
assert(Networks.fromString("regtest").contains(RegTest))
|
||||
assert(Networks.fromString("").isEmpty)
|
||||
assert(Networks.fromString("craig wright is a fraud").isEmpty)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -72,7 +72,7 @@ class CurrencyUnitTest extends BitcoinSUnitTest {
|
|||
}
|
||||
}
|
||||
|
||||
it must "subtract satoshis " in {
|
||||
it must "subtract satoshis" in {
|
||||
forAll(CurrencyUnitGenerator.satoshis, CurrencyUnitGenerator.satoshis) {
|
||||
(num1: Satoshis, num2: Satoshis) =>
|
||||
val result: Try[Int64] = Try(Int64(num1.toBigInt - num2.toBigInt))
|
||||
|
@ -150,6 +150,7 @@ class CurrencyUnitTest extends BitcoinSUnitTest {
|
|||
forAll(CurrencyUnitGenerator.satoshis) { satoshis =>
|
||||
val b = Bitcoins(satoshis)
|
||||
assert(b.satoshis == satoshis)
|
||||
assert(CurrencyUnits.toSatoshis(b) == satoshis)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -164,4 +165,16 @@ class CurrencyUnitTest extends BitcoinSUnitTest {
|
|||
else actual.isFailure && expected.isFailure
|
||||
}
|
||||
}
|
||||
|
||||
it must "correctly compare two currency units" in {
|
||||
forAll(CurrencyUnitGenerator.satoshis, CurrencyUnitGenerator.satoshis) {
|
||||
(num1, num2) =>
|
||||
if (num1 > num2)
|
||||
assert(num1.compare(num2) == 1)
|
||||
else if (num1 < num2)
|
||||
assert(num1.compare(num2) == -1)
|
||||
else
|
||||
assert(num1.compare(num2) == 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,5 +15,6 @@ class FilterTypeTest extends BitcoinSUnitTest {
|
|||
assert(FilterType.getCode(FilterType.Basic) == 0)
|
||||
assert(FilterType.byCode(0) == FilterType.Basic)
|
||||
assertThrows[IllegalArgumentException](FilterType.byCode(1))
|
||||
assertThrows[IllegalArgumentException](FilterType.getCode(FilterType.fromHex("ffff")))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,4 +37,15 @@ class ServiceIdentifierTest extends BitcoinSUnitTest {
|
|||
it must "parse NODE_NETWORK_LIMITED" in {
|
||||
assert(ServiceIdentifier.NODE_NETWORK_LIMITED.nodeNetworkLimited)
|
||||
}
|
||||
|
||||
it must "correctly get a ServiceIdentifier from string" in {
|
||||
assert(ServiceIdentifier.fromString("NETWORK") == ServiceIdentifier.NODE_NETWORK)
|
||||
assert(ServiceIdentifier.fromString("NETWORK_LIMITED") == ServiceIdentifier.NODE_NETWORK_LIMITED)
|
||||
assert(ServiceIdentifier.fromString("WITNESS") == ServiceIdentifier.NODE_WITNESS)
|
||||
assert(ServiceIdentifier.fromString("BLOOM") == ServiceIdentifier.NODE_BLOOM)
|
||||
assert(ServiceIdentifier.fromString("GETUTXO") == ServiceIdentifier.NODE_GET_UTXO)
|
||||
assert(ServiceIdentifier.fromString("COMPACT_FILTERS") == ServiceIdentifier.NODE_COMPACT_FILTERS)
|
||||
assert(ServiceIdentifier.fromString("XTHIN") == ServiceIdentifier.NODE_XTHIN)
|
||||
assertThrows[IllegalArgumentException](ServiceIdentifier.fromString("this is invalid"))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,10 @@ import org.bitcoins.testkit.util.BitcoinSUnitTest
|
|||
|
||||
class TypeIdentifierTest extends BitcoinSUnitTest {
|
||||
|
||||
it must "Create a MsgUnassigned" in {
|
||||
assert(TypeIdentifier(100000).isInstanceOf[MsgUnassigned])
|
||||
}
|
||||
|
||||
"MsgTx" must "serialize to 01000000" in {
|
||||
MsgTx.hex must be("01000000")
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package org.bitcoins.core.protocol
|
||||
|
||||
import org.bitcoins.core.config.{MainNet, RegTest, TestNet3}
|
||||
import org.bitcoins.core.crypto.Sha256Hash160Digest
|
||||
import org.bitcoins.core.protocol.script.ScriptPubKey
|
||||
import org.bitcoins.core.crypto.{ECPublicKey, Sha256Hash160Digest}
|
||||
import org.bitcoins.core.protocol.script.{EmptyScriptPubKey, P2PKHScriptPubKey, P2SHScriptPubKey, P2WPKHWitnessSPKV0, ScriptPubKey, WitnessScriptPubKeyV0}
|
||||
import org.bitcoins.testkit.util.BitcoinSUnitTest
|
||||
|
||||
import scala.util.{Failure, Success, Try}
|
||||
|
@ -90,4 +90,36 @@ class BitcoinAddressTest extends BitcoinSUnitTest {
|
|||
val addr = P2SHAddress(scriptPubKey, MainNet)
|
||||
addr must be(BitcoinAddress("3P14159f73E4gFr7JterCCQh9QjiTjiZrG").get)
|
||||
}
|
||||
|
||||
it must "create a bech32 address from a WitnessScriptPubKey" in {
|
||||
val scriptPubKey = P2WPKHWitnessSPKV0(ECPublicKey.freshPublicKey)
|
||||
assert(Bech32Address.fromScriptPubKey(scriptPubKey, RegTest).isSuccess)
|
||||
}
|
||||
|
||||
it must "fail to create a bech32 address from an invalid ScriptPubKey" in {
|
||||
assert(Bech32Address.fromScriptPubKey(EmptyScriptPubKey, RegTest).isFailure)
|
||||
}
|
||||
|
||||
it must "create an address from a P2PKHScriptPubKey" in {
|
||||
val scriptPubKey = P2PKHScriptPubKey(ECPublicKey.freshPublicKey)
|
||||
assert(P2PKHAddress.fromScriptPubKey(scriptPubKey, RegTest).isSuccess)
|
||||
}
|
||||
|
||||
it must "fail to create a P2PKHAddress address from an invalid ScriptPubKey" in {
|
||||
assert(P2PKHAddress.fromScriptPubKey(EmptyScriptPubKey, RegTest).isFailure)
|
||||
}
|
||||
|
||||
it must "create an address from a P2SHScriptPubKey" in {
|
||||
val scriptPubKey = P2SHScriptPubKey(EmptyScriptPubKey)
|
||||
assert(P2SHAddress.fromScriptPubKey(scriptPubKey, RegTest).isSuccess)
|
||||
}
|
||||
|
||||
it must "fail to create a P2SHScriptPubKey address from an invalid ScriptPubKey" in {
|
||||
assert(P2SHAddress.fromScriptPubKey(EmptyScriptPubKey, RegTest).isFailure)
|
||||
}
|
||||
|
||||
it must "create an address from a ScriptPubKey" in {
|
||||
val scriptPubKey = P2SHScriptPubKey(EmptyScriptPubKey)
|
||||
assert(Address.fromScriptPubKey(scriptPubKey, RegTest).isSuccess)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,7 +16,6 @@ import org.bitcoins.core.script.result.ScriptOk
|
|||
import org.bitcoins.core.serializers.transaction.RawBaseTransactionParser
|
||||
import org.bitcoins.core.util.{CryptoUtil, TestUtil}
|
||||
import org.bitcoins.testkit.util.BitcoinSUnitTest
|
||||
import org.slf4j.LoggerFactory
|
||||
import spray.json._
|
||||
import scodec.bits._
|
||||
|
||||
|
@ -71,10 +70,11 @@ class TransactionTest extends BitcoinSUnitTest {
|
|||
"0000000000000000000000000000000000000000000000000000000000000000")
|
||||
}
|
||||
|
||||
it must "calculate the size of a tranaction correctly" in {
|
||||
it must "calculate the size of a transaction correctly" in {
|
||||
val rawTx = TestUtil.rawTransaction
|
||||
val tx = Transaction(rawTx)
|
||||
//size is in bytes so divide by 2
|
||||
assert(tx.size == tx.totalSize)
|
||||
tx.size must be(rawTx.size / 2)
|
||||
}
|
||||
|
||||
|
@ -135,7 +135,7 @@ class TransactionTest extends BitcoinSUnitTest {
|
|||
tx2.size must be(216)
|
||||
tx2.weight must be(534)
|
||||
tx2.vsize must be(134)
|
||||
|
||||
tx2.baseSize must be(106)
|
||||
}
|
||||
|
||||
it must "parse a transaction with an OP_PUSHDATA4 op code but not enough data to push" in {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package org.bitcoins.core.serializers.script
|
||||
|
||||
import org.bitcoins.core.protocol.script.ScriptPubKey
|
||||
import org.bitcoins.core.protocol.script.{EmptyScriptPubKey, ScriptPubKey}
|
||||
import org.bitcoins.core.script.bitwise.OP_EQUALVERIFY
|
||||
import org.bitcoins.core.script.constant._
|
||||
import org.bitcoins.core.script.crypto.{OP_CHECKSIG, OP_HASH160}
|
||||
|
@ -21,6 +21,10 @@ class RawScriptPubKeyParserTest extends BitcoinSUnitTest {
|
|||
TestUtil.rawScriptPubKey)
|
||||
}
|
||||
|
||||
it must "read an EmptyScriptPubKey" in {
|
||||
assert(RawScriptPubKeyParser.read(ByteVector.empty) == EmptyScriptPubKey)
|
||||
}
|
||||
|
||||
it must "read a raw scriptPubKey and give us the expected asm" in {
|
||||
val scriptPubKey = RawScriptPubKeyParser.read(TestUtil.rawP2PKHScriptPubKey)
|
||||
val expectedAsm: Seq[ScriptToken] =
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package org.bitcoins.core.serializers.script
|
||||
|
||||
import org.bitcoins.core.protocol.script.ScriptSignature
|
||||
import org.bitcoins.core.protocol.script.{EmptyScriptSignature, ScriptSignature}
|
||||
import org.bitcoins.core.script.constant._
|
||||
import org.bitcoins.core.util.{BitcoinSUtil, TestUtil}
|
||||
import org.bitcoins.testkit.util.BitcoinSUnitTest
|
||||
|
@ -21,6 +21,10 @@ class RawScriptSignatureParserTest extends BitcoinSUnitTest {
|
|||
encode(RawScriptSignatureParser.write(scriptSig)) must be(rawScriptSig)
|
||||
}
|
||||
|
||||
it must "read an EmptyScriptSignature" in {
|
||||
assert(RawScriptSignatureParser.read(ByteVector.empty) == EmptyScriptSignature)
|
||||
}
|
||||
|
||||
it must "read then write a raw script sig" in {
|
||||
//from this tx
|
||||
//https://tbtc.blockr.io/api/v1/tx/raw/bdc221db675c06dbee2ae75d33e31cad4e2555efea10c337ff32c8cdf97f8e74
|
||||
|
|
|
@ -190,7 +190,7 @@ object Bech32Address extends AddressFactory[Bech32Address] {
|
|||
np: NetworkParameters): Try[Bech32Address] =
|
||||
spk match {
|
||||
case witSPK: WitnessScriptPubKey =>
|
||||
Bech32Address.fromScriptPubKey(witSPK, np)
|
||||
Success(Bech32Address(witSPK, np))
|
||||
case x @ (_: P2PKScriptPubKey | _: P2PKHScriptPubKey |
|
||||
_: P2PKWithTimeoutScriptPubKey | _: MultiSignatureScriptPubKey |
|
||||
_: P2SHScriptPubKey | _: LockTimeScriptPubKey |
|
||||
|
|
|
@ -1078,6 +1078,7 @@ sealed abstract class ScriptInterpreter extends BitcoinSLogger {
|
|||
val inputOutputsNotZero =
|
||||
!(transaction.inputs.isEmpty || transaction.outputs.isEmpty)
|
||||
val txNotLargerThanBlock = transaction.bytes.size < Consensus.maxBlockSize
|
||||
val txNotHeavierThanBlock = transaction.weight < Consensus.maxBlockWeight
|
||||
val outputsSpendValidAmountsOfMoney = !transaction.outputs.exists(o =>
|
||||
o.value < CurrencyUnits.zero || o.value > Consensus.maxMoney)
|
||||
|
||||
|
@ -1094,7 +1095,7 @@ sealed abstract class ScriptInterpreter extends BitcoinSLogger {
|
|||
} else {
|
||||
!transaction.inputs.exists(_.previousOutput == EmptyTransactionOutPoint)
|
||||
}
|
||||
inputOutputsNotZero && txNotLargerThanBlock && outputsSpendValidAmountsOfMoney &&
|
||||
inputOutputsNotZero && txNotLargerThanBlock && txNotHeavierThanBlock && outputsSpendValidAmountsOfMoney &&
|
||||
allOutputsValidMoneyRange && noDuplicateInputs && isValidScriptSigForCoinbaseTx
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue