Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/bech32m.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use super::*;

const VERSION: Fe32 = Fe32::A;

pub(crate) trait Bech32m<const PREFIX: usize, const DATA: usize> {
pub(crate) trait Bech32m<const PREFIX: usize, const BODY: usize> {
const HRP: Hrp;
const TYPE: &'static str;

type Suffix: Bech32mSuffix;

fn decode_bech32m(s: &str) -> Result<Bech32mPayload<PREFIX, DATA, Self::Suffix>, Bech32mError> {
fn decode_bech32m(s: &str) -> Result<Bech32mPayload<PREFIX, BODY, Self::Suffix>, Bech32mError> {
let hrp_string = CheckedHrpstring::new::<bech32::Bech32m>(s)
.context(bech32m_error::Decode { ty: Self::TYPE })?;

Expand Down Expand Up @@ -40,14 +40,14 @@ pub(crate) trait Bech32m<const PREFIX: usize, const DATA: usize> {
})?;
}

let mut data = [0; DATA];
let mut body = [0; BODY];

let mut bytes = fe32s.fes_to_bytes();

for (actual, byte) in data.iter_mut().enumerate() {
*byte = bytes.next().context(bech32m_error::DataLength {
for (actual, byte) in body.iter_mut().enumerate() {
*byte = bytes.next().context(bech32m_error::BodyLength {
actual,
expected: DATA,
expected: BODY,
ty: Self::TYPE,
})?;
}
Expand All @@ -57,26 +57,26 @@ pub(crate) trait Bech32m<const PREFIX: usize, const DATA: usize> {
Self::validate_padding(&hrp_string).context(bech32m_error::Padding { ty: Self::TYPE })?;

Ok(Bech32mPayload {
data,
body,
prefix,
suffix,
})
}

fn encode_bech32m(
f: &mut Formatter,
payload: Bech32mPayload<PREFIX, DATA, Self::Suffix>,
payload: Bech32mPayload<PREFIX, BODY, Self::Suffix>,
) -> fmt::Result {
let Bech32mPayload {
data,
body,
prefix,
suffix,
} = payload;

let chars = prefix
.into_iter()
.chain(
data
body
.iter()
.copied()
.chain(suffix.into_bytes())
Expand Down Expand Up @@ -134,7 +134,7 @@ mod tests {

impl Display for EmptyPublicKey {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Self::encode_bech32m(f, Bech32mPayload::from_data([]))
Self::encode_bech32m(f, Bech32mPayload::from_body([]))
}
}

Expand All @@ -148,13 +148,13 @@ mod tests {

impl Display for LongPublicKey {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Self::encode_bech32m(f, Bech32mPayload::from_data([0; 33]))
Self::encode_bech32m(f, Bech32mPayload::from_body([0; 33]))
}
}

#[test]
fn implementations() {
fn case<const PREFIX: usize, const DATA: usize, T: Bech32m<PREFIX, DATA>>(hrp: &str, ty: &str) {
fn case<const PREFIX: usize, const BODY: usize, T: Bech32m<PREFIX, BODY>>(hrp: &str, ty: &str) {
use bech32::Checksum;

let max = (bech32::Bech32m::CODE_LENGTH
Expand All @@ -166,7 +166,7 @@ mod tests {
* 5
/ 8;

assert!(DATA <= max);
assert!(BODY <= max);

assert_eq!(T::HRP.as_str(), hrp);

Expand Down Expand Up @@ -198,7 +198,7 @@ mod tests {

case(
&EmptyPublicKey.to_string(),
"expected bech32m public key to have 32 data bytes but found 0",
"expected bech32m public key to have 32 body bytes but found 0",
);

case(
Expand Down
4 changes: 2 additions & 2 deletions src/bech32m_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use super::*;
pub enum Bech32mError {
#[snafu(display(
"expected bech32m {ty} to have {} but found {actual}",
Count(*expected, "data byte"),
Count(*expected, "body byte"),
))]
DataLength {
BodyLength {
expected: usize,
actual: usize,
ty: &'static str,
Expand Down
14 changes: 7 additions & 7 deletions src/bech32m_payload.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use super::*;

#[derive(Debug)]
pub(crate) struct Bech32mPayload<const PREFIX: usize, const DATA: usize, T> {
pub(crate) data: [u8; DATA],
pub(crate) struct Bech32mPayload<const PREFIX: usize, const BODY: usize, T> {
pub(crate) body: [u8; BODY],
pub(crate) prefix: [Fe32; PREFIX],
pub(crate) suffix: T,
}

impl<const DATA: usize> Bech32mPayload<0, DATA, ()> {
pub(crate) fn from_data(data: [u8; DATA]) -> Self {
impl<const BODY: usize> Bech32mPayload<0, BODY, ()> {
pub(crate) fn from_body(body: [u8; BODY]) -> Self {
Self {
data,
body,
prefix: [],
suffix: (),
}
}

pub(crate) fn into_data(self) -> [u8; DATA] {
self.data
pub(crate) fn into_body(self) -> [u8; BODY] {
self.body
}
}
2 changes: 1 addition & 1 deletion src/display_secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ pub(crate) struct DisplaySecret(pub(crate) PrivateKey);

impl Display for DisplaySecret {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
PrivateKey::encode_bech32m(f, Bech32mPayload::from_data(self.0.as_secret_bytes()))
PrivateKey::encode_bech32m(f, Bech32mPayload::from_body(self.0.as_secret_bytes()))
}
}
4 changes: 2 additions & 2 deletions src/fingerprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ impl Bech32m<0, { Fingerprint::LEN }> for Fingerprint {

impl Display for Fingerprint {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Self::encode_bech32m(f, Bech32mPayload::from_data(*self.as_bytes()))
Self::encode_bech32m(f, Bech32mPayload::from_body(*self.as_bytes()))
}
}

impl FromStr for Fingerprint {
type Err = Bech32mError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(Self::decode_bech32m(s)?.into_data().into()))
Ok(Self(Self::decode_bech32m(s)?.into_body().into()))
}
}
2 changes: 1 addition & 1 deletion src/private_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl FromStr for PrivateKey {
type Err = Bech32mError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let inner = ed25519_dalek::SigningKey::from_bytes(&Self::decode_bech32m(s)?.into_data());
let inner = ed25519_dalek::SigningKey::from_bytes(&Self::decode_bech32m(s)?.into_body());
assert!(!inner.verifying_key().is_weak());
Ok(Self(inner))
}
Expand Down
4 changes: 2 additions & 2 deletions src/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl FromStr for PublicKey {
type Err = PublicKeyError;

fn from_str(key: &str) -> Result<Self, Self::Err> {
let data = Self::decode_bech32m(key)?.into_data();
let data = Self::decode_bech32m(key)?.into_body();

let inner = ed25519_dalek::VerifyingKey::from_bytes(&data)
.map_err(DalekSignatureError)
Expand All @@ -66,7 +66,7 @@ impl FromStr for PublicKey {

impl Display for PublicKey {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Self::encode_bech32m(f, Bech32mPayload::from_data(*self.0.as_bytes()))
Self::encode_bech32m(f, Bech32mPayload::from_body(*self.0.as_bytes()))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ impl FromStr for Signature {
fn from_str(signature: &str) -> Result<Self, Self::Err> {
let Bech32mPayload {
prefix: [scheme, version],
data,
body,
suffix,
} = Self::decode_bech32m(signature)?;

Ok(Self {
inner: ed25519_dalek::Signature::from_bytes(&data),
inner: ed25519_dalek::Signature::from_bytes(&body),
scheme: SignatureScheme::new(scheme, version, suffix)?,
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/signature_scheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ impl SignatureScheme {
};

Bech32mPayload {
body: signature.to_bytes(),
prefix,
data: signature.to_bytes(),
suffix: suffix.into(),
}
}
Expand Down