mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-01-19 05:43:51 +01:00
Refactoring script parser into marshallers package
This commit is contained in:
commit
bf0caf4371
@ -0,0 +1,23 @@
|
||||
package org.scalacoin.marshallers
|
||||
|
||||
/**
|
||||
* Created by chris on 1/11/16.
|
||||
*/
|
||||
trait RawBitcoinSerializer[T] {
|
||||
|
||||
/**
|
||||
* Reads a hexadecimal value and transforms it into the native
|
||||
* scala type T
|
||||
* @param hex
|
||||
* @return
|
||||
*/
|
||||
def read(hex : String) : T
|
||||
|
||||
/**
|
||||
* Takes a type T and writes it into the appropriate type T
|
||||
* @param t
|
||||
* @return
|
||||
*/
|
||||
def write(t : T) : String
|
||||
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package org.scalacoin.script.parsing
|
||||
package org.scalacoin.marshallers.script
|
||||
|
||||
import org.scalacoin.script._
|
||||
import org.scalacoin.script.constant._
|
||||
@ -55,7 +55,7 @@ trait ScriptParser extends ScalacoinUtil {
|
||||
loop(bytes, List()).reverse
|
||||
}
|
||||
|
||||
def pushConstant(op : ScriptNumber, bytes : List[Byte]) : (ScriptConstant, List[Byte]) = {
|
||||
private def pushConstant(op : ScriptNumber, bytes : List[Byte]) : (ScriptConstant, List[Byte]) = {
|
||||
val finalIndex = op.opCode
|
||||
val constant : ScriptConstantImpl = ScriptConstantImpl(encodeHex(bytes.slice(0,finalIndex)))
|
||||
(constant, bytes.slice(finalIndex,bytes.size))
|
@ -0,0 +1,27 @@
|
||||
package org.scalacoin.marshallers.transaction
|
||||
|
||||
import org.scalacoin.marshallers.RawBitcoinSerializer
|
||||
import org.scalacoin.protocol.transaction.{TransactionOutPointImpl, TransactionOutPoint}
|
||||
import org.scalacoin.util.ScalacoinUtil
|
||||
|
||||
/**
|
||||
* Source for serialization
|
||||
* https://bitcoin.org/en/developer-reference#outpoint
|
||||
*
|
||||
*/
|
||||
object RawTransactionOutPointMarshaller extends RawBitcoinSerializer[TransactionOutPoint] {
|
||||
|
||||
|
||||
def read(str : String) : TransactionOutPoint = {
|
||||
val bytes = ScalacoinUtil.decodeHex(str)
|
||||
val txId : List[Byte] = bytes.slice(0,16)
|
||||
val index : BigInt = BigInt(bytes.slice(16, bytes.size).toArray)
|
||||
TransactionOutPointImpl(ScalacoinUtil.encodeHex(txId), index.toInt)
|
||||
}
|
||||
|
||||
def write(outPoint : TransactionOutPoint) : String = {
|
||||
val indexBytes : List[Byte] = List(0x00,0x00,0x00,outPoint.vout.toByte)
|
||||
outPoint.txId + ScalacoinUtil.encodeHex(indexBytes)
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package org.scalacoin.marshallers.transaction
|
||||
|
||||
import org.scalacoin.currency.Satoshis
|
||||
import org.scalacoin.marshallers.RawBitcoinSerializer
|
||||
import org.scalacoin.marshallers.script.ScriptParser
|
||||
import org.scalacoin.protocol.transaction.TransactionOutput
|
||||
import org.scalacoin.util.ScalacoinUtil
|
||||
|
||||
/**
|
||||
* Created by chris on 1/11/16.
|
||||
* https://bitcoin.org/en/developer-reference#txout
|
||||
*/
|
||||
object RawTransactionOutputMarshaller extends RawBitcoinSerializer[Seq[TransactionOutput]] with ScriptParser {
|
||||
|
||||
|
||||
override def read(str : String) : Seq[TransactionOutput] = {
|
||||
val bytes = ScalacoinUtil.decodeHex(str)
|
||||
|
||||
val numOutputs = bytes(0).toInt
|
||||
val satoshisHex = ScalacoinUtil.encodeHex(bytes.slice(1,9))
|
||||
val satoshis = Satoshis(Integer.parseInt(satoshisHex,16))
|
||||
???
|
||||
}
|
||||
|
||||
override def write(outputs : Seq[TransactionOutput]) : String = ???
|
||||
}
|
@ -1,8 +1,5 @@
|
||||
package org.scalacoin.script.parsing
|
||||
package org.scalacoin.marshallers.script
|
||||
|
||||
import org.scalacoin.script.constant.{ScriptNumberImpl, OP_14}
|
||||
import org.scalacoin.script.crypto.OP_HASH160
|
||||
import org.scalacoin.script.stack.OP_DUP
|
||||
import org.scalacoin.util.{ScalacoinUtil, TestUtil}
|
||||
import org.scalatest.{FlatSpec, MustMatchers}
|
||||
|
@ -0,0 +1,19 @@
|
||||
package org.scalacoin.marshallers.transaction
|
||||
|
||||
import org.scalacoin.marshallers.RawBitcoinSerializer
|
||||
import org.scalacoin.protocol.transaction.TransactionOutput
|
||||
import org.scalatest.{MustMatchers, FlatSpec}
|
||||
|
||||
/**
|
||||
* Created by chris on 1/11/16.
|
||||
* https://bitcoin.org/en/developer-reference#txout
|
||||
*/
|
||||
class RawTransactionOutputMarshallerTest extends FlatSpec with MustMatchers {
|
||||
|
||||
//txid cad1082e674a7bd3bc9ab1bc7804ba8a57523607c876b8eb2cbe645f2b1803d6
|
||||
val rawTxOutput = "02204e00000000000017a914eda8ae08b5c9f973f49543e90a7c292367b3337c87197d2d000000000017a914be2319b9060429692ebeffaa3be38497dc5380c887"
|
||||
"RawTransactionOutputMarshaller" must "read a serialized tx output" in {
|
||||
|
||||
val _ : Seq[TransactionOutput] = ???
|
||||
}
|
||||
}
|
@ -45,4 +45,12 @@ object TestUtil {
|
||||
val p2shOutputScriptNotParsedAsm = "OP_HASH160 eda8ae08b5c9f973f49543e90a7c292367b3337c OP_EQUAL"
|
||||
val p2shOutputScriptAsm = List(OP_HASH160, ScriptConstantImpl("eda8ae08b5c9f973f49543e90a7c292367b3337c"), OP_EQUAL)
|
||||
|
||||
val rawTranasction = "010000000185d6b0da2edf96b282030d3f4f79d14cc8c882cfef1b3064170c850660317de1000000006" +
|
||||
"f0047304402207df6dd8dad22d49c3c83d8031733c32a53719278eb7985d3b35b375d776f84f102207054f9209a1e87d55feaf" +
|
||||
"c90aa04c33008e5bae9191da22" +
|
||||
"aeaa16efde96f41f00125512102b022902a0fdd71e831c37e4136c2754a59887be0618fb75336d7ab67e2982ff551aeffffffff" +
|
||||
"02204e00000000000017a914eda8ae08b5c9f973f49543e90a7c292367b3337c87197d2d000000000017a914be2319b90604296" +
|
||||
"92ebeffaa3be38497dc5380c88700000000"
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user