Skip to content

Commit

Permalink
fix: Make StacksAddress construction in stx_address infallible
Browse files Browse the repository at this point in the history
  • Loading branch information
netrome committed Apr 12, 2023
1 parent 80983f7 commit 5620634
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions src/chainstate/burn/operations/peg_out_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use stacks_common::address::public_keys_to_address_hash;
use stacks_common::address::AddressHashMode;
use stacks_common::codec::StacksMessageCodec;
use stacks_common::types::chainstate::StacksPublicKey;
Expand Down Expand Up @@ -126,13 +127,9 @@ impl PegOutRequestOp {
let pub_key = StacksPublicKey::recover_to_pubkey(msg_hash.as_bytes(), &self.signature)
.map_err(RecoverError::PubKeyRecoveryFailed)?;

StacksAddress::from_public_keys(
version,
&AddressHashMode::SerializeP2PKH,
1,
&vec![pub_key],
)
.ok_or(RecoverError::AddressConstructionFailed)
let hash_bits =
public_keys_to_address_hash(&AddressHashMode::SerializeP2PKH, 1, &vec![pub_key]);
Ok(StacksAddress::new(version, hash_bits))
}

pub fn check(&self) -> Result<(), OpError> {
Expand Down Expand Up @@ -165,7 +162,6 @@ enum ParseError {
#[derive(Debug, PartialEq)]
pub enum RecoverError {
PubKeyRecoveryFailed(&'static str),
AddressConstructionFailed,
}

impl From<ParseError> for OpError {
Expand All @@ -182,8 +178,6 @@ impl From<std::array::TryFromSliceError> for ParseError {

#[cfg(test)]
mod tests {
use super::*;

use stacks_common::deps_common::bitcoin::blockdata::transaction::Transaction;
use stacks_common::deps_common::bitcoin::network::serialize::deserialize;
use stacks_common::types::chainstate::BurnchainHeaderHash;
Expand All @@ -192,6 +186,7 @@ mod tests {
use stacks_common::types::StacksEpochId;
use stacks_common::util::hash::{hex_bytes, to_hex};

use super::*;
use crate::burnchains::bitcoin::blocks::BitcoinBlockParser;
use crate::burnchains::bitcoin::BitcoinNetworkType;
use crate::burnchains::Txid;
Expand Down

0 comments on commit 5620634

Please sign in to comment.