| 598 | |
| 599 | template <typename Stream> |
| 600 | inline void Unserialize(Stream& s) { |
| 601 | // Read the magic bytes |
| 602 | uint8_t magic[5]; |
| 603 | s >> magic; |
| 604 | if (!std::equal(magic, magic + 5, PSBT_MAGIC_BYTES)) { |
| 605 | throw std::ios_base::failure("Invalid PSBT magic bytes"); |
| 606 | } |
| 607 | |
| 608 | // Read global data |
| 609 | bool found_sep = false; |
| 610 | while(!s.empty()) { |
| 611 | // Read |
| 612 | std::vector<unsigned char> key; |
| 613 | s >> key; |
| 614 | |
| 615 | // the key is empty if that was actually a separator byte |
| 616 | // This is a special case for key lengths 0 as those are not allowed (except for separator) |
| 617 | if (key.empty()) { |
| 618 | found_sep = true; |
| 619 | break; |
| 620 | } |
| 621 | |
| 622 | // First byte of key is the type |
| 623 | unsigned char type = key[0]; |
| 624 | |
| 625 | // Do stuff based on type |
| 626 | switch(type) { |
| 627 | case PSBT_GLOBAL_UNSIGNED_TX: |
| 628 | { |
| 629 | if (tx) { |
| 630 | throw std::ios_base::failure("Duplicate Key, unsigned tx already provided"); |
| 631 | } else if (key.size() != 1) { |
| 632 | throw std::ios_base::failure("Global unsigned tx key is more than one byte type"); |
| 633 | } |
| 634 | CMutableTransaction mtx; |
| 635 | // Set the stream to serialize with non-witness since this should always be non-witness |
| 636 | OverrideStream<Stream> os(&s, s.GetType(), s.GetVersion() | SERIALIZE_TRANSACTION_NO_WITNESS); |
| 637 | UnserializeFromVector(os, mtx); |
| 638 | tx = std::move(mtx); |
| 639 | // Make sure that all scriptSigs and scriptWitnesses are empty |
| 640 | for (const CTxIn& txin : tx->vin) { |
| 641 | if (!txin.scriptSig.empty() || !txin.scriptWitness.IsNull()) { |
| 642 | throw std::ios_base::failure("Unsigned tx does not have empty scriptSigs and scriptWitnesses."); |
| 643 | } |
| 644 | } |
| 645 | break; |
| 646 | } |
| 647 | // Unknown stuff |
| 648 | default: { |
| 649 | if (unknown.count(key) > 0) { |
| 650 | throw std::ios_base::failure("Duplicate Key, key for unknown value already provided"); |
| 651 | } |
| 652 | // Read in the value |
| 653 | std::vector<unsigned char> val_bytes; |
| 654 | s >> val_bytes; |
| 655 | unknown.emplace(std::move(key), std::move(val_bytes)); |
| 656 | } |
| 657 | } |
nothing calls this directly
no test coverage detected