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

Add KeyringController wrapper and a CoreController #7

Merged
merged 1 commit into from
Jul 30, 2018
Merged
Show file tree
Hide file tree
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
3,291 changes: 1,876 additions & 1,415 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 14 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,33 @@
"@types/jest": "^22.2.3",
"@types/node": "^10.1.4",
"@types/sinon": "^4.3.3",
"eth-block-tracker": "^4.0.1",
"eth-phishing-detect": "^1.1.13",
"eth-query": "^2.1.2",
"ethjs-provider-http": "^0.1.6",
"fetch-mock": "^6.4.3",
"husky": "^0.14.3",
"isomorphic-fetch": "^2.2.1",
"jest": "^22.1.4",
"jsdom": "11.11.0",
"lint-staged": "^6.1.0",
"prettier": "^1.10.2",
"scrypt": "github:barrysteyn/node-scrypt#pull/144/head",
"sinon": "^5.0.10",
"ts-jest": "^22.4.6",
"tslint": "^5.10.0",
"tslint-config-prettier": "^1.13.0",
"typedoc": "^0.11.1",
"typescript": "^2.8.3",
"web3-provider-engine": "^14.0.5"
"typescript": "^2.8.3"
},
"dependencies": {
"tslib": "^1.9.2"
"await-semaphore": "^0.1.3",
"eth-block-tracker": "3.0.1",
"eth-json-rpc-infura": "^3.1.2",
"eth-keyring-controller": "^4.0.0",
"eth-phishing-detect": "^1.1.13",
"eth-query": "^2.1.2",
"ethereumjs-util": "^5.2.0",
"ethereumjs-wallet": "0.6.0",
"percentile": "^1.2.1",
"tslib": "^1.9.2",
"web3-provider-engine": "^14.0.5"
}
}
2 changes: 1 addition & 1 deletion src/BlockHistoryController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface Block {
difficulty: string;
extraData: string;
gasLimit: string;
gasPrices: number[];
gasPrices: string[];
gasUsed: string;
hash: string;
logsBloom: string;
Expand Down
2 changes: 1 addition & 1 deletion src/ComposableController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface ChildControllerContext {
*/
export class ComposableController extends BaseController<BaseState, BaseConfig> {
/**
* Array of stores to compose together
* Map of stores to compose together
*/
context: ChildControllerContext = {};

Expand Down
169 changes: 169 additions & 0 deletions src/CoreController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import {
AccountTrackerController,
AddressBookController,
BaseConfig,
BaseController,
BaseState,
BlockHistoryController,
ComposableController,
CurrencyRateController,
KeyringController,
NetworkController,
NetworkStatusController,
NetworkType,
PhishingController,
PreferencesController,
ShapeShiftController,
TokenRatesController
} from '.';
import { getBuyURL, getGasPrice } from './util';

const BlockTracker = require('eth-block-tracker');

/**
* Core controller responsible for composing other child controllers together
* and exposing convenience methods for common wallet operations.
*/
export class CoreController extends BaseController<BaseState, BaseConfig> {
private initializeBlockTracker() {
const {
accountTracker,
blockHistory,
network: { provider }
} = this.api;
provider.sendAsync = provider.sendAsync.bind(provider);
const blockTracker = new BlockTracker({ provider });
blockHistory.configure({ provider, blockTracker });
accountTracker.configure({ provider, blockTracker });
blockTracker.start();
}

/**
* Child controller instances keyed by controller name
*/
api = {
accountTracker: new AccountTrackerController(),
addressBook: new AddressBookController(),
blockHistory: new BlockHistoryController(),
currencyRate: new CurrencyRateController(),
keyring: new KeyringController(),
network: new NetworkController(undefined, {
providerConfig: {
/* todo */
}
}),
networkStatus: new NetworkStatusController(),
phishing: new PhishingController(),
preferences: new PreferencesController(),
shapeShift: new ShapeShiftController(),
tokenRates: new TokenRatesController()
};

/**
* ComposableController reference containing all child controllers
*/
datamodel: ComposableController;

/**
* Creates a CoreController instance
*
* @param state - Initial state to set on this controller
* @param config - Initial options used to configure this controller
*/
constructor(state?: Partial<BaseState>, config?: Partial<BaseConfig>) {
super(state, config);
this.datamodel = new ComposableController(this.api);
this.initializeBlockTracker();
this.initialize();
}

getState() {
return this.datamodel.flatState;
}

setCurrentCurrency(currency: string) {
return this.api.currencyRate.updateCurrency(currency);
}

getGasPrice() {
return getGasPrice(this.api.blockHistory.state.recentBlocks);
}

getBuyEthUrl(address: string, amount: number) {
return getBuyURL(this.api.network.state.network, address, amount);
}

createShapeShiftTx(depositAddress: string, depositType: string) {
return this.api.shapeShift.createTransaction(depositAddress, depositType);
}

setSelectedAddress(selectedAddress: string) {
return this.api.preferences.update({ selectedAddress });
}

addToken(address: string, symbol: string, decimals: number) {
return this.api.preferences.addToken(address, symbol, decimals);
}

removeToken(address: string) {
return this.api.preferences.removeToken(address);
}

setAccountLabel(address: string, label: string) {
return this.api.preferences.setAccountLabel(address, label);
}

setFeatureFlag(feature: string, flag: boolean) {
return this.api.preferences.setFeatureFlag(feature, flag);
}

setProviderType(type: NetworkType) {
return this.api.network.setProviderType(type);
}

setCustomRpc(rpcTarget: string) {
return this.api.network.setRpcTarget(rpcTarget);
}

setAddressBook(address: string, name: string) {
return this.api.addressBook.set(address, name);
}

setLocked() {
return this.api.keyring.setLocked();
}

exportAccount(address: string) {
return this.api.keyring.exportAccount(address);
}

createNewVaultAndKeychain(password: string) {
return this.api.keyring.createNewVaultAndKeychain(password);
}

createNewVaultAndRestore(password: string, seed: string) {
return this.api.keyring.createNewVaultAndRestore(password, seed);
}

submitPassword(password: string) {
return this.api.keyring.submitPassword(password);
}

addNewAccount() {
return this.api.keyring.addNewAccount();
}

removeAccount(address: string) {
return this.api.keyring.removeAccount(address);
}

verifySeedPhrase() {
return this.api.keyring.verifySeedPhrase();
}

importAccountWithStrategy(strategy: string, args: any) {
return this.api.keyring.importAccountWithStrategy(strategy, args);
}
}

export default CoreController;
4 changes: 2 additions & 2 deletions src/CurrencyRateController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ describe('CurrencyRateController', () => {
});
});

it('should update exchange rate', async () => {
it('should update currency', async () => {
const controller = new CurrencyRateController(undefined, { interval: 10 });
expect(controller.state.conversionRate).toEqual(0);
await controller.updateExchangeRate();
await controller.updateCurrency('eur');
expect(controller.state.conversionRate).toBeGreaterThan(0);
});

Expand Down
11 changes: 11 additions & 0 deletions src/CurrencyRateController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ export class CurrencyRateController extends BaseController<CurrencyRateState, Cu
};
}

/**
* Sets a new currency to track and fetches its exchange rate
*
* @param currency - ISO 4217 currency code
* @returns - Promise resolving to exchange rate for given currecy
*/
async updateCurrency(currency: string): Promise<CurrencyRateState | void> {
this.configure({ currency });
return this.updateExchangeRate();
}

/**
* Updates exchange rate for the current currency
*
Expand Down
Loading