From 0bb5bd8704f709fa98e33aa5573620e9ad82f7ae Mon Sep 17 00:00:00 2001 From: Chris Stewart Date: Wed, 1 Jun 2016 20:32:09 -0500 Subject: [PATCH] Moving apply(bytes) & apply(hex) into the Factory trait --- build.sbt | 2 +- .../bitcoins/core/crypto/ECDigitalSignature.scala | 6 ------ .../scala/org/bitcoins/core/protocol/Address.scala | 3 +-- .../core/protocol/script/ScriptPubKey.scala | 4 ---- .../core/protocol/script/ScriptSignature.scala | 3 +-- .../core/protocol/transaction/Transaction.scala | 2 -- .../protocol/transaction/TransactionInput.scala | 1 - .../protocol/transaction/TransactionOutPoint.scala | 3 +-- .../protocol/transaction/TransactionOutput.scala | 4 +++- .../bitcoins/core/script/constant/Constants.scala | 7 ------- src/main/scala/org/bitcoins/core/util/Factory.scala | 13 +++++++++++++ 11 files changed, 20 insertions(+), 28 deletions(-) diff --git a/build.sbt b/build.sbt index 7c2bdc3d2a..2de010cce3 100644 --- a/build.sbt +++ b/build.sbt @@ -1 +1 @@ -import AssemblyKeys._ \ No newline at end of file +import AssemblyKeys._ test in assembly := {} \ No newline at end of file diff --git a/src/main/scala/org/bitcoins/core/crypto/ECDigitalSignature.scala b/src/main/scala/org/bitcoins/core/crypto/ECDigitalSignature.scala index 402b4c60aa..6ff0d0d823 100644 --- a/src/main/scala/org/bitcoins/core/crypto/ECDigitalSignature.scala +++ b/src/main/scala/org/bitcoins/core/crypto/ECDigitalSignature.scala @@ -64,12 +64,6 @@ object ECDigitalSignature extends Factory[ECDigitalSignature] { else ECDigitalSignatureImpl(bytes) } - override def fromHex(hex : String) : ECDigitalSignature = fromBytes(BitcoinSUtil.decodeHex(hex)) - - def apply(bytes : Seq[Byte]) : ECDigitalSignature = fromBytes(bytes) - - def apply(hex : String) : ECDigitalSignature = fromHex(hex) - def apply(r : BigInt, s : BigInt) = fromRS(r,s) /** * Takes in the r and s component of a digital signature and gives back a ECDigitalSignature object diff --git a/src/main/scala/org/bitcoins/core/protocol/Address.scala b/src/main/scala/org/bitcoins/core/protocol/Address.scala index 5cbbafe1da..115c1d6622 100644 --- a/src/main/scala/org/bitcoins/core/protocol/Address.scala +++ b/src/main/scala/org/bitcoins/core/protocol/Address.scala @@ -149,6 +149,5 @@ object Address extends Factory[Address] { override def fromHex(hex : String) : Address = throw new RuntimeException("We cannot create a bitcoin address from hex - bitcoin addresses are base 58 encoded") - def apply(bytes : Seq[Byte]) : Address = fromBytes(bytes) - def apply(str : String) : Address = factory(str) + override def apply(str : String) : Address = factory(str) } \ No newline at end of file diff --git a/src/main/scala/org/bitcoins/core/protocol/script/ScriptPubKey.scala b/src/main/scala/org/bitcoins/core/protocol/script/ScriptPubKey.scala index 12ed85224a..0ec09888f3 100644 --- a/src/main/scala/org/bitcoins/core/protocol/script/ScriptPubKey.scala +++ b/src/main/scala/org/bitcoins/core/protocol/script/ScriptPubKey.scala @@ -170,8 +170,4 @@ object ScriptPubKey extends Factory[ScriptPubKey] { def fromBytes(bytes : Seq[Byte]) : ScriptPubKey = RawScriptPubKeyParser.read(bytes) - def apply(bytes: Seq[Byte]) : ScriptPubKey = fromBytes(bytes) - - def apply(hex : String) : ScriptPubKey = fromHex(hex) - } \ No newline at end of file diff --git a/src/main/scala/org/bitcoins/core/protocol/script/ScriptSignature.scala b/src/main/scala/org/bitcoins/core/protocol/script/ScriptSignature.scala index d5ba25bc45..20e29cd43a 100644 --- a/src/main/scala/org/bitcoins/core/protocol/script/ScriptSignature.scala +++ b/src/main/scala/org/bitcoins/core/protocol/script/ScriptSignature.scala @@ -347,9 +347,8 @@ object ScriptSignature extends Factory[ScriptSignature] with BitcoinSLogger { } } - def apply(bytes: Seq[Byte]) : ScriptSignature = fromBytes(bytes) - def apply(hex : String) : ScriptSignature = fromHex(hex) def apply(signature : ECDigitalSignature, pubKey : ECPublicKey) : ScriptSignature = factory(signature,pubKey) + def apply(tokens : Seq[ScriptToken], scriptPubKey : ScriptPubKey) : ScriptSignature = fromScriptPubKey(tokens, scriptPubKey) } diff --git a/src/main/scala/org/bitcoins/core/protocol/transaction/Transaction.scala b/src/main/scala/org/bitcoins/core/protocol/transaction/Transaction.scala index 9c10d34207..591241c3ef 100644 --- a/src/main/scala/org/bitcoins/core/protocol/transaction/Transaction.scala +++ b/src/main/scala/org/bitcoins/core/protocol/transaction/Transaction.scala @@ -126,8 +126,6 @@ object Transaction extends Factory[Transaction] { def fromBytes(bytes : Seq[Byte]) : Transaction = RawTransactionParser.read(bytes) - def apply(bytes : Seq[Byte]) : Transaction = fromBytes(bytes) - def apply(hex: String) : Transaction = fromHex(hex) def apply(bytes : Array[Byte]) : Transaction = factory(bytes) def apply(oldTx : Transaction, lockTime : Long) : Transaction = factory(oldTx,lockTime) def apply(oldTx : Transaction, updatedInputs : UpdateTransactionInputs) : Transaction = factory(oldTx, updatedInputs) diff --git a/src/main/scala/org/bitcoins/core/protocol/transaction/TransactionInput.scala b/src/main/scala/org/bitcoins/core/protocol/transaction/TransactionInput.scala index 3f7f71e1be..3884da4ca2 100644 --- a/src/main/scala/org/bitcoins/core/protocol/transaction/TransactionInput.scala +++ b/src/main/scala/org/bitcoins/core/protocol/transaction/TransactionInput.scala @@ -112,5 +112,4 @@ object TransactionInput extends Factory[TransactionInput] { def apply(outPoint : TransactionOutPoint, scriptSignature : ScriptSignature, sequenceNumber : Long) : TransactionInput = factory(outPoint,scriptSignature,sequenceNumber) - def apply(bytes : Seq[Byte]) : TransactionInput = fromBytes(bytes) } \ No newline at end of file diff --git a/src/main/scala/org/bitcoins/core/protocol/transaction/TransactionOutPoint.scala b/src/main/scala/org/bitcoins/core/protocol/transaction/TransactionOutPoint.scala index 6c49df648c..7041feac94 100644 --- a/src/main/scala/org/bitcoins/core/protocol/transaction/TransactionOutPoint.scala +++ b/src/main/scala/org/bitcoins/core/protocol/transaction/TransactionOutPoint.scala @@ -58,9 +58,8 @@ object TransactionOutPoint extends Factory[TransactionOutPoint] { def fromBytes(bytes : Seq[Byte]) : TransactionOutPoint = RawTransactionOutPointParser.read(bytes) - def apply(bytes : Seq[Byte]) : TransactionOutPoint = fromBytes(bytes) - def apply(hex : String) : TransactionOutPoint = fromHex(hex) def apply(output : TransactionOutput,parentTransaction : Transaction) : TransactionOutPoint = factory(output,parentTransaction) + def apply(txId : DoubleSha256Digest, index: Int) : TransactionOutPoint = factory(txId,index) } diff --git a/src/main/scala/org/bitcoins/core/protocol/transaction/TransactionOutput.scala b/src/main/scala/org/bitcoins/core/protocol/transaction/TransactionOutput.scala index ccdebaeaba..d8fd1c4c7f 100644 --- a/src/main/scala/org/bitcoins/core/protocol/transaction/TransactionOutput.scala +++ b/src/main/scala/org/bitcoins/core/protocol/transaction/TransactionOutput.scala @@ -43,7 +43,9 @@ object TransactionOutput extends Factory[TransactionOutput] { def fromBytes(bytes : Seq[Byte]) : TransactionOutput = RawTransactionOutputParser.read(bytes).head def apply(oldOutput : TransactionOutput, newCurrencyUnit: CurrencyUnit) : TransactionOutput = factory(oldOutput,newCurrencyUnit) + def apply(oldOutput : TransactionOutput, newScriptPubKey : ScriptPubKey) : TransactionOutput = factory(oldOutput, newScriptPubKey) + def apply(currencyUnit: CurrencyUnit, scriptPubKey: ScriptPubKey) : TransactionOutput = factory(currencyUnit, scriptPubKey) - def apply(bytes : Seq[Byte]) : TransactionOutput = fromBytes(bytes) + } \ No newline at end of file diff --git a/src/main/scala/org/bitcoins/core/script/constant/Constants.scala b/src/main/scala/org/bitcoins/core/script/constant/Constants.scala index 5e2060f959..0069ab1a6d 100644 --- a/src/main/scala/org/bitcoins/core/script/constant/Constants.scala +++ b/src/main/scala/org/bitcoins/core/script/constant/Constants.scala @@ -133,10 +133,6 @@ object ScriptNumber extends Factory[ScriptNumber] { if (num == 0) zero else apply(BitcoinSUtil.longToHex(num)) } - def apply(hex : String) : ScriptNumber = fromHex(hex) - - def apply(bytes : Seq[Byte]) : ScriptNumber = fromBytes(bytes) - def apply(hex : String, requireMinimal : Boolean) : Try[ScriptNumber] = { if (requireMinimal && !BitcoinScriptUtil.isShortestEncoding(hex)) { Failure(new IllegalArgumentException("The given hex was not the shortest encoding for the script number: " + hex)) @@ -404,7 +400,4 @@ object ScriptConstant extends Factory[ScriptConstant] { */ def fromBytes(bytes : Seq[Byte]) : ScriptConstant = ScriptConstantImpl(BitcoinSUtil.encodeHex(bytes)) - def apply(hex : String) : ScriptConstant = fromHex(hex) - - def apply(bytes : Seq[Byte]) : ScriptConstant = fromBytes(bytes) } diff --git a/src/main/scala/org/bitcoins/core/util/Factory.scala b/src/main/scala/org/bitcoins/core/util/Factory.scala index 52350cd3c7..3a907bbfe0 100644 --- a/src/main/scala/org/bitcoins/core/util/Factory.scala +++ b/src/main/scala/org/bitcoins/core/util/Factory.scala @@ -20,4 +20,17 @@ trait Factory[T] { */ def fromBytes(bytes : Seq[Byte]) : T + /** + * Creates a T out of a sequence of bytes + * @param bytes + * @return + */ + def apply(bytes : Seq[Byte]) : T = fromBytes(bytes) + + /** + * Creates a T from a hex string + * @param hex + * @return + */ + def apply(hex : String) : T = fromHex(hex) }