OSDN Git Service

remove all the localstorage logic to chrome storage.
authorZhiting Lin <zlin035@uottawa.ca>
Thu, 8 Aug 2019 02:01:37 +0000 (10:01 +0800)
committerZhiting Lin <zlin035@uottawa.ca>
Thu, 8 Aug 2019 02:01:37 +0000 (10:01 +0800)
package-lock.json
package.json
src/background.js
src/content.js
src/views/home.vue
src/views/sendTransaction/advancedTransfer.vue
src/views/sendTransaction/transfer.vue
src/views/welcome/creation.vue

index 8de914d..bb036aa 100644 (file)
         "async": "2.6.1",
         "buffer-crc32": "0.2.13",
         "glob": "7.1.2",
-        "lodash": "4.17.10",
+        "lodash": "4.17.15",
         "readable-stream": "2.3.6",
         "tar-stream": "1.6.1",
         "walkdir": "0.0.11",
         "glob": "7.1.2",
         "graceful-fs": "4.1.11",
         "lazystream": "1.0.0",
-        "lodash": "4.17.10",
+        "lodash": "4.17.15",
         "normalize-path": "2.1.1",
         "readable-stream": "2.3.6"
       }
       "integrity": "sha512-fNEiL2+AZt6AlAw/29Cr0UDe4sRAHCpEHh54WMz+Bb7QfNcFw4h3loofyJpLeQs4Yx7yuqu/2dLgM5hKOs6HlQ==",
       "dev": true,
       "requires": {
-        "lodash": "4.17.10"
+        "lodash": "4.17.15"
       }
     },
     "async-each": {
         "convert-source-map": "1.5.1",
         "debug": "2.6.9",
         "json5": "0.5.1",
-        "lodash": "4.17.10",
+        "lodash": "4.17.15",
         "minimatch": "3.0.4",
         "path-is-absolute": "1.0.1",
         "private": "0.1.8",
         "babel-types": "6.26.0",
         "detect-indent": "4.0.0",
         "jsesc": "1.3.0",
-        "lodash": "4.17.10",
+        "lodash": "4.17.15",
         "source-map": "0.5.7",
         "trim-right": "1.0.1"
       },
         "babel-helper-function-name": "6.24.1",
         "babel-runtime": "6.26.0",
         "babel-types": "6.26.0",
-        "lodash": "4.17.10"
+        "lodash": "4.17.15"
       }
     },
     "babel-helper-explode-assignable-expression": {
       "requires": {
         "babel-runtime": "6.26.0",
         "babel-types": "6.26.0",
-        "lodash": "4.17.10"
+        "lodash": "4.17.15"
       }
     },
     "babel-helper-remap-async-to-generator": {
         "babel-template": "6.26.0",
         "babel-traverse": "6.26.0",
         "babel-types": "6.26.0",
-        "lodash": "4.17.10"
+        "lodash": "4.17.15"
       }
     },
     "babel-plugin-transform-es2015-classes": {
         "babel-runtime": "6.26.0",
         "core-js": "2.5.7",
         "home-or-tmp": "2.0.0",
-        "lodash": "4.17.10",
+        "lodash": "4.17.15",
         "mkdirp": "0.5.1",
         "source-map-support": "0.4.18"
       }
         "babel-traverse": "6.26.0",
         "babel-types": "6.26.0",
         "babylon": "6.18.0",
-        "lodash": "4.17.10"
+        "lodash": "4.17.15"
       }
     },
     "babel-traverse": {
         "debug": "2.6.9",
         "globals": "9.18.0",
         "invariant": "2.2.4",
-        "lodash": "4.17.10"
+        "lodash": "4.17.15"
       },
       "dependencies": {
         "debug": {
       "requires": {
         "babel-runtime": "6.26.0",
         "esutils": "2.0.2",
-        "lodash": "4.17.10",
+        "lodash": "4.17.15",
         "to-fast-properties": "1.0.3"
       }
     },
         "js-yaml": "3.12.0",
         "json-stable-stringify-without-jsonify": "1.0.1",
         "levn": "0.3.0",
-        "lodash": "4.17.10",
+        "lodash": "4.17.15",
         "minimatch": "3.0.4",
         "mkdirp": "0.5.1",
         "natural-compare": "1.4.0",
       "dev": true,
       "requires": {
         "glob": "7.1.2",
-        "lodash": "4.17.10",
+        "lodash": "4.17.15",
         "minimatch": "3.0.4"
       }
     },
       "requires": {
         "html-minifier": "3.5.20",
         "loader-utils": "0.2.17",
-        "lodash": "4.17.10",
+        "lodash": "4.17.15",
         "pretty-error": "2.1.1",
         "tapable": "1.0.0",
         "toposort": "1.0.7",
       "requires": {
         "http-proxy": "1.17.0",
         "is-glob": "3.1.0",
-        "lodash": "4.17.10",
+        "lodash": "4.17.15",
         "micromatch": "2.3.11"
       },
       "dependencies": {
         "cli-width": "2.2.0",
         "external-editor": "3.0.3",
         "figures": "2.0.0",
-        "lodash": "4.17.10",
+        "lodash": "4.17.15",
         "mute-stream": "0.0.7",
         "run-async": "2.3.0",
         "rxjs": "6.3.3",
       }
     },
     "lodash": {
-      "version": "4.17.10",
-      "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.10.tgz",
-      "integrity": "sha512-UejweD1pDoXu+AD825lWwp4ZGtSwgnpZxb3JDViD7StjQz+Nb/6l093lx4OQ0foGWNRoc19mWy7BzL+UAK2iVg=="
+      "version": "4.17.15",
+      "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz",
+      "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A=="
     },
     "lodash.assign": {
       "version": "4.2.0",
       "dev": true,
       "requires": {
         "glob": "7.1.2",
-        "lodash": "4.17.10",
+        "lodash": "4.17.15",
         "scss-tokenizer": "0.2.3",
         "yargs": "7.1.0"
       }
       "dev": true,
       "requires": {
         "chalk": "2.4.1",
-        "lodash": "4.17.10",
+        "lodash": "4.17.15",
         "webpack-sources": "1.1.0",
         "websocket-driver": "0.7.0"
       }
       "integrity": "sha512-TmSe1HZKeOPey3oy1Ov2iS3guIZjWvMT2BBJDzzT5jScHTjVC3mpjJofgueEzaEd6ibhxRDD6MIblDr8tzh8iQ==",
       "dev": true,
       "requires": {
-        "lodash": "4.17.10"
+        "lodash": "4.17.15"
       }
     },
     "webpack-sources": {
       "requires": {
         "archiver-utils": "1.3.0",
         "compress-commons": "1.2.2",
-        "lodash": "4.17.10",
+        "lodash": "4.17.15",
         "readable-stream": "2.3.6"
       }
     }
index 2cd0f00..7198826 100644 (file)
@@ -20,6 +20,7 @@
         "extension-streams": "^1.0.7",
         "file-saver": "^2.0.0-rc.3",
         "iview": "^3.0.0",
+        "lodash": "^4.17.15",
         "moment": "^2.22.2",
         "qrcodejs2": "0.0.2",
         "uuid": "^3.3.2",
index 48191d7..d153dae 100644 (file)
@@ -7,34 +7,12 @@ import Prompt from './prompts/Prompt';
 import * as PromptTypes from './prompts/PromptTypes'
 
 import Error from './utils/errors/Error'
-import accountAction from "@/models/account";
-import bytom from "@/models/bytom";
 
 let prompt = null;
 
 export default class Background {
   constructor() {
     this.setupInternalMessaging()
-    this.setupBytom()
-  }
-
-  setupBytom(){
-    const network = localStorage.bytomNet||'mainnet'
-    bytom.setupNet(network)
-
-    window.addEventListener('storage', storageEventHandler, false);
-    function storageEventHandler(evt){
-      if(evt.key === 'bytomNet'){
-        bytom.setupNet( evt.newValue )
-        chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
-          chrome.tabs.sendMessage(tabs[0].id, {action: "updateNetAndAccounts"}, function(response) {});
-        });
-      }else if(evt.key === 'currentAccount'){
-        chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
-          chrome.tabs.sendMessage(tabs[0].id, {action: "updateAccount"}, function(response) {});
-        });
-      }
-    }
   }
 
   setupInternalMessaging() {
@@ -192,7 +170,7 @@ export default class Background {
     Background.load(bytom => {
       const domain = payload.domain;
       if(bytom.settings.domains.find(_domain => _domain === domain)) {
-        const currentAccount = JSON.parse(localStorage.currentAccount)
+        const currentAccount =  Object.assign({}, bytom.currentAccount)
         delete(currentAccount['label'])
         delete(currentAccount['net'])
         currentAccount['accountId'] = currentAccount['guid']
@@ -209,7 +187,9 @@ export default class Background {
   }
 
   requestCurrentNetwork(sendResponse){
-    sendResponse(localStorage.bytomNet)
+    Background.load(bytom => {
+        sendResponse(bytom.settings.network);
+    })
   }
 
   send(sendResponse, payload) {
@@ -256,7 +236,7 @@ export default class Background {
   static authenticate(sendResponse, payload){
     Background.load(bytom => {
       const domain = payload.domain;
-      const currentAccount = JSON.parse(localStorage.currentAccount)
+      const currentAccount = Object.assign({}, bytom.currentAccount)
       delete(currentAccount['label'])
       delete(currentAccount['net'])
       currentAccount['accountId'] = currentAccount['guid']
index 534cb80..c16139f 100644 (file)
@@ -5,6 +5,7 @@ import InternalMessage from '@/messages/internal'
 import * as MsgTypes from './messages/types'
 import * as EventNames from '@/messages/event'
 import {strippedHost} from '@/utils/GenericTools'
+import _ from 'lodash';
 
 
 let stream = new WeakMap()
@@ -46,27 +47,28 @@ class Content {
       document.dispatchEvent(new CustomEvent('chromeBytomLoaded'))
     })
 
-    chrome.runtime.onMessage.addListener(async (obj) => {
-      switch (obj.action) {
-        case 'updateAccount':
-          const defaultAccount = await this.getDefaultAccount();
-          if(defaultAccount){
-            stream.send(
-              NetworkMessage.payload(MsgTypes.UPDATE_BYTOM, {type:'default_account', value: defaultAccount}),
-              EventNames.INJECT
-            )
-          }
-          break
-        case 'updateNetAndAccounts':
-          const net = await this.getDefaultNetwork();
+    chrome.storage.onChanged.addListener(async (evt) => {
+      if(evt.bytom.newValue.settings.network !== evt.bytom.oldValue.settings.network){
+        const net = await this.getDefaultNetwork();
+        const defaultAccount = await this.getDefaultAccount();
+        stream.send(
+          NetworkMessage.payload(MsgTypes.UPDATE_BYTOM, {type:'net', value: net}),
+          EventNames.INJECT
+        )
+        stream.send(
+          NetworkMessage.payload(MsgTypes.UPDATE_BYTOM, {type:'default_account', value: defaultAccount}),
+          EventNames.INJECT
+        )
+      }else if(!_.isEqual(evt.bytom.newValue.currentAccount,evt.bytom.oldValue.currentAccount)){
+        const defaultAccount = await this.getDefaultAccount();
+        if(defaultAccount){
           stream.send(
-            NetworkMessage.payload(MsgTypes.UPDATE_BYTOM, {type:'net', value: net}),
+            NetworkMessage.payload(MsgTypes.UPDATE_BYTOM, {type:'default_account', value: defaultAccount}),
             EventNames.INJECT
           )
-          break
+        }
       }
-      return true;
-    })
+    });
   }
 
 
index 0d4e7d9..f3eb1e8 100644 (file)
@@ -262,7 +262,10 @@ import address from "@/utils/address";
 import account from "@/models/account";
 import transaction from "@/models/transaction";
 import { BTM } from "@/utils/constants";
-import { mapGetters, mapState } from 'vuex'
+import { mapActions, mapGetters, mapState } from 'vuex'
+import * as Actions from '@/store/constants';
+import _ from 'lodash';
+
 
 const EnterActive = 'animated faster fadeInLeft';
 const LeaveActive = 'animated faster fadeOutLeft';
@@ -286,12 +289,6 @@ export default {
                 this.maskShow = false
             }
 
-            //account toggle by the list from menu
-            if (to.name == 'home' && to.params.selectedAccount != undefined) {
-                this.currentAccount = to.params.selectedAccount
-                this.refreshAccounts();
-            }
-
             // remove transition for some page
             this.enterActive = EnterActive
             this.leaveActive = LeaveActive
@@ -299,6 +296,11 @@ export default {
                 this.enterActive = ''
                 this.leaveActive = ''
             }
+            if (from.name == 'transfer-confirm') {
+              this.refreshTransactions(this.currentAccount.guid, this.currentAccount.address).then(transactions => {
+                this.transactions = transactions
+              });
+            }
         },
         currentAccount(newVal, oldVal) {
             if (newVal.guid == undefined){
@@ -342,10 +344,10 @@ export default {
             account.setupNet(this.net);
         },
         networkToggle: function (val) {
-            localStorage.bytomNet = this.network;
-            account.setupNet(this.network);
-            this.currentAccount = {}
-            this.refreshAccounts();
+            // localStorage.bytomNet = this.network;
+            // account.setupNet(this.network);
+            // this.currentAccount = {}
+            // this.refreshAccounts();
         },
         showQrcode: function () {
           this.$router.push('received')
@@ -376,6 +378,19 @@ export default {
         },
         refreshBalance: function (guid) {
             account.balance(guid)
+              .then((balances)=>{
+                if(!_.isEqual(this.currentAccount.balances, balances)){
+                    const bytom = this.bytom.clone();
+
+                    bytom.currentAccount.balances = balances;
+
+                    account.setupNet(this.net)
+                    account.list().then(accounts => {
+                      bytom.accountList = accounts;
+                      this[Actions.UPDATE_STORED_BYTOM](bytom)
+                    })
+                }
+              })
               .catch(error => {
                 console.log(error);
             });
@@ -432,6 +447,9 @@ export default {
             });
           return formattedTransactions;
         },
+      ...mapActions([
+        Actions.UPDATE_STORED_BYTOM,
+      ])
     },
     mounted() {
         this.setupNetwork();
index 46c59d3..5e75fa7 100644 (file)
@@ -83,7 +83,7 @@
         <tbody>
           <tr class="row">
             <td class="col label">{{ $t('transfer.from') }}</td>
-            <td class="col value">{{account.alias}}</td>
+            <td class="col value">{{currentAccount.alias}}</td>
           </tr>
           <div class="divider"></div>
           <tr class="row">
@@ -94,7 +94,7 @@
             <td class="col label">Output</td>
             <td class="col value" v-bind:class="{ hide: !full }" >{{transaction.output}}</td>
           </tr>
-          <tr class="row">
+          <tr v-if="transaction.args" class="row">
             <td class="col label">Args</td>
             <td class="col value" v-bind:class="{ hide: !full }" >{{transaction.args}}</td>
           </tr>
@@ -139,13 +139,14 @@ import getLang from "@/assets/language/sdk";
 import { LocalStream } from 'extension-streams';
 import {apis} from '@/utils/BrowserApis';
 import NotificationService from '../../services/NotificationService'
+import { mapActions, mapGetters, mapState } from 'vuex'
 
 export default {
     data() {
         return {
             full: false,
             // full2: false,
-            account: {},
+            // account: {},
             transaction: {
                 input: "",
                 output: "",
@@ -159,6 +160,9 @@ export default {
         };
     },
     computed: {
+      ...mapGetters([
+        'currentAccount',
+      ])
     },
     watch: {
     },
@@ -174,14 +178,14 @@ export default {
                 onCancel: this.onCancel
             });
 
-            transaction.buildTransaction(this.account.guid,  this.transaction.input, this.transaction.output, this.transaction.fee * 100000000, this.transaction.confirmations).then(async (result) => {
+            transaction.buildTransaction(this.currentAccount.guid,  this.transaction.input, this.transaction.output, this.transaction.fee * 100000000, this.transaction.confirmations).then(async (result) => {
 
               let arrayData
               if(this.transaction.args){
                 arrayData =  await transaction.convertArgument(this.transaction.args)
               }
 
-              return transaction.advancedTransfer(this.account.guid, result, this.password, arrayData)
+              return transaction.advancedTransfer(this.currentAccount.guid, result, this.password, arrayData)
                   .then((resp) => {
                       loader.hide();
                       this.prompt.responder(resp);
@@ -203,8 +207,6 @@ export default {
             });
         }
     }, mounted() {
-          this.account = JSON.parse(localStorage.currentAccount);
-
           this.prompt = window.data || apis.extension.getBackgroundPage().notification || null;
 
           if(this.prompt.data !== undefined){
index 403a9d6..cdea3ed 100644 (file)
@@ -119,6 +119,8 @@ import transaction from "@/models/transaction";
 import getLang from "@/assets/language/sdk";
 import Confirm from "./transferConfirm";
 import { BTM } from "@/utils/constants";
+import { mapActions, mapGetters, mapState } from 'vuex'
+import * as Actions from '@/store/constants';
 
 export default {
     components: {
@@ -139,7 +141,6 @@ export default {
                 }
             ],
             show: false,
-            accounts: [],
             assets: {
                 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff: "BTM"
             },
@@ -161,7 +162,14 @@ export default {
     computed: {
         unit() {
             return this.assets[this.transaction.asset];
-        }
+        },
+      ...mapState([
+        'bytom'
+      ]),
+      ...mapGetters([
+        'currentAccount',
+        'accountList'
+      ])
     },
     watch: {
         selectAsset: function (val) {
@@ -176,7 +184,7 @@ export default {
             this.guid = newAccount.guid;
         },
         guid: function (newGuid) {
-            this.accounts.forEach(account => {
+            this.accountList.forEach(account => {
                 if (account.guid == newGuid.guid) {
                     this.account = account;
                     return;
@@ -239,16 +247,11 @@ export default {
             });
         }
     }, mounted() {
-        if (this.$route.params.account != undefined) {
-            this.account = this.$route.params.account;
-        }
-
         //detect injection
         if(this.$route.query.type === 'popup'){
           if (this.$route.query.from != undefined) {
               this.guid = this.$route.query.from
-          }else{
-              this.account = JSON.parse(localStorage.currentAccount);
+              this.account = this.accountList.filter(e => e.guid === this.guid)[0]
           }
 
           if (this.$route.query.asset != undefined) {
@@ -266,22 +269,9 @@ export default {
           if(this.$route.query.confirmations != undefined) {
               this.transaction.confirmations = this.$route.query.confirmations
           }
+        }else{
+          this.account = this.currentAccount
         }
-
-
-        account.setupNet(localStorage.bytomNet);
-        account.list().then(accounts => {
-            this.accounts = accounts;
-            this.accounts.forEach(function (ele) {
-                ele.label = ele.alias
-                ele.value = ele.guid
-            });
-            if (!this.guid) {
-                this.account = accounts[0]
-            }else{
-              this.account = this.accounts.filter(e => e.guid === this.guid)[0]
-            }
-        });
     }
 };
 </script>
index 150a01f..fd23c44 100644 (file)
@@ -288,7 +288,6 @@ export default {
                 onCancel: this.onCancel
             });
             account.create(this.formItem.accAlias, null, this.formItem.passwd1).then(currentAccount => {
-                // localStorage.login = true;
               this[Actions.CREATE_NEW_BYTOM](this.selected.value).then(() =>{
                 loader.hide();
                 this.formItem = {};
@@ -318,7 +317,6 @@ export default {
             return;
           }
           account.restore(this.restore.fileTxt).then(res => {
-            // localStorage.login = true;
             this[Actions.IMPORT_BYTOM]().then(() =>{
               this.$router.push('/');
             });