@@ -447,12 +447,144 @@ bool PartiallySignedTransaction::IsNull() const
447
447
return !tx && inputs.empty () && outputs.empty () && unknown.empty ();
448
448
}
449
449
450
+ void PartiallySignedTransaction::Merge (const PartiallySignedTransaction& psbt)
451
+ {
452
+ for (unsigned int i = 0 ; i < inputs.size (); ++i) {
453
+ inputs[i].Merge (psbt.inputs [i]);
454
+ }
455
+ for (unsigned int i = 0 ; i < outputs.size (); ++i) {
456
+ outputs[i].Merge (psbt.outputs [i]);
457
+ }
458
+ }
459
+
460
+ bool PartiallySignedTransaction::IsSane () const
461
+ {
462
+ for (PSBTInput input : inputs) {
463
+ if (!input.IsSane ()) return false ;
464
+ }
465
+ return true ;
466
+ }
467
+
450
468
bool PSBTInput::IsNull () const
451
469
{
452
470
return !non_witness_utxo && witness_utxo.IsNull () && partial_sigs.empty () && unknown.empty () && hd_keypaths.empty () && redeem_script.empty () && witness_script.empty ();
453
471
}
454
472
473
+ void PSBTInput::FillSignatureData (SignatureData& sigdata) const
474
+ {
475
+ if (!final_script_sig.empty ()) {
476
+ sigdata.scriptSig = final_script_sig;
477
+ sigdata.complete = true ;
478
+ }
479
+ if (!final_script_witness.IsNull ()) {
480
+ sigdata.scriptWitness = final_script_witness;
481
+ sigdata.complete = true ;
482
+ }
483
+ if (sigdata.complete ) {
484
+ return ;
485
+ }
486
+
487
+ sigdata.signatures .insert (partial_sigs.begin (), partial_sigs.end ());
488
+ if (!redeem_script.empty ()) {
489
+ sigdata.redeem_script = redeem_script;
490
+ }
491
+ if (!witness_script.empty ()) {
492
+ sigdata.witness_script = witness_script;
493
+ }
494
+ for (const auto & key_pair : hd_keypaths) {
495
+ sigdata.misc_pubkeys .emplace (key_pair.first .GetID (), key_pair.first );
496
+ }
497
+ }
498
+
499
+ void PSBTInput::FromSignatureData (const SignatureData& sigdata)
500
+ {
501
+ if (sigdata.complete ) {
502
+ partial_sigs.clear ();
503
+ hd_keypaths.clear ();
504
+ redeem_script.clear ();
505
+ witness_script.clear ();
506
+
507
+ if (!sigdata.scriptSig .empty ()) {
508
+ final_script_sig = sigdata.scriptSig ;
509
+ }
510
+ if (!sigdata.scriptWitness .IsNull ()) {
511
+ final_script_witness = sigdata.scriptWitness ;
512
+ }
513
+ return ;
514
+ }
515
+
516
+ partial_sigs.insert (sigdata.signatures .begin (), sigdata.signatures .end ());
517
+ if (redeem_script.empty () && !sigdata.redeem_script .empty ()) {
518
+ redeem_script = sigdata.redeem_script ;
519
+ }
520
+ if (witness_script.empty () && !sigdata.witness_script .empty ()) {
521
+ witness_script = sigdata.witness_script ;
522
+ }
523
+ }
524
+
525
+ void PSBTInput::Merge (const PSBTInput& input)
526
+ {
527
+ if (!non_witness_utxo && input.non_witness_utxo ) non_witness_utxo = input.non_witness_utxo ;
528
+ if (witness_utxo.IsNull () && !input.witness_utxo .IsNull ()) {
529
+ witness_utxo = input.witness_utxo ;
530
+ non_witness_utxo = nullptr ; // Clear out any non-witness utxo when we set a witness one.
531
+ }
532
+
533
+ partial_sigs.insert (input.partial_sigs .begin (), input.partial_sigs .end ());
534
+ hd_keypaths.insert (input.hd_keypaths .begin (), input.hd_keypaths .end ());
535
+ unknown.insert (input.unknown .begin (), input.unknown .end ());
536
+
537
+ if (redeem_script.empty () && !input.redeem_script .empty ()) redeem_script = input.redeem_script ;
538
+ if (witness_script.empty () && !input.witness_script .empty ()) witness_script = input.witness_script ;
539
+ if (final_script_sig.empty () && !input.final_script_sig .empty ()) final_script_sig = input.final_script_sig ;
540
+ if (final_script_witness.IsNull () && !input.final_script_witness .IsNull ()) final_script_witness = input.final_script_witness ;
541
+ }
542
+
543
+ bool PSBTInput::IsSane () const
544
+ {
545
+ // Cannot have both witness and non-witness utxos
546
+ if (!witness_utxo.IsNull () && non_witness_utxo) return false ;
547
+
548
+ // If we have a witness_script or a scriptWitness, we must also have a witness utxo
549
+ if (!witness_script.empty () && witness_utxo.IsNull ()) return false ;
550
+ if (!final_script_witness.IsNull () && witness_utxo.IsNull ()) return false ;
551
+
552
+ return true ;
553
+ }
554
+
555
+ void PSBTOutput::FillSignatureData (SignatureData& sigdata) const
556
+ {
557
+ if (!redeem_script.empty ()) {
558
+ sigdata.redeem_script = redeem_script;
559
+ }
560
+ if (!witness_script.empty ()) {
561
+ sigdata.witness_script = witness_script;
562
+ }
563
+ for (const auto & key_pair : hd_keypaths) {
564
+ sigdata.misc_pubkeys .emplace (key_pair.first .GetID (), key_pair.first );
565
+ }
566
+ }
567
+
568
+ void PSBTOutput::FromSignatureData (const SignatureData& sigdata)
569
+ {
570
+ if (redeem_script.empty () && !sigdata.redeem_script .empty ()) {
571
+ redeem_script = sigdata.redeem_script ;
572
+ }
573
+ if (witness_script.empty () && !sigdata.witness_script .empty ()) {
574
+ witness_script = sigdata.witness_script ;
575
+ }
576
+ }
577
+
455
578
bool PSBTOutput::IsNull () const
456
579
{
457
580
return redeem_script.empty () && witness_script.empty () && hd_keypaths.empty () && unknown.empty ();
458
581
}
582
+
583
+ void PSBTOutput::Merge (const PSBTOutput& output)
584
+ {
585
+ hd_keypaths.insert (output.hd_keypaths .begin (), output.hd_keypaths .end ());
586
+ unknown.insert (output.unknown .begin (), output.unknown .end ());
587
+
588
+ if (redeem_script.empty () && !output.redeem_script .empty ()) redeem_script = output.redeem_script ;
589
+ if (witness_script.empty () && !output.witness_script .empty ()) witness_script = output.witness_script ;
590
+ }
0 commit comments