Skip to content

Commit

Permalink
use backwards compatible account address format for OS keyring
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed Dec 5, 2023
1 parent 9ab748a commit cdb5b9a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src-tauri/src/commands/wallets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub async fn init_from_private_key(pri_key_string: String) -> Result<CarpeProfil
.await
.unwrap_or(acc_struct.account); // the account may not have been created on chain. If we can't get the address, we'll just use the one we derived from the private key

key_manager::set_private_key(&address.to_string(), acc_struct.pri_key)
key_manager::set_private_key(&address, acc_struct.pri_key)
.map_err(|e| CarpeError::config(&e.to_string()))?;

configs_profile::set_account_profile(address, authkey).await?;
Expand Down
29 changes: 23 additions & 6 deletions src-tauri/src/key_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ use crate::carpe_error::{CarpeError, ErrorCat, E_KEY_NOT_REGISTERED};

const KEYRING_APP_NAME: &str = "carpe";

fn keyring_addr_string_format(address: &AccountAddress) -> String {
address.short_str_lossless().to_uppercase().to_string()
}

/// overwrite then delete
pub fn erase_keyring_address(address: AccountAddress) -> anyhow::Result<()> {
let addr_str = &address.to_string();
let kr = keyring::Entry::new(KEYRING_APP_NAME, addr_str);
pub fn erase_keyring_address(address: &AccountAddress) -> anyhow::Result<()> {
let ol_address: String = keyring_addr_string_format(address);
let kr = keyring::Entry::new(KEYRING_APP_NAME, &ol_address);

let bytes = &[0u8, 64];
let encoded = hex::encode(bytes);
Expand All @@ -27,8 +31,9 @@ pub fn erase_keyring_address(address: AccountAddress) -> anyhow::Result<()> {
Ok(())
}
/// send the encoded private key to OS keyring
pub fn set_private_key(ol_address: &str, key: Ed25519PrivateKey) -> Result<(), keyring::Error> {
let kr = keyring::Entry::new(KEYRING_APP_NAME, ol_address);
pub fn set_private_key(address: &AccountAddress, key: Ed25519PrivateKey) -> Result<(), keyring::Error> {
let ol_address: String = keyring_addr_string_format(address);
let kr = keyring::Entry::new(KEYRING_APP_NAME, &ol_address);

let bytes: &[u8] = &(key.to_bytes());
let encoded = hex::encode(bytes);
Expand All @@ -38,7 +43,7 @@ pub fn set_private_key(ol_address: &str, key: Ed25519PrivateKey) -> Result<(), k

/// retrieve a private key from OS keyring
pub fn get_private_key(address: &AccountAddress) -> Result<Ed25519PrivateKey, anyhow::Error> {
let ol_address = address.to_string();
let ol_address: String = keyring_addr_string_format(address);
let kr = keyring::Entry::new(KEYRING_APP_NAME, &ol_address);
match kr.get_password() {
Ok(s) => {
Expand Down Expand Up @@ -84,3 +89,15 @@ pub fn inject_private_key_to_cfg(app_cfg_mut: &mut AppCfg) -> anyhow::Result<(),
// NOTE: intentionally not saving profile
Ok(())
}


#[test]
fn test_keyring_addr_format() {
use std::str::FromStr;

let addr = AccountAddress::from_str("0x4c613c2f4b1e67ca8d98a542ee3f59f5").unwrap();

let s = keyring_addr_string_format(&addr);
assert!("4C613C2F4B1E67CA8D98A542EE3F59F5" == s);

}
1 change: 0 additions & 1 deletion src/components/txs/Transactions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import type { CarpeProfile } from '../../modules/accounts'
import { raise_error } from '../../modules/carpeError'
import CantStart from '../miner/cards/CantStart.svelte'
import UIkit from 'uikit'
const errorDic = {
'120127': $_('txs.transfer.error_slow_wallet'),
Expand Down

0 comments on commit cdb5b9a

Please sign in to comment.