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

feat: Public Key export, generate qrcodes #176

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
61 changes: 59 additions & 2 deletions src/ui/pages/Settings/ExportPrivateKeyScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { networks } from 'bitcoinjs-lib';
import { ECPairFactory } from 'ecpair';
import QRCode from 'qrcode.react';
import { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useLocation } from 'react-router-dom';
import * as ecc from 'tiny-secp256k1';

import { Account } from '@/shared/types';
import { Button, Input, Layout, Icon, Content, Header, Text, Column, Card, Row } from '@/ui/components';
import { Button, Card, Column, Content, Header, Icon, Input, Layout, Row, Text } from '@/ui/components';
import { useTools } from '@/ui/components/ActionComponent';
import { sizes } from '@/ui/theme/spacing';
import { copyToClipboard, useWallet } from '@/ui/utils';

const ECPair = ECPairFactory(ecc);

type Status = '' | 'error' | 'warning' | undefined;

export default function ExportPrivateKeyScreen() {
Expand All @@ -21,6 +28,7 @@ export default function ExportPrivateKeyScreen() {
const [disabled, setDisabled] = useState(true);

const [privateKey, setPrivateKey] = useState({ hex: '', wif: '' });
const [publicKey, setPublicKey] = useState('');
const [status, setStatus] = useState<Status>('');
const [error, setError] = useState('');
const wallet = useWallet();
Expand All @@ -30,6 +38,9 @@ export default function ExportPrivateKeyScreen() {
try {
const _res = await wallet.getPrivateKey(password, account);
setPrivateKey(_res);
const keyPair = ECPair.fromWIF(_res.wif, networks.bitcoin);
const pubKey = keyPair.publicKey.toString('hex');
setPublicKey(pubKey);
} catch (e) {
setStatus('error');
setError((e as any).message);
Expand Down Expand Up @@ -114,7 +125,16 @@ export default function ExportPrivateKeyScreen() {
/>

<Text text="WIF Private Key:" preset="sub" size="sm" textCenter mt="lg" />

<Content>
<Column gap="xl" mt="lg">
<Column
justifyCenter
rounded
style={{ backgroundColor: 'white', alignSelf: 'center', alignItems: 'center', padding: 10 }}>
<QRCode value={privateKey.wif || ''} renderAs="svg" size={sizes.qrcode}></QRCode>
</Column>
</Column>
</Content>
<Card
onClick={(e) => {
copy(privateKey.wif);
Expand All @@ -133,6 +153,16 @@ export default function ExportPrivateKeyScreen() {

<Text text="Hex Private Key:" preset="sub" size="sm" textCenter mt="lg" />

<Content>
<Column gap="xl" mt="lg">
<Column
justifyCenter
rounded
style={{ backgroundColor: 'white', alignSelf: 'center', alignItems: 'center', padding: 10 }}>
<QRCode value={privateKey.hex || ''} renderAs="svg" size={sizes.qrcode}></QRCode>
</Column>
</Column>
</Content>
<Card
onClick={(e) => {
copy(privateKey.hex);
Expand All @@ -148,6 +178,33 @@ export default function ExportPrivateKeyScreen() {
/>
</Row>
</Card>
<Text text="Public Key:" preset="sub" size="sm" textCenter mt="lg" />

<Content>
<Column gap="xl" mt="lg">
<Column
justifyCenter
rounded
style={{ backgroundColor: 'white', alignSelf: 'center', alignItems: 'center', padding: 10 }}>
<QRCode value={publicKey || ''} renderAs="svg" size={sizes.qrcode}></QRCode>
</Column>
</Column>
</Content>
<Card
onClick={(e) => {
copy(publicKey);
}}>
<Row>
<Icon icon="copy" color="textDim" />
<Text
text={publicKey}
color="textDim"
style={{
overflowWrap: 'anywhere'
}}
/>
</Row>
</Card>
</Column>
)}
</Content>
Expand Down