Update for nostr-types new PublicKey type

This commit is contained in:
Mike Dilger 2023-08-02 15:21:43 +12:00
parent 915e94f818
commit d4a7104a58
8 changed files with 32 additions and 30 deletions

View File

@ -23,7 +23,7 @@ impl Delegation {
pub fn get_delegator_pubkey(&self) -> Option<PublicKey> {
if let Some(Tag::Delegation { pubkey, .. }) = self.get_delegatee_tag() {
if let Ok(pk) = PublicKey::try_from_hex_string(pubkey.as_str()) {
if let Ok(pk) = PublicKey::try_from_hex_string(pubkey.as_str(), true) {
return Some(pk);
}
}

View File

@ -347,7 +347,7 @@ impl Feed {
let events: Vec<(Unixtime, Id)> = GLOBALS
.storage
.find_events(
&kinds, // feed kinds
&kinds, // feed kinds
&[], // any person (due to delegation condition) // FIXME GINA
Some(one_month_ago), // one year ago
|e| {

View File

@ -50,7 +50,7 @@ pub async fn validate_nip05(person: Person) -> Result<(), Error> {
let mut valid = false;
match nip05file.names.get(&user) {
Some(pk) => {
if let Ok(pubkey) = PublicKey::try_from_hex_string(pk) {
if let Ok(pubkey) = PublicKey::try_from_hex_string(pk, true) {
if pubkey == person.pubkey {
// Validated
GLOBALS
@ -100,7 +100,7 @@ pub async fn get_and_follow_nip05(nip05: String) -> Result<(), Error> {
// Get their pubkey
let pubkey = match nip05file.names.get(&user) {
Some(pk) => PublicKey::try_from_hex_string(pk)?,
Some(pk) => PublicKey::try_from_hex_string(pk, true)?,
None => return Err((ErrorKind::Nip05KeyNotFound, file!(), line!()).into()),
};

View File

@ -540,7 +540,7 @@ impl Overlord {
}));
}
ToOverlordMessage::FollowNprofile(nprofile) => {
match Profile::try_from_bech32_string(&nprofile) {
match Profile::try_from_bech32_string(&nprofile, true) {
Ok(np) => self.follow_nprofile(np).await?,
Err(e) => GLOBALS.status_queue.write().write(format!("{}", e)),
}
@ -582,8 +582,8 @@ impl Overlord {
}
}
ToOverlordMessage::ImportPub(pubstr) => {
let maybe_pk1 = PublicKey::try_from_bech32_string(&pubstr);
let maybe_pk2 = PublicKey::try_from_hex_string(&pubstr);
let maybe_pk1 = PublicKey::try_from_bech32_string(&pubstr, true);
let maybe_pk2 = PublicKey::try_from_hex_string(&pubstr, true);
if maybe_pk1.is_err() && maybe_pk2.is_err() {
GLOBALS
.status_queue
@ -835,9 +835,9 @@ impl Overlord {
pubkeystr: String,
relay: RelayUrl,
) -> Result<(), Error> {
let pubkey = match PublicKey::try_from_bech32_string(&pubkeystr) {
let pubkey = match PublicKey::try_from_bech32_string(&pubkeystr, true) {
Ok(pk) => pk,
Err(_) => PublicKey::try_from_hex_string(&pubkeystr)?,
Err(_) => PublicKey::try_from_hex_string(&pubkeystr, true)?,
};
GLOBALS.people.follow(&pubkey, true)?;
@ -1003,7 +1003,7 @@ impl Overlord {
.iter()
.filter_map(|t| {
if let Tag::Pubkey { pubkey, .. } = t {
match PublicKey::try_from_hex_string(pubkey) {
match PublicKey::try_from_hex_string(pubkey, true) {
Ok(pk) => Some(pk),
_ => None,
}
@ -1782,7 +1782,7 @@ impl Overlord {
..
} = tag
{
if let Ok(pubkey) = PublicKey::try_from_hex_string(pubkey) {
if let Ok(pubkey) = PublicKey::try_from_hex_string(pubkey, true) {
// Make sure we have that person
GLOBALS.people.create_all_if_missing(&[pubkey.to_owned()])?;
@ -1847,7 +1847,7 @@ impl Overlord {
// If npub, convert to hex so we can find it in the database
// (This will only work with full npubs)
let mut pubkeytext = text.clone();
if let Ok(pk) = PublicKey::try_from_bech32_string(&text) {
if let Ok(pk) = PublicKey::try_from_bech32_string(&text, true) {
pubkeytext = pk.as_hex_string();
}

View File

@ -112,7 +112,7 @@ pub async fn process_new_event(
recommended_relay_url: Some(should_be_url),
..
} => {
if let Ok(pubkey) = PublicKey::try_from_hex_string(pubkey) {
if let Ok(pubkey) = PublicKey::try_from_hex_string(pubkey, true) {
if let Ok(url) = RelayUrl::try_from_unchecked_url(should_be_url) {
GLOBALS.storage.write_relay_if_missing(&url)?;

View File

@ -158,7 +158,7 @@ where
}
}
"public_key" => {
settings.public_key = match PublicKey::try_from_hex_string(&value) {
settings.public_key = match PublicKey::try_from_hex_string(&value, false) {
Ok(pk) => Some(pk),
Err(e) => {
tracing::error!("Public key in database is invalid or corrupt: {}", e);
@ -373,7 +373,7 @@ where
};
let pk: String = row.get(0)?;
let person = Person {
pubkey: match PublicKey::try_from_hex_string(&pk) {
pubkey: match PublicKey::try_from_hex_string(&pk, false) {
Ok(pk) => pk,
Err(e) => {
tracing::error!("{}", e);
@ -410,7 +410,7 @@ where
let mut rows = stmt.raw_query();
while let Some(row) = rows.next()? {
let pkstr: String = row.get(0)?;
let pubkey = match PublicKey::try_from_hex_string(&pkstr) {
let pubkey = match PublicKey::try_from_hex_string(&pkstr, false) {
Ok(pk) => pk,
Err(e) => {
tracing::error!("{}", e);

View File

@ -879,10 +879,11 @@ impl Storage {
}
// Break if we moved to a different public key
let this_pubkey = match PublicKey::from_bytes(&key[4..4 + 32]) {
Err(_) => continue,
Ok(pk) => pk,
};
let this_pubkey =
match PublicKey::from_bytes(&key[4..4 + 32], false) {
Err(_) => continue,
Ok(pk) => pk,
};
if this_pubkey != *pubkey {
continue 'pubkeyloop;
}
@ -1114,7 +1115,7 @@ impl Storage {
let mut pubkeys: HashSet<PublicKey> = HashSet::new();
for (pubkeyhex, _, _) in event.people() {
let pubkey = match PublicKey::try_from_hex_string(pubkeyhex.as_str()) {
let pubkey = match PublicKey::try_from_hex_string(pubkeyhex.as_str(), false) {
Ok(pk) => pk,
Err(_) => continue,
};
@ -1126,7 +1127,7 @@ impl Storage {
if !pubkeys.is_empty() {
let mut txn = self.env.begin_rw_txn()?;
for pubkey in pubkeys.drain() {
let mut key: Vec<u8> = pubkey.as_bytes();
let mut key: Vec<u8> = pubkey.to_bytes();
key.extend((i64::MAX - event.created_at.0).to_be_bytes().as_slice()); // reverse created_at
txn.put(
self.event_references_person,
@ -1153,7 +1154,7 @@ impl Storage {
let txn = self.env.begin_ro_txn()?;
let mut cursor = txn.open_ro_cursor(self.event_references_person)?;
let now = Unixtime::now().unwrap();
let mut start_key: Vec<u8> = pubkey.as_bytes();
let mut start_key: Vec<u8> = pubkey.to_bytes();
start_key.extend((i64::MAX - now.0).to_be_bytes().as_slice()); // work back from now
let iter = cursor.iter_from(start_key);
let mut events: Vec<Event> = Vec::new();
@ -1162,7 +1163,7 @@ impl Storage {
Err(e) => return Err(e.into()),
Ok((key, val)) => {
// Break if we moved to a different pubkey
let this_pubkey = match PublicKey::from_bytes(&key[..32]) {
let this_pubkey = match PublicKey::from_bytes(&key[..32], false) {
Err(_) => continue,
Ok(pk) => pk,
};
@ -1427,7 +1428,7 @@ impl Storage {
// Note that we use serde instead of speedy because the complexity of the
// serde_json::Value type makes it difficult. Any other serde serialization
// should work though: Consider bincode.
let key: Vec<u8> = person.pubkey.as_bytes();
let key: Vec<u8> = person.pubkey.to_bytes();
let bytes = serde_json::to_vec(person)?;
let mut txn = self.env.begin_rw_txn()?;
txn.put(self.people, &key, &bytes, WriteFlags::empty())?;
@ -1439,7 +1440,7 @@ impl Storage {
// Note that we use serde instead of speedy because the complexity of the
// serde_json::Value type makes it difficult. Any other serde serialization
// should work though: Consider bincode.
let key: Vec<u8> = pubkey.as_bytes();
let key: Vec<u8> = pubkey.to_bytes();
let txn = self.env.begin_ro_txn()?;
match txn.get(self.people, &key) {
Ok(bytes) => Ok(Some(serde_json::from_slice(bytes)?)),
@ -1479,7 +1480,7 @@ impl Storage {
}
pub fn write_person_relay(&self, person_relay: &PersonRelay) -> Result<(), Error> {
let mut key = person_relay.pubkey.as_bytes();
let mut key = person_relay.pubkey.to_bytes();
key.extend(person_relay.url.0.as_bytes());
key.truncate(MAX_LMDB_KEY);
let bytes = person_relay.write_to_vec()?;
@ -1494,7 +1495,7 @@ impl Storage {
pubkey: PublicKey,
url: &RelayUrl,
) -> Result<Option<PersonRelay>, Error> {
let mut key = pubkey.as_bytes();
let mut key = pubkey.to_bytes();
key.extend(url.0.as_bytes());
key.truncate(MAX_LMDB_KEY);
let txn = self.env.begin_ro_txn()?;
@ -1506,7 +1507,7 @@ impl Storage {
}
pub fn get_person_relays(&self, pubkey: PublicKey) -> Result<Vec<PersonRelay>, Error> {
let start_key = pubkey.as_bytes();
let start_key = pubkey.to_bytes();
let txn = self.env.begin_ro_txn()?;
let mut cursor = txn.open_ro_cursor(self.person_relays)?;
let iter = cursor.iter_from(start_key.clone());

View File

@ -109,7 +109,8 @@ pub(super) fn render_content(
if let Some(tag) = note.event.tags.get(*num) {
match tag {
Tag::Pubkey { pubkey, .. } => {
if let Ok(pubkey) = PublicKey::try_from_hex_string(pubkey.as_str())
if let Ok(pubkey) =
PublicKey::try_from_hex_string(pubkey.as_str(), false)
{
render_profile_link(app, ui, &pubkey);
}