Skip to content

Commit

Permalink
feat: get privatekey (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
BBK912 authored Feb 21, 2024
1 parent 2f31cd2 commit 33a62a0
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src-tauri/src/commands/wallets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
commands::query,
configs::{self, get_cfg, get_client},
configs_profile,
key_manager::{self, inject_private_key_to_cfg},
key_manager::{self, inject_private_key_to_cfg, get_private_key},
};

use anyhow::{anyhow, Context};
Expand Down Expand Up @@ -247,3 +247,10 @@ pub async fn set_slow_wallet() -> Result<(), CarpeError> {
// sender.sign_submit_wait(payload).await?;
Ok(())
}

#[tauri::command]
pub fn get_private_key_from_os(address: AccountAddress) -> Result<String, CarpeError> {
let pk = get_private_key(&address)?;
let acc_struct = account_keys::get_account_from_private(&pk);
Ok(acc_struct.pri_key.to_encoded_string()?)
}
1 change: 1 addition & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ async fn main() {
commands::wallets::switch_profile,
commands::wallets::is_slow,
commands::wallets::set_slow_wallet,
commands::wallets::get_private_key_from_os,
//////// Networks ////////
commands::preferences::refresh_upstream_peer_stats,
commands::networks::force_upstream,
Expand Down
49 changes: 49 additions & 0 deletions src/components/dev/DebugGetPrivateKey.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<script lang="ts">
import { onMount } from 'svelte'
import { getPrivateKey } from '../../modules/accountActions'
import Copy from '../../components/layout/Copy.svelte'
import { signingAccount } from '../../modules/accounts'
import { get } from 'svelte/store'
import { notify_success } from '../../modules/carpeNotify'
let address = ''
let privateKey = ''
onMount(() => {
const account = get(signingAccount)
if (account) {
address = account.account
}
})
function onCopy() {
notify_success('copy success')
}
</script>

<main>
<div class="uk-margin-bottom">
<h4 class="uk-text-light uk-text-uppercase uk-text-muted uk-text-thin">PrivateKey</h4>
<form id="account-form" on:submit|preventDefault={() => {}}>
<fieldset class="uk-fieldset">
<div class="uk-margin uk-inline-block uk-width-1-1">
<span> Address </span>
<input class="uk-input" type="text" placeholder={address} bind:value={address} />
</div>
<div>
PrivateKey:
{#if privateKey}
<code class="uk-text-light">
{privateKey.slice(0, 8) + '......' + privateKey.slice(-8)}</code>
<Copy text={privateKey} on:copy={onCopy}></Copy>
{/if}
</div>
<div>
<button
on:click={getPrivateKey(address, (res) => (privateKey = res))}
class="uk-button uk-button-primary uk-align-right"
id="add-btn">Get PrivateKey</button
>
</div>
</fieldset>
</form>

</div>
</main>
3 changes: 2 additions & 1 deletion src/components/dev/DevMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import DebugSwitcher from './DebugSwitcher.svelte'
import DebugPaths from './DebugPaths.svelte'
import DebugNetworkInfo from './DebugNetworkInfo.svelte'
import DebugGetPrivateKey from './DebugGetPrivateKey.svelte'
let debugEnabled = false
onMount(async () => {
debugMode.subscribe((b) => (debugEnabled = b))
Expand All @@ -26,6 +26,7 @@
<DebugSwitchRexMainnet />
<DebugSwitchProdTest />
<DebugActions />
<DebugGetPrivateKey />
{#if !debugEnabled}
<DebugCard />
{/if}
Expand Down
7 changes: 5 additions & 2 deletions src/components/layout/Copy.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<script lang="ts">
import { copyToClipboard } from '@svelte-put/copy'
import {createEventDispatcher} from 'svelte'
const dispatch = createEventDispatcher();
export let text: string
let icon = 'copy'
const copyThis = () => {
icon = 'check'
copyToClipboard(text)
copyToClipboard(text).then(() => {
dispatch('copy')
})
setTimeout(() => {
icon = 'copy'
}, 3000)
Expand Down
11 changes: 11 additions & 0 deletions src/modules/accountActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,14 @@ export function claimMakeWhole(account: string, callback = null) {
}
})
}

export function getPrivateKey(address: string, callback = null) {
invoke('get_private_key_from_os', { address })
.then((res) => {
callback && callback(res)
})
.catch((e) => {
callback && callback('')
raise_error(e, false, 'get_private_key')
})
}

0 comments on commit 33a62a0

Please sign in to comment.