mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-02-22 22:36:34 +01:00
Add fromString for TransactionOutPoint (#4261)
This commit is contained in:
parent
ff8f44282b
commit
0a092e4e03
2 changed files with 24 additions and 8 deletions
|
@ -1,6 +1,7 @@
|
|||
package org.bitcoins.core.protocol.transaction
|
||||
|
||||
import org.bitcoins.core.number.UInt32
|
||||
import org.bitcoins.crypto.DoubleSha256DigestBE
|
||||
import org.bitcoins.testkitcore.util.TestUtil
|
||||
import org.bitcoins.testkitcore.util.BitcoinSUnitTest
|
||||
|
||||
|
@ -18,4 +19,13 @@ class TransactionOutPointTest extends BitcoinSUnitTest {
|
|||
val outPoint = TestUtil.simpleTransaction.inputs.head.previousOutput
|
||||
TransactionOutPoint(outPoint.hex).hex must be(outPoint.hex)
|
||||
}
|
||||
|
||||
it must "read a transaction outpoint from string" in {
|
||||
val txIdBE = DoubleSha256DigestBE(
|
||||
"1d8a6f050746882216518afac933f5c0139e288fbdc3fea8de627b886b0d68cf")
|
||||
val string = s"${txIdBE.hex}:1"
|
||||
val expected = TransactionOutPoint(txIdBE, UInt32.one)
|
||||
|
||||
assert(TransactionOutPoint.fromString(string) == expected)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,12 +2,7 @@ package org.bitcoins.core.protocol.transaction
|
|||
|
||||
import org.bitcoins.core.number.UInt32
|
||||
import org.bitcoins.core.serializers.transaction.RawTransactionOutPointParser
|
||||
import org.bitcoins.crypto.{
|
||||
DoubleSha256Digest,
|
||||
DoubleSha256DigestBE,
|
||||
Factory,
|
||||
NetworkElement
|
||||
}
|
||||
import org.bitcoins.crypto._
|
||||
import scodec.bits._
|
||||
|
||||
/** @param txId The transaction id for the crediting transaction for this input
|
||||
|
@ -21,7 +16,9 @@ case class TransactionOutPoint(txId: DoubleSha256Digest, vout: UInt32)
|
|||
override def bytes = RawTransactionOutPointParser.write(this)
|
||||
|
||||
override def toString: String =
|
||||
s"TransactionOutPoint(${txIdBE.hex}:${vout.toBigInt})"
|
||||
s"TransactionOutPoint($toHumanReadableString)"
|
||||
|
||||
lazy val toHumanReadableString: String = s"${txIdBE.hex}:${vout.toBigInt}"
|
||||
|
||||
def ==(outPoint: TransactionOutPoint): Boolean =
|
||||
txId == outPoint.txId && vout == outPoint.vout
|
||||
|
@ -48,7 +45,9 @@ final object EmptyTransactionOutPoint
|
|||
override def toString(): String = "EmptyTransactionOutPoint"
|
||||
}
|
||||
|
||||
object TransactionOutPoint extends Factory[TransactionOutPoint] {
|
||||
object TransactionOutPoint
|
||||
extends Factory[TransactionOutPoint]
|
||||
with StringFactory[TransactionOutPoint] {
|
||||
|
||||
def fromBytes(bytes: ByteVector): TransactionOutPoint =
|
||||
RawTransactionOutPointParser.read(bytes)
|
||||
|
@ -59,4 +58,11 @@ object TransactionOutPoint extends Factory[TransactionOutPoint] {
|
|||
def apply(txId: DoubleSha256DigestBE, vout: UInt32): TransactionOutPoint = {
|
||||
TransactionOutPoint(txId.flip, vout)
|
||||
}
|
||||
|
||||
override def fromString(string: String): TransactionOutPoint = {
|
||||
val idx = string.indexOf(":")
|
||||
val (txId, vout) = string.splitAt(idx)
|
||||
val uint = UInt32(vout.tail.toLong)
|
||||
TransactionOutPoint(DoubleSha256DigestBE(txId), uint)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue