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@51c97295[Running, parallelism = 2, size = 1, active = 0, running = 0, steals = 8, tasks = 0, submissions = 0]
+// ec: ExecutionContext = scala.concurrent.impl.ExecutionContextImpl$$anon$3@7022ff0b[Running, parallelism = 2, size = 2, active = 0, running = 0, steals = 8, 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(0353cb3f1980acda4107274dd7ef64bb8cb28f56f53262021cf4eed641d38514a3)
+// pubKey: ECPublicKey = ECPublicKey(027d1ed894a7626ab075e9698cd87720b380a2d9044f7d309d47d97ac188c0d5e8)
// 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(fdf33873314787582960581016b3b9f426d071e8)
+// creditingSpk: P2PKHScriptPubKey = pkh(940f7543e805f116e512632f994f9f4c2fddf3bc)
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(fdf33873314787582960581016b3b9f426d071e8))
+// utxo: TransactionOutput = TransactionOutput(10000 sats,pkh(940f7543e805f116e512632f994f9f4c2fddf3bc))
// 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(ec9d2520d5b8419bb6ec7b11c6e7497660510256)
+// destinationSPK: P2PKHScriptPubKey = pkh(db3d6197033cfbcbdd29c55ace7988222ae60fd9)
// 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(ec9d2520d5b8419bb6ec7b11c6e7497660510256)))
+// destinations: Vector[TransactionOutput] = Vector(TransactionOutput(5000 sats,pkh(db3d6197033cfbcbdd29c55ace7988222ae60fd9)))
// 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(fdf33873314787582960581016b3b9f426d071e8))),UInt32Impl(0))
+// creditingTx: BaseTransaction = BaseTransactionImpl(Int32Impl(1),Vector(),Vector(TransactionOutput(10000 sats,pkh(940f7543e805f116e512632f994f9f4c2fddf3bc))),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(63f03f551ff9454e58e32fc53f14cc6b27af1eca4f8476c2dbc7c64328c95f1d:0)
+// outPoint: TransactionOutPoint = TransactionOutPoint(e4755c54994d113af6ecb444df2b662ddc3e482f20bcbee4ab2b2c5a31620be6: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(63f03f551ff9454e58e32fc53f14cc6b27af1eca4f8476c2dbc7c64328c95f1d:0),10000 sats,pkh(fdf33873314787582960581016b3b9f426d071e8),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1)))
+// utxoSpendingInfo: BitcoinUTXOSpendingInfoFull = P2PKHSpendingInfo(TransactionOutPoint(e4755c54994d113af6ecb444df2b662ddc3e482f20bcbee4ab2b2c5a31620be6:0),10000 sats,pkh(940f7543e805f116e512632f994f9f4c2fddf3bc),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(63f03f551ff9454e58e32fc53f14cc6b27af1eca4f8476c2dbc7c64328c95f1d:0),10000 sats,pkh(fdf33873314787582960581016b3b9f426d071e8),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1))))
+// utxos: Vector[BitcoinUTXOSpendingInfoFull] = Vector(P2PKHSpendingInfo(TransactionOutPoint(e4755c54994d113af6ecb444df2b662ddc3e482f20bcbee4ab2b2c5a31620be6:0),10000 sats,pkh(940f7543e805f116e512632f994f9f4c2fddf3bc),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(a382075626293d99f7dcdfa2232bc50d09cbd194)
+// changeSPK: P2PKHScriptPubKey = pkh(9e8f15d49e1de57146bff521a929f7467a42bda1)
// 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(ec9d2520d5b8419bb6ec7b11c6e7497660510256))),Map(TransactionOutPoint(63f03f551ff9454e58e32fc53f14cc6b27af1eca4f8476c2dbc7c64328c95f1d:0) -> P2PKHSpendingInfo(TransactionOutPoint(63f03f551ff9454e58e32fc53f14cc6b27af1eca4f8476c2dbc7c64328c95f1d:0),10000 sats,pkh(fdf33873314787582960581016b3b9f426d071e8),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1)))),SatoshisPerByte(1 sat),pkh(a382075626293d99f7dcdfa2232bc50d09cbd194),RegTest)
+// txBuilder: BitcoinTxBuilder = BitcoinTxBuilderImpl(Vector(TransactionOutput(5000 sats,pkh(db3d6197033cfbcbdd29c55ace7988222ae60fd9))),Map(TransactionOutPoint(e4755c54994d113af6ecb444df2b662ddc3e482f20bcbee4ab2b2c5a31620be6:0) -> P2PKHSpendingInfo(TransactionOutPoint(e4755c54994d113af6ecb444df2b662ddc3e482f20bcbee4ab2b2c5a31620be6:0),10000 sats,pkh(940f7543e805f116e512632f994f9f4c2fddf3bc),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1)))),SatoshisPerByte(1 sat),pkh(9e8f15d49e1de57146bff521a929f7467a42bda1),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(63f03f551ff9454e58e32fc53f14cc6b27af1eca4f8476c2dbc7c64328c95f1d:0),P2PKHScriptSignature(ECPublicKey(0353cb3f1980acda4107274dd7ef64bb8cb28f56f53262021cf4eed641d38514a3), ECDigitalSignature(3045022100e3436249ff9f3078b3558c3a207cc373f910c9419f3a513eb267966f3581d6f30220659e40f02482f24f7aee3f18dac77008d8f11d1ca6482639015d658275c1160401)),UInt32Impl(0))),Vector(TransactionOutput(5000 sats,pkh(ec9d2520d5b8419bb6ec7b11c6e7497660510256)), TransactionOutput(4774 sats,pkh(a382075626293d99f7dcdfa2232bc50d09cbd194))),UInt32Impl(0))
+// signedTx: Transaction = BaseTransactionImpl(Int32Impl(2),List(TransactionInputImpl(TransactionOutPoint(e4755c54994d113af6ecb444df2b662ddc3e482f20bcbee4ab2b2c5a31620be6:0),P2PKHScriptSignature(ECPublicKey(027d1ed894a7626ab075e9698cd87720b380a2d9044f7d309d47d97ac188c0d5e8), ECDigitalSignature(304402203ed047527664d4f1aaaf7c467163d3a5280f84f3cbb49f85cc0295106fa9a57602201236ca577c37308a931030f4dd37d771b426c129413be8f24bcd206bb40f62f701)),UInt32Impl(0))),Vector(TransactionOutput(5000 sats,pkh(db3d6197033cfbcbdd29c55ace7988222ae60fd9)), TransactionOutput(4774 sats,pkh(9e8f15d49e1de57146bff521a929f7467a42bda1))),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 = 02000000011d5fc92843c6c7dbc276844fca1eaf276bcc143fc52fe3584e45f91f553ff063000000006b483045022100e3436249ff9f3078b3558c3a207cc373f910c9419f3a513eb267966f3581d6f30220659e40f02482f24f7aee3f18dac77008d8f11d1ca6482639015d658275c1160401210353cb3f1980acda4107274dd7ef64bb8cb28f56f53262021cf4eed641d38514a3000000000288130000000000001976a914ec9d2520d5b8419bb6ec7b11c6e749766051025688aca6120000000000001976a914a382075626293d99f7dcdfa2232bc50d09cbd19488ac00000000
+// res2: String = 0200000001e60b62315a2c2babe4bebc202f483edc2d662bdf44b4ecf63a114d99545c75e4000000006a47304402203ed047527664d4f1aaaf7c467163d3a5280f84f3cbb49f85cc0295106fa9a57602201236ca577c37308a931030f4dd37d771b426c129413be8f24bcd206bb40f62f70121027d1ed894a7626ab075e9698cd87720b380a2d9044f7d309d47d97ac188c0d5e8000000000288130000000000001976a914db3d6197033cfbcbdd29c55ace7988222ae60fd988aca6120000000000001976a9149e8f15d49e1de57146bff521a929f7467a42bda188ac00000000