mirror of
https://github.com/aljazceru/notedeck.git
synced 2026-01-17 07:14:20 +01:00
user can upgrade their npub -> nsec
closes https://github.com/damus-io/notedeck/issues/476 Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
@@ -138,27 +138,45 @@ impl Accounts {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn has_account_pubkey(&self, pubkey: &[u8; 32]) -> bool {
|
fn contains_account(&self, pubkey: &[u8; 32]) -> Option<ContainsAccount> {
|
||||||
for account in &self.accounts {
|
for (index, account) in self.accounts.iter().enumerate() {
|
||||||
if account.pubkey.bytes() == pubkey {
|
let has_pubkey = account.pubkey.bytes() == pubkey;
|
||||||
return true;
|
let has_nsec = account.secret_key.is_some();
|
||||||
|
if has_pubkey {
|
||||||
|
return Some(ContainsAccount { has_nsec, index });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
false
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use = "UnknownIdAction's must be handled. Use .process_unknown_id_action()"]
|
#[must_use = "UnknownIdAction's must be handled. Use .process_unknown_id_action()"]
|
||||||
pub fn add_account(&mut self, account: Keypair) -> SingleUnkIdAction {
|
pub fn add_account(&mut self, account: Keypair) -> LoginAction {
|
||||||
if self.has_account_pubkey(account.pubkey.bytes()) {
|
let pubkey = account.pubkey;
|
||||||
info!("already have account, not adding {}", account.pubkey);
|
let switch_to_index = if let Some(contains_acc) = self.contains_account(pubkey.bytes()) {
|
||||||
return SingleUnkIdAction::pubkey(account.pubkey);
|
if account.secret_key.is_some() && !contains_acc.has_nsec {
|
||||||
}
|
info!(
|
||||||
|
"user provided nsec, but we already have npub {}. Upgrading to nsec",
|
||||||
|
pubkey
|
||||||
|
);
|
||||||
|
let _ = self.key_store.add_key(&account);
|
||||||
|
|
||||||
let _ = self.key_store.add_key(&account);
|
self.accounts[contains_acc.index] = account;
|
||||||
let pk = account.pubkey;
|
} else {
|
||||||
self.accounts.push(account);
|
info!("already have account, not adding {}", pubkey);
|
||||||
SingleUnkIdAction::pubkey(pk)
|
}
|
||||||
|
contains_acc.index
|
||||||
|
} else {
|
||||||
|
info!("adding new account {}", pubkey);
|
||||||
|
let _ = self.key_store.add_key(&account);
|
||||||
|
self.accounts.push(account);
|
||||||
|
self.accounts.len() - 1
|
||||||
|
};
|
||||||
|
|
||||||
|
LoginAction {
|
||||||
|
unk: SingleUnkIdAction::pubkey(pubkey),
|
||||||
|
switch_to_index,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn num_accounts(&self) -> usize {
|
pub fn num_accounts(&self) -> usize {
|
||||||
@@ -217,12 +235,23 @@ pub fn process_login_view_response(
|
|||||||
manager: &mut Accounts,
|
manager: &mut Accounts,
|
||||||
response: AccountLoginResponse,
|
response: AccountLoginResponse,
|
||||||
) -> SingleUnkIdAction {
|
) -> SingleUnkIdAction {
|
||||||
let r = match response {
|
let login_action = match response {
|
||||||
AccountLoginResponse::CreateNew => {
|
AccountLoginResponse::CreateNew => {
|
||||||
manager.add_account(FullKeypair::generate().to_keypair())
|
manager.add_account(FullKeypair::generate().to_keypair())
|
||||||
}
|
}
|
||||||
AccountLoginResponse::LoginWith(keypair) => manager.add_account(keypair),
|
AccountLoginResponse::LoginWith(keypair) => manager.add_account(keypair),
|
||||||
};
|
};
|
||||||
manager.select_account(manager.num_accounts() - 1);
|
manager.select_account(login_action.switch_to_index);
|
||||||
r
|
login_action.unk
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct LoginAction {
|
||||||
|
pub unk: SingleUnkIdAction,
|
||||||
|
pub switch_to_index: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct ContainsAccount {
|
||||||
|
pub has_nsec: bool,
|
||||||
|
pub index: usize,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -429,6 +429,7 @@ impl Damus {
|
|||||||
info!("adding account: {}", key.pubkey);
|
info!("adding account: {}", key.pubkey);
|
||||||
accounts
|
accounts
|
||||||
.add_account(key)
|
.add_account(key)
|
||||||
|
.unk
|
||||||
.process_action(&mut unknown_ids, &ndb, &txn);
|
.process_action(&mut unknown_ids, &ndb, &txn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -102,9 +102,11 @@ pub fn test_app() -> Damus {
|
|||||||
let accounts = get_test_accounts();
|
let accounts = get_test_accounts();
|
||||||
let txn = Transaction::new(&app.ndb).expect("txn");
|
let txn = Transaction::new(&app.ndb).expect("txn");
|
||||||
for account in accounts {
|
for account in accounts {
|
||||||
app.accounts_mut()
|
app.accounts_mut().add_account(account).unk.process_action(
|
||||||
.add_account(account)
|
&mut app.unknown_ids,
|
||||||
.process_action(&mut app.unknown_ids, &app.ndb, &txn)
|
&app.ndb,
|
||||||
|
&txn,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
app
|
app
|
||||||
|
|||||||
Reference in New Issue
Block a user