Skip to content

Commit

Permalink
Merge conflicts, linter
Browse files Browse the repository at this point in the history
  • Loading branch information
youaresofunny committed Jul 29, 2018
1 parent ca2b85c commit 5dcea37
Show file tree
Hide file tree
Showing 10 changed files with 455 additions and 126 deletions.
59 changes: 29 additions & 30 deletions src/actions/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,29 @@ export const lockWallet = ({ commit }) => {
commit(types.ACCOUNT_LOCK_WALLET);
};

export const suggestPassword = API.Account.suggestPassword;
export const { suggestPassword } = API.Account;

export const loginWithPassword = async ({ commit }, { name, password }) => {
let {privKey: activeKey} = API.Account.generateKeyFromPassword(
name,
"owner",
password
const { privKey: activeKey } = API.Account.generateKeyFromPassword(
name,
'owner',
password
);
let {privKey: ownerKey} = API.Account.generateKeyFromPassword(
name,
"active",
password
const { 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 });
Expand All @@ -96,21 +96,21 @@ export const loginWithPassword = async ({ commit }, { name, password }) => {
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};
return { success: false, error: restored.error };
}

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

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

const passwordAes = Aes.fromSeed(password);
Expand All @@ -121,7 +121,7 @@ export const restoreBackup = async ({ commit }, { backup, password }) => {

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

const user = await API.Account.getUser(name)
const user = await API.Account.getUser(name);
if (user.success) {
const userType = 'wallet';
PersistentStorage.saveUserData({
Expand All @@ -134,22 +134,21 @@ export const restoreBackup = async ({ commit }, { backup, password }) => {

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' };
}
}
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
const { privKey: activeKey } = API.Account.generateKeyFromPassword(
name,
'owner',
password
);
let {privKey: ownerKey} = API.Account.generateKeyFromPassword(
name,
"active",
password
const { privKey: ownerKey } = API.Account.generateKeyFromPassword(
name,
'active',
password
);

const result = await API.Account.createAccount({
Expand All @@ -163,7 +162,7 @@ export const signupWithPassword = async ({ commit }, { name, password }) => {
const keys = {
active: activeKey,
owner: ownerKey
}
};

const userType = 'password';
PersistentStorage.saveUserData({
Expand All @@ -180,7 +179,7 @@ export const signupWithPassword = async ({ commit }, { name, password }) => {
success: false,
error: result.error
};
}
};

/**
* Creates account & wallet for user
Expand Down
14 changes: 7 additions & 7 deletions 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, hidden = false }) => {
export const fetchAssets = async (store, { assets }) => {
const { commit, getters } = store;
const currentAssetsIds = Object.keys(getters.getAssets);

Expand Down Expand Up @@ -51,10 +51,10 @@ export const fetchDefaultAssets = async (store) => {
}
};

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

export const showAsset = async ({ commit }, asset_id) => {
commit(types.SHOW_ASSET, asset_id);
}
export const showAsset = async ({ commit }, assetId) => {
commit(types.SHOW_ASSET, assetId);
};
2 changes: 1 addition & 1 deletion src/getters/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ export function getAssetById({ assets }) {

export function getHideList({ hiddenAssetsIds }) {
return hiddenAssetsIds;
}
}
1 change: 0 additions & 1 deletion src/modules/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ const mutations = {
state.brainkeyBackupDate = backupDate;
state.passwordPubkey = passwordPubkey;
state.userType = userType;

},
[types.ACCOUNT_LOGOUT]: (state) => {
state.userId = null;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const mutations = {
Vue.set(state.assets, id, assets[id]);
});
state.hiddenAssetsIds = PersistentStorage.getJSON('hidden_assets') || [];
console.log(state.hiddenAssetsIds)
console.log(state.hiddenAssetsIds);
state.pending = false;
},
[types.FETCH_ASSETS_ERROR](state) {
Expand Down
4 changes: 1 addition & 3 deletions src/modules/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ const getters = {
const byteLength = utils.getMemoSizeFast(memo);
const memoPrice = Math.floor((byteLength * state.fees.transfer.kbytePrice) / 1024);
return transferPrice + memoPrice;
} else {
return transferPrice;
}

return transferPrice;
};
}
};
Expand Down
14 changes: 7 additions & 7 deletions src/services/api/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ export const suggestBrainkey = (dictionary) => {
};

export const suggestPassword = () => {
console.log('inside')
return "P" + key.get_random_key().toWif().substr(0, 45);
console.log('inside');
return 'P' + key.get_random_key().toWif().substr(0, 45);
};

export const generateKeyFromPassword = (accountName, role, password) => {
let seed = accountName + role + password;
let privKey = PrivateKey.fromSeed(seed);
let pubKey = privKey.toPublicKey().toString();
const seed = accountName + role + password;
const privKey = PrivateKey.fromSeed(seed);
const pubKey = privKey.toPublicKey().toString();

return {privKey, pubKey};
}
return { privKey, pubKey };
};

export const getUser = async (nameOrId) => {
try {
Expand Down
130 changes: 63 additions & 67 deletions src/services/api/backup.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,80 @@
import { key, PrivateKey, PublicKey, Aes } from 'bitsharesjs';
import { PrivateKey, PublicKey, Aes } from 'bitsharesjs';
import { getAccountIdByOwnerPubkey } from './account';
import lib from '../../utils/lzma/lzma_worker-min.js';

const decryptWalletBackup = (wif, input) => {
return new Promise((resolve, reject) => {
let backupBuffer;
if (!Buffer.isBuffer(input)) {
backupBuffer = Buffer.from(input, 'binary');
} else {
backupBuffer = input;
}

const restoreBackup = async ({ password, backup }) => {
const privateKey = PrivateKey.fromSeed(password || "");
const result = await restore(privateKey.toWif(), backup, 'test');
return result;
}

const restore = async (backup_wif, backup, wallet_name) => {
const privateKey = PrivateKey.fromWif(wif);
let publicKey;
try {
const wallet = await decryptWalletBackup(backup_wif, backup);
console.log('Wallet restore', wallet);
if (!wallet.linked_accounts.length) {
const pubkey = wallet.private_keys[0].pubkey;
const id = await getAccountIdByOwnerPubkey(pubkey);
wallet.linked_accounts[0] = { name: id[0] };
}
return { success: true, wallet };
publicKey = PublicKey.fromBuffer(backupBuffer.slice(0, 33));
} catch (e) {
return { success: false, error: e }
console.error(e, e.stack);
throw new Error('Invalid backup file');
}
}

const decryptWalletBackup = (backup_wif, backup_buffer) => {
return new Promise((resolve, reject) => {
if (!Buffer.isBuffer(backup_buffer))
backup_buffer = new Buffer(backup_buffer, "binary");

let private_key = PrivateKey.fromWif(backup_wif);
let public_key;
try {
public_key = PublicKey.fromBuffer(backup_buffer.slice(0, 33));
} catch (e) {
console.error(e, e.stack);
throw new Error("Invalid backup file");
}

const slicedBuffer = backupBuffer.slice(33);
let decryptedBuffer;
try {
decryptedBuffer = Aes.decrypt_with_checksum(
privateKey,
publicKey,
null /* nonce */,
slicedBuffer
);
} catch (error) {
reject(new Error('invalid_decryption_key'));
return;
}

console.log('restoreBackup', backup_buffer, backup_wif)
console.log('Decrypted', decryptedBuffer);

backup_buffer = backup_buffer.slice(33);
try {
lib.LZMA_WORKER.decompress(decryptedBuffer, walletJson => {
try {
backup_buffer = Aes.decrypt_with_checksum(
private_key,
public_key,
null /*nonce*/,
backup_buffer
);
const wallet = JSON.parse(walletJson);
console.log('Wallet obj', wallet);
resolve(wallet);
} catch (error) {
reject("invalid_decryption_key");
return;
console.error('Error parsing wallet json');
reject(new Error('Error parsing wallet json'));
}
});
} catch (error) {
console.error('Error decompressing wallet', error, error.stack);
reject(new Error('Error decompressing wallet'));
}
});
};

console.log('Decrypted', backup_buffer);
const restore = async (wif, backup) => {
try {
const wallet = await decryptWalletBackup(wif, backup);
if (!wallet.linked_accounts.length) {
const { pubkey } = wallet.private_keys[0];
const id = await getAccountIdByOwnerPubkey(pubkey);
wallet.linked_accounts[0] = { name: id[0] };
}
return { success: true, wallet };
} catch (e) {
return { success: false, error: e };
}
};

const restoreBackup = async ({ password, backup }) => {
const privateKey = PrivateKey.fromSeed(password || '');
const result = await restore(privateKey.toWif(), backup);
return result;
};

try {
lib.LZMA_WORKER.decompress(backup_buffer, wallet_string => {
try {
let wallet_object = JSON.parse(wallet_string);
console.log('Wallet obj', wallet_object)
resolve(wallet_object);
} catch (error) {
if (!wallet_string) wallet_string = "";
console.error(
"Error parsing wallet json",
wallet_string.substring(0, 10) + "..."
);
reject("Error parsing wallet json");
}
});
} catch (error) {
console.error("Error decompressing wallet", error, error.stack);
reject("Error decompressing wallet");
return;
}
});
}

export default {
restoreBackup
}
restoreBackup
};
13 changes: 6 additions & 7 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,13 @@ export const getMemoSizeFast = (memo) => {

if (memo.length < 12) {
return minimalLength;
} else {
let countSteps = 0;
for (let i = memo.length; i >= 12; i -= step) {
countSteps++;
}
return minimalLength + countSteps * step;
}
}
let countSteps = 0;
for (let i = memo.length; i >= 12; i -= step) {
countSteps += 1;
}
return minimalLength + (countSteps * step);
};


/** Calculates distribution 0..1 of total amount of assets expressed
Expand Down
342 changes: 340 additions & 2 deletions src/utils/lzma/lzma_worker-min.js

Large diffs are not rendered by default.

0 comments on commit 5dcea37

Please sign in to comment.