OSDN Git Service

update the bapp prompt function
authorZhiting Lin <zlin035@uottawa.ca>
Mon, 24 Aug 2020 07:12:32 +0000 (15:12 +0800)
committerZhiting Lin <zlin035@uottawa.ca>
Mon, 24 Aug 2020 07:12:32 +0000 (15:12 +0800)
src/background.js
src/content.js
src/views/prompts/bappPrompt.vue

index 0b52e6f..06e2065 100644 (file)
@@ -85,26 +85,30 @@ export default class Background {
   }
 
   signMessage(sendResponse, payload) {
-    if(payload.address === undefined){
+    var requestBody = payload.value
+
+    if(requestBody.address === undefined){
       sendResponse(Error.typeMissed('address'));
       return false;
     }
-    if(payload.message === undefined){
+    if(requestBody.message === undefined){
       sendResponse(Error.typeMissed('message'));
       return false;
     }
 
-    const {domain,  ...txAttrs} = payload;
-
-    txAttrs.type = 'message'
+    const {domain} = payload;
+    const data={
+      value: requestBody,
+      type:  'message'
+    }
 
-    NotificationService.open(new Prompt(PromptTypes.REQUEST_PROMPT, domain, txAttrs ,approved => {
+    NotificationService.open(new Prompt(PromptTypes.REQUEST_PROMPT, domain, data ,approved => {
       sendResponse(camelize(approved));
     }));
   }
 
   transfer(sendResponse, payload) {
-    var requestBody = payload
+    var requestBody = payload.value
 
     if(requestBody.from === undefined){
       sendResponse(Error.typeMissed('from'));
@@ -123,46 +127,55 @@ export default class Background {
       return false;
     }
 
-    const {domain,  ...txAttrs} = payload;
-
-    txAttrs.type = 'transfer'
+    const {domain} = payload;
+    const data={
+      value: requestBody,
+      type:  'transfer'
+    }
 
-    NotificationService.open(new Prompt(PromptTypes.REQUEST_PROMPT, domain, txAttrs ,approved => {
+    NotificationService.open(new Prompt(PromptTypes.REQUEST_PROMPT, domain, data ,approved => {
       sendResponse(camelize(approved));
     }));
   }
 
   advancedTransfer(sendResponse, payload) {
+    var requestBody = payload.value
 
-    if(payload.input === undefined){
+    if(requestBody.input === undefined){
       sendResponse(Error.typeMissed('input'));
       return false;
     }
-    if(payload.output === undefined){
+    if(requestBody.output === undefined){
       sendResponse(Error.typeMissed('output'));
       return false;
     }
-    if(payload.gas === undefined){
+    if(requestBody.gas === undefined){
       sendResponse(Error.typeMissed('gas'));
       return false;
     }
 
-    const {domain,  ...txAttrs} = payload;
+    const {domain} = payload;
 
-    txAttrs.type = 'advTransfer'
+    const data={
+      value: requestBody,
+      type:  'advTransfer'
+    }
 
-    NotificationService.open(new Prompt(PromptTypes.REQUEST_PROMPT, domain, txAttrs ,approved => {
+    NotificationService.open(new Prompt(PromptTypes.REQUEST_PROMPT, domain, data ,approved => {
       sendResponse(camelize(approved));
     }));
 
   }
 
   signTransaction(sendResponse, payload) {
-    const {domain,  ...txAttrs} = payload;
+    const {domain,  value} = payload;
 
-    txAttrs.type = 'signTransaction'
+    const data={
+      value: value,
+      type:  'signTransaction'
+    }
 
-    NotificationService.open(new Prompt(PromptTypes.REQUEST_PROMPT, domain, txAttrs ,approved => {
+    NotificationService.open(new Prompt(PromptTypes.REQUEST_PROMPT, domain, data ,approved => {
       sendResponse(camelize(approved));
     }));
   }
index 9e77359..b938a99 100644 (file)
@@ -165,7 +165,11 @@ class Content {
   prompt(type, message) {
     if (!isReady) return
 
-    const payload = Object.assign(message.payload, {domain: strippedHost()})
+    const payload = {
+      value: message.payload,
+      domain: strippedHost()
+    }
+
     InternalMessage.payload(type,payload)
       .send()
       .then(res => this.respond(message, res))
index 2508343..f775aa8 100644 (file)
         } else if(this.prompt.data.type ==='transfer'){
           transaction.transfer(this.transaction, this.password, this.address, this).then(result => {
             loader.hide();
+            this.prompt.responder(result);
+            this.$toast.success(
+              this.$t("transfer.success")
+            );
             NotificationService.close();
 
           }).catch(error => {
             );
           });
         } else if(this.prompt.data.type ==='signTransaction'){
-          transaction.signTransaction(this.address,  this.prompt.data,  this.password, this).then( (result) => {
-            loader.hide();
-            this.prompt.responder(result);
-            this.$toast.success(this.$t("transfer.success"));
-            NotificationService.close();
-          }).catch(error => {
-            loader.hide();
+          const data = this.prompt.data.value
+
+          if(Array.isArray(data)){
+            Promise.all(data.map( (rawdata) => transaction.signTransaction(this.address, rawdata, this.password, this)))
+              .then( (result) => {
+                loader.hide();
+                this.prompt.responder(result);
+                this.$toast.success(this.$t("transfer.success"));
+                NotificationService.close();
+              }).catch(error => {
+              loader.hide();
+
+              this.$toast.error(
+                getLang(error.message) || error.message || error
+              );
+            });
+          }else{
+            transaction.signTransaction(this.address,  data,  this.password, this).then( (result) => {
+              loader.hide();
+              this.prompt.responder(result);
+              this.$toast.success(this.$t("transfer.success"));
+
+              NotificationService.close();
+            }).catch(error => {
+              loader.hide();
+
+              this.$toast.error(
+                getLang(error.message) || error.message || error
+              );
+            });
+          }
 
-            this.$toast.error(
-              getLang(error.message) || error.message || error
-            );
-          });
         } else if(this.prompt.data.type ==='message'){
           transaction.signMessage(this.transaction.message, this.password, this.address, this).then((resp) => {
             loader.hide();
       this.prompt = window.data || apis.extension.getBackgroundPage().notification || null;
       console.log(this.prompt)
 
-      const data = this.prompt.data
-      if (data !== undefined) {
-        switch(data.type){
+      const params = this.prompt.data
+      if (params !== undefined) {
+        const data = params.value
+        switch(params.type){
           case "advTransfer":{
             const inout = data
             if (inout.input !== undefined) {