PSBT signing with BitcoinUTXOSpendingInfoSingle

This commit is contained in:
Ben Carman 2020-03-06 08:37:41 -06:00
parent a70b03d08a
commit c535985d6f

View file

@ -16,6 +16,7 @@ creation, updating, combining, signing, finalizing,
and transaction extraction.
An example on a typical PSBT workflow:
```scala mdoc:invisible
import org.bitcoins.core.crypto.ECPrivateKey
import org.bitcoins.core.protocol.script.ScriptPubKey
@ -26,7 +27,7 @@ import scodec.bits._
import scala.concurrent.{ExecutionContext, ExecutionContextExecutor}
```
```scala mdoc:to-string
```scala mdoc:compile-only
implicit val ec: ExecutionContextExecutor = ExecutionContext.global
// First you need an unsigned transaction,
@ -105,6 +106,33 @@ val psbtFirstSigF =
.sign(inputIndex = 0, signer = privKey0)
.flatMap(_.sign(inputIndex = 0, signer = privKey1))
// Alternatively, you can use produce a signature with a BitcoinUTXOSpendingInfoSingle
// using the BitcoinSingleSigner will return a PartialSignature that can be added to a PSBT
// First we need to declare out spendingInfoSingle
val outPoint = unsignedTransaction.inputs.head.previousOutput
val output = utxo0.outputs(outPoint.vout.toInt)
val spendingInfoSingle = BitcoinUTXOSpendingInfoSingle(
outPoint = outPoint,
output = output,
signer = privKey0,
redeemScriptOpt = Some(redeemScript0),
scriptWitnessOpt = None,
hashType = HashType.sigHashAll,
conditionalPath = ConditionalPath.NoConditionsLeft
)
// Then we can sign the transaction
val signatureF = BitcoinSignerSingle.signSingle(spendingInfo = spendingInfoSingle,
unsignedTx = unsignedTransaction,
isDummySignature = false)
// We can then add the signature to the PSBT
// Note: this signature could be produced by us or another party
signatureF.map(sig => psbtWithSigHashFlags.addSignature(sig, inputIndex = 0)
// With our first input signed we can now move on to showing how another party could sign our second input
psbtFirstSigF.map { psbtFirstSig =>
// In this scenario, let's say that the second input does not belong to us and we need
// another party to sign it. In this case we would need to send the PSBT to the other party.