mirror of
https://github.com/status-im/MyCrypto.git
synced 2025-01-10 11:05:47 +00:00
ab5fa1a799
* Make UnlockHeader a PureComponent * MVP * actually disable wallet format if not determined to be valid format for wallet * default to correct derivation in mnemonic modal * cleanup * fix tslint * use enums for HD wallet getPath * Add stricter typing * Fix labels not updating on selector * Ban hardware wallet support for custom network unsupported chainIds * Fix type error * Fix custom node dPath not being saved * Fix mnemonic modal * default path bugfixes * add react-select * misc fixes; rabbit holing hard. * fix tslint * revert identicon changes * reload on network change :/ * actually reload on network change * really really reload on network change * tslint fixes * Update styles * set table width * fix package versioning * push broken sagas * Fix saga test * fix tslint * address round of review * move non-selectors out to utilty; adjust reload timer * cleanup network util comments * manage wallet disable at WalletDecrypt instead of in both WalletDecrypt and WalletButton * Separate WalletDecrypt props into ownProps / StateProps * disable payment requests on non-eth networks * specialize connect; separate props * remove unused state prop * remove bad import * create tests for networks * Clarify Lite-Send error on non-ethereum networkS * remove string option for network config name * Create concept of always-on 'EXTRA_PATHS'; include SINGULAR_DTV legacy dPath in 'EXTRA_PATHS' * fix multiple imports * address PR comments
49 lines
1.5 KiB
TypeScript
49 lines
1.5 KiB
TypeScript
import { fromPrivateKey, IFullWallet, fromV3 } from 'ethereumjs-wallet';
|
||
import { isValidPrivKey } from 'libs/validators';
|
||
import { stripHexPrefix } from 'libs/values';
|
||
import { makeBlob } from 'utils/blob';
|
||
import { N_FACTOR } from 'config';
|
||
|
||
export interface KeystoreFile {
|
||
filename: string;
|
||
blob: string;
|
||
}
|
||
|
||
export function makeKeystoreWalletBlob(wallet: IFullWallet, password: string): string {
|
||
const keystore = wallet.toV3(password, { n: N_FACTOR });
|
||
return makeBlob('text/json;charset=UTF-8', keystore);
|
||
}
|
||
|
||
export function checkKeystoreWallet(
|
||
wallet: IFullWallet,
|
||
privateKey: string,
|
||
password: string
|
||
): boolean {
|
||
const keystore = wallet.toV3(password, { n: N_FACTOR });
|
||
const backToWallet = fromV3(keystore, password, true);
|
||
return stripHexPrefix(backToWallet.getPrivateKeyString()) === stripHexPrefix(privateKey);
|
||
}
|
||
|
||
export async function generateKeystoreFileInfo(
|
||
privateKey: string,
|
||
password: string
|
||
): Promise<KeystoreFile | null> {
|
||
if (!isValidPrivKey(privateKey)) {
|
||
throw new Error('Invalid private key supplied');
|
||
}
|
||
|
||
// Make the wallet from their private key
|
||
const keyBuffer = Buffer.from(stripHexPrefix(privateKey), 'hex');
|
||
const wallet = fromPrivateKey(keyBuffer);
|
||
|
||
// Validate the password and keystore generation
|
||
if (!checkKeystoreWallet(wallet, privateKey, password)) {
|
||
throw new Error('Keystore file generation failed, keystore wallet didn’t match private key');
|
||
}
|
||
|
||
return {
|
||
filename: wallet.getV3Filename(),
|
||
blob: makeKeystoreWalletBlob(wallet, password)
|
||
};
|
||
}
|