HenryNguyen5 a00269507c EthereumJS-Wallet (Part 3) (#316)
* Progress commit -- ethereumjs-wallet typings

* Add hdkey module + better wallet typing

* Add provider-engine typings

* Add jsdoc descriptions for hdkey constructor methods

* Fix missing return type

* Fix another missing return

* Make provider engine options optional

* Add priv/pubkey members to wallet instance

* Turn into SFC + Use ethereumjs-lib

* Use proper interface naming for V3Wallet

* Switch to ethereumjs-wallet

* Switch to ethereumjs-wallet and refactor using NewTabLink

* Use proper interface naming for V3Wallet

* Use proper interface naming for PublicKeyOnlyWallet

* Strip out wallet classes for ethereumjs-wallet, stuff wallet types in privkey for now

* Seperate wallets into deterministic and non-deterministic, change IWallet and deterministic wallets to adhere to getAddressString

* Fix broken test, remove scryptsy

* Fix broken test, re-add scryptsy to make this PR pass

* Remove uuid from deps and keystore test

* Add ethereumjs-wallet to DLL

* Wrap mnemonic wallet

* Fix definition module for thirdparty wallets

* Fix MewV1 wallet not loading due to wrong library

* Fix tsc error

* Decrease n-factor to 1024, checksum address of keystore

* Fix isKeystorePassRequired

* Fix tsc errors

* Merge package lock

* Update package lock

* regenerate lock file

* Lock typescript to 2.5.2

* Merge develop
2017-11-08 10:16:43 -08:00

93 lines
2.7 KiB
TypeScript

import { IFullWallet } from 'ethereumjs-wallet';
import { RawTransaction } from 'libs/transaction';
import { signMessageWithPrivKeyV2, signRawTxWithPrivKey } from 'libs/signing';
import {
EncryptedPrivateKeyWallet,
MewV1Wallet,
PresaleWallet,
PrivKeyWallet,
UtcWallet
} from './wallets';
enum KeystoreTypes {
presale = 'presale',
utc = 'v2-v3-utc',
v1Unencrypted = 'v1-unencrypted',
v1Encrypted = 'v1-encrypted',
v2Unencrypted = 'v2-unencrypted'
}
interface ISignWrapper {
signRawTransaction(rawTx: RawTransaction): string;
signMessage(msg: string): string;
unlock();
}
type WrappedWallet = IFullWallet & ISignWrapper;
export const signWrapper = (walletToWrap: IFullWallet): WrappedWallet =>
Object.assign(walletToWrap, {
signRawTransaction: (rawTx: RawTransaction) =>
signRawTxWithPrivKey(walletToWrap.getPrivateKey(), rawTx),
signMessage: (msg: string) =>
signMessageWithPrivKeyV2(walletToWrap.getPrivateKey(), msg),
unlock: () => Promise.resolve()
});
function determineKeystoreType(file: string): string {
const parsed = JSON.parse(file);
if (parsed.encseed) {
return KeystoreTypes.presale;
} else if (parsed.Crypto || parsed.crypto) {
return KeystoreTypes.utc;
} else if (parsed.hash && parsed.locked === true) {
return KeystoreTypes.v1Encrypted;
} else if (parsed.hash && parsed.locked === false) {
return KeystoreTypes.v1Unencrypted;
} else if (parsed.publisher === 'MyEtherWallet') {
return KeystoreTypes.v2Unencrypted;
} else {
throw new Error('Invalid keystore');
}
}
const isKeystorePassRequired = (file: string): boolean => {
const keystoreType = determineKeystoreType(file);
return (
keystoreType === KeystoreTypes.presale ||
keystoreType === KeystoreTypes.v1Encrypted ||
keystoreType === KeystoreTypes.utc
);
};
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'));
case KeystoreTypes.utc:
return UtcWallet(file, password);
default:
throw Error('Unknown wallet');
}
};
export { isKeystorePassRequired, getPrivKeyWallet, getKeystoreWallet };