Move Address::payload_as_bytes to Payload::as_bytes

This commit is contained in:
Fredrik Meringdal 2022-05-02 12:32:08 +02:00
parent 525ea00e0f
commit 7ca30b6aa8
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
pub 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