Skip to content

Instantly share code, notes, and snippets.

@rorp
Created December 2, 2019 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rorp/befcfccce79355f51c0f8ba3ca842926 to your computer and use it in GitHub Desktop.
Save rorp/befcfccce79355f51c0f8ba3ca842926 to your computer and use it in GitHub Desktop.
import java.net.InetSocketAddress
import java.nio.file.Files
import java.time.LocalDateTime
import akka.actor.ActorSystem
import org.bitcoins.node.models.Peer
import org.bitcoins.node.{NeutrinoNode, SpvNodeCallbacks}
import org.bitcoins.server.BitcoinSAppConfig
import org.bitcoins.server.Main.error
import org.bitcoins.wallet.api._
import org.bitcoins.wallet.{LockedWallet, Wallet, WalletStorage}
import scala.concurrent.Future
object neutrino_rescan extends App {
val NumberOfAddressesToMatch = 10
val system = ActorSystem()
implicit val executionContext = system.dispatcher
val conf = BitcoinSAppConfig.fromDefaultDatadir()
implicit val walletConf = conf.walletConf
val peer = Peer.fromSocket(
new InetSocketAddress(conf.nodeConf.peers.head, conf.nodeConf.network.port))
def initWallet(): Future[UnlockedWalletApi] = {
def hasWallet: Boolean = {
val walletDB = walletConf.dbPath resolve walletConf.dbName
Files.exists(walletDB) && WalletStorage.seedExists()
}
if (hasWallet) {
val locked = LockedWallet()
locked.unlock(Wallet.badPassphrase) match {
case UnlockWalletSuccess(wallet) => Future.successful(wallet)
case err: UnlockWalletError => error(err)
}
} else {
Wallet.initialize().map {
case InitializeWalletSuccess(wallet) => wallet
case err: InitializeWalletError => error(err)
}
}
}
val node =
NeutrinoNode(peer,
SpvNodeCallbacks.empty,
conf.nodeConf,
conf.chainConf,
system)
for {
wallet <- initWallet()
// pre-generate 100 addresses. this line needs to be executed only once
// _ <- Future.sequence(1.to(100).map(_ => wallet.getNewAddress()))
addressesToMatch <- wallet.listAddresses()
startedNode <- node.start()
_ = println(s"Rescan started: ${LocalDateTime.now()}")
_ <- startedNode.rescan(
addressesToMatch.take(NumberOfAddressesToMatch).map(_.scriptPubKey))
} yield {
println(s"Rescan done: ${LocalDateTime.now()}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment