Skip to content

Commit 2f9767c

Browse files
authored
Bech32 prefix instead of Bech32m (#218)
1 parent 7e87482 commit 2f9767c

20 files changed

+142
-142
lines changed
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use super::*;
22

3-
pub(crate) struct Bech32mDecoder<'a> {
3+
pub(crate) struct Bech32Decoder<'a> {
44
data: &'a [u8],
55
i: usize,
6-
ty: Bech32mType,
6+
ty: Bech32Type,
77
}
88

9-
impl<'a> Bech32mDecoder<'a> {
10-
pub(crate) fn byte_array<const LEN: usize>(&mut self) -> Result<[u8; LEN], Bech32mError> {
9+
impl<'a> Bech32Decoder<'a> {
10+
pub(crate) fn byte_array<const LEN: usize>(&mut self) -> Result<[u8; LEN], Bech32Error> {
1111
let mut array = [0; LEN];
1212

1313
for (slot, byte) in array.iter_mut().zip(self.bytes(LEN)?) {
@@ -17,7 +17,7 @@ impl<'a> Bech32mDecoder<'a> {
1717
Ok(array)
1818
}
1919

20-
fn bytes(&mut self, len: usize) -> Result<impl Iterator<Item = u8>, Bech32mError> {
20+
fn bytes(&mut self, len: usize) -> Result<impl Iterator<Item = u8>, Bech32Error> {
2121
let fe_len = (len * 8).div_ceil(5);
2222

2323
let fes = self.fes(fe_len)?;
@@ -26,14 +26,14 @@ impl<'a> Bech32mDecoder<'a> {
2626
let padding_len = (i + 1) * 5 % 8;
2727

2828
if padding_len > 4 {
29-
return Err(Bech32mError::Padding {
29+
return Err(Bech32Error::Padding {
3030
source: PaddingError::TooMuch,
3131
ty: self.ty,
3232
});
3333
}
3434

3535
if u64::from(last.to_u8().trailing_zeros()) < padding_len.into_u64() {
36-
return Err(Bech32mError::Padding {
36+
return Err(Bech32Error::Padding {
3737
source: PaddingError::NonZero,
3838
ty: self.ty,
3939
});
@@ -43,30 +43,30 @@ impl<'a> Bech32mDecoder<'a> {
4343
Ok(fes.fes_to_bytes())
4444
}
4545

46-
pub(crate) fn done(self) -> Result<(), Bech32mError> {
46+
pub(crate) fn done(self) -> Result<(), Bech32Error> {
4747
let excess = self.data.len() - self.i;
4848

4949
ensure! {
5050
excess == 0,
51-
bech32m_error::Overlong { excess, ty: self.ty },
51+
bech32_error::Overlong { excess, ty: self.ty },
5252
}
5353

5454
Ok(())
5555
}
5656

57-
pub(crate) fn fe(&mut self) -> Result<Fe32, Bech32mError> {
57+
pub(crate) fn fe(&mut self) -> Result<Fe32, Bech32Error> {
5858
let next = self
5959
.data
6060
.get(self.i)
6161
.map(|c| Fe32::from_char_unchecked(*c))
62-
.context(bech32m_error::Truncated { ty: self.ty })?;
62+
.context(bech32_error::Truncated { ty: self.ty })?;
6363

6464
self.i += 1;
6565

6666
Ok(next)
6767
}
6868

69-
pub(crate) fn fe_array<const LEN: usize>(&mut self) -> Result<[Fe32; LEN], Bech32mError> {
69+
pub(crate) fn fe_array<const LEN: usize>(&mut self) -> Result<[Fe32; LEN], Bech32Error> {
7070
let mut array = [Fe32::Q; LEN];
7171

7272
for slot in &mut array {
@@ -79,11 +79,11 @@ impl<'a> Bech32mDecoder<'a> {
7979
fn fes(
8080
&mut self,
8181
len: usize,
82-
) -> Result<impl Iterator<Item = Fe32> + Clone + use<'a>, Bech32mError> {
82+
) -> Result<impl Iterator<Item = Fe32> + Clone + use<'a>, Bech32Error> {
8383
let end = self.i + len;
8484

8585
if end > self.data.len() {
86-
return Err(Bech32mError::Truncated { ty: self.ty });
86+
return Err(Bech32Error::Truncated { ty: self.ty });
8787
}
8888

8989
let fes = &self.data[self.i..end];
@@ -93,29 +93,29 @@ impl<'a> Bech32mDecoder<'a> {
9393
Ok(fes.iter().map(|c| Fe32::from_char_unchecked(*c)))
9494
}
9595

96-
pub(crate) fn into_bytes(mut self) -> Result<Vec<u8>, Bech32mError> {
96+
pub(crate) fn into_bytes(mut self) -> Result<Vec<u8>, Bech32Error> {
9797
let fes = self.data.len() - self.i;
9898
Ok(self.bytes(fes * 5 / 8)?.collect())
9999
}
100100

101-
pub(crate) fn new(ty: Bech32mType, s: &'a str) -> Result<Self, Bech32mError> {
101+
pub(crate) fn new(ty: Bech32Type, s: &'a str) -> Result<Self, Bech32Error> {
102102
let hrp_string =
103-
CheckedHrpstring::new::<bech32::Bech32m>(s).context(bech32m_error::Decode { ty })?;
103+
CheckedHrpstring::new::<bech32::Bech32m>(s).context(bech32_error::Decode { ty })?;
104104

105105
let actual = hrp_string.hrp();
106106

107107
ensure! {
108108
actual == *ty.hrp(),
109-
bech32m_error::Hrp { ty, actual },
109+
bech32_error::Hrp { ty, actual },
110110
}
111111

112112
let mut fes = hrp_string.fe32_iter::<std::vec::IntoIter<u8>>();
113113

114-
let version = fes.next().context(bech32m_error::Truncated { ty })?;
114+
let version = fes.next().context(bech32_error::Truncated { ty })?;
115115

116116
ensure! {
117-
version == BECH32M_VERSION,
118-
bech32m_error::UnsupportedVersion { ty, version },
117+
version == BECH32_VERSION,
118+
bech32_error::UnsupportedVersion { ty, version },
119119
}
120120

121121
Ok(Self {
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use super::*;
22

3-
pub(crate) struct Bech32mEncoder {
3+
pub(crate) struct Bech32Encoder {
44
fes: Vec<Fe32>,
5-
ty: Bech32mType,
5+
ty: Bech32Type,
66
}
77

8-
impl Bech32mEncoder {
8+
impl Bech32Encoder {
99
pub(crate) fn bytes(&mut self, bytes: &[u8]) {
1010
self.fes.extend(bytes.iter().copied().bytes_to_fes());
1111
}
@@ -14,15 +14,15 @@ impl Bech32mEncoder {
1414
self.fes.extend_from_slice(fes);
1515
}
1616

17-
pub(crate) fn new(ty: Bech32mType) -> Self {
17+
pub(crate) fn new(ty: Bech32Type) -> Self {
1818
Self {
19-
fes: vec![BECH32M_VERSION],
19+
fes: vec![BECH32_VERSION],
2020
ty,
2121
}
2222
}
2323
}
2424

25-
impl Display for Bech32mEncoder {
25+
impl Display for Bech32Encoder {
2626
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
2727
for c in self
2828
.fes

src/bech32_error.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use super::*;
2+
3+
#[derive(Debug, Snafu)]
4+
#[snafu(context(suffix(false)), visibility(pub(crate)))]
5+
pub enum Bech32Error {
6+
#[snafu(display("failed to decode bech32 {ty}"))]
7+
Decode {
8+
ty: Bech32Type,
9+
source: CheckedHrpstringError,
10+
},
11+
#[snafu(display(
12+
"expected bech32 human-readable part `{}1...` but found `{actual}1...`",
13+
ty.hrp(),
14+
))]
15+
Hrp { ty: Bech32Type, actual: crate::Hrp },
16+
#[snafu(display("bech32 {ty} overlong by {}", Count(*excess, "characters")))]
17+
Overlong { excess: usize, ty: Bech32Type },
18+
#[snafu(display("bech32 {ty} has invalid padding"))]
19+
Padding {
20+
ty: Bech32Type,
21+
source: PaddingError,
22+
},
23+
#[snafu(display("bech32 {ty} truncated"))]
24+
Truncated { ty: Bech32Type },
25+
#[snafu(display("bech32 {ty} version `{version}` is not supported"))]
26+
UnsupportedVersion {
27+
ty: Bech32Type,
28+
version: bech32::Fe32,
29+
},
30+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use super::*;
22

33
#[derive(Clone, Copy, Debug, PartialEq)]
4-
pub enum Bech32mType {
4+
pub enum Bech32Type {
55
Fingerprint,
66
PrivateKey,
77
PublicKey,
88
Signature,
99
}
1010

11-
impl Bech32mType {
11+
impl Bech32Type {
1212
pub(crate) fn hrp(self) -> &'static Hrp {
1313
static FINGERPRINT: Hrp = Hrp::parse_unchecked("package");
1414
static PRIVATE_KEY: Hrp = Hrp::parse_unchecked("private");
@@ -24,7 +24,7 @@ impl Bech32mType {
2424
}
2525
}
2626

27-
impl Display for Bech32mType {
27+
impl Display for Bech32Type {
2828
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
2929
match self {
3030
Self::Fingerprint => write!(f, "package fingerprint"),

src/bech32m_error.rs

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/display_secret.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub(crate) struct DisplaySecret(pub(crate) PrivateKey);
44

55
impl Display for DisplaySecret {
66
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
7-
let mut encoder = Bech32mEncoder::new(Bech32mType::PrivateKey);
7+
let mut encoder = Bech32Encoder::new(Bech32Type::PrivateKey);
88
encoder.bytes(&self.0.as_secret_bytes());
99
write!(f, "{encoder}")
1010
}

src/error.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,44 @@ pub enum Error {
99
actual: Hash,
1010
expected: Hash,
1111
},
12-
#[snafu(display("failed to decode bech32m `{bech32m}`"))]
13-
Bech32mDecode {
12+
#[snafu(display("failed to decode bech32 `{bech32}`"))]
13+
Bech32Decode {
1414
backtrace: Option<Backtrace>,
15-
bech32m: String,
15+
bech32: String,
1616
source: CheckedHrpstringError,
1717
},
18-
#[snafu(display("failed to encode bech32m"))]
19-
Bech32mEncode {
18+
#[snafu(display("failed to encode bech32"))]
19+
Bech32Encode {
2020
backtrace: Option<Backtrace>,
2121
source: bech32::EncodeError,
2222
},
23-
#[snafu(display("failed to parse bech32m human-readable part"))]
24-
Bech32mHrp {
23+
#[snafu(display("failed to parse bech32 human-readable part"))]
24+
Bech32Hrp {
2525
backtrace: Option<Backtrace>,
2626
source: bech32::primitives::hrp::Error,
2727
},
28-
#[snafu(display("invalid bech32m prefix character `{character}`"))]
29-
Bech32mPrefix {
28+
#[snafu(display("invalid bech32 prefix character `{character}`"))]
29+
Bech32Prefix {
3030
backtrace: Option<Backtrace>,
3131
character: char,
3232
source: bech32::primitives::gf32::FromCharError,
3333
},
34-
#[snafu(display("bech32m prefix missing"))]
35-
Bech32mPrefixMissing { backtrace: Option<Backtrace> },
36-
#[snafu(display("invalid bech32m version character `{version}`"))]
37-
Bech32mVersion {
34+
#[snafu(display("bech32 prefix missing"))]
35+
Bech32PrefixMissing { backtrace: Option<Backtrace> },
36+
#[snafu(display("invalid bech32 version character `{version}`"))]
37+
Bech32Version {
3838
backtrace: Option<Backtrace>,
3939
source: bech32::primitives::gf32::FromCharError,
4040
version: char,
4141
},
42-
#[snafu(display("bech32m version `{actual}` does not match expected `{expected}`"))]
43-
Bech32mVersionMismatch {
42+
#[snafu(display("bech32 version `{actual}` does not match expected `{expected}`"))]
43+
Bech32VersionMismatch {
4444
backtrace: Option<Backtrace>,
4545
actual: bech32::Fe32,
4646
expected: bech32::Fe32,
4747
},
48-
#[snafu(display("bech32m version character missing"))]
49-
Bech32mVersionMissing { backtrace: Option<Backtrace> },
48+
#[snafu(display("bech32 version character missing"))]
49+
Bech32VersionMissing { backtrace: Option<Backtrace> },
5050
#[snafu(display("failed to get current directory"))]
5151
CurrentDir {
5252
backtrace: Option<Backtrace>,
@@ -227,7 +227,7 @@ pub enum Error {
227227
PrivateKeyLoad {
228228
backtrace: Option<Backtrace>,
229229
path: DisplayPath,
230-
source: Bech32mError,
230+
source: Bech32Error,
231231
},
232232
#[snafu(display("private key not found: `{path}`"))]
233233
PrivateKeyNotFound {

src/fingerprint.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ impl Fingerprint {
1414

1515
impl Display for Fingerprint {
1616
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
17-
let mut encoder = Bech32mEncoder::new(Bech32mType::Fingerprint);
17+
let mut encoder = Bech32Encoder::new(Bech32Type::Fingerprint);
1818
encoder.bytes(self.as_bytes());
1919
write!(f, "{encoder}")
2020
}
2121
}
2222

2323
impl FromStr for Fingerprint {
24-
type Err = Bech32mError;
24+
type Err = Bech32Error;
2525

2626
fn from_str(s: &str) -> Result<Self, Self::Err> {
27-
let mut decoder = Bech32mDecoder::new(Bech32mType::Fingerprint, s)?;
27+
let mut decoder = Bech32Decoder::new(Bech32Type::Fingerprint, s)?;
2828
let inner = decoder.byte_array()?;
2929
decoder.done()?;
3030
Ok(Self(inner.into()))

src/lib.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
use {
2020
self::{
2121
arguments::Arguments,
22-
bech32m_decoder::Bech32mDecoder,
23-
bech32m_encoder::Bech32mEncoder,
24-
bech32m_error::Bech32mError,
25-
bech32m_type::Bech32mType,
22+
bech32_decoder::Bech32Decoder,
23+
bech32_encoder::Bech32Encoder,
24+
bech32_error::Bech32Error,
25+
bech32_type::Bech32Type,
2626
component::Component,
2727
component_error::ComponentError,
2828
count::Count,
@@ -134,10 +134,10 @@ macro_rules! assert_matches {
134134
}
135135

136136
mod arguments;
137-
mod bech32m_decoder;
138-
mod bech32m_encoder;
139-
mod bech32m_error;
140-
mod bech32m_type;
137+
mod bech32_decoder;
138+
mod bech32_encoder;
139+
mod bech32_error;
140+
mod bech32_type;
141141
mod component;
142142
mod component_error;
143143
mod count;
@@ -201,7 +201,7 @@ mod ssh;
201201
#[cfg(test)]
202202
mod test;
203203

204-
const BECH32M_VERSION: Fe32 = Fe32::A;
204+
const BECH32_VERSION: Fe32 = Fe32::A;
205205

206206
type Result<T = (), E = Error> = std::result::Result<T, E>;
207207

0 commit comments

Comments
 (0)