Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sender error after account switch #297

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src-tauri/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub(crate) mod app_version;
pub(crate) mod mining;
// pub(crate) mod mining;
pub(crate) mod networks;
pub(crate) mod preferences;
pub(crate) mod query;
Expand Down
4 changes: 2 additions & 2 deletions src-tauri/src/commands/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pub async fn coin_transfer(
};

let mut config = get_cfg()?;
inject_private_key_to_cfg(&mut config)?;
let mut sender = Sender::from_app_cfg(&config, None, legacy).await?;
inject_private_key_to_cfg(&mut config, _sender)?;
let mut sender = Sender::from_app_cfg(&config, Some(_sender.to_string()), legacy).await?;
sender
.transfer(receiver_account, amount as f64, false)
.await?;
Expand Down
8 changes: 4 additions & 4 deletions src-tauri/src/commands/wallets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ pub async fn get_originating_address(
}
}

/// Switch tx profiles, change 0L.toml to use selected account
/// Switch tx profiles, change libra.yaml to use selected account
#[tauri::command(async)]
// IMPORTANT: don't return the profile, since it has keys
pub async fn switch_profile(account: AccountAddress) -> Result<CarpeProfile, CarpeError> {
Expand Down Expand Up @@ -266,11 +266,11 @@ async fn test_fetch_originating() {
}

#[tauri::command(async)]
pub async fn set_slow_wallet(legacy: bool) -> Result<(), CarpeError> {
pub async fn set_slow_wallet(legacy: bool, _sender: AccountAddress) -> Result<(), CarpeError> {
// NOTE: unsure Serde was catching all cases check serialization
let mut config = get_cfg()?;
inject_private_key_to_cfg(&mut config)?;
let mut sender = Sender::from_app_cfg(&config, None, legacy).await?;
inject_private_key_to_cfg(&mut config, _sender)?;
let mut sender = Sender::from_app_cfg(&config, Some(_sender.to_string()), legacy).await?;

let t = SetSlowTx {};
t.run(&mut sender).await?;
Expand Down
7 changes: 5 additions & 2 deletions src-tauri/src/key_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,12 @@ pub fn get_keypair(
/// insert the public key into the AppCfg temporarily so that we don't need
/// to prompt user for mnemonic.
// NOTE to future devs: DANGER: make sure this is never called in a flow that uses save_file(). The upstream prevents the key from serializing, but it should be guarded here as well.
pub fn inject_private_key_to_cfg(app_cfg_mut: &mut AppCfg) -> anyhow::Result<(), CarpeError> {
pub fn inject_private_key_to_cfg(
app_cfg_mut: &mut AppCfg,
account: AccountAddress,
) -> anyhow::Result<(), CarpeError> {
// gets the default profile
let profile = app_cfg_mut.get_profile_mut(None)?;
let profile = app_cfg_mut.get_profile_mut(Some(account.to_string()))?;
let pri_key = get_private_key(&profile.account).map_err(|e| CarpeError {
category: ErrorCat::Configs,
uid: E_KEY_NOT_REGISTERED,
Expand Down
16 changes: 8 additions & 8 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ async fn main() {
commands::tx::coin_transfer,
// claim_make_whole,
//////// Tower ////////
commands::query::get_onchain_tower_state,
commands::mining::miner_once,
commands::mining::start_backlog_sender_listener,
commands::mining::get_local_height,
commands::mining::get_epoch_rules,
commands::mining::submit_backlog,
commands::mining::get_last_local_proof,
commands::mining::debug_highest_proof_path,
// commands::query::get_onchain_tower_state,
// commands::mining::miner_once,
// commands::mining::start_backlog_sender_listener,
// commands::mining::get_local_height,
// commands::mining::get_epoch_rules,
// commands::mining::submit_backlog,
// commands::mining::get_last_local_proof,
// commands::mining::debug_highest_proof_path,
// submit_proof_zero,

//////// Preferences ////////
Expand Down
1 change: 1 addition & 0 deletions src/components/settings/UpdateNetwork.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
.then((res: NetworkPlaylist) => {
network_profile.set(res);
notify_success("Network Settings Updated");
check_sync();
})
.catch((error) => {
raise_error(error as CarpeError, false, "forceUpstream");
Expand Down
6 changes: 4 additions & 2 deletions src/components/txs/Transactions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import type { CarpeProfile } from '../../modules/accounts'
import { raise_error } from '../../modules/carpeError'
import CantStart from '../miner/cards/CantStart.svelte'
import { refreshAccounts } from '../../modules/accountActions'

const errorDic = {
'120127': $_('txs.transfer.error_slow_wallet'),
Expand Down Expand Up @@ -46,8 +47,8 @@
account && amount > unscaledCoins(account.balance)
? $_('txs.transfer.error_amount_greater_than_balance')
: receiver && receiver.toUpperCase() == account.account.toUpperCase()
? $_('txs.transfer.error_receiver_equals_sender')
: ''
? $_('txs.transfer.error_receiver_equals_sender')
: ''

onDestroy(async () => {
unsubs && unsubs()
Expand All @@ -68,6 +69,7 @@
waitingTxs = false
amount = 0
receiver = null
refreshAccounts()
})
.catch((error) => {
responses.set(JSON.stringify(error))
Expand Down
19 changes: 9 additions & 10 deletions src/components/wallet/AccountsList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import Icons from 'uikit/dist/js/uikit-icons'
import { allAccounts, formatAccount, signingAccount } from '../../modules/accounts'
import { printCoins, unscaledCoins } from '../../modules/coinHelpers'
import { minerLoopEnabled } from '../../modules/miner'
import { connected } from '../../modules/networks'
import { setAccount } from '../../modules/accountActions'
import Actions from './Actions.svelte'
Expand Down Expand Up @@ -113,17 +112,17 @@
<tbody>
{#each $allAccounts as a}
<!-- svelte-ignore missing-declaration -->
<tr
class={$minerLoopEnabled && a.account == $signingAccount.account
? 'uk-text-primary'
: ''}
>
<td>
<tr>
<td >
<span>
{#if a.account == $signingAccount.account}
<span uk-icon="user" />
<button uk-icon="settings" class="uk-margin-left" on:click={toggleOptions} />
{/if}
<span uk-icon="user" />
<button
uk-icon="settings"
class="uk-margin-left"
on:click={toggleOptions}
/>
{/if}
</span>

{#if a.watch_only}
Expand Down
1 change: 0 additions & 1 deletion src/components/wallet/Keygen.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { invoke } from '@tauri-apps/api/tauri'
import { tempCreateAccount } from '../../modules/accounts'
import type { CarpeProfile } from '../../modules/accounts'
// import { minerLoopEnabled } from '../../modules/miner'

import { raise_error } from '../../modules/carpeError'
import { responses } from '../../modules/debug'
Expand Down
14 changes: 3 additions & 11 deletions src/modules/accountActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,6 @@ export function findOneAccount(account: string): CarpeProfile | undefined {
export const setAccount = async (account: string, notifySucess = true) => {
if (get(signingAccount).account == account) return

// cannot switch profile with miner running
if (get(minerLoopEnabled)) {
notify_error('To switch accounts you need to turn miner off first.')
return
}

invoke('switch_profile', { account })
.then((res: CarpeProfile) => {
res.account = res.account.toLocaleUpperCase()
Expand Down Expand Up @@ -440,11 +434,9 @@ async function onAccountAdd(res: CarpeProfile) {
// set as init so we don't get sent back to Newbie account creation.
isInit.set(true)
responses.set(JSON.stringify(res))
// cannot switch profile with miner running
if (!get(minerLoopEnabled)) {
res.account = res.account.toLocaleUpperCase()
signingAccount.set(res)
}
res.account = res.account.toLocaleUpperCase()
signingAccount.set(res)

await initNetwork()
if (!get(isCarpeTickRunning)) {
// start the carpe tick for every 30 secs, this is async
Expand Down
1 change: 1 addition & 0 deletions src/modules/accountTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const setWalletType = async (wtype: WalletType) => {
const account = get(signingAccount)
invoke('set_slow_wallet', {
legacy: account.account.startsWith('0'.repeat(32)),
sender: account.account,
})
.then((res: string) => {
notify_success(`The account is set to: SlowWallet`)
Expand Down