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@2d449de9[Running, parallelism = 2, size = 2, active = 0, running = 0, steals = 9, tasks = 0, submissions = 0]
+// ec: ExecutionContext = scala.concurrent.impl.ExecutionContextImpl$$anon$3@430bbf4b[Running, parallelism = 2, size = 1, active = 0, running = 0, steals = 9, 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(03dc36663d0d4d83751fbad4bc3b5297578b7fbcec18735fe125ed53b76181f5f7)
+// pubKey: ECPublicKey = ECPublicKey(0226f3848952e3a0eefaae6998d4ae588f4998ed5981679c6602aae073a5145b56)
// 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(1ea74312a61bdd48723ad875cd240cebd5cfaef6)
+// creditingSpk: P2PKHScriptPubKey = pkh(5830771fe741a85013f5c1048293791c1f084355)
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(1ea74312a61bdd48723ad875cd240cebd5cfaef6))
+// utxo: TransactionOutput = TransactionOutput(10000 sats,pkh(5830771fe741a85013f5c1048293791c1f084355))
// 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(37753773876bd819d38cf08812c685ea727d562f)
+// destinationSPK: P2PKHScriptPubKey = pkh(500a2a16b5fa3c4661c26bed6605f6da905f1bab)
// 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(37753773876bd819d38cf08812c685ea727d562f)))
+// destinations: Vector[TransactionOutput] = Vector(TransactionOutput(5000 sats,pkh(500a2a16b5fa3c4661c26bed6605f6da905f1bab)))
// 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(1ea74312a61bdd48723ad875cd240cebd5cfaef6))),UInt32Impl(0))
+// creditingTx: BaseTransaction = BaseTransactionImpl(Int32Impl(1),Vector(),Vector(TransactionOutput(10000 sats,pkh(5830771fe741a85013f5c1048293791c1f084355))),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(85e217773d402ed6afea9e7caddd08f356e73eb38967f3bfa2792fe69fc68915:0)
+// outPoint: TransactionOutPoint = TransactionOutPoint(22698a6d5a7fd3b2e9bc009830388aba7b6f4e8457bab8a0a3ffb0a746c2d2e6: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(85e217773d402ed6afea9e7caddd08f356e73eb38967f3bfa2792fe69fc68915:0),10000 sats,pkh(1ea74312a61bdd48723ad875cd240cebd5cfaef6),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1)))
+// utxoSpendingInfo: BitcoinUTXOSpendingInfoFull = P2PKHSpendingInfo(TransactionOutPoint(22698a6d5a7fd3b2e9bc009830388aba7b6f4e8457bab8a0a3ffb0a746c2d2e6:0),10000 sats,pkh(5830771fe741a85013f5c1048293791c1f084355),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(85e217773d402ed6afea9e7caddd08f356e73eb38967f3bfa2792fe69fc68915:0),10000 sats,pkh(1ea74312a61bdd48723ad875cd240cebd5cfaef6),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1))))
+// utxos: Vector[BitcoinUTXOSpendingInfoFull] = Vector(P2PKHSpendingInfo(TransactionOutPoint(22698a6d5a7fd3b2e9bc009830388aba7b6f4e8457bab8a0a3ffb0a746c2d2e6:0),10000 sats,pkh(5830771fe741a85013f5c1048293791c1f084355),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(954c8e5b17f220557409cd91ce375ff8f34b3855)
+// changeSPK: P2PKHScriptPubKey = pkh(da629e94594b79e41b8c6d25768073b89ac56f13)
// 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(37753773876bd819d38cf08812c685ea727d562f))),Map(TransactionOutPoint(85e217773d402ed6afea9e7caddd08f356e73eb38967f3bfa2792fe69fc68915:0) -> P2PKHSpendingInfo(TransactionOutPoint(85e217773d402ed6afea9e7caddd08f356e73eb38967f3bfa2792fe69fc68915:0),10000 sats,pkh(1ea74312a61bdd48723ad875cd240cebd5cfaef6),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1)))),SatoshisPerByte(1 sat),pkh(954c8e5b17f220557409cd91ce375ff8f34b3855),RegTest)
+// txBuilder: BitcoinTxBuilder = BitcoinTxBuilderImpl(Vector(TransactionOutput(5000 sats,pkh(500a2a16b5fa3c4661c26bed6605f6da905f1bab))),Map(TransactionOutPoint(22698a6d5a7fd3b2e9bc009830388aba7b6f4e8457bab8a0a3ffb0a746c2d2e6:0) -> P2PKHSpendingInfo(TransactionOutPoint(22698a6d5a7fd3b2e9bc009830388aba7b6f4e8457bab8a0a3ffb0a746c2d2e6:0),10000 sats,pkh(5830771fe741a85013f5c1048293791c1f084355),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1)))),SatoshisPerByte(1 sat),pkh(da629e94594b79e41b8c6d25768073b89ac56f13),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(85e217773d402ed6afea9e7caddd08f356e73eb38967f3bfa2792fe69fc68915:0),P2PKHScriptSignature(ECPublicKey(03dc36663d0d4d83751fbad4bc3b5297578b7fbcec18735fe125ed53b76181f5f7), ECDigitalSignature(3045022100e3d34674e38c389f6cc406d13ead08171c53530dd7ee6149977e2763d76e32620220527f3900fe16078cd83fbabf3d36b53b80e31464b7b3c7043044200bd2d343e601)),UInt32Impl(0))),Vector(TransactionOutput(5000 sats,pkh(37753773876bd819d38cf08812c685ea727d562f)), TransactionOutput(4774 sats,pkh(954c8e5b17f220557409cd91ce375ff8f34b3855))),UInt32Impl(0))
+// signedTx: Transaction = BaseTransactionImpl(Int32Impl(2),List(TransactionInputImpl(TransactionOutPoint(22698a6d5a7fd3b2e9bc009830388aba7b6f4e8457bab8a0a3ffb0a746c2d2e6:0),P2PKHScriptSignature(ECPublicKey(0226f3848952e3a0eefaae6998d4ae588f4998ed5981679c6602aae073a5145b56), ECDigitalSignature(3045022100dfb8f794c83522fb018400ccf0a25fff400204aeeb8014061d6e20373535482602203f2d9b8be0a3b09b4277df2a6c5c6d1f8bdf27373868c215e8088b961775c65501)),UInt32Impl(0))),Vector(TransactionOutput(5000 sats,pkh(500a2a16b5fa3c4661c26bed6605f6da905f1bab)), TransactionOutput(4774 sats,pkh(da629e94594b79e41b8c6d25768073b89ac56f13))),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 = 02000000011589c69fe62f79a2bff36789b33ee756f308ddad7c9eeaafd62e403d7717e285000000006b483045022100e3d34674e38c389f6cc406d13ead08171c53530dd7ee6149977e2763d76e32620220527f3900fe16078cd83fbabf3d36b53b80e31464b7b3c7043044200bd2d343e6012103dc36663d0d4d83751fbad4bc3b5297578b7fbcec18735fe125ed53b76181f5f7000000000288130000000000001976a91437753773876bd819d38cf08812c685ea727d562f88aca6120000000000001976a914954c8e5b17f220557409cd91ce375ff8f34b385588ac00000000
+// res2: String = 0200000001e6d2c246a7b0ffa3a0b8ba57844e6f7bba8a38309800bce9b2d37f5a6d8a6922000000006b483045022100dfb8f794c83522fb018400ccf0a25fff400204aeeb8014061d6e20373535482602203f2d9b8be0a3b09b4277df2a6c5c6d1f8bdf27373868c215e8088b961775c65501210226f3848952e3a0eefaae6998d4ae588f4998ed5981679c6602aae073a5145b56000000000288130000000000001976a914500a2a16b5fa3c4661c26bed6605f6da905f1bab88aca6120000000000001976a914da629e94594b79e41b8c6d25768073b89ac56f1388ac00000000