2018-06-17 20:53:00 -05:00
|
|
|
import { fromPrivateKey, fromEthSale, IFullWallet } from 'ethereumjs-wallet';
|
2017-11-08 13:16:43 -05:00
|
|
|
import { fromEtherWallet } from 'ethereumjs-wallet/thirdparty';
|
2018-06-17 20:53:00 -05:00
|
|
|
|
2017-11-08 13:16:43 -05:00
|
|
|
import { decryptPrivKey } from 'libs/decrypt';
|
2018-02-02 01:01:30 -05:00
|
|
|
import { fromV3 } from 'libs/web-workers';
|
2018-06-17 20:53:00 -05:00
|
|
|
import { signWrapper, determineKeystoreType, KeystoreTypes } from './helpers';
|
2017-11-09 22:30:20 -05:00
|
|
|
import Web3Wallet from './web3';
|
2017-11-29 15:14:57 -08:00
|
|
|
import AddressOnlyWallet from './address';
|
2018-04-06 23:32:25 +02:00
|
|
|
import ParitySignerWallet from './parity';
|
2017-11-08 13:16:43 -05:00
|
|
|
|
2017-12-18 18:29:26 -05:00
|
|
|
const EncryptedPrivateKeyWallet = (encryptedPrivateKey: string, password: string) =>
|
|
|
|
signWrapper(fromPrivateKey(decryptPrivKey(encryptedPrivateKey, password)));
|
2017-11-08 13:16:43 -05:00
|
|
|
|
|
|
|
const PresaleWallet = (keystore: string, password: string) =>
|
|
|
|
signWrapper(fromEthSale(keystore, password));
|
|
|
|
|
|
|
|
const MewV1Wallet = (keystore: string, password: string) =>
|
|
|
|
signWrapper(fromEtherWallet(keystore, password));
|
|
|
|
|
|
|
|
const PrivKeyWallet = (privkey: Buffer) => signWrapper(fromPrivateKey(privkey));
|
|
|
|
|
2018-01-11 01:44:13 -05:00
|
|
|
const UtcWallet = (keystore: string, password: string) => fromV3(keystore, password, true);
|
2017-11-08 13:16:43 -05:00
|
|
|
|
2018-06-17 20:53:00 -05:00
|
|
|
// Getters
|
|
|
|
const getUtcWallet = (file: string, password: string): Promise<IFullWallet> => {
|
|
|
|
return UtcWallet(file, password);
|
|
|
|
};
|
|
|
|
|
|
|
|
const getPrivKeyWallet = (key: string, password: string) =>
|
|
|
|
key.length === 64
|
|
|
|
? PrivKeyWallet(Buffer.from(key, 'hex'))
|
|
|
|
: EncryptedPrivateKeyWallet(key, password);
|
|
|
|
|
|
|
|
const getKeystoreWallet = (file: string, password: string) => {
|
|
|
|
const parsed = JSON.parse(file);
|
|
|
|
|
|
|
|
switch (determineKeystoreType(file)) {
|
|
|
|
case KeystoreTypes.presale:
|
|
|
|
return PresaleWallet(file, password);
|
|
|
|
|
|
|
|
case KeystoreTypes.v1Unencrypted:
|
|
|
|
return PrivKeyWallet(Buffer.from(parsed.private, 'hex'));
|
|
|
|
|
|
|
|
case KeystoreTypes.v1Encrypted:
|
|
|
|
return MewV1Wallet(file, password);
|
|
|
|
|
|
|
|
case KeystoreTypes.v2Unencrypted:
|
|
|
|
return PrivKeyWallet(Buffer.from(parsed.privKey, 'hex'));
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw Error('Unknown wallet');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-08 13:16:43 -05:00
|
|
|
export {
|
|
|
|
EncryptedPrivateKeyWallet,
|
|
|
|
PresaleWallet,
|
|
|
|
MewV1Wallet,
|
|
|
|
PrivKeyWallet,
|
2017-11-09 22:30:20 -05:00
|
|
|
UtcWallet,
|
2017-11-29 15:14:57 -08:00
|
|
|
Web3Wallet,
|
2018-04-06 23:32:25 +02:00
|
|
|
AddressOnlyWallet,
|
2018-06-17 20:53:00 -05:00
|
|
|
ParitySignerWallet,
|
|
|
|
getUtcWallet,
|
|
|
|
getPrivKeyWallet,
|
|
|
|
getKeystoreWallet
|
2017-11-08 13:16:43 -05:00
|
|
|
};
|