mirror of
https://github.com/status-im/MyCrypto.git
synced 2025-02-18 14:07:31 +00:00
Use async in download wallet (#127)
* convert keystore gen to async * add & use type UtcKeystore * add types, refactor * remove unneeded prop checking
This commit is contained in:
parent
bf4171dfbd
commit
982d70a56c
@ -4,6 +4,7 @@ import translate from 'translations';
|
|||||||
import type PrivKeyWallet from 'libs/wallet/privkey';
|
import type PrivKeyWallet from 'libs/wallet/privkey';
|
||||||
import { makeBlob } from 'utils/blob';
|
import { makeBlob } from 'utils/blob';
|
||||||
import { getV3Filename } from 'libs/keystore';
|
import { getV3Filename } from 'libs/keystore';
|
||||||
|
import type { UtcKeystore } from 'libs/keystore';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
wallet: PrivKeyWallet,
|
wallet: PrivKeyWallet,
|
||||||
@ -11,32 +12,43 @@ type Props = {
|
|||||||
continueToPaper: Function
|
continueToPaper: Function
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type State = {
|
||||||
|
hasDownloadedWallet: boolean,
|
||||||
|
address: string,
|
||||||
|
keystore: UtcKeystore | null
|
||||||
|
};
|
||||||
|
|
||||||
export default class DownloadWallet extends Component {
|
export default class DownloadWallet extends Component {
|
||||||
props: Props;
|
props: Props;
|
||||||
keystore: Object;
|
state: State = {
|
||||||
state = {
|
|
||||||
hasDownloadedWallet: false,
|
hasDownloadedWallet: false,
|
||||||
address: ''
|
address: '',
|
||||||
|
keystore: null
|
||||||
};
|
};
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
if (!this.props.wallet) return;
|
|
||||||
this.props.wallet.getAddress().then(addr => {
|
this.props.wallet.getAddress().then(addr => {
|
||||||
this.setState({ address: addr });
|
this.setState({ address: addr });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillMount() {
|
componentWillMount() {
|
||||||
this.keystore = this.props.wallet.toKeystore(this.props.password);
|
this.props.wallet.toKeystore(this.props.password).then(utcKeystore => {
|
||||||
|
this.setState({ keystore: utcKeystore });
|
||||||
|
});
|
||||||
}
|
}
|
||||||
componentWillUpdate(nextProps: Props) {
|
componentWillUpdate(nextProps: Props) {
|
||||||
if (this.props.wallet !== nextProps.wallet) {
|
if (this.props.wallet !== nextProps.wallet) {
|
||||||
this.keystore = nextProps.wallet.toKeystore(nextProps.password);
|
nextProps.wallet.toKeystore(nextProps.password).then(utcKeystore => {
|
||||||
|
this.setState({ keystore: utcKeystore });
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_markDownloaded = () => {
|
_markDownloaded = () => {
|
||||||
|
if (this.state.keystore) {
|
||||||
this.setState({ hasDownloadedWallet: true });
|
this.setState({ hasDownloadedWallet: true });
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
_handleContinue = () => {
|
_handleContinue = () => {
|
||||||
@ -72,6 +84,7 @@ export default class DownloadWallet extends Component {
|
|||||||
className="btn btn-primary btn-block"
|
className="btn btn-primary btn-block"
|
||||||
aria-label="Download Keystore File (UTC / JSON · Recommended · Encrypted)"
|
aria-label="Download Keystore File (UTC / JSON · Recommended · Encrypted)"
|
||||||
aria-describedby="x_KeystoreDesc"
|
aria-describedby="x_KeystoreDesc"
|
||||||
|
disabled={!this.state.keystore}
|
||||||
download={this.getFilename()}
|
download={this.getFilename()}
|
||||||
href={this.getBlob()}
|
href={this.getBlob()}
|
||||||
onClick={this._markDownloaded}
|
onClick={this._markDownloaded}
|
||||||
@ -115,7 +128,9 @@ export default class DownloadWallet extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getBlob() {
|
getBlob() {
|
||||||
return makeBlob('text/json;charset=UTF-8', this.keystore);
|
if (this.state.keystore) {
|
||||||
|
return makeBlob('text/json;charset=UTF-8', this.state.keystore);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getFilename() {
|
getFilename() {
|
||||||
|
@ -10,6 +10,13 @@ import { sha3, privateToAddress } from 'ethereumjs-util';
|
|||||||
import scrypt from 'scryptsy';
|
import scrypt from 'scryptsy';
|
||||||
import uuid from 'uuid';
|
import uuid from 'uuid';
|
||||||
|
|
||||||
|
export type UtcKeystore = {
|
||||||
|
version: number,
|
||||||
|
id: string,
|
||||||
|
address: string,
|
||||||
|
Crypto: Object
|
||||||
|
};
|
||||||
|
|
||||||
//adapted from https://github.com/kvhnuke/etherwallet/blob/de536ffebb4f2d1af892a32697e89d1a0d906b01/app/scripts/myetherwallet.js#L342
|
//adapted from https://github.com/kvhnuke/etherwallet/blob/de536ffebb4f2d1af892a32697e89d1a0d906b01/app/scripts/myetherwallet.js#L342
|
||||||
export function determineKeystoreType(file: string): string {
|
export function determineKeystoreType(file: string): string {
|
||||||
const parsed = JSON.parse(file);
|
const parsed = JSON.parse(file);
|
||||||
@ -106,7 +113,7 @@ export function pkeyToKeystore(
|
|||||||
pkey: Buffer,
|
pkey: Buffer,
|
||||||
address: string,
|
address: string,
|
||||||
password: string
|
password: string
|
||||||
) {
|
): UtcKeystore {
|
||||||
const salt = randomBytes(32);
|
const salt = randomBytes(32);
|
||||||
const iv = randomBytes(16);
|
const iv = randomBytes(16);
|
||||||
let derivedKey;
|
let derivedKey;
|
||||||
|
@ -11,6 +11,7 @@ import { signRawTxWithPrivKey, signMessageWithPrivKey } from 'libs/signing';
|
|||||||
|
|
||||||
import { isValidPrivKey } from 'libs/validators';
|
import { isValidPrivKey } from 'libs/validators';
|
||||||
import type { RawTransaction } from 'libs/transaction';
|
import type { RawTransaction } from 'libs/transaction';
|
||||||
|
import type { UtcKeystore } from 'libs/keystore';
|
||||||
|
|
||||||
export default class PrivKeyWallet extends BaseWallet {
|
export default class PrivKeyWallet extends BaseWallet {
|
||||||
privKey: Buffer;
|
privKey: Buffer;
|
||||||
@ -40,7 +41,7 @@ export default class PrivKeyWallet extends BaseWallet {
|
|||||||
return new PrivKeyWallet(randomBytes(32));
|
return new PrivKeyWallet(randomBytes(32));
|
||||||
}
|
}
|
||||||
|
|
||||||
toKeystore(password: string): Promise<any> {
|
toKeystore(password: string): Promise<UtcKeystore> {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
this.getNakedAddress().then(address => {
|
this.getNakedAddress().then(address => {
|
||||||
resolve(pkeyToKeystore(this.privKey, address, password));
|
resolve(pkeyToKeystore(this.privKey, address, password));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user