OSDN Git Service

update dapp return format
authorZhiting Lin <zlin035@uottawa.ca>
Thu, 19 Sep 2019 01:52:58 +0000 (09:52 +0800)
committerZhiting Lin <zlin035@uottawa.ca>
Thu, 19 Sep 2019 01:52:58 +0000 (09:52 +0800)
src/background.js
src/content.js
src/dapp.js
src/messages/types.js
src/prompts/PromptTypes.js
src/views/assetList.vue
src/views/home.vue
src/views/sendTransaction/transfer.vue

index d153dae..f665b05 100644 (file)
@@ -43,6 +43,9 @@ export default class Background {
       case MsgTypes.REQUEST_CURRENT_NETWORK:
         this.requestCurrentNetwork(sendResponse)
         break
+      case MsgTypes.REQUEST_CURRENT_CHAIN_TYPE:
+        this.requestCurrentChain(sendResponse)
+        break
       case MsgTypes.ENABLE:
         Background.authenticate(sendResponse, message.payload)
         break
@@ -109,6 +112,9 @@ export default class Background {
       return false;
     }
 
+    // NotificationService.open(new Prompt(PromptTypes.REQUEST_TRANSFER, '', payload ,approved => {
+    //   sendResponse(approved);
+    // }));
 
       chrome.windows.create(
       {
@@ -170,14 +176,29 @@ export default class Background {
     Background.load(bytom => {
       const domain = payload.domain;
       if(bytom.settings.domains.find(_domain => _domain === domain)) {
-        const currentAccount =  Object.assign({}, bytom.currentAccount)
-        delete(currentAccount['label'])
-        delete(currentAccount['net'])
-        currentAccount['accountId'] = currentAccount['guid']
-        delete(currentAccount['guid'])
-        delete(currentAccount['balance'])
-
-        sendResponse(currentAccount);
+        const currentAccount =  bytom.currentAccount
+        let account
+        if(bytom.settings.netType === 'vapor'){
+          account = {
+            address: currentAccount.vpAddress,
+            alias:currentAccount.alias,
+            balances: currentAccount.vpBalances|| [],
+            accountId: currentAccount.guid,
+            rootXPub: currentAccount.rootXPub
+          };
+        }else{
+          let balances = currentAccount.balances ||[]
+
+          account ={
+            address: currentAccount.address,
+            alias:currentAccount.alias,
+            balances: currentAccount.balances|| [],
+            accountId: currentAccount.guid,
+            rootXPub: currentAccount.rootXPub
+          };
+        }
+
+        sendResponse(account)
       } else{
         sendResponse(null);
         return false;
@@ -192,6 +213,14 @@ export default class Background {
     })
   }
 
+  requestCurrentChain(sendResponse){
+    Background.load(bytom => {
+      const chain = bytom.settings.netType ==='vapor'?'vapor':'bytom'
+      sendResponse(chain);
+    })
+  }
+
+
   send(sendResponse, payload) {
     const action = payload.action
     if(action){
@@ -236,22 +265,40 @@ export default class Background {
   static authenticate(sendResponse, payload){
     Background.load(bytom => {
       const domain = payload.domain;
-      const currentAccount = Object.assign({}, bytom.currentAccount)
-      delete(currentAccount['label'])
-      delete(currentAccount['net'])
-      currentAccount['accountId'] = currentAccount['guid']
-      delete(currentAccount['guid'])
-      delete(currentAccount['balance'])
+      const currentAccount =  bytom.currentAccount
+
+      let account
+      if(bytom.settings.netType === 'vapor'){
+        let balances = currentAccount.vpBalances ||[]
+
+
+        account ={
+          address: currentAccount.vpAddress,
+          alias:currentAccount.alias,
+          balances: currentAccount.vpBalances || [],
+          accountId: currentAccount.guid,
+          rootXPub: currentAccount.rootXPub
+        };
+
+      }else{
+        account ={
+          address: currentAccount.address,
+          alias:currentAccount.alias,
+          balances: currentAccount.balances|| [],
+          accountId: currentAccount.guid,
+          rootXPub: currentAccount.rootXPub
+        };
+      }
 
       if(bytom.settings.domains.find(_domain => _domain === domain)) {
-        sendResponse(currentAccount);
+        sendResponse(account);
       } else{
         NotificationService.open(new Prompt(PromptTypes.REQUEST_AUTH, payload.domain, {}, approved => {
           if(approved === false || approved.hasOwnProperty('isError')) sendResponse(approved);
           else {
             bytom.settings.domains.unshift(domain);
             if(approved === true){
-              this.update(() => sendResponse(currentAccount), bytom);
+              this.update(() => sendResponse(account), bytom);
             }else{
               this.update(() => sendResponse(approved), bytom);
             }
index c16139f..888c3c4 100644 (file)
@@ -34,10 +34,11 @@ class Content {
     stream.onSync(async () => {
       const defaultAccount = await this.getDefaultAccount();
       const net = await this.getDefaultNetwork();
+      const chain = await this.getDefaultChain()
 
       // Pushing an instance of Bytomdapp to the web application
       stream.send(
-        NetworkMessage.payload(MsgTypes.PUSH_BYTOM, {defaultAccount, net}),
+        NetworkMessage.payload(MsgTypes.PUSH_BYTOM, {defaultAccount, net, chain}),
         EventNames.INJECT
       )
 
@@ -67,6 +68,17 @@ class Content {
             EventNames.INJECT
           )
         }
+      }else if(evt.bytom.newValue.settings.netType!== evt.bytom.oldValue.settings.netType){
+        const chain = await this.getDefaultChain();
+        const defaultAccount = await this.getDefaultAccount();
+        stream.send(
+          NetworkMessage.payload(MsgTypes.UPDATE_BYTOM, {type:'chain', value: chain}),
+          EventNames.INJECT
+        )
+        stream.send(
+          NetworkMessage.payload(MsgTypes.UPDATE_BYTOM, {type:'default_account', value: defaultAccount}),
+          EventNames.INJECT
+        )
       }
     });
   }
@@ -114,6 +126,11 @@ class Content {
       .send()
   }
 
+  getDefaultChain(){
+    return InternalMessage.signal(MsgTypes.REQUEST_CURRENT_CHAIN_TYPE)
+      .send()
+  }
+
   respond(message, payload) {
     if (!isReady) return
 
index 58d44d7..dcc13d1 100644 (file)
@@ -62,6 +62,7 @@ export default class Bytomdapp {
     resolvers = []
     this.default_account = _options.defaultAccount
     this.net = _options.net
+    this.chain = _options.chain
 
     _subscribe()
   }
index 8d68427..d2825c2 100644 (file)
@@ -14,6 +14,7 @@ export const UPDATE = 'update';
 
 export const REQUEST_CURRENT_ACCOUNT = 'defaultAccount';
 export const REQUEST_CURRENT_NETWORK = 'currentNetwork';
+export const REQUEST_CURRENT_CHAIN_TYPE = 'currentChain';
 export const REQUEST_ACCOUNT_LIST = 'accountList';
 
 
index 83268d3..b865426 100644 (file)
@@ -1,6 +1,7 @@
 export const REQUEST_AUTH = 'enable';
 export const REQUEST_SIGN = 'signMessage';
 export const REQUEST_ADVANCED_TRANSFER = 'advancedTransfer';
+export const REQUEST_TRANSFER = 'transfer';
 export const REQUEST_ADD_NETWORK = 'requestAddNetwork';
 export const REQUEST_UNLOCK = 'requestUnlock';
 export const UPDATE_VERSION = 'updateVersion';
index 0467b53..4434e0b 100644 (file)
@@ -4,7 +4,6 @@
     display:flex;
   }
   .topbar .topbar-left {
-    width:140px;
     overflow:hidden;
     white-space:nowrap;
   }
@@ -13,6 +12,7 @@
     margin-top: 10px;
     padding: 0 20px;
     text-align: center;
+    width: 245px;
   }
 
 .topbar-left .btn-menu i {
index 3eead09..6561fd8 100644 (file)
@@ -419,7 +419,9 @@ export default {
                   const objectIndex = bytom.accountList.findIndex(a => a.guid == this.currentAccount.guid)
                   bytom.accountList[objectIndex].vpAddress = accounts.vpAddress
 
-                  this[Actions.UPDATE_STORED_BYTOM](bytom)
+                  this[Actions.UPDATE_STORED_BYTOM](bytom).then(()=>{
+                    this.refreshBalance(this.currentAccount.guid)
+                  })
                 }).catch(e =>{
                   if(e.message == 'Error: wallet has exist'){
                     account.listVapor(this.currentAccount.guid).then(accounts => {
@@ -429,15 +431,18 @@ export default {
                       const objectIndex = bytom.accountList.findIndex(a => a.guid == this.currentAccount.guid)
                       bytom.accountList[objectIndex].vpAddress = accounts.vpAddress
 
-                      this[Actions.UPDATE_STORED_BYTOM](bytom)
+                      this[Actions.UPDATE_STORED_BYTOM](bytom).then(()=>{
+                        this.refreshBalance(this.currentAccount.guid)
+                      })
                     })
 
                   }
                 })
               }else{
-                this[Actions.UPDATE_STORED_BYTOM](bytom)
+                this[Actions.UPDATE_STORED_BYTOM](bytom).then(()=>{
+                  this.refreshBalance(this.currentAccount.guid)
+                })
               }
-              this.refreshBalance(this.currentAccount.guid)
             }
         },
         showQrcode: function () {
index 789440f..a89c254 100644 (file)
                   </label>
                   <div class="form-item-content" style=" display: flex;">
                     <input type="number" v-model="transaction.amount" placeholder="0" @keypress="limitAmount">
-                      <span class="color-grey" style="width: 40px; font-size: 15px;position: absolute;right: 0;text-transform: uppercase;">{{unit}}</span>
+                      <span class="color-grey" style="font-size: 15px;position: absolute;right: 0;text-transform: uppercase;">{{unit}}</span>
                   </div>
               </div>
               <div class="form-item">
@@ -199,9 +199,17 @@ export default {
     computed: {
         assets(){
           if(this.netType === 'vapor'){
-            return this.currentAccount.vpBalances
+            if(!this.currentAccount.vpBalances ||this.currentAccount.vpBalances.length == 0){
+              return [this.selectAsset]
+            }else{
+              return this.currentAccount.vpBalances
+            }
           }else{
-            return this.currentAccount.balances
+            if(!this.currentAccount.balances ||this.currentAccount.balances.length == 0){
+              return [this.selectAsset]
+            }else{
+              return this.currentAccount.balances
+            }
           }
         },
         unit() {
@@ -219,7 +227,8 @@ export default {
     },
     watch: {
         "transaction.amount": function (newAmount) {
-            this.transaction.cost = Number(this.selectAsset[currencyInPrice[this.currency]] * newAmount).toFixed(2);
+            const singlePrice = this.selectAsset[currencyInPrice[this.currency]]||this.selectAsset[this.currency]||0
+            this.transaction.cost = Number( singlePrice * newAmount).toFixed(2);
         },
         account: function (newAccount) {
             this.guid = newAccount.guid;
@@ -261,7 +270,7 @@ export default {
         assetChange: function (val) {
           if(val.asset !== this.selectAsset.asset){
             this.transaction.asset = val.asset;
-            const balances = this.currentAccount.balances
+            const balances = this.netType === 'vapor'?this.currentAccount.vpBalances:this.currentAccount.balances
             let balance = 0.00
             if(balances.length >0 ) {
               const balanceObject = balances.filter(b => b.asset === val.asset)[0]
@@ -325,7 +334,14 @@ export default {
           }
 
           if (this.$route.query.asset != undefined) {
-              this.transaction.asset= this.$route.query.asset
+            const currentAssetId = this.$route.query.asset
+
+            this.transaction.asset= currentAssetId
+
+            const assets = this.assets
+            this.selectAsset = assets.filter(b => b.asset === currentAssetId.toLowerCase())[0]
+
+
           }
           if (this.$route.query.to != undefined) {
               this.transaction.to = this.$route.query.to
@@ -341,15 +357,16 @@ export default {
           }
         }else{
           this.account = this.currentAccount
-        }
 
-        const currentAsset = this.currentAccount.balances[0]
+          const currentAsset = this.currentAccount.balances[0]
 
-        if(currentAsset){
-          transaction.asset(currentAsset.asset).then(ret => {
-              this.selectAsset = Object.assign(ret,currentAsset)
-          });
+          if(currentAsset){
+            transaction.asset(currentAsset.asset).then(ret => {
+                this.selectAsset = Object.assign(ret,currentAsset)
+            });
+          }
         }
+
     }
 };
 </script>