Merge rust-bitcoin/rust-bitcoin#978: Make Address::get_payload_bytes public

7ca30b6aa8 Move Address::payload_as_bytes to Payload::as_bytes (Fredrik Meringdal)
525ea00e0f Make Address::get_payload_bytes public (Fredrik Meringdal)

Pull request description:

  Hi, thanks for the amazing work on this crate.

  I am trying to upgrade from v0.27 to v0.28, but unable to do so because the `Address::get_payload_bytes` was made private. My use-case is that I have a script hash address and an `Address` and need to compare the two, and in order to do so I need access to the payload bytes of `Address`.
  I hope you will consider making this function public again 🙏

ACKs for top commit:
  apoelstra:
    ACK 7ca30b6
  tcharding:
    ACK 7ca30b6aa8
  sanket1729:
    ACK 7ca30b6aa8. Sorry for the delay and congratz on your first time contribution

Tree-SHA512: 02af4565853d93506751ed7cb004f52cb5d8c7936067e06b3e237b448ccdf5716470448eeccbe211958e095b66bb37c7027800c0470c6988dc18d8bd5b48f459
This commit is contained in:
sanket1729 2022-05-19 18:38:07 -07:00
commit 48466bdf93
No known key found for this signature in database
GPG Key ID: 648FFB183E0870A2
1 changed files with 11 additions and 11 deletions

View File

@ -468,6 +468,15 @@ impl Payload {
program: output_key.as_inner().serialize().to_vec(), program: output_key.as_inner().serialize().to_vec(),
} }
} }
/// Returns a byte slice of the payload
pub fn as_bytes(&self) -> &[u8] {
match self {
Payload::ScriptHash(hash) => hash,
Payload::PubkeyHash(hash) => hash,
Payload::WitnessProgram { program, .. } => program,
}
}
} }
/// A utility struct to encode an address payload with the given parameters. /// A utility struct to encode an address payload with the given parameters.
@ -722,7 +731,7 @@ impl Address {
/// given key. For taproot addresses, the supplied key is assumed to be tweaked /// given key. For taproot addresses, the supplied key is assumed to be tweaked
pub fn is_related_to_pubkey(&self, pubkey: &PublicKey) -> bool { pub fn is_related_to_pubkey(&self, pubkey: &PublicKey) -> bool {
let pubkey_hash = pubkey.pubkey_hash(); let pubkey_hash = pubkey.pubkey_hash();
let payload = self.payload_as_bytes(); let payload = self.payload.as_bytes();
let xonly_pubkey = XOnlyPublicKey::from(pubkey.inner); let xonly_pubkey = XOnlyPublicKey::from(pubkey.inner);
(*pubkey_hash == *payload) || (xonly_pubkey.serialize() == *payload) || (*segwit_redeem_hash(&pubkey_hash) == *payload) (*pubkey_hash == *payload) || (xonly_pubkey.serialize() == *payload) || (*segwit_redeem_hash(&pubkey_hash) == *payload)
@ -733,18 +742,9 @@ impl Address {
/// This will only work for Taproot addresses. The Public Key is /// This will only work for Taproot addresses. The Public Key is
/// assumed to have already been tweaked. /// assumed to have already been tweaked.
pub fn is_related_to_xonly_pubkey(&self, xonly_pubkey: &XOnlyPublicKey) -> bool { pub fn is_related_to_xonly_pubkey(&self, xonly_pubkey: &XOnlyPublicKey) -> bool {
let payload = self.payload_as_bytes(); let payload = self.payload.as_bytes();
payload == xonly_pubkey.serialize() payload == xonly_pubkey.serialize()
} }
/// Return the address payload as a byte slice
fn payload_as_bytes(&self) -> &[u8] {
match &self.payload {
Payload::ScriptHash(hash) => hash,
Payload::PubkeyHash(hash) => hash,
Payload::WitnessProgram { program, .. } => program,
}
}
} }
// Alternate formatting `{:#}` is used to return uppercase version of bech32 addresses which should // Alternate formatting `{:#}` is used to return uppercase version of bech32 addresses which should