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@1f5aa277[Running, parallelism = 2, size = 1, active = 0, running = 0, steals = 9, tasks = 0, submissions = 0]
+// ec: ExecutionContext = scala.concurrent.impl.ExecutionContextImpl$$anon$3@694108ec[Running, parallelism = 2, size = 1, 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(0203add5a55a462ea0bf39e36997c34b7cd3b53c885a640ad579976a0994278824)
+// pubKey: ECPublicKey = ECPublicKey(03c56166349342a9fdd3482482293901ebe8db9e648650c8160d10d52b977ffdd2)
// 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(c08509e1279b009c02de88a1a6a1e8d1e20c97f1)
+// creditingSpk: P2PKHScriptPubKey = pkh(adda6b4da67029bdf0f12e82d6dc38eec98392f1)
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(c08509e1279b009c02de88a1a6a1e8d1e20c97f1))
+// utxo: TransactionOutput = TransactionOutput(10000 sats,pkh(adda6b4da67029bdf0f12e82d6dc38eec98392f1))
// 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(e975cad237d2fd5612353d4a76b84d1a097da1b3)
+// destinationSPK: P2PKHScriptPubKey = pkh(aff4f9d308c0a8a4d14fe5f4eec2cca0acdd59a2)
// 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(e975cad237d2fd5612353d4a76b84d1a097da1b3)))
+// destinations: Vector[TransactionOutput] = Vector(TransactionOutput(5000 sats,pkh(aff4f9d308c0a8a4d14fe5f4eec2cca0acdd59a2)))
// 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(c08509e1279b009c02de88a1a6a1e8d1e20c97f1))),UInt32Impl(0))
+// creditingTx: BaseTransaction = BaseTransactionImpl(Int32Impl(1),Vector(),Vector(TransactionOutput(10000 sats,pkh(adda6b4da67029bdf0f12e82d6dc38eec98392f1))),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(aeea86acb54cacc3ad74eb2cc3a22efdb307b6a6517d41b911ae9826259416d7:0)
+// outPoint: TransactionOutPoint = TransactionOutPoint(4662fb9f72417a438722b26e4bcd818b2cfd43a8a398f24a82246493c81afbde: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(aeea86acb54cacc3ad74eb2cc3a22efdb307b6a6517d41b911ae9826259416d7:0),10000 sats,pkh(c08509e1279b009c02de88a1a6a1e8d1e20c97f1),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1)))
+// utxoSpendingInfo: BitcoinUTXOSpendingInfoFull = P2PKHSpendingInfo(TransactionOutPoint(4662fb9f72417a438722b26e4bcd818b2cfd43a8a398f24a82246493c81afbde:0),10000 sats,pkh(adda6b4da67029bdf0f12e82d6dc38eec98392f1),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(aeea86acb54cacc3ad74eb2cc3a22efdb307b6a6517d41b911ae9826259416d7:0),10000 sats,pkh(c08509e1279b009c02de88a1a6a1e8d1e20c97f1),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1))))
+// utxos: Vector[BitcoinUTXOSpendingInfoFull] = Vector(P2PKHSpendingInfo(TransactionOutPoint(4662fb9f72417a438722b26e4bcd818b2cfd43a8a398f24a82246493c81afbde:0),10000 sats,pkh(adda6b4da67029bdf0f12e82d6dc38eec98392f1),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(c2f42931a423a80247117213e5af53c6d7a06e63)
+// changeSPK: P2PKHScriptPubKey = pkh(a05c8abb98bbecc70b95b2fba9e471a1233b48ac)
// 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(e975cad237d2fd5612353d4a76b84d1a097da1b3))),Map(TransactionOutPoint(aeea86acb54cacc3ad74eb2cc3a22efdb307b6a6517d41b911ae9826259416d7:0) -> P2PKHSpendingInfo(TransactionOutPoint(aeea86acb54cacc3ad74eb2cc3a22efdb307b6a6517d41b911ae9826259416d7:0),10000 sats,pkh(c08509e1279b009c02de88a1a6a1e8d1e20c97f1),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1)))),SatoshisPerByte(1 sat),pkh(c2f42931a423a80247117213e5af53c6d7a06e63),RegTest)
+// txBuilder: BitcoinTxBuilder = BitcoinTxBuilderImpl(Vector(TransactionOutput(5000 sats,pkh(aff4f9d308c0a8a4d14fe5f4eec2cca0acdd59a2))),Map(TransactionOutPoint(4662fb9f72417a438722b26e4bcd818b2cfd43a8a398f24a82246493c81afbde:0) -> P2PKHSpendingInfo(TransactionOutPoint(4662fb9f72417a438722b26e4bcd818b2cfd43a8a398f24a82246493c81afbde:0),10000 sats,pkh(adda6b4da67029bdf0f12e82d6dc38eec98392f1),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1)))),SatoshisPerByte(1 sat),pkh(a05c8abb98bbecc70b95b2fba9e471a1233b48ac),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(aeea86acb54cacc3ad74eb2cc3a22efdb307b6a6517d41b911ae9826259416d7:0),P2PKHScriptSignature(ECPublicKey(0203add5a55a462ea0bf39e36997c34b7cd3b53c885a640ad579976a0994278824), ECDigitalSignature(30450221009543d8685d5359a11fe4e899f42eef552bc1109ea522e4946a6815670628611502201ea5c9b138cb6312e7b7728641050b16dccffd7153d64753285775558ace066001)),UInt32Impl(0))),Vector(TransactionOutput(5000 sats,pkh(e975cad237d2fd5612353d4a76b84d1a097da1b3)), TransactionOutput(4774 sats,pkh(c2f42931a423a80247117213e5af53c6d7a06e63))),UInt32Impl(0))
+// signedTx: Transaction = BaseTransactionImpl(Int32Impl(2),List(TransactionInputImpl(TransactionOutPoint(4662fb9f72417a438722b26e4bcd818b2cfd43a8a398f24a82246493c81afbde:0),P2PKHScriptSignature(ECPublicKey(03c56166349342a9fdd3482482293901ebe8db9e648650c8160d10d52b977ffdd2), ECDigitalSignature(304402204af180fb7ae0e570c3ff015d297924862635a490e31d4b54912ce0d6c74cca0c02204ce1398f6a3fa9aeda242bc6cbaf214774b1c6e2f0a72aecbb8c612c8d6c263c01)),UInt32Impl(0))),Vector(TransactionOutput(5000 sats,pkh(aff4f9d308c0a8a4d14fe5f4eec2cca0acdd59a2)), TransactionOutput(4774 sats,pkh(a05c8abb98bbecc70b95b2fba9e471a1233b48ac))),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 = 0200000001d71694252698ae11b9417d51a6b607b3fd2ea2c32ceb74adc3ac4cb5ac86eaae000000006b4830450221009543d8685d5359a11fe4e899f42eef552bc1109ea522e4946a6815670628611502201ea5c9b138cb6312e7b7728641050b16dccffd7153d64753285775558ace066001210203add5a55a462ea0bf39e36997c34b7cd3b53c885a640ad579976a0994278824000000000288130000000000001976a914e975cad237d2fd5612353d4a76b84d1a097da1b388aca6120000000000001976a914c2f42931a423a80247117213e5af53c6d7a06e6388ac00000000
+// res2: String = 0200000001defb1ac8936424824af298a3a843fd2c8b81cd4b6eb22287437a41729ffb6246000000006a47304402204af180fb7ae0e570c3ff015d297924862635a490e31d4b54912ce0d6c74cca0c02204ce1398f6a3fa9aeda242bc6cbaf214774b1c6e2f0a72aecbb8c612c8d6c263c012103c56166349342a9fdd3482482293901ebe8db9e648650c8160d10d52b977ffdd2000000000288130000000000001976a914aff4f9d308c0a8a4d14fe5f4eec2cca0acdd59a288aca6120000000000001976a914a05c8abb98bbecc70b95b2fba9e471a1233b48ac88ac00000000