Skip to content

Commit

Permalink
nostr: remove generics from parsing methods in key module
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Nov 30, 2024
1 parent 370247a commit 4340821
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

* nostr: update `FromBech32::from_bech32` method signature ([Yuki Kishimoto])
* nostr: update `NostrURI::from_nostr_uri` method signature ([Yuki Kishimoto])
* nostr: remove generics from parsing methods in `key` module ([Yuki Kishimoto])

### Changed

Expand Down
2 changes: 1 addition & 1 deletion crates/nostr-cli/src/cli/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ where
S: Into<String>,
{
let secret_key = Password::new().with_prompt(prompt).interact()?;
Ok(Keys::parse(secret_key)?)
Ok(Keys::parse(&secret_key)?)
}

/* pub fn get_password_with_confirmation() -> Result<String> {
Expand Down
2 changes: 1 addition & 1 deletion crates/nostr/src/event/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl TryFrom<RawEvent> for Event {

fn try_from(raw: RawEvent) -> Result<Self, Self::Error> {
let id: EventId = EventId::from_hex(raw.id)?;
let public_key: PublicKey = PublicKey::from_hex(raw.pubkey)?;
let public_key: PublicKey = PublicKey::from_hex(&raw.pubkey)?;
let created_at: Timestamp = Timestamp::from(raw.created_at);
let kind: Kind = Kind::from(raw.kind);
let tags: Vec<Tag> = raw
Expand Down
8 changes: 2 additions & 6 deletions crates/nostr/src/key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,15 @@ impl Keys {
/// Parse secret key from `hex` or `bech32` and compose keys
#[inline]
#[cfg(feature = "std")]
pub fn parse<S>(secret_key: S) -> Result<Self, Error>
where
S: AsRef<str>,
{
pub fn parse(secret_key: &str) -> Result<Self, Error> {
Self::parse_with_ctx(&SECP256K1, secret_key)
}

/// Parse secret key from `hex` or `bech32` and compose keys
#[inline]
pub fn parse_with_ctx<C, S>(secp: &Secp256k1<C>, secret_key: S) -> Result<Self, Error>
pub fn parse_with_ctx<C>(secp: &Secp256k1<C>, secret_key: &str) -> Result<Self, Error>
where
C: Signing,
S: AsRef<str>,
{
let secret_key: SecretKey = SecretKey::parse(secret_key)?;
Ok(Self::new_with_ctx(secp, secret_key))
Expand Down
20 changes: 6 additions & 14 deletions crates/nostr/src/key/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,7 @@ impl PublicKey {
pub const LEN: usize = 32;

/// Parse from `hex`, `bech32` or [NIP21](https://github.com/nostr-protocol/nips/blob/master/21.md) uri
pub fn parse<S>(public_key: S) -> Result<Self, Error>
where
S: AsRef<str>,
{
let public_key: &str = public_key.as_ref();

pub fn parse(public_key: &str) -> Result<Self, Error> {
// Try from hex
if let Ok(public_key) = Self::from_hex(public_key) {
return Ok(public_key);
Expand All @@ -77,22 +72,19 @@ impl PublicKey {
Err(Error::InvalidPublicKey)
}

/// Parse [PublicKey] from `bytes`
/// Parse from `bytes`
#[inline]
pub fn from_slice(slice: &[u8]) -> Result<Self, Error> {
Ok(Self {
inner: XOnlyPublicKey::from_slice(slice)?,
})
}

/// Parse [PublicKey] from `hex` string
/// Parse from `hex` string
#[inline]
pub fn from_hex<S>(hex: S) -> Result<Self, Error>
where
S: AsRef<str>,
{
pub fn from_hex(hex: &str) -> Result<Self, Error> {
Ok(Self {
inner: XOnlyPublicKey::from_str(hex.as_ref())?,
inner: XOnlyPublicKey::from_str(hex)?,
})
}

Expand Down Expand Up @@ -141,7 +133,7 @@ impl<'de> Deserialize<'de> for PublicKey {
D: Deserializer<'de>,
{
let public_key: String = String::deserialize(deserializer)?;
Self::parse(public_key).map_err(serde::de::Error::custom)
Self::parse(&public_key).map_err(serde::de::Error::custom)
}
}

Expand Down
16 changes: 4 additions & 12 deletions crates/nostr/src/key/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,7 @@ impl SecretKey {
pub const LEN: usize = 32;

/// Parse from `hex` or `bech32`
pub fn parse<S>(secret_key: S) -> Result<Self, Error>
where
S: AsRef<str>,
{
let secret_key: &str = secret_key.as_ref();

pub fn parse(secret_key: &str) -> Result<Self, Error> {
// Try from hex
if let Ok(secret_key) = Self::from_hex(secret_key) {
return Ok(secret_key);
Expand All @@ -82,12 +77,9 @@ impl SecretKey {

/// Parse [SecretKey] from `hex` string
#[inline]
pub fn from_hex<S>(hex: S) -> Result<Self, Error>
where
S: AsRef<str>,
{
pub fn from_hex(hex: &str) -> Result<Self, Error> {
Ok(Self {
inner: secp256k1::SecretKey::from_str(hex.as_ref())?,
inner: secp256k1::SecretKey::from_str(hex)?,
})
}

Expand Down Expand Up @@ -167,7 +159,7 @@ impl<'de> Deserialize<'de> for SecretKey {
D: Deserializer<'de>,
{
let secret_key: String = String::deserialize(deserializer)?;
Self::parse(secret_key).map_err(serde::de::Error::custom)
Self::parse(&secret_key).map_err(serde::de::Error::custom)
}
}

Expand Down

0 comments on commit 4340821

Please sign in to comment.