TxBuilder Example
Bitcoin-S features a transaction building API that allows you to construct and sign 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@4b3a6ea5[Running, parallelism = 2, size = 2, active = 0, running = 0, steals = 33, tasks = 0, submissions = 0]
+// ec: ExecutionContext = scala.concurrent.impl.ExecutionContextImpl$$anon$3@6a16e73b[Running, parallelism = 2, size = 2, active = 0, running = 0, steals = 37, tasks = 0, submissions = 0]
// Initialize a transaction builder
val builder = RawTxBuilder()
@@ -74,19 +74,19 @@
val privKey = ECPrivateKey.freshPrivateKey
// privKey: ECPrivateKey = Masked(ECPrivateKeyImpl)
val pubKey = privKey.publicKey
-// pubKey: ECPublicKey = ECPublicKey(0373a3b855988ce74d6bdeeb213aae05bfe3943f25a1277546062ca3b16771864f)
+// pubKey: ECPublicKey = ECPublicKey(035202285fdab29d57c6929b3bc05f40f67e840ddb0b947ab23175bc1a0b584ec4)
// 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(158c8712c7607842dbe63223fb4e8a7323162e47)
+// creditingSpk: P2PKHScriptPubKey = pkh(783f42380c222643924d0ea83b1de85bc32e12c7)
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(158c8712c7607842dbe63223fb4e8a7323162e47))
+// utxo: TransactionOutput = TransactionOutput(10000 sats,pkh(783f42380c222643924d0ea83b1de85bc32e12c7))
// the private key that locks the funds for the script we are spending too
val destinationPrivKey = ECPrivateKey.freshPrivateKey
@@ -99,7 +99,7 @@
// the script that corresponds to destination private key, this is what is receiving the money
val destinationSPK =
P2PKHScriptPubKey(pubKey = destinationPrivKey.publicKey)
-// destinationSPK: P2PKHScriptPubKey = pkh(466b97c9607fa3f44cec1aabe265d6df6853ac36)
+// destinationSPK: P2PKHScriptPubKey = pkh(36f24e0c097994f0a2257e4258976439b7e5e371)
// this is where we are sending money too
// we could add more destinations here if we
@@ -110,7 +110,7 @@
Vector(destination0)
}
-// destinations: Vector[TransactionOutput] = Vector(TransactionOutput(5000 sats,pkh(466b97c9607fa3f44cec1aabe265d6df6853ac36)))
+// destinations: Vector[TransactionOutput] = Vector(TransactionOutput(5000 sats,pkh(36f24e0c097994f0a2257e4258976439b7e5e371)))
// Add the destinations to the tx builder
builder ++= destinations
@@ -123,17 +123,17 @@ builder ++= destinations
inputs = Vector.empty,
outputs = Vector(utxo),
lockTime = UInt32.zero)
-// creditingTx: BaseTransaction = BaseTransaction(Int32Impl(1),Vector(),Vector(TransactionOutput(10000 sats,pkh(158c8712c7607842dbe63223fb4e8a7323162e47))),UInt32Impl(0))
+// creditingTx: BaseTransaction = BaseTransaction(Int32Impl(1),Vector(),Vector(TransactionOutput(10000 sats,pkh(783f42380c222643924d0ea83b1de85bc32e12c7))),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(fb6aad808e64d036f08710744968f4c1c99a19738933013965b48cf72932bac1:0)
+// outPoint: TransactionOutPoint = TransactionOutPoint(0070c7ed683231eddf0f3cd892528c48d747ac05bbd97b51781c6db3f306db5a:0)
val input = TransactionInput(
outPoint,
EmptyScriptSignature,
sequenceNumber = UInt32.zero)
-// input: TransactionInput = TransactionInputImpl(TransactionOutPoint(fb6aad808e64d036f08710744968f4c1c99a19738933013965b48cf72932bac1:0),EmptyScriptSignature,UInt32Impl(0))
+// input: TransactionInput = TransactionInputImpl(TransactionOutPoint(0070c7ed683231eddf0f3cd892528c48d747ac05bbd97b51781c6db3f306db5a:0),EmptyScriptSignature,UInt32Impl(0))
// Add a new input to our builder
builder += input
@@ -141,11 +141,11 @@ builder += input
// We can now generate a RawTxBuilderResult ready to be finalized
val builderResult = builder.result()
-// builderResult: RawTxBuilderResult = RawTxBuilderResult(Int32Impl(2),Vector(TransactionInputImpl(TransactionOutPoint(fb6aad808e64d036f08710744968f4c1c99a19738933013965b48cf72932bac1:0),EmptyScriptSignature,UInt32Impl(0))),Vector(TransactionOutput(5000 sats,pkh(466b97c9607fa3f44cec1aabe265d6df6853ac36))),UInt32Impl(0))
+// builderResult: RawTxBuilderResult = RawTxBuilderResult(Int32Impl(2),Vector(TransactionInputImpl(TransactionOutPoint(0070c7ed683231eddf0f3cd892528c48d747ac05bbd97b51781c6db3f306db5a:0),EmptyScriptSignature,UInt32Impl(0))),Vector(TransactionOutput(5000 sats,pkh(36f24e0c097994f0a2257e4258976439b7e5e371))),UInt32Impl(0))
// this contains the information needed to analyze our input during finalization
val inputInfo = P2PKHInputInfo(outPoint, amount, privKey.publicKey)
-// inputInfo: P2PKHInputInfo = P2PKHInputInfo(TransactionOutPoint(fb6aad808e64d036f08710744968f4c1c99a19738933013965b48cf72932bac1:0),10000 sats,ECPublicKey(0373a3b855988ce74d6bdeeb213aae05bfe3943f25a1277546062ca3b16771864f))
+// inputInfo: P2PKHInputInfo = P2PKHInputInfo(TransactionOutPoint(0070c7ed683231eddf0f3cd892528c48d747ac05bbd97b51781c6db3f306db5a:0),10000 sats,ECPublicKey(035202285fdab29d57c6929b3bc05f40f67e840ddb0b947ab23175bc1a0b584ec4))
// 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
@@ -155,18 +155,18 @@ builder += input
val changePrivKey = ECPrivateKey.freshPrivateKey
// changePrivKey: ECPrivateKey = Masked(ECPrivateKeyImpl)
val changeSPK = P2PKHScriptPubKey(pubKey = changePrivKey.publicKey)
-// changeSPK: P2PKHScriptPubKey = pkh(7a9e13a8ec8f3b0e1de4afd84fc72831ec91001f)
+// changeSPK: P2PKHScriptPubKey = pkh(f64e23855bb0787017a6333903777c615d848bd6)
// We chose a finalizer that adds a change output to our tx based on a fee rate
val finalizer = StandardNonInteractiveFinalizer(
Vector(inputInfo),
feeRate,
changeSPK)
-// finalizer: StandardNonInteractiveFinalizer = StandardNonInteractiveFinalizer(Vector(P2PKHInputInfo(TransactionOutPoint(fb6aad808e64d036f08710744968f4c1c99a19738933013965b48cf72932bac1:0),10000 sats,ECPublicKey(0373a3b855988ce74d6bdeeb213aae05bfe3943f25a1277546062ca3b16771864f))),1 sats/byte,pkh(7a9e13a8ec8f3b0e1de4afd84fc72831ec91001f))
+// finalizer: StandardNonInteractiveFinalizer = StandardNonInteractiveFinalizer(Vector(P2PKHInputInfo(TransactionOutPoint(0070c7ed683231eddf0f3cd892528c48d747ac05bbd97b51781c6db3f306db5a:0),10000 sats,ECPublicKey(035202285fdab29d57c6929b3bc05f40f67e840ddb0b947ab23175bc1a0b584ec4))),1 sats/byte,pkh(f64e23855bb0787017a6333903777c615d848bd6))
// We can now finalize the tx builder result from earlier with this finalizer
val unsignedTxF: Future[Transaction] = finalizer.buildTx(builderResult)
-// unsignedTxF: Future[Transaction] = Future(Success(BaseTransaction(Int32Impl(2),Vector(TransactionInputImpl(TransactionOutPoint(fb6aad808e64d036f08710744968f4c1c99a19738933013965b48cf72932bac1:0),EmptyScriptSignature,UInt32Impl(0))),Vector(TransactionOutput(5000 sats,pkh(466b97c9607fa3f44cec1aabe265d6df6853ac36)), TransactionOutput(4775 sats,pkh(7a9e13a8ec8f3b0e1de4afd84fc72831ec91001f))),UInt32Impl(0))))
+// unsignedTxF: Future[Transaction] = Future(Success(BaseTransaction(Int32Impl(2),Vector(TransactionInputImpl(TransactionOutPoint(0070c7ed683231eddf0f3cd892528c48d747ac05bbd97b51781c6db3f306db5a:0),EmptyScriptSignature,UInt32Impl(0))),Vector(TransactionOutput(5000 sats,pkh(36f24e0c097994f0a2257e4258976439b7e5e371)), TransactionOutput(4775 sats,pkh(f64e23855bb0787017a6333903777c615d848bd6))),UInt32Impl(0))))
// We now turn to signing the unsigned transaction
// this contains all the information we need to
@@ -176,12 +176,12 @@ builder += input
signers = Vector(privKey),
hashType =
HashType.sigHashAll)
-// utxoInfo: ScriptSignatureParams[P2PKHInputInfo] = ScriptSignatureParams(P2PKHInputInfo(TransactionOutPoint(fb6aad808e64d036f08710744968f4c1c99a19738933013965b48cf72932bac1:0),10000 sats,ECPublicKey(0373a3b855988ce74d6bdeeb213aae05bfe3943f25a1277546062ca3b16771864f)),BaseTransaction(Int32Impl(1),Vector(),Vector(TransactionOutput(10000 sats,pkh(158c8712c7607842dbe63223fb4e8a7323162e47))),UInt32Impl(0)),Vector(Masked(ECPrivateKeyImpl)),SIGHASH_ALL(Int32Impl(1)))
+// utxoInfo: ScriptSignatureParams[P2PKHInputInfo] = ScriptSignatureParams(P2PKHInputInfo(TransactionOutPoint(0070c7ed683231eddf0f3cd892528c48d747ac05bbd97b51781c6db3f306db5a:0),10000 sats,ECPublicKey(035202285fdab29d57c6929b3bc05f40f67e840ddb0b947ab23175bc1a0b584ec4)),BaseTransaction(Int32Impl(1),Vector(),Vector(TransactionOutput(10000 sats,pkh(783f42380c222643924d0ea83b1de85bc32e12c7))),UInt32Impl(0)),Vector(Masked(ECPrivateKeyImpl)),SIGHASH_ALL(Int32Impl(1)))
// all of the UTXO spending information, since we only have
// one input, this is just one element
val utxoInfos: Vector[ScriptSignatureParams[InputInfo]] = Vector(utxoInfo)
-// utxoInfos: Vector[ScriptSignatureParams[InputInfo]] = Vector(ScriptSignatureParams(P2PKHInputInfo(TransactionOutPoint(fb6aad808e64d036f08710744968f4c1c99a19738933013965b48cf72932bac1:0),10000 sats,ECPublicKey(0373a3b855988ce74d6bdeeb213aae05bfe3943f25a1277546062ca3b16771864f)),BaseTransaction(Int32Impl(1),Vector(),Vector(TransactionOutput(10000 sats,pkh(158c8712c7607842dbe63223fb4e8a7323162e47))),UInt32Impl(0)),Vector(Masked(ECPrivateKeyImpl)),SIGHASH_ALL(Int32Impl(1))))
+// utxoInfos: Vector[ScriptSignatureParams[InputInfo]] = Vector(ScriptSignatureParams(P2PKHInputInfo(TransactionOutPoint(0070c7ed683231eddf0f3cd892528c48d747ac05bbd97b51781c6db3f306db5a:0),10000 sats,ECPublicKey(035202285fdab29d57c6929b3bc05f40f67e840ddb0b947ab23175bc1a0b584ec4)),BaseTransaction(Int32Impl(1),Vector(),Vector(TransactionOutput(10000 sats,pkh(783f42380c222643924d0ea83b1de85bc32e12c7))),UInt32Impl(0)),Vector(Masked(ECPrivateKeyImpl)),SIGHASH_ALL(Int32Impl(1))))
// Yay! Now we use the RawTxSigner object to sign the tx.
// The 'sign' method is going produce a validly signed transaction
@@ -201,7 +201,7 @@ builder += input
}
Await.result(signedTxF, 30.seconds)
}
-// signedTx: Transaction = BaseTransaction(Int32Impl(2),Vector(TransactionInputImpl(TransactionOutPoint(fb6aad808e64d036f08710744968f4c1c99a19738933013965b48cf72932bac1:0),P2PKHScriptSignature(ECPublicKey(0373a3b855988ce74d6bdeeb213aae05bfe3943f25a1277546062ca3b16771864f), ECDigitalSignature(304402204244bb64b8c4a0eed9edbe5493db7e18062ff5eef71e4f87aeb4a7cc803dc9f902202adeae461a06261fd57a89200ef663b5e34c210259458300096b555ad7bc1d2501)),UInt32Impl(0))),Vector(TransactionOutput(5000 sats,pkh(466b97c9607fa3f44cec1aabe265d6df6853ac36)), TransactionOutput(4775 sats,pkh(7a9e13a8ec8f3b0e1de4afd84fc72831ec91001f))),UInt32Impl(0))
+// signedTx: Transaction = BaseTransaction(Int32Impl(2),Vector(TransactionInputImpl(TransactionOutPoint(0070c7ed683231eddf0f3cd892528c48d747ac05bbd97b51781c6db3f306db5a:0),P2PKHScriptSignature(ECPublicKey(035202285fdab29d57c6929b3bc05f40f67e840ddb0b947ab23175bc1a0b584ec4), ECDigitalSignature(30440220138bfd0ef1e4e6ead8605a1a7d3aa89de3b18dc6b1c178d05dd033b36d8747af02207883329f56e1e7585bcbb031206379a1e06aa45bdbe02e45345da83139d3a78701)),UInt32Impl(0))),Vector(TransactionOutput(5000 sats,pkh(36f24e0c097994f0a2257e4258976439b7e5e371)), TransactionOutput(4775 sats,pkh(f64e23855bb0787017a6333903777c615d848bd6))),UInt32Impl(0))
signedTx.inputs.length
// res2: Int = 1
@@ -211,7 +211,7 @@ signedTx.outputs.length
//remember, you can call .hex on any bitcoin-s data structure to get the hex representation!
signedTx.hex
-// res4: String = 0200000001c1ba3229f78cb4653901338973199ac9c1f46849741087f036d0648e80ad6afb000000006a47304402204244bb64b8c4a0eed9edbe5493db7e18062ff5eef71e4f87aeb4a7cc803dc9f902202adeae461a06261fd57a89200ef663b5e34c210259458300096b555ad7bc1d2501210373a3b855988ce74d6bdeeb213aae05bfe3943f25a1277546062ca3b16771864f000000000288130000000000001976a914466b97c9607fa3f44cec1aabe265d6df6853ac3688aca7120000000000001976a9147a9e13a8ec8f3b0e1de4afd84fc72831ec91001f88ac00000000
+// res4: String = 02000000015adb06f3b36d1c78517bd9bb05ac47d7488c5292d83c0fdfed313268edc77000000000006a4730440220138bfd0ef1e4e6ead8605a1a7d3aa89de3b18dc6b1c178d05dd033b36d8747af02207883329f56e1e7585bcbb031206379a1e06aa45bdbe02e45345da83139d3a7870121035202285fdab29d57c6929b3bc05f40f67e840ddb0b947ab23175bc1a0b584ec4000000000288130000000000001976a91436f24e0c097994f0a2257e4258976439b7e5e37188aca7120000000000001976a914f64e23855bb0787017a6333903777c615d848bd688ac00000000