From 525ea00e0fad53e42e918819cb71f42c222a7f89 Mon Sep 17 00:00:00 2001 From: Fredrik Meringdal Date: Fri, 29 Apr 2022 11:54:53 +0200 Subject: [PATCH 1/2] Make Address::get_payload_bytes public --- src/util/address.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/address.rs b/src/util/address.rs index 982ae118..4ddfcf83 100644 --- a/src/util/address.rs +++ b/src/util/address.rs @@ -738,7 +738,7 @@ impl Address { } /// Return the address payload as a byte slice - fn payload_as_bytes(&self) -> &[u8] { + pub fn payload_as_bytes(&self) -> &[u8] { match &self.payload { Payload::ScriptHash(hash) => hash, Payload::PubkeyHash(hash) => hash, From 7ca30b6aa8727ce2f5095c675d382b0a6c596470 Mon Sep 17 00:00:00 2001 From: Fredrik Meringdal Date: Mon, 2 May 2022 12:32:08 +0200 Subject: [PATCH 2/2] Move Address::payload_as_bytes to Payload::as_bytes --- src/util/address.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/util/address.rs b/src/util/address.rs index 4ddfcf83..afdd879f 100644 --- a/src/util/address.rs +++ b/src/util/address.rs @@ -468,6 +468,15 @@ impl Payload { 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. @@ -722,7 +731,7 @@ impl Address { /// given key. For taproot addresses, the supplied key is assumed to be tweaked pub fn is_related_to_pubkey(&self, pubkey: &PublicKey) -> bool { 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); (*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 /// assumed to have already been tweaked. 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() } - - /// 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