TxBuilder Example
Bitcoin-S features a transaction builder that constructs and signs Bitcoin transactions. Here's an example of how to use it
implicit val ec: ExecutionContext = ExecutionContext.Implicits.global
// ec: ExecutionContext = scala.concurrent.impl.ExecutionContextImpl$$anon$3@59febca9[Running, parallelism = 2, size = 2, active = 0, running = 0, steals = 6, tasks = 0, submissions = 0]
// generate a fresh private key that we are going to use in the scriptpubkey
val privKey = ECPrivateKey.freshPrivateKey
// privKey: ECPrivateKey = Masked(ECPrivateKeyImpl)
val pubKey = privKey.publicKey
// pubKey: ECPublicKey = ECPublicKey(02ae88e98a34a1ee74cdb57706cf73c6980028dd0a649c165e7d60d2f22fba5238)
// this is the script that the TxBuilder is going to create a
// script signature that validly spends this scriptPubKey
val creditingSpk = P2PKHScriptPubKey(pubKey = privKey.publicKey)
// creditingSpk: P2PKHScriptPubKey = pkh(8afef656ddc1e797ae67f4138285e7e0f7d5cfca)
val amount = 10000.satoshis
// amount: Satoshis = 10000 sats
// this is the UTXO we are going to be spending
val utxo =
TransactionOutput(value = amount, scriptPubKey = creditingSpk)
// utxo: TransactionOutput = TransactionOutput(10000 sats,pkh(8afef656ddc1e797ae67f4138285e7e0f7d5cfca))
// the private key that locks the funds for the script we are spending too
val destinationPrivKey = ECPrivateKey.freshPrivateKey
// destinationPrivKey: ECPrivateKey = Masked(ECPrivateKeyImpl)
// the amount we are sending -- 5000 satoshis -- to the destinationSPK
val destinationAmount = 5000.satoshis
// destinationAmount: Satoshis = 5000 sats
// the script that corresponds to destination private key, this is what is protecting the money
val destinationSPK =
P2PKHScriptPubKey(pubKey = destinationPrivKey.publicKey)
// destinationSPK: P2PKHScriptPubKey = pkh(e49d9f7b4eadb0939580c963a8d3840528a04569)
// this is where we are sending money too
// we could add more destinations here if we
// wanted to batch transactions
val destinations = {
val destination1 = TransactionOutput(value = destinationAmount,
scriptPubKey = destinationSPK)
Vector(destination1)
}
// destinations: Vector[TransactionOutput] = Vector(TransactionOutput(5000 sats,pkh(e49d9f7b4eadb0939580c963a8d3840528a04569)))
// we have to fabricate a transaction that contains the
// UTXO we are trying to spend. If this were a real blockchain
// we would need to reference the UTXO set
val creditingTx = BaseTransaction(version = Int32.one,
inputs = Vector.empty,
outputs = Vector(utxo),
lockTime = UInt32.zero)
// creditingTx: BaseTransaction = BaseTransactionImpl(Int32Impl(1),Vector(),Vector(TransactionOutput(10000 sats,pkh(8afef656ddc1e797ae67f4138285e7e0f7d5cfca))),UInt32Impl(0))
// this is the information we need from the crediting TX
// to properly "link" it in the transaction we are creating
val outPoint = TransactionOutPoint(creditingTx.txId, UInt32.zero)
// outPoint: TransactionOutPoint = TransactionOutPoint(4e7a1d92edacbd385d72c1f3d1501bae83812bda5b1dc044c7a485348db4a335:0)
// this contains all the information we need to
// validly sign the UTXO above
val utxoSpendingInfo = BitcoinUTXOSpendingInfoFull(outPoint = outPoint,
output = utxo,
signers = Vector(privKey),
redeemScriptOpt = None,
scriptWitnessOpt = None,
hashType =
HashType.sigHashAll,
conditionalPath =
ConditionalPath.NoConditionsLeft)
// utxoSpendingInfo: BitcoinUTXOSpendingInfoFull = P2PKHSpendingInfo(TransactionOutPoint(4e7a1d92edacbd385d72c1f3d1501bae83812bda5b1dc044c7a485348db4a335:0),10000 sats,pkh(8afef656ddc1e797ae67f4138285e7e0f7d5cfca),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1)))
// all of the UTXO spending information, since we are only
//spending one UTXO, this is just one element
val utxos: Vector[BitcoinUTXOSpendingInfoFull] = Vector(utxoSpendingInfo)
// utxos: Vector[BitcoinUTXOSpendingInfoFull] = Vector(P2PKHSpendingInfo(TransactionOutPoint(4e7a1d92edacbd385d72c1f3d1501bae83812bda5b1dc044c7a485348db4a335:0),10000 sats,pkh(8afef656ddc1e797ae67f4138285e7e0f7d5cfca),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1))))
// this is how much we are going to pay as a fee to the network
// for this example, we are going to pay 1 satoshi per byte
val feeRate = SatoshisPerByte(1.satoshi)
// feeRate: SatoshisPerByte = SatoshisPerByte(1 sat)
val changePrivKey = ECPrivateKey.freshPrivateKey
// changePrivKey: ECPrivateKey = Masked(ECPrivateKeyImpl)
val changeSPK = P2PKHScriptPubKey(pubKey = changePrivKey.publicKey)
// changeSPK: P2PKHScriptPubKey = pkh(c1ef50f8932bbef135f3bc1aa20e9e63a92d0634)
// the network we are on, for this example we are using
// the regression test network. This is a network you control
// on your own machine
val networkParams = RegTest
// networkParams: RegTest.type = RegTest
// Yay! Now we have a TxBuilder object that we can use
// to sign the TX.
val txBuilder: BitcoinTxBuilder = {
val builderF = BitcoinTxBuilder(
destinations = destinations,
utxos = utxos,
feeRate = feeRate,
changeSPK = changeSPK,
network = networkParams)
Await.result(builderF, 30.seconds)
}
// txBuilder: BitcoinTxBuilder = BitcoinTxBuilderImpl(Vector(TransactionOutput(5000 sats,pkh(e49d9f7b4eadb0939580c963a8d3840528a04569))),Map(TransactionOutPoint(4e7a1d92edacbd385d72c1f3d1501bae83812bda5b1dc044c7a485348db4a335:0) -> P2PKHSpendingInfo(TransactionOutPoint(4e7a1d92edacbd385d72c1f3d1501bae83812bda5b1dc044c7a485348db4a335:0),10000 sats,pkh(8afef656ddc1e797ae67f4138285e7e0f7d5cfca),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1)))),SatoshisPerByte(1 sat),pkh(c1ef50f8932bbef135f3bc1aa20e9e63a92d0634),RegTest)
// Let's finally produce a validly signed tx!
// The 'sign' method is going produce a validly signed transaction
// This is going to iterate through each of the UTXOs and use
// the corresponding UTXOSpendingInfo to produce a validly
// signed input. This UTXO has:
// 1: one input
// 2: outputs (destination and change outputs)
// 3: a fee rate of 1 satoshi/byte
val signedTx: Transaction = {
val signF = txBuilder.sign
Await.result(signF, 30.seconds)
}
// signedTx: Transaction = BaseTransactionImpl(Int32Impl(2),List(TransactionInputImpl(TransactionOutPoint(4e7a1d92edacbd385d72c1f3d1501bae83812bda5b1dc044c7a485348db4a335:0),P2PKHScriptSignature(ECPublicKey(02ae88e98a34a1ee74cdb57706cf73c6980028dd0a649c165e7d60d2f22fba5238), ECDigitalSignature(30440220652b7ac145646d9eeb43e9ea98ad74dfcfaab03337efb4d1b340d7d56e8491430220571f1a3d334dabdcb4c67d61b73c3fe1c037ad0a9174ae020d14084b380fc25601)),UInt32Impl(0))),Vector(TransactionOutput(5000 sats,pkh(e49d9f7b4eadb0939580c963a8d3840528a04569)), TransactionOutput(4774 sats,pkh(c1ef50f8932bbef135f3bc1aa20e9e63a92d0634))),UInt32Impl(0))
signedTx.inputs.length
// res0: Int = 1
signedTx.outputs.length
// res1: Int = 2
//remember, you can call .hex on any bitcoin-s data structure to get the hex representation!
signedTx.hex
// res2: String = 020000000135a3b48d3485a4c744c01d5bda2b8183ae1b50d1f3c1725d38bdaced921d7a4e000000006a4730440220652b7ac145646d9eeb43e9ea98ad74dfcfaab03337efb4d1b340d7d56e8491430220571f1a3d334dabdcb4c67d61b73c3fe1c037ad0a9174ae020d14084b380fc256012102ae88e98a34a1ee74cdb57706cf73c6980028dd0a649c165e7d60d2f22fba5238000000000288130000000000001976a914e49d9f7b4eadb0939580c963a8d3840528a0456988aca6120000000000001976a914c1ef50f8932bbef135f3bc1aa20e9e63a92d063488ac00000000