Implement --list and --list-all commands

Requires a MSRV of 1.44 due to the transitive dependency on bitvec 0.19.
This commit is contained in:
Jack Grigg
2021-01-01 03:23:37 +00:00
parent 3ee6d59dcd
commit babe64da42
7 changed files with 890 additions and 12 deletions
+46
View File
@@ -0,0 +1,46 @@
use bech32::ToBase32;
use elliptic_curve::sec1::EncodedPoint;
use p256::NistP256;
use std::fmt;
use crate::RECIPIENT_PREFIX;
/// Wrapper around a compressed secp256r1 curve point.
#[derive(Clone)]
pub struct Recipient(EncodedPoint<NistP256>);
impl fmt::Debug for Recipient {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Recipient({:?})", self.as_bytes())
}
}
impl fmt::Display for Recipient {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(
bech32::encode(RECIPIENT_PREFIX, self.as_bytes().to_base32())
.expect("HRP is valid")
.as_str(),
)
}
}
impl Recipient {
/// Attempts to parse a valid secp256r1 public key from its SEC-1 encoding.
pub(crate) fn from_pubkey(pubkey: EncodedPoint<NistP256>) -> Option<Self> {
if pubkey.is_compressed() {
if pubkey.decompress().is_some().into() {
Some(Recipient(pubkey))
} else {
None
}
} else {
Some(Recipient(pubkey.compress()))
}
}
/// Returns the compressed SEC-1 encoding of this public key.
pub(crate) fn as_bytes(&self) -> &[u8] {
self.0.as_bytes()
}
}