Skip to content

Commit

Permalink
caching
Browse files Browse the repository at this point in the history
  • Loading branch information
nsjames committed Jan 30, 2019
1 parent 920a626 commit b50490b
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"node-notifier": "^5.3.0",
"scrypt-async": "^2.0.1",
"socket.io": "^2.1.1",
"tweetnacl": "^1.0.1",
"vue-markdown": "^2.2.4"
},
"devDependencies": {
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/PluginRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ class PluginRepositorySingleton {
return acc;
}, {})
}

bustCaches(){
this.signatureProviders().map(sp => {
sp.bustCache();
})
}
}

const PluginRepository = new PluginRepositorySingleton();
Expand Down
6 changes: 5 additions & 1 deletion src/plugins/defaults/eos.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ const EXPLORER = {
export default class EOS extends Plugin {

constructor(){ super(Blockchains.EOSIO, PluginTypes.BLOCKCHAIN_SUPPORT) }

bustCache(){ cachedInstances = {}; }
defaultExplorer(){ return EXPLORER; }
accountFormatter(account){ return `${account.name}@${account.authority}` }
returnableAccount(account){ return { name:account.name, authority:account.authority, publicKey:account.publicKey, blockchain:Blockchains.EOSIO }}
Expand Down Expand Up @@ -209,6 +211,7 @@ export default class EOS extends Plugin {
const network = account.network();
const eos = Eos({httpEndpoint:network.fullhost(), chainId:network.chainId, signProvider});


const perms = Object.keys(keys).map(permission => {
if(!keys[permission].length) return;

Expand Down Expand Up @@ -245,7 +248,8 @@ export default class EOS extends Plugin {
}
}).filter(x => !!x);

const options = {authorization:[`${account.name}@owner`]};
const hasOwner = (keys.hasOwnProperty('owner') && keys.owner.length) || account.authorities().map(x => x.authority).includes('owner');
const options = {authorization:[`${account.name}@${hasOwner?'owner':'active'}`]};
return eos.transaction(tr => perms.map(perm => tr.updateauth(perm, options)))
.catch(res => {
popupError(res);
Expand Down
12 changes: 11 additions & 1 deletion src/plugins/defaults/eth.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import BigNumber from "bignumber.js";
import TokenService from "../../services/TokenService";
import {localizedState} from "../../localization/locales";
import LANG_KEYS from "../../localization/keys";
import nacl from 'tweetnacl'
const erc20abi = require('../../data/abis/erc20');

const web3util = new Web3();
Expand Down Expand Up @@ -67,6 +68,8 @@ const EXPLORER = {
export default class ETH extends Plugin {

constructor(){ super(Blockchains.ETH, PluginTypes.BLOCKCHAIN_SUPPORT) }

bustCache(){ cachedInstances = {}; }
defaultExplorer(){ return EXPLORER; }
accountFormatter(account){ return `${account.publicKey}` }
returnableAccount(account){ return { address:account.publicKey, blockchain:Blockchains.ETH }}
Expand Down Expand Up @@ -213,13 +216,20 @@ export default class ETH extends Plugin {
if(account && KeyPairService.isHardware(publicKey))
return await HardwareService.sign(account, transaction);

const loom = transaction.hasOwnProperty('loom') && !!transaction.loom;
if(transaction.hasOwnProperty('transaction')) transaction = transaction.transaction;

const basePrivateKey = await KeyPairService.publicToPrivate(publicKey);
if(!basePrivateKey) return;

const privateKey = ethUtil.addHexPrefix(basePrivateKey);

if(loom){
const sigMsg = nacl.sign(transaction, basePrivateKey);
return sigMsg.slice(0, nacl.sign.signatureLength);
}

const tx = new EthTx(transaction);
const privateKey = ethUtil.addHexPrefix(basePrivateKey);
tx.sign(ethUtil.toBuffer(privateKey));
return ethUtil.addHexPrefix(tx.serialize().toString('hex'));
}
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/defaults/trx.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const EXPLORER = {
export default class TRX extends Plugin {

constructor(){ super(Blockchains.TRX, PluginTypes.BLOCKCHAIN_SUPPORT) }

bustCache(){ cachedInstances = {}; }
defaultExplorer(){ return EXPLORER; }
accountFormatter(account){ return `${account.publicKey}` }
returnableAccount(account){ return { address:account.publicKey, blockchain:Blockchains.TRX }}
Expand Down
8 changes: 8 additions & 0 deletions src/services/ApiService.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,10 @@ export default class ApiService {
if(store.state.scatter.settings.networks.find(x => x.fromOrigin === request.payload.origin && x.createdAt > (+new Date() - ((3600 * 12)*1000))))
return resolve({id:request.id, result:new Error("network_timeout", "You can only add 1 network every 12 hours.")});

// All applications can only add 5 networks every 12 hours.
if(store.state.scatter.settings.networks.filter(x => x.createdAt > (+new Date() - ((3600 * 12)*1000))) > 5)
return resolve({id:request.id, result:new Error("network_timeout", "Too many networks were added over the past 12 hours")});

network.fromOrigin = request.payload.origin;
const scatter = store.state.scatter.clone();
scatter.settings.networks.push(network);
Expand Down Expand Up @@ -552,6 +556,10 @@ export default class ApiService {
if(store.state.scatter.settings.tokens.filter(x => x.fromOrigin === request.payload.origin && x.createdAt > (+new Date() - ((3600 * 12)*1000))).length > 5)
return resolve({id:request.id, result:new Error("token_timeout", "You can only add up to 5 tokens every 12 hours.")});

// All applications can only add 15 tokens every 12 hours.
if(store.state.scatter.settings.tokens.filter(x => x.createdAt > (+new Date() - ((3600 * 12)*1000))).length > 15)
return resolve({id:request.id, result:new Error("token_timeout", "Too many tokens were added over the past 12 hours.")});

const exists = await TokenService.hasToken(token);
if(exists) return resolve({id:request.id, result:new Error("token_exists", "The user already has this token in their Scatter.")});

Expand Down
4 changes: 4 additions & 0 deletions src/services/NetworkService.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {Popup} from '../models/popups/Popup'
import BalanceService from "./BalanceService";
import {localizedState} from "../localization/locales";
import LANG_KEYS from "../localization/keys";
import PluginRepository from "../plugins/PluginRepository";


export default class NetworkService {
Expand Down Expand Up @@ -37,13 +38,15 @@ export default class NetworkService {
await AccountService.importAllAccountsForNetwork(network);
BalanceService.loadAllBalances(true);
PopupService.push(Popup.snackbar(localizedState(NETWORK.Saved), "check"));
PluginRepository.bustCaches();
return true;
}

static async removeNetwork(network){
return new Promise(resolve => {
PopupService.push(Popup.prompt("Deleting Network", "This will delete this network, as well as all associated accounts and their permissions.", async accepted => {
if(accepted) {
PluginRepository.bustCaches();
const scatter = store.state.scatter.clone();

// Removing accounts and permissions for this network
Expand All @@ -64,6 +67,7 @@ export default class NetworkService {
scatter.settings.updateOrPushNetwork(network);
await store.dispatch(Actions.SET_SCATTER, scatter);
PopupService.push(Popup.snackbar(localizedState(LANG_KEYS.SNACKBARS.NETWORK.Saved), "check"));
PluginRepository.bustCaches();
}

}
2 changes: 1 addition & 1 deletion src/styles/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ html, body {
}

*, *::after, *::before {
-webkit-user-select: none;
//-webkit-user-select: none;
-webkit-user-drag: none;
-webkit-app-region: no-drag;
}
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9741,6 +9741,11 @@ tweetnacl@^0.14.3, tweetnacl@~0.14.0:
version "0.14.5"
resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-0.14.5.tgz#5ae68177f192d4456269d108afa93ff8743f4f64"

tweetnacl@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-1.0.1.tgz#2594d42da73cd036bd0d2a54683dd35a6b55ca17"
integrity sha512-kcoMoKTPYnoeS50tzoqjPY3Uv9axeuuFAZY9M/9zFnhoVvRfxz9K29IMPD7jGmt2c8SW7i3gT9WqDl2+nV7p4A==

type-check@~0.3.2:
version "0.3.2"
resolved "https://registry.yarnpkg.com/type-check/-/type-check-0.3.2.tgz#5884cab512cf1d355e3fb784f30804b2b520db72"
Expand Down

0 comments on commit b50490b

Please sign in to comment.