Skip to content

Commit

Permalink
fixed not awaiting mnemonicToSeed
Browse files Browse the repository at this point in the history
- this fixes #14
- added more debugging things to remote-interface.ts
- updated typings for wrapped-node.ts
  • Loading branch information
MichaelFedora committed Aug 5, 2019
1 parent 875fb77 commit 910e918
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/common/data/wrapped-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class WrappedNode {
return new WrappedNode(bip32.fromPublicKey(publicKey, chainCode, network));
}

public static fromSeed(seed: any, network?: any) {
public static fromSeed(seed: Buffer, network?: any) {
return new WrappedNode(bip32.fromSeed(seed, network));
}

Expand Down
27 changes: 19 additions & 8 deletions src/common/vuex/remote-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export enum RemoteMessageType {
};

export async function initializeRemoteChild<S>(store: Store<S>) {
if(debug) console.log('Initializing remote child!');
const initialState = await browser.runtime.sendMessage({ type: RemoteMessageType.FETCH_INITIAL_STATE }).then(res => JSON.parse(res));
store.replaceState(initialState);

Expand All @@ -32,30 +33,40 @@ export function commit(type: string, payload?: any, options?: CommitOptions) {
}

export function dispatch(type: string, payload?: any, options?: DispatchOptions) {
if(debug) console.log('dispatch', type, payload);
if(debug) console.log('dispatch SEND:', type, payload);
return browser.runtime.sendMessage({ type: RemoteMessageType.INVOKE_ACTION, data: { type, payload, options } })
.then(a => a != null ? JSON.parse(a) : a);
.then(a => {
if(debug) console.log('dispatch RETURN: ', a);
return a != null ? JSON.parse(a) : a;
}, e => {
if(debug) console.error('dispatch ERROR: ', e);
throw e;
});
}

export function initializeRemoteMaster<S = any>(store: Store<S>) {
browser.runtime.onMessage.addListener((msg: any) => {
if(debug) console.log('Initializing remote master!');
browser.runtime.onMessage.addListener(async (msg: any) => {
if(debug) console.log('Master: onMessage', msg);
if(!msg)
return Promise.resolve();
return;
else if(msg.type === RemoteMessageType.FETCH_INITIAL_STATE) {
return Promise.resolve(JSON.stringify(store.state));
return JSON.stringify(store.state);
} else if(msg.type === RemoteMessageType.INVOKE_ACTION)
return store.dispatch(msg.data.type, msg.data.payload, msg.data.options)
.then(a => a != null ? JSON.stringify(a) : a)
.then(a => new Promise( resolve => setTimeout(() => resolve(a)) ));
.then(a => a != null ? JSON.stringify(a) : a,
e => {
if(debug) console.error('Master Store Dispatch ERROR: ', e);
throw e;
});
else if(msg.type === RemoteMessageType.INVOKE_MUTATION) {
store.commit(msg.data.type, msg.data.payload, msg.data.options);
return new Promise(resolve => setTimeout(() => resolve()));
}
});

// Sync mutations on change with other parts of extension
store.subscribe((mutation) => {
if(debug) console.log('Master MUTATION: ', mutation);
browser.runtime.sendMessage({
type: RemoteMessageType.SYNC_MUTATION,
data: JSON.stringify(mutation)
Expand Down
6 changes: 3 additions & 3 deletions src/common/vuex/stores/account.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { AccountStateType, SATOSHIS_IN_BTC } from './types/account.state';
import { StateType } from './types/state';
import { decrypt, encrypt, createIv } from '../../util';
import { validateMnemonic, mnemonicToSeed } from 'bip39';
import Axios, { AxiosPromise, AxiosResponse } from 'axios';
import { config, transactions, network } from 'blockstack';
import Axios, { AxiosResponse } from 'axios';
import { config, transactions } from 'blockstack';
import { WrappedNode } from '../../data/wrapped-node';

function makeState(): AccountStateType {
Expand Down Expand Up @@ -111,7 +111,7 @@ export const accountModule: Module<AccountStateType, StateType> = {
if(amount > state.bitcoinAccount.balances[0]) throw new Error('Will not overwithdraw from the account.')
const phrase = await decrypt(state.encryptedBackupPhrase, password, state.iv);
if(!validateMnemonic(phrase)) throw new Error('Wrong password!');
const seedBuffer = mnemonicToSeed(phrase);
const seedBuffer = await mnemonicToSeed(phrase);
const masterKeychain = WrappedNode.fromSeed(seedBuffer);
const wrapped = new WrappedKeychain(masterKeychain);

Expand Down
2 changes: 1 addition & 1 deletion src/common/vuex/stores/identity.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export const identityModule: Module<IdentityStateType, StateType> = {
throw new Error('Cannot create an identity with an index < 0!');
}

const seedBuffer = mnemonicToSeed(phrase);
const seedBuffer = await mnemonicToSeed(phrase);
const masterKeychain = WrappedNode.fromSeed(seedBuffer);
const wrapped = new WrappedKeychain(masterKeychain);
const identityOwnerAddressNode = wrapped.getIdentityOwnerAddressNode(index);
Expand Down

0 comments on commit 910e918

Please sign in to comment.