Skip to content

Commit 8afc81b

Browse files
committed
address: do a better job slicing bech32 data
Currently we allow decoding segwit v0 programs which have uncompressed/hybrid keys (not allowed) and I suspect that if you provide a too-short address then you'll get a panic here.
1 parent 2886ea2 commit 8afc81b

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

src/address.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use crate::hashes::Hash;
2727
use bitcoin::base58;
2828
use bitcoin::PublicKey;
2929
use internals::array::ArrayExt as _;
30+
use internals::slice::SliceExt;
3031
use secp256k1_zkp;
3132
use secp256k1_zkp::Secp256k1;
3233
use secp256k1_zkp::Verification;
@@ -468,13 +469,17 @@ impl Address {
468469
};
469470

470471
let (blinding_pubkey, program) = match blinded {
471-
true => (
472+
true => {
473+
let (pk, rest) = SliceExt::split_first_chunk::<33>(data.as_slice())
474+
.ok_or(AddressError::InvalidSegwitV0Encoding)?;
475+
(
472476
Some(
473-
secp256k1_zkp::PublicKey::from_slice(&data[..33])
477+
secp256k1_zkp::PublicKey::from_slice(pk)
474478
.map_err(AddressError::InvalidBlindingPubKey)?,
475-
),
476-
data[33..].to_vec(),
477-
),
479+
),
480+
rest.to_vec(),
481+
)
482+
},
478483
false => (None, data),
479484
};
480485

0 commit comments

Comments
 (0)