mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-01-18 13:24:25 +01:00
Renaming scriptSigVarInt & VarInt classes to CompactSizeUInt
This commit is contained in:
parent
4bc28b5d37
commit
8a39a55431
@ -2,7 +2,7 @@ package org.scalacoin.marshallers.transaction
|
||||
|
||||
import org.scalacoin.marshallers.RawBitcoinSerializer
|
||||
import org.scalacoin.marshallers.script.RawScriptSignatureParser
|
||||
import org.scalacoin.protocol.{VarIntImpl, VarInt}
|
||||
import org.scalacoin.protocol.{CompactSizeUInt}
|
||||
import org.scalacoin.protocol.script.ScriptSignature
|
||||
import org.scalacoin.protocol.transaction.{TransactionInputImpl, TransactionOutPoint, TransactionInput}
|
||||
import org.scalacoin.util.ScalacoinUtil
|
||||
@ -31,23 +31,23 @@ trait RawTransactionInputParser extends RawBitcoinSerializer[Seq[TransactionInpu
|
||||
val outPointBytes = bytes.take(outPointBytesSize)
|
||||
val outPoint : TransactionOutPoint = RawTransactionOutPointParser.read(outPointBytes)
|
||||
|
||||
val scriptVarIntSize : Int = ScalacoinUtil.parseVarIntSize(bytes(outPointBytesSize)).toInt
|
||||
logger.debug("VarInt hex: " + ScalacoinUtil.encodeHex(bytes.slice(outPointBytesSize,outPointBytesSize + scriptVarIntSize)))
|
||||
val scriptSigVarInt : VarInt = ScalacoinUtil.parseVarInt(bytes.slice(outPointBytesSize,outPointBytesSize + scriptVarIntSize))
|
||||
val scriptCompactSizeUIntSize : Int = ScalacoinUtil.parseCompactSizeUIntSize(bytes(outPointBytesSize)).toInt
|
||||
logger.debug("VarInt hex: " + ScalacoinUtil.encodeHex(bytes.slice(outPointBytesSize,outPointBytesSize + scriptCompactSizeUIntSize)))
|
||||
val scriptSigCompactSizeUInt : CompactSizeUInt = ScalacoinUtil.parseCompactSizeUInt(bytes.slice(outPointBytesSize,outPointBytesSize + scriptCompactSizeUIntSize))
|
||||
|
||||
val scriptSigBytes = bytes.slice(outPointBytesSize+ scriptVarIntSize,
|
||||
outPointBytesSize + scriptVarIntSize + scriptSigVarInt.num.toInt)
|
||||
val scriptSigBytes = bytes.slice(outPointBytesSize+ scriptCompactSizeUIntSize,
|
||||
outPointBytesSize + scriptCompactSizeUIntSize + scriptSigCompactSizeUInt.num.toInt)
|
||||
|
||||
val scriptSig : ScriptSignature = RawScriptSignatureParser.read(scriptSigBytes)
|
||||
|
||||
val sequenceBytesSize = 4
|
||||
val endOfScriptSigBytes = outPointBytesSize + scriptSigVarInt.num.toInt + scriptVarIntSize
|
||||
val endOfScriptSigBytes = outPointBytesSize + scriptSigCompactSizeUInt.num.toInt + scriptCompactSizeUIntSize
|
||||
val lastInputByte = endOfScriptSigBytes + sequenceBytesSize
|
||||
val sequenceBytes = bytes.slice(endOfScriptSigBytes,lastInputByte)
|
||||
logger.debug("Sequence bytes: " + ScalacoinUtil.encodeHex(sequenceBytes))
|
||||
val sequenceNumberHex : String = ScalacoinUtil.encodeHex(sequenceBytes)
|
||||
val sequenceNumber : Long = java.lang.Long.parseLong(sequenceNumberHex,16)
|
||||
val txInput = TransactionInputImpl(outPoint,scriptSigVarInt, scriptSig,sequenceNumber)
|
||||
val txInput = TransactionInputImpl(outPoint,scriptSigCompactSizeUInt, scriptSig,sequenceNumber)
|
||||
|
||||
val newAccum = txInput :: accum
|
||||
val bytesToBeParsed = bytes.slice(lastInputByte, bytes.size)
|
||||
@ -79,7 +79,7 @@ trait RawTransactionInputParser extends RawBitcoinSerializer[Seq[TransactionInpu
|
||||
*/
|
||||
def write(input : TransactionInput) : String = {
|
||||
val outPoint = RawTransactionOutPointParser.write(input.previousOutput)
|
||||
val varInt = input.scriptSigVarInt.hex
|
||||
val varInt = input.scriptSigCompactSizeUInt.hex
|
||||
val scriptSig = RawScriptSignatureParser.write(input.scriptSignature)
|
||||
val sequenceWithoutPadding = input.sequence.toHexString
|
||||
val paddingNeeded = 8 - sequenceWithoutPadding.size
|
||||
|
@ -7,7 +7,7 @@ import org.scalacoin.util.ScalacoinUtil
|
||||
*/
|
||||
|
||||
|
||||
trait VarInt {
|
||||
trait CompactSizeUInt {
|
||||
|
||||
/**
|
||||
* The number parsed from VarInt
|
||||
@ -31,7 +31,7 @@ trait VarInt {
|
||||
|
||||
}
|
||||
|
||||
case class VarIntImpl(num : Long, size : Long) extends VarInt
|
||||
case class CompactSizeUIntImpl(num : Long, size : Long) extends CompactSizeUInt
|
||||
|
||||
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
package org.scalacoin.protocol.transaction
|
||||
|
||||
import org.scalacoin.marshallers.transaction.{RawTransactionInputParser, TransactionElement}
|
||||
import org.scalacoin.protocol.VarInt
|
||||
import org.scalacoin.protocol.{CompactSizeUInt}
|
||||
import org.scalacoin.protocol.script.ScriptSignature
|
||||
|
||||
/**
|
||||
@ -12,12 +12,13 @@ trait TransactionInput extends TransactionElement {
|
||||
def scriptSignature : ScriptSignature
|
||||
def sequence : Long
|
||||
|
||||
def scriptSigVarInt : VarInt
|
||||
def scriptSigCompactSizeUInt : CompactSizeUInt
|
||||
//https://bitcoin.org/en/developer-reference#txin
|
||||
override def size = previousOutput.size + scriptSignature.size + scriptSigVarInt.size.toInt + 4
|
||||
override def size = previousOutput.size + scriptSignature.size +
|
||||
scriptSigCompactSizeUInt.size.toInt + 4
|
||||
|
||||
def hex = RawTransactionInputParser.write(Seq(this))
|
||||
}
|
||||
|
||||
case class TransactionInputImpl(previousOutput : TransactionOutPoint, scriptSigVarInt : VarInt,
|
||||
case class TransactionInputImpl(previousOutput : TransactionOutPoint, scriptSigCompactSizeUInt : CompactSizeUInt,
|
||||
scriptSignature : ScriptSignature, sequence : Long) extends TransactionInput
|
||||
|
@ -2,7 +2,6 @@ package org.scalacoin.protocol.transaction
|
||||
|
||||
import org.scalacoin.currency.{CurrencyUnit, Satoshis}
|
||||
import org.scalacoin.marshallers.transaction.{RawTransactionOutputParser, TransactionElement}
|
||||
import org.scalacoin.protocol.VarInt
|
||||
import org.scalacoin.protocol.script.ScriptPubKey
|
||||
|
||||
/**
|
||||
|
@ -1,6 +1,6 @@
|
||||
package org.scalacoin.util
|
||||
|
||||
import org.scalacoin.protocol.{VarIntImpl, VarInt}
|
||||
import org.scalacoin.protocol.{CompactSizeUInt, CompactSizeUIntImpl}
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
/**
|
||||
@ -114,18 +114,18 @@ trait NumberUtil {
|
||||
|
||||
def toByteList(long : Long) = BigInt(long).toByteArray.toList
|
||||
|
||||
def parseVarInt(hex : String) : VarInt = parseVarInt(ScalacoinUtil.decodeHex(hex))
|
||||
def parseCompactSizeUInt(hex : String) : CompactSizeUInt = parseCompactSizeUInt(ScalacoinUtil.decodeHex(hex))
|
||||
|
||||
def parseVarInt(bytes : Seq[Byte]) : VarInt = {
|
||||
def parseCompactSizeUInt(bytes : Seq[Byte]) : CompactSizeUInt = {
|
||||
require(bytes.size > 0, "Cannot parse a VarInt if the byte array is size 0")
|
||||
//8 bit number
|
||||
if (parseLong(bytes.head) < 253) VarIntImpl(parseLong(bytes.head),1)
|
||||
if (parseLong(bytes.head) < 253) CompactSizeUIntImpl(parseLong(bytes.head),1)
|
||||
//16 bit number
|
||||
else if (parseLong(bytes.head) == 253) VarIntImpl(parseLong(bytes.slice(1,3).reverse),3)
|
||||
else if (parseLong(bytes.head) == 253) CompactSizeUIntImpl(parseLong(bytes.slice(1,3).reverse),3)
|
||||
//32 bit number
|
||||
else if (parseLong(bytes.head) == 254) VarIntImpl(parseLong(bytes.slice(1,5).reverse),5)
|
||||
else if (parseLong(bytes.head) == 254) CompactSizeUIntImpl(parseLong(bytes.slice(1,5).reverse),5)
|
||||
//64 bit number
|
||||
else VarIntImpl(parseLong(bytes.slice(1,9).reverse),9)
|
||||
else CompactSizeUIntImpl(parseLong(bytes.slice(1,9).reverse),9)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -133,7 +133,7 @@ trait NumberUtil {
|
||||
* @param byte
|
||||
* @return
|
||||
*/
|
||||
def parseVarIntSize(byte : Byte) : Long = {
|
||||
def parseCompactSizeUIntSize(byte : Byte) : Long = {
|
||||
//8 bit number
|
||||
if (parseLong(byte) < 253) 1
|
||||
//16 bit number
|
||||
|
@ -100,7 +100,7 @@ class RawTransactionInputParserTest extends FlatSpec with MustMatchers with RawT
|
||||
txInput.head.previousOutput.txId must be ("e99eb3e6551844d0db252ef242c043796b3b0ccfb126c0ae09f9dd0230e2f10d")
|
||||
txInput.head.previousOutput.vout must be (0)
|
||||
txInput.head.scriptSignature.hex must be ("004730440220028c02f14654a0cc12c7e3229adb09d5d35bebb6ba1057e39adb1b2706607b0d0220564fab12c6da3d5acef332406027a7ff1cbba980175ffd880e1ba1bf40598f6b014830450221009362f8d67b60773745e983d07ba10efbe566127e244b724385b2ca2e47292dda022033def393954c320653843555ddbe7679b35cc1cacfe1dad923977de8cd6cc6d7014c695221025e9adcc3d65c11346c8a6069d6ebf5b51b348d1d6dc4b95e67480c34dc0bc75c21030585b3c80f4964bf0820086feda57c8e49fa1eab925db7c04c985467973df96521037753a5e3e9c4717d3f81706b38a6fb82b5fb89d29e580d7b98a37fea8cdefcad53ae")
|
||||
txInput.head.scriptSigVarInt.num must be (txInput.head.scriptSignature.size)
|
||||
txInput.head.scriptSigCompactSizeUInt.num must be (txInput.head.scriptSignature.size)
|
||||
txInput.head.sequence must be (4294967295L)
|
||||
RawTransactionInputParser.write(txInput) must be (rawTxInput)
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
package org.scalacoin.util
|
||||
|
||||
import org.scalacoin.currency.CurrencyUnits
|
||||
import org.scalacoin.protocol.VarIntImpl
|
||||
import org.scalacoin.protocol.{CompactSizeUIntImpl}
|
||||
import org.scalacoin.protocol.script.{ScriptSignatureImpl, ScriptPubKey, ScriptPubKeyImpl, ScriptSignature}
|
||||
import org.scalacoin.protocol.transaction._
|
||||
import org.scalacoin.script.constant.{OP_0, ScriptToken}
|
||||
@ -20,7 +20,7 @@ trait TransactionTestUtil {
|
||||
def buildSpendingTransaction(scriptSignature : ScriptSignature, tx : Transaction) : Transaction = {
|
||||
|
||||
val outpoint = TransactionOutPointImpl(tx.txId,0)
|
||||
val input = TransactionInputImpl(outpoint,VarIntImpl(0,0),scriptSignature,0xFFFFFFFF)
|
||||
val input = TransactionInputImpl(outpoint,CompactSizeUIntImpl(0,0),scriptSignature,0xFFFFFFFF)
|
||||
//empty script pubkey
|
||||
val scriptPubKey = ScriptPubKeyImpl(Seq(),"",Seq())
|
||||
val output = TransactionOutputImpl(CurrencyUnits.oneSatoshi,0,scriptPubKey)
|
||||
@ -38,7 +38,7 @@ trait TransactionTestUtil {
|
||||
val outpoint = TransactionOutPointImpl("",0)
|
||||
|
||||
val scriptSignature = ScriptSignatureImpl(Seq(OP_0,OP_0),"0000")
|
||||
val input = TransactionInputImpl(outpoint,VarIntImpl(0,0),scriptSignature,0xFFFFFFFF)
|
||||
val input = TransactionInputImpl(outpoint,CompactSizeUIntImpl(0,0),scriptSignature,0xFFFFFFFF)
|
||||
val output = TransactionOutputImpl(CurrencyUnits.oneSatoshi,0,scriptPubKey)
|
||||
|
||||
TransactionImpl(TransactionConstants.version,Seq(input),Seq(output),TransactionConstants.lockTime)
|
||||
|
Loading…
Reference in New Issue
Block a user