def serialize(self, check_validity: bool = True) -> bytes: if check_validity: self.assert_valid() psbt_out_bin: List[bytes] = [] if self.redeem_script: psbt_out_bin.append( serialize_bytes(PSBT_OUT_REDEEM_SCRIPT, self.redeem_script)) if self.witness_script: psbt_out_bin.append( serialize_bytes(PSBT_OUT_WITNESS_SCRIPT, self.witness_script)) if self.hd_key_paths: psbt_out_bin.append( serialize_hd_key_paths(PSBT_OUT_BIP32_DERIVATION, self.hd_key_paths)) if self.unknown: psbt_out_bin.append(serialize_dict_bytes_bytes(b"", self.unknown)) return b"".join(psbt_out_bin)
def serialize(self, check_validity: bool = True) -> bytes: if check_validity: self.assert_valid() psbt_bin: List[bytes] = [PSBT_MAGIC_BYTES, PSBT_SEPARATOR] temp = self.tx.serialize(include_witness=False) psbt_bin.append(serialize_bytes(PSBT_GLOBAL_UNSIGNED_TX, temp)) if self.version: temp = self.version.to_bytes(4, byteorder="little", signed=False) psbt_bin.append(serialize_bytes(PSBT_GLOBAL_VERSION, temp)) if self.hd_key_paths: psbt_bin.append( serialize_hd_key_paths(PSBT_GLOBAL_XPUB, self.hd_key_paths)) if self.unknown: psbt_bin.append(serialize_dict_bytes_bytes(b"", self.unknown)) psbt_bin.append(PSBT_DELIMITER) for input_map in self.inputs: psbt_bin.append(input_map.serialize() + b"\x00") for output_map in self.outputs: psbt_bin.append(output_map.serialize() + b"\x00") return b"".join(psbt_bin)
def serialize(self, check_validity: bool = True) -> bytes: if check_validity: self.assert_valid() psbt_in_bin: List[bytes] = [] if self.non_witness_utxo: temp = self.non_witness_utxo.serialize(include_witness=True) psbt_in_bin.append(serialize_bytes(PSBT_IN_NON_WITNESS_UTXO, temp)) if self.witness_utxo: psbt_in_bin.append( serialize_bytes(PSBT_IN_WITNESS_UTXO, self.witness_utxo.serialize())) if not self.final_script_sig and not self.final_script_witness: if self.partial_sigs: psbt_in_bin.append( serialize_dict_bytes_bytes(PSBT_IN_PARTIAL_SIG, self.partial_sigs)) if self.sig_hash_type: temp = self.sig_hash_type.to_bytes(4, byteorder="little", signed=False) psbt_in_bin.append(serialize_bytes(PSBT_IN_SIG_HASH_TYPE, temp)) if self.redeem_script: psbt_in_bin.append( serialize_bytes(PSBT_IN_REDEEM_SCRIPT, self.redeem_script)) if self.witness_script: psbt_in_bin.append( serialize_bytes(PSBT_IN_WITNESS_SCRIPT, self.witness_script)) if self.hd_key_paths: psbt_in_bin.append( serialize_hd_key_paths(PSBT_IN_BIP32_DERIVATION, self.hd_key_paths)) if self.final_script_sig: psbt_in_bin.append( serialize_bytes(PSBT_IN_FINAL_SCRIPTSIG, self.final_script_sig)) if self.final_script_witness: temp = self.final_script_witness.serialize() psbt_in_bin.append( serialize_bytes(PSBT_IN_FINAL_SCRIPTWITNESS, temp)) if self.unknown: psbt_in_bin.append(serialize_dict_bytes_bytes(b"", self.unknown)) if self.ripemd160_preimages: print( serialize_dict_bytes_bytes(PSBT_IN_RIPEMD160, self.ripemd160_preimages)) print(self.ripemd160_preimages) psbt_in_bin.append( serialize_dict_bytes_bytes(PSBT_IN_RIPEMD160, self.ripemd160_preimages)) if self.sha256_preimages: psbt_in_bin.append( serialize_dict_bytes_bytes(PSBT_IN_SHA256, self.sha256_preimages)) if self.hash160_preimages: psbt_in_bin.append( serialize_dict_bytes_bytes(PSBT_IN_HASH160, self.hash160_preimages)) if self.hash256_preimages: psbt_in_bin.append( serialize_dict_bytes_bytes(PSBT_IN_HASH256, self.hash256_preimages)) return b"".join(psbt_in_bin)
def test_invalid_serialize_hd_key_paths(): with pytest.raises(BTClibValueError): serialize_hd_key_paths(b"\x01\x01", [])