Skip to content

Commit

Permalink
initial check-in
Browse files Browse the repository at this point in the history
  • Loading branch information
spew committed May 26, 2014
1 parent 958e6fd commit f6c8142
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
target
*.wallet
*.spvchain
13 changes: 13 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name := "bitcoin-scala"

version := "1.0"

scalaVersion := "2.10.4"

libraryDependencies ++= Seq(
"com.typesafe.scala-logging" %% "scala-logging-slf4j" % "2.1.2",
"ch.qos.logback" % "logback-classic" % "1.1.2",
"com.github.scopt" %% "scopt" % "3.2.0",
"com.google" % "bitcoinj" % "0.11.2"
)

1 change: 1 addition & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
logLevel := Level.Warn
35 changes: 35 additions & 0 deletions src/main/scala/BitcoinApplication.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import com.google.bitcoin.core.ECKey
import com.google.bitcoin.kits.WalletAppKit
import com.google.bitcoin.params.{MainNetParams, RegTestParams, TestNet3Params}
import java.io.File

class BitcoinApplication(configuration: ProgramConfiguration) {
def run() {
val kit = createWalletAppKit()
kit.startAndWait()
kit.stopAndWait()
}

private def createWalletAppKit() = {
val networkParams = this.networkParams
val kit = new WalletAppKit(networkParams._1, new File("."), networkParams._2) {
override def onSetupCompleted() = {
if (wallet().getKeychainSize() < 1)
wallet().addKey(new ECKey());
}
}
if (configuration.networkName == "regtest") {
kit.connectToLocalHost()
}
kit
}

private def networkParams = {
configuration.networkName match {
case "testnet" => (TestNet3Params.get, "forwarding-service-testnet")
case "regtest" => (RegTestParams.get, "forwarding-service-regtest")
case "prod" => (MainNetParams.get, "forwarding-service")
case _ => throw new Exception(s"Unknown network name: ${configuration.networkName}")
}
}
}
33 changes: 33 additions & 0 deletions src/main/scala/Program.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import com.typesafe.scalalogging.slf4j.LazyLogging
import scopt.OptionParser

object Program extends LazyLogging {
def main(args: Array[String]) {
try {
val parser = this.createParser();
parser.parse(args, createDefaultConfiguration()) map { configuration =>
val application = new BitcoinApplication(configuration)
application.run()
} getOrElse {
logger.warn("Unable to properly parse arguments, exiting...")
}
} catch {
case t: Throwable =>
logger.error("Uncaught exception in main thread", t)
throw t
}
}

private def createDefaultConfiguration(): ProgramConfiguration = {
ProgramConfiguration()
}

private def createParser(): OptionParser[ProgramConfiguration] = {
val parser = new OptionParser[ProgramConfiguration]("pokerbot") {
head("bitcoin-scala", "1.0")
opt[String]('n', "network-name") optional() action { (n, c) => c.copy(networkName = n) } text("the bitcoin network to connect to: { testnet, regtest, prod }")
help("help") text("prints usage")
}
parser
}
}
4 changes: 4 additions & 0 deletions src/main/scala/ProgramConfiguration.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

case class ProgramConfiguration (
networkName: String = "testnet"
)

0 comments on commit f6c8142

Please sign in to comment.