Skip to content

Commit

Permalink
Merge pull request #78 from TrustyFund/bench
Browse files Browse the repository at this point in the history
More features
  • Loading branch information
youaresofunny authored Jul 29, 2018
2 parents c43595c + 442daa5 commit ca2b85c
Show file tree
Hide file tree
Showing 16 changed files with 324 additions and 17 deletions.
136 changes: 132 additions & 4 deletions src/actions/account.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { PrivateKey, key, Aes } from 'bitsharesjs';
import * as types from '../mutations';
// import { getAccountIdByOwnerPubkey, getAccount } from '../services/wallet.js';
import API from '../services/api';
import PersistentStorage from '../services/persistent-storage';

Expand Down Expand Up @@ -60,6 +59,129 @@ export const lockWallet = ({ commit }) => {
commit(types.ACCOUNT_LOCK_WALLET);
};

export const suggestPassword = API.Account.suggestPassword;

export const loginWithPassword = async ({ commit }, { name, password }) => {
let {privKey: activeKey} = API.Account.generateKeyFromPassword(
name,
"owner",
password
);
let {privKey: ownerKey} = API.Account.generateKeyFromPassword(
name,
"active",
password
);

const ownerPubkey = ownerKey.toPublicKey().toString();
const userId = await API.Account.getAccountIdByOwnerPubkey(ownerPubkey);

const id = userId && userId[0];
if (id) {
const keys = {
active: activeKey,
owner: ownerKey
}

const userType = 'password';
PersistentStorage.saveUserData({ id, userType });

commit(types.ACCOUNT_PASSWORD_LOGIN_COMPLETE, { keys, userId: id });
return {
success: true
};
}
commit(types.ACCOUNT_LOGIN_ERROR, { error: 'Login error' });
return {
success: false,
error: 'Invalid username or password'
};
}

export const restoreBackup = async ({ commit }, { backup, password }) => {
const restored = await API.Backup.restoreBackup({ backup, password });
console.log('restored', restored);
if (!restored.success) {
commit(types.ACCOUNT_LOGIN_ERROR, { error: 'Login error' });
return { success: false, error: restored.error};
}

console.log('Restored action', restored);

const {
wallet: [wallet],
linked_accounts: [ { name }]
} = restored.wallet;

const passwordAes = Aes.fromSeed(password);
const encryptionPlainbuffer = passwordAes.decryptHexToBuffer(wallet.encryption_key);
const aesPrivate = Aes.fromSeed(encryptionPlainbuffer);

const brainkey = aesPrivate.decryptHexToText(wallet.encrypted_brainkey);

const newWallet = createWallet({ password, brainkey });

const user = await API.Account.getUser(name)
if (user.success) {
const userType = 'wallet';
PersistentStorage.saveUserData({
id: user.data.account.id,
encryptedBrainkey: newWallet.encryptedBrainkey,
encryptionKey: newWallet.encryptionKey,
passwordPubkey: newWallet.passwordPubkey,
userType
});

commit(types.ACCOUNT_LOGIN_COMPLETE, { wallet: newWallet, userId: user.data.account.id });
return { success: true };
} else {
commit(types.ACCOUNT_LOGIN_ERROR, { error: 'Login error' });
return { success: false, error: 'No such user' };
}
}

export const signupWithPassword = async ({ commit }, { name, password }) => {
let {privKey: activeKey} = API.Account.generateKeyFromPassword(
name,
"owner",
password
);
let {privKey: ownerKey} = API.Account.generateKeyFromPassword(
name,
"active",
password
);

const result = await API.Account.createAccount({
name,
activeKey,
ownerKey,
});

if (result.success) {
const userId = result.id;
const keys = {
active: activeKey,
owner: ownerKey
}

const userType = 'password';
PersistentStorage.saveUserData({
id: userId,
userType
});

commit(types.ACCOUNT_PASSWORD_LOGIN_COMPLETE, { keys, userId });
return { success: true };
}

commit(types.ACCOUNT_SIGNUP_ERROR, { error: result.error });
return {
success: false,
error: result.error
};
}

/**
* Creates account & wallet for user
* @param {string} name - user name
Expand All @@ -81,11 +203,14 @@ export const signup = async (state, { name, password, dictionary, email }) => {
const userId = result.id;
const wallet = createWallet({ password, brainkey });
commit(types.ACCOUNT_SIGNUP_COMPLETE, { wallet, userId });

const userType = 'wallet';
PersistentStorage.saveUserData({
id: userId,
encryptedBrainkey: wallet.encryptedBrainkey,
encryptionKey: wallet.encryptionKey,
passwordPubkey: wallet.passwordPubkey
passwordPubkey: wallet.passwordPubkey,
userType
});
return { success: true };
}
Expand Down Expand Up @@ -122,11 +247,13 @@ export const login = async (state, { password, brainkey }) => {
const userId = await API.Account.getAccountIdByOwnerPubkey(ownerPubkey);
const id = userId && userId[0];
if (id) {
const userType = 'wallet';
PersistentStorage.saveUserData({
id,
encryptedBrainkey: wallet.encryptedBrainkey,
encryptionKey: wallet.encryptionKey,
passwordPubkey: wallet.passwordPubkey
passwordPubkey: wallet.passwordPubkey,
userType
});
commit(types.ACCOUNT_LOGIN_COMPLETE, { wallet, userId: id });
return {
Expand Down Expand Up @@ -179,7 +306,8 @@ export const checkCachedUserData = ({ commit }) => {
encryptedBrainkey: data.encryptedBrainkey,
encryptionKey: data.encryptionKey,
backupDate,
passwordPubkey: data.passwordPubkey
passwordPubkey: data.passwordPubkey,
userType: data.userType
});
}
};
Expand Down
10 changes: 9 additions & 1 deletion src/actions/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import config from '../../config';
* Fetches assets objects from bitsharesjs-ws
* @param {Array} assets - list of assets ids/symbold to fetch
*/
export const fetchAssets = async (store, { assets }) => {
export const fetchAssets = async (store, { assets, hidden = false }) => {
const { commit, getters } = store;
const currentAssetsIds = Object.keys(getters.getAssets);

Expand Down Expand Up @@ -50,3 +50,11 @@ export const fetchDefaultAssets = async (store) => {
commit(types.SAVE_DEFAULT_ASSETS_IDS, { ids });
}
};

export const hideAsset = async ({ commit }, asset_id) => {
commit(types.HIDE_ASSET, asset_id );
}

export const showAsset = async ({ commit }, asset_id) => {
commit(types.SHOW_ASSET, asset_id);
}
7 changes: 6 additions & 1 deletion src/getters/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export const getBrainkey = state => {
};

export const getKeys = state => {
if (state.keys) {
return state.keys;
}
const brainkey = getBrainkey(state);
if (!brainkey) return null;
return {
Expand All @@ -26,7 +29,7 @@ export const isValidPassword = state => {
};

export const isLocked = state => {
return !state.aesPrivate;
return !state.aesPrivate && !state.keys;
};

export const getAccountError = state => {
Expand Down Expand Up @@ -60,3 +63,5 @@ export const getCurrentUserBalances = state => {
export const getCurrentUserData = state => {
return state.userData;
};

export const isPasswordLogin = state => state.userType === 'password';
4 changes: 4 additions & 0 deletions src/getters/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ export function getAssetById({ assets }) {
precision: 1
});
}

export function getHideList({ hiddenAssetsIds }) {
return hiddenAssetsIds;
}
16 changes: 14 additions & 2 deletions src/modules/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ const initialState = {
encryptedBrainkey: null,
brainkeyBackupDate: null,
encryptionKey: null,
keys: null,
created: null,
aesPrivate: null,
userId: null,
error: null,
pending: false,
userData: null,
userFetching: false,
userError: false
userError: false,
userType: 'wallet',
};

const mutations = {
Expand All @@ -32,6 +34,12 @@ const mutations = {
state.created = new Date();
state.userId = userId;
},
[types.ACCOUNT_PASSWORD_LOGIN_COMPLETE]: (state, { keys, userId }) => {
state.pending = false;
state.userId = userId;
state.keys = keys;
state.userType = 'password';
},
[types.ACCOUNT_SIGNUP_ERROR]: (state, { error }) => {
state.pending = false;
state.error = error;
Expand All @@ -46,24 +54,28 @@ const mutations = {
state.encryptedBrainkey = wallet.encryptedBrainkey;
state.encryptionKey = wallet.encryptionKey;
state.aesPrivate = wallet.aesPrivate;
state.userType = 'wallet';
},
[types.ACCOUNT_LOGIN_ERROR]: (state, { error }) => {
state.pending = false;
state.error = error;
},
[types.ACCOUNT_LOCK_WALLET]: (state) => {
state.aesPrivate = null;
state.keys = null;
},
[types.ACCOUNT_UNLOCK_WALLET]: (state, aesPrivate) => {
state.aesPrivate = aesPrivate;
},
[types.SET_ACCOUNT_USER_DATA]: (state, { userId, encryptedBrainkey,
encryptionKey, backupDate, passwordPubkey }) => {
encryptionKey, backupDate, passwordPubkey, userType }) => {
state.userId = userId;
state.encryptedBrainkey = encryptedBrainkey;
state.encryptionKey = encryptionKey;
state.brainkeyBackupDate = backupDate;
state.passwordPubkey = passwordPubkey;
state.userType = userType;

},
[types.ACCOUNT_LOGOUT]: (state) => {
state.userId = null;
Expand Down
15 changes: 15 additions & 0 deletions src/modules/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import Vue from 'vue';
import * as types from '../mutations';
import * as actions from '../actions/assets';
import * as getters from '../getters/assets';
import PersistentStorage from '../services/persistent-storage.js';

const initialState = {
defaultAssetsIds: [],
assets: {},
hiddenAssetsIds: [],
pending: false
};

Expand All @@ -17,13 +19,26 @@ const mutations = {
Object.keys(assets).forEach(id => {
Vue.set(state.assets, id, assets[id]);
});
state.hiddenAssetsIds = PersistentStorage.getJSON('hidden_assets') || [];
console.log(state.hiddenAssetsIds)
state.pending = false;
},
[types.FETCH_ASSETS_ERROR](state) {
state.pending = false;
},
[types.SAVE_DEFAULT_ASSETS_IDS](state, { ids }) {
state.defaultAssetsIds = ids;
},
[types.HIDE_ASSET](state, id) {
state.hiddenAssetsIds.push(id);
PersistentStorage.set('hidden_assets', state.hiddenAssetsIds);
},
[types.SHOW_ASSET](state, id) {
state.hiddenAssetsIds.splice(
state.hiddenAssetsIds.indexOf(id),
1
);
PersistentStorage.set('hidden_assets', state.hiddenAssetsIds);
}
};

Expand Down
11 changes: 8 additions & 3 deletions src/modules/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ const getters = {
getTransferFee: state => state.fees.transfer.fee,
getMemoPrice: (state) => {
return (memo) => {
const byteLength = utils.getMemoSize(memo);
const transferPrice = state.fees.transfer.fee;
const memoPrice = Math.floor((byteLength * state.fees.transfer.kbytePrice) / 1024);
return transferPrice + memoPrice;
if (memo) {
const byteLength = utils.getMemoSizeFast(memo);
const memoPrice = Math.floor((byteLength * state.fees.transfer.kbytePrice) / 1024);
return transferPrice + memoPrice;
} else {
return transferPrice;
}

};
}
};
Expand Down
3 changes: 3 additions & 0 deletions src/mutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const FETCH_USER_ERROR = 'FETCH_USER_ERROR';
export const ACCOUNT_SIGNUP_REQUEST = 'ACCOUNT_SIGNUP_REQUEST';
export const ACCOUNT_SIGNUP_COMPLETE = 'ACCOUNT_SIGNUP_COMPLETE';
export const ACCOUNT_SIGNUP_ERROR = 'ACCOUNT_SIGNUP_ERROR';
export const ACCOUNT_PASSWORD_LOGIN_COMPLETE = 'ACCOUNT_PASSWORD_LOGIN_COMPLETE';
export const ACCOUNT_LOGIN_REQUEST = 'ACCOUNT_LOGIN_REQUEST';
export const ACCOUNT_LOGIN_COMPLETE = 'ACCOUNT_LOGIN_COMPLETE';
export const ACCOUNT_LOGIN_ERROR = 'ACCOUNT_LOGIN_ERROR';
Expand Down Expand Up @@ -38,6 +39,8 @@ export const FETCH_ASSETS_ERROR = 'FETCH_ASSETS_ERROR';
export const FETCH_DEFAULT_ASSETS_REQUEST = 'FETCH_DEFAULT_ASSETS_REQUEST';
export const FETCH_DEFAULT_ASSETS_COMPLETE = 'FETCH_DEFAULT_ASSETS_COMPLETE';
export const FETCH_DEFAULT_ASSETS_ERROR = 'FETCH_DEFAULT_ASSETS_ERROR';
export const HIDE_ASSET = 'HIDE_ASSET';
export const SHOW_ASSET = 'SHOW_ASSET';

export const SAVE_DEFAULT_ASSETS_IDS = 'SAVE_DEFAULT_ASSETS_IDS';

Expand Down
4 changes: 3 additions & 1 deletion src/services/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Market from './api/market';
import Operations from './api/operations';
import Openledger from './api/openledger';
import Parameters from './api/parameters';
import Backup from './api/backup';

const API = {
Connection,
Expand All @@ -17,7 +18,8 @@ const API = {
Market,
Operations,
Openledger,
Parameters
Parameters,
Backup
};

export default API;
Loading

0 comments on commit ca2b85c

Please sign in to comment.