OSDN Git Service

update signMessage and sendAdvancedTransaction logic.
authorZhiting Lin <zlin035@uottawa.ca>
Tue, 25 Jun 2019 06:03:55 +0000 (14:03 +0800)
committerZhiting Lin <zlin035@uottawa.ca>
Tue, 25 Jun 2019 06:03:55 +0000 (14:03 +0800)
src/background.js
src/content.js
src/prompts/Prompt.js
src/prompts/PromptTypes.js
src/views/prompts/authentication.vue
src/views/sendTransaction/advancedTransfer.vue
src/views/sendTransaction/signMessage.vue
src/views/sendTransaction/transfer.vue

index fd85f89..3cbeec6 100644 (file)
@@ -87,53 +87,18 @@ export default class Background {
   }
 
   signMessage(sendResponse, payload) {
-    var promptURL = chrome.extension.getURL('pages/prompt.html')
-    var requestBody = payload
-    var queryString = new URLSearchParams(requestBody).toString()
-    console.log(promptURL, queryString)
-
-    if(requestBody.address === undefined){
+    if(payload.address === undefined){
       sendResponse(Error.typeMissed('address'));
       return false;
     }
-    if(requestBody.message === undefined){
+    if(payload.message === undefined){
       sendResponse(Error.typeMissed('message'));
       return false;
     }
 
-    chrome.windows.create(
-      {
-        url: `${promptURL}#signMessage?${queryString}`,
-        type: 'popup',
-        width: 360,
-        height: 623,
-        top: 0,
-        left: 0
-      },
-      (window) => {
-        chrome.runtime.onMessage.addListener(function(request, sender) {
-          if(sender.tab.windowId === window.id){
-            switch (request.method){
-              case 'sign-message':
-                if (request.action === 'success'){
-                  sendResponse(request.message);
-                  return true;
-                } else if (request.action === 'reject'){
-                  sendResponse(request.message);
-                  return false;
-                }
-            }
-          }
-        });
-
-        // chrome.windows.onRemoved.addListener(function(windowId){
-        //   if(windowId === window.id) {
-        //     sendResponse(Error.promptClosedWithoutAction());
-        //     return false;
-        //   }
-        // });
-      }
-    )
+    NotificationService.open(new Prompt(PromptTypes.REQUEST_SIGN, '', payload ,approved => {
+     sendResponse(approved);
+    }));
   }
 
   transfer(sendResponse, payload) {
@@ -197,9 +162,6 @@ export default class Background {
   }
 
   advancedTransfer(sendResponse, payload) {
-    var promptURL = chrome.extension.getURL('pages/prompt.html')
-    var queryString = 'object='+JSON.stringify(payload)
-    console.log(promptURL, queryString)
 
     if(payload.input === undefined){
       sendResponse(Error.typeMissed('input'));
@@ -214,38 +176,10 @@ export default class Background {
       return false;
     }
 
-    chrome.windows.create(
-      {
-        url: `${promptURL}#advancedTransfer?${queryString}`,
-        type: 'popup',
-        width: 360,
-        height: 623,
-        top: 0,
-        left: 0
-      },
-      (window) => {
-        chrome.runtime.onMessage.addListener(function(request, sender) {
-          if(sender.tab.windowId === window.id){
-            switch (request.method){
-              case 'advanced-transfer':
-                if (request.action === 'success'){
-                  sendResponse(request.message);
-                  return true;
-                } else if (request.action === 'reject'){
-                  sendResponse(request.message);
-                  return false;
-                }
-            }
-          }
-        });
-        chrome.windows.onRemoved.addListener(function(windowId){
-          if(windowId === window.id) {
-            sendResponse(Error.promptClosedWithoutAction());
-            return false;
-          }
-        });
-      }
-    )
+    NotificationService.open(new Prompt(PromptTypes.REQUEST_ADVANCED_TRANSFER, '', payload ,approved => {
+      sendResponse(approved);
+    }));
+
   }
 
   requestCurrentAccount(sendResponse, payload){
@@ -326,7 +260,7 @@ export default class Background {
       if(bytom.settings.domains.find(_domain => _domain === domain)) {
         sendResponse(currentAccount);
       } else{
-        NotificationService.open(new Prompt(PromptTypes.REQUEST_AUTH, payload.domain, approved => {
+        NotificationService.open(new Prompt(PromptTypes.REQUEST_AUTH, payload.domain, {}, approved => {
           if(approved === false || approved.hasOwnProperty('isError')) sendResponse(approved);
           else {
             bytom.settings.domains.unshift(domain);
index cb918ae..534cb80 100644 (file)
@@ -59,15 +59,10 @@ class Content {
           break
         case 'updateNetAndAccounts':
           const net = await this.getDefaultNetwork();
-          const accountList = await this.getAccountList();
           stream.send(
             NetworkMessage.payload(MsgTypes.UPDATE_BYTOM, {type:'net', value: net}),
             EventNames.INJECT
           )
-          stream.send(
-            NetworkMessage.payload(MsgTypes.UPDATE_BYTOM, {type:'accounts', value: accountList}),
-            EventNames.INJECT
-          )
           break
       }
       return true;
index b87e806..cb78157 100644 (file)
@@ -1,10 +1,9 @@
 export default class Prompt {
 
-    constructor(_type = '', _domain = '', _responder = null){
+    constructor(_type = '', _domain = '', _data={},  _responder = null){
         this.type = _type;
         this.domain = _domain;
-        // this.network = _network;
-        // this.data = _data;
+        this.data = _data;
         this.responder = _responder;
     }
 
index 3438561..83268d3 100644 (file)
@@ -1,6 +1,6 @@
 export const REQUEST_AUTH = 'enable';
-export const REQUEST_SIGNATURE = 'requestSignature';
-export const REQUEST_ARBITRARY_SIGNATURE = 'signatureArbitrary';
+export const REQUEST_SIGN = 'signMessage';
+export const REQUEST_ADVANCED_TRANSFER = 'advancedTransfer';
 export const REQUEST_ADD_NETWORK = 'requestAddNetwork';
 export const REQUEST_UNLOCK = 'requestUnlock';
 export const UPDATE_VERSION = 'updateVersion';
index 0f827f3..cd58c1c 100644 (file)
@@ -118,7 +118,6 @@ export default {
       }
     }, mounted() {
         this.prompt = window.data || apis.extension.getBackgroundPage().notification || null;
-        console.log(this.prompt)
       }
 };
 </script>
index 4782809..46c59d3 100644 (file)
 import transaction from "@/models/transaction";
 import getLang from "@/assets/language/sdk";
 import { LocalStream } from 'extension-streams';
+import {apis} from '@/utils/BrowserApis';
+import NotificationService from '../../services/NotificationService'
 
 export default {
     data() {
@@ -152,7 +154,8 @@ export default {
                 confirmations:1,
                 amounts: []
             },
-            password:''
+            password:'',
+            prompt:''
         };
     },
     computed: {
@@ -161,7 +164,7 @@ export default {
     },
     methods: {
         close: function () {
-            window.close();
+            NotificationService.close();
         },
         transfer: function () {
             let loader = this.$loading.show({
@@ -181,12 +184,12 @@ export default {
               return transaction.advancedTransfer(this.account.guid, result, this.password, arrayData)
                   .then((resp) => {
                       loader.hide();
-                      LocalStream.send({method:'advanced-transfer',action:'success', message:resp});
+                      this.prompt.responder(resp);
                       this.$dialog.show({
                           type: 'success',
                           body: this.$t("transfer.success")
                       });
-                    window.close();
+                    NotificationService.close();
                   })
                   .catch(error => {
                        throw error
@@ -202,8 +205,10 @@ export default {
     }, mounted() {
           this.account = JSON.parse(localStorage.currentAccount);
 
-          if(this.$route.query.object !== undefined){
-              const inout = JSON.parse(this.$route.query.object)
+          this.prompt = window.data || apis.extension.getBackgroundPage().notification || null;
+
+          if(this.prompt.data !== undefined){
+              const inout = this.prompt.data
               if(inout.input !== undefined){
                  this.transaction.input = inout.input
               }
index d858a7d..54819fc 100644 (file)
 import transaction from "@/models/transaction";
 import account from "@/models/account";
 import getLang from "@/assets/language/sdk";
-import { LocalStream } from 'extension-streams';
 import { BTM } from "@/utils/constants";
-
+import {apis} from '@/utils/BrowserApis';
+import NotificationService from '../../services/NotificationService'
 
 export default {
     data() {
@@ -169,7 +169,8 @@ export default {
             accountBalance: 0.00,
             address: '',
             message: '',
-            password:''
+            password:'',
+            prompt:''
         };
     },
     computed: {
@@ -178,7 +179,7 @@ export default {
     },
     methods: {
         close: function () {
-            window.close();
+          NotificationService.close();
         },
         sign: function () {
             let loader = this.$loading.show({
@@ -189,12 +190,12 @@ export default {
 
             transaction.signMessage(this.message, this.password, this.address).then((resp) => {
                 loader.hide();
-                LocalStream.send({method:'sign-message',action:'success', message:resp});
+                this.prompt.responder(resp);
                 this.$dialog.show({
                     type: 'success',
                     body: this.$t("transfer.success")
                 });
-                window.close();
+              NotificationService.close();
             }).catch(error => {
                 loader.hide();
 
@@ -204,7 +205,9 @@ export default {
             });
         }
     }, mounted() {
-        const object = this.$route.query
+        this.prompt = window.data || apis.extension.getBackgroundPage().notification || null;
+        const object = this.prompt.data
+
         if(object.address !== undefined){
            this.address = object.address
         }
index aeb695a..886cb89 100644 (file)
@@ -169,7 +169,7 @@ export default {
         },
         "transaction.amount": function (newAmount) {
             transaction.asset(this.transaction.asset).then(ret => {
-                this.transaction.cost = Number(ret.result.data.cny_price * newAmount).toFixed(2);
+                this.transaction.cost = Number(ret.cny_price * newAmount).toFixed(2);
             });
         },
         account: function (newAccount) {