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@42448218[Running, parallelism = 2, size = 1, active = 0, running = 0, steals = 7, tasks = 0, submissions = 0]
+// ec: ExecutionContext = scala.concurrent.impl.ExecutionContextImpl$$anon$3@18668514[Running, parallelism = 2, size = 2, active = 0, running = 0, steals = 4, 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(0381a8de12f09a76e7d8c59640be15bcb51db3ccc2b092aa7707bcabb050863882)
+// pubKey: ECPublicKey = ECPublicKey(033e2fc29879e842d51ba974ac74b52e8350d5272095ea3e882e6d952ccc267d20)
// 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(ee0907430c3ae0435e5f213de2f4a78457275b0f)
+// creditingSpk: P2PKHScriptPubKey = pkh(55b0c6fc1a66c26cc6cef25f8d73e2cc4fd3f068)
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(ee0907430c3ae0435e5f213de2f4a78457275b0f))
+// utxo: TransactionOutput = TransactionOutput(10000 sats,pkh(55b0c6fc1a66c26cc6cef25f8d73e2cc4fd3f068))
// the private key that locks the funds for the script we are spending too
val destinationPrivKey = ECPrivateKey.freshPrivateKey
@@ -96,7 +96,7 @@ transactions. Here's an example of how to use it
// the script that corresponds to destination private key, this is what is protecting the money
val destinationSPK =
P2PKHScriptPubKey(pubKey = destinationPrivKey.publicKey)
-// destinationSPK: P2PKHScriptPubKey = pkh(7ee0bfbfd9be2744e9f6f405ef27e85555bbc181)
+// destinationSPK: P2PKHScriptPubKey = pkh(983719e0f050f8f2162abe1e8e57f2cb61e16185)
// this is where we are sending money too
// we could add more destinations here if we
@@ -107,7 +107,7 @@ transactions. Here's an example of how to use it
Vector(destination1)
}
-// destinations: Vector[TransactionOutput] = Vector(TransactionOutput(5000 sats,pkh(7ee0bfbfd9be2744e9f6f405ef27e85555bbc181)))
+// destinations: Vector[TransactionOutput] = Vector(TransactionOutput(5000 sats,pkh(983719e0f050f8f2162abe1e8e57f2cb61e16185)))
// we have to fabricate a transaction that contains the
// UTXO we are trying to spend. If this were a real blockchain
@@ -116,12 +116,12 @@ transactions. Here's an example of how to use it
inputs = Vector.empty,
outputs = Vector(utxo),
lockTime = UInt32.zero)
-// creditingTx: BaseTransaction = BaseTransactionImpl(Int32Impl(1),Vector(),Vector(TransactionOutput(10000 sats,pkh(ee0907430c3ae0435e5f213de2f4a78457275b0f))),UInt32Impl(0))
+// creditingTx: BaseTransaction = BaseTransactionImpl(Int32Impl(1),Vector(),Vector(TransactionOutput(10000 sats,pkh(55b0c6fc1a66c26cc6cef25f8d73e2cc4fd3f068))),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(a632d313b91b723578e9be749a2513a4ce92069e3f599761ef17d176ff2ceea8:0)
+// outPoint: TransactionOutPoint = TransactionOutPoint(d3cff73f79947dab66250608a4ae123661829dd5c55f384658b270cff867b888:0)
// this contains all the information we need to
// validly sign the UTXO above
@@ -134,12 +134,12 @@ transactions. Here's an example of how to use it
HashType.sigHashAll,
conditionalPath =
ConditionalPath.NoConditionsLeft)
-// utxoSpendingInfo: BitcoinUTXOSpendingInfoFull = P2PKHSpendingInfo(TransactionOutPoint(a632d313b91b723578e9be749a2513a4ce92069e3f599761ef17d176ff2ceea8:0),10000 sats,pkh(ee0907430c3ae0435e5f213de2f4a78457275b0f),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1)))
+// utxoSpendingInfo: BitcoinUTXOSpendingInfoFull = P2PKHSpendingInfo(TransactionOutPoint(d3cff73f79947dab66250608a4ae123661829dd5c55f384658b270cff867b888:0),10000 sats,pkh(55b0c6fc1a66c26cc6cef25f8d73e2cc4fd3f068),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(a632d313b91b723578e9be749a2513a4ce92069e3f599761ef17d176ff2ceea8:0),10000 sats,pkh(ee0907430c3ae0435e5f213de2f4a78457275b0f),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1))))
+// utxos: Vector[BitcoinUTXOSpendingInfoFull] = Vector(P2PKHSpendingInfo(TransactionOutPoint(d3cff73f79947dab66250608a4ae123661829dd5c55f384658b270cff867b888:0),10000 sats,pkh(55b0c6fc1a66c26cc6cef25f8d73e2cc4fd3f068),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
@@ -149,7 +149,7 @@ transactions. Here's an example of how to use it
val changePrivKey = ECPrivateKey.freshPrivateKey
// changePrivKey: ECPrivateKey = Masked(ECPrivateKeyImpl)
val changeSPK = P2PKHScriptPubKey(pubKey = changePrivKey.publicKey)
-// changeSPK: P2PKHScriptPubKey = pkh(2612422760bfb2420a6827d882f48cd914c5ae18)
+// changeSPK: P2PKHScriptPubKey = pkh(474d19d36bbb49e0eddec5a3c1d80fa192f435c3)
// the network we are on, for this example we are using
// the regression test network. This is a network you control
@@ -168,7 +168,7 @@ transactions. Here's an example of how to use it
network = networkParams)
Await.result(builderF, 30.seconds)
}
-// txBuilder: BitcoinTxBuilder = BitcoinTxBuilderImpl(Vector(TransactionOutput(5000 sats,pkh(7ee0bfbfd9be2744e9f6f405ef27e85555bbc181))),Map(TransactionOutPoint(a632d313b91b723578e9be749a2513a4ce92069e3f599761ef17d176ff2ceea8:0) -> P2PKHSpendingInfo(TransactionOutPoint(a632d313b91b723578e9be749a2513a4ce92069e3f599761ef17d176ff2ceea8:0),10000 sats,pkh(ee0907430c3ae0435e5f213de2f4a78457275b0f),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1)))),SatoshisPerByte(1 sat),pkh(2612422760bfb2420a6827d882f48cd914c5ae18),RegTest)
+// txBuilder: BitcoinTxBuilder = BitcoinTxBuilderImpl(Vector(TransactionOutput(5000 sats,pkh(983719e0f050f8f2162abe1e8e57f2cb61e16185))),Map(TransactionOutPoint(d3cff73f79947dab66250608a4ae123661829dd5c55f384658b270cff867b888:0) -> P2PKHSpendingInfo(TransactionOutPoint(d3cff73f79947dab66250608a4ae123661829dd5c55f384658b270cff867b888:0),10000 sats,pkh(55b0c6fc1a66c26cc6cef25f8d73e2cc4fd3f068),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1)))),SatoshisPerByte(1 sat),pkh(474d19d36bbb49e0eddec5a3c1d80fa192f435c3),RegTest)
// Let's finally produce a validly signed tx!
// The 'sign' method is going produce a validly signed transaction
@@ -182,7 +182,7 @@ transactions. Here's an example of how to use it
val signF = txBuilder.sign
Await.result(signF, 30.seconds)
}
-// signedTx: Transaction = BaseTransactionImpl(Int32Impl(2),List(TransactionInputImpl(TransactionOutPoint(a632d313b91b723578e9be749a2513a4ce92069e3f599761ef17d176ff2ceea8:0),P2PKHScriptSignature(ECPublicKey(0381a8de12f09a76e7d8c59640be15bcb51db3ccc2b092aa7707bcabb050863882), ECDigitalSignature(3044022011c589321131c082f2f0bdc51e4080c6ff6423e0d1944bcf950bbdbd0d498b40022046067534ce9b44a1492f2f281107bf5b7248ebc2ad9dac89ff4da6533a33062e01)),UInt32Impl(0))),Vector(TransactionOutput(5000 sats,pkh(7ee0bfbfd9be2744e9f6f405ef27e85555bbc181)), TransactionOutput(4774 sats,pkh(2612422760bfb2420a6827d882f48cd914c5ae18))),UInt32Impl(0))
+// signedTx: Transaction = BaseTransactionImpl(Int32Impl(2),List(TransactionInputImpl(TransactionOutPoint(d3cff73f79947dab66250608a4ae123661829dd5c55f384658b270cff867b888:0),P2PKHScriptSignature(ECPublicKey(033e2fc29879e842d51ba974ac74b52e8350d5272095ea3e882e6d952ccc267d20), ECDigitalSignature(3045022100fd2542595ab78eac480b2c140a13c3a79d978f6aeddb4e9310074955a7f934c202207e6086a505190d5826fb9928ac326ed7eb2a257a2fd7926f28e52c8089da796501)),UInt32Impl(0))),Vector(TransactionOutput(5000 sats,pkh(983719e0f050f8f2162abe1e8e57f2cb61e16185)), TransactionOutput(4774 sats,pkh(474d19d36bbb49e0eddec5a3c1d80fa192f435c3))),UInt32Impl(0))
signedTx.inputs.length
// res0: Int = 1
@@ -192,7 +192,7 @@ signedTx.outputs.length
//remember, you can call .hex on any bitcoin-s data structure to get the hex representation!
signedTx.hex
-// res2: String = 0200000001a8ee2cff76d117ef6197593f9e0692cea413259a74bee97835721bb913d332a6000000006a473044022011c589321131c082f2f0bdc51e4080c6ff6423e0d1944bcf950bbdbd0d498b40022046067534ce9b44a1492f2f281107bf5b7248ebc2ad9dac89ff4da6533a33062e01210381a8de12f09a76e7d8c59640be15bcb51db3ccc2b092aa7707bcabb050863882000000000288130000000000001976a9147ee0bfbfd9be2744e9f6f405ef27e85555bbc18188aca6120000000000001976a9142612422760bfb2420a6827d882f48cd914c5ae1888ac00000000
+// res2: String = 020000000188b867f8cf70b25846385fc5d59d82613612aea408062566ab7d94793ff7cfd3000000006b483045022100fd2542595ab78eac480b2c140a13c3a79d978f6aeddb4e9310074955a7f934c202207e6086a505190d5826fb9928ac326ed7eb2a257a2fd7926f28e52c8089da79650121033e2fc29879e842d51ba974ac74b52e8350d5272095ea3e882e6d952ccc267d20000000000288130000000000001976a914983719e0f050f8f2162abe1e8e57f2cb61e1618588aca6120000000000001976a914474d19d36bbb49e0eddec5a3c1d80fa192f435c388ac00000000