rust-nostr / nostr

Nostr protocol implementation, SDK and FFI
https://rust-nostr.org/
MIT License
416 stars 90 forks source link

Question: how to distinguish channel ids from pub keys of individuals? #2

Closed 8go closed 1 year ago

8go commented 1 year ago

"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245" is a pub key of an individual.

"25e5c82273a271cb1a840d0060391a0bf4965cafeb029d5ab55350b418953fbb" is a pub key of a Public Channel. Some call it a channel id.

Question: given some string like "25e5c82273a271cb1a840d0060391a0bf4965cafeb029d5ab55350b418953fbb" how can I determine programmatically if it is a public channel or a person?

I know that the strings can be distinguished because anigma.io distinguishes them, anigma.io only let's you subscribe to public channels but rejects pub keys of individuals. How can I do it?

Would that be an interesting function to add to nostr-sdk as a feature enhancement?

yukibtc commented 1 year ago

I just added a new method to Client called get_entity_of_pubkey.

let pubkey = XOnlyPublicKey::from_str("...")?;
let entity: nostr_sdk::Entity = client.get_entity_of_pubkey(pubkey).await?;
println!("Entity: {:?}", entity);

The possible results are: Account, Channel or Unknown

8go commented 1 year ago

Thank you @yukibtc for implementing this so quickly. :clap: :clap: :clap:

It is now also available to the "end" user in https://github.com/8go/nostr-commander-rs/commit/04add1e740b53d5354cd3023a38758b52d3d301f

$ nostr-commander --get-pubkey-entity 25e5c82273a271cb1a840d0060391a0bf4965cafeb029d5ab55350b418953fbb
{"entity":"Channel","hex":"25e5c82273a271cb1a840d0060391a0bf4965cafeb029d5ab55350b418953fbb"}