OSDN Git Service

update sign transaction using web assembly logic 2.2.1
authorZhiting Lin <zlin035@uottawa.ca>
Mon, 6 Jul 2020 05:57:27 +0000 (13:57 +0800)
committerZhiting Lin <zlin035@uottawa.ca>
Mon, 6 Jul 2020 05:57:27 +0000 (13:57 +0800)
package.json
src/assets/language/cn.js
src/assets/language/en.js
src/dapp.js
src/models/transaction.js
src/utils/constants.js
src/utils/utils.js
src/views/sendTransaction/signTransaction.vue
static/wasm/main.wasm

index 79fc027..7f9ef57 100644 (file)
@@ -1,7 +1,7 @@
 {
     "name": "Byone",
     "description": "Bytom Chrome Extension Wallet",
-    "version": "2.2.0",
+    "version": "2.2.1",
     "author": "zhiting.fly@8btc.com",
     "license": "MIT",
     "private": true,
index da2ebf3..88e655d 100644 (file)
@@ -2,6 +2,16 @@ const cn = {
   home:{
     title:"比原链插件钱包"
   },
+  common:{
+    veto: '取消投票',
+    cross_chain_in: '跨入Vapor',
+    spend : '花费',
+    coinbase: 'Coinbase',
+    control : '接受',
+    cross_chain_out: '跨出Vapor',
+    vote: '投票',
+    transfer:'转账'
+  },
   welcome: {
     title: '欢迎使用BYTOM精简Chrome钱包',
     use: '开始',
@@ -46,7 +56,8 @@ const cn = {
     view:'展开',
     hide:'隐藏',
     viewAll:'展示全部',
-    hideAll:'隐藏全部'
+    hideAll:'隐藏全部',
+    types:'交易类型'
   },
   crossChain:{
     title:'跨链',
index a6a0cbc..31b95e2 100644 (file)
@@ -2,6 +2,16 @@ const en = {
   home:{
     title:"Bytom Wallet for Chrome"
   },
+  common:{
+    veto: 'Veto',
+    cross_chain_in: 'Cross In Vapor',
+    spend : 'Spend ',
+    coinbase: 'Coinbase',
+    control : 'Received ',
+    cross_chain_out: 'Cross Out Vapor',
+    vote: 'Vote',
+    transfer:'Transfer'
+  },
   welcome: {
     title: 'Welcome to the BYTOM Chrome Wallet',
     use: 'Start',
@@ -46,7 +56,8 @@ const en = {
     view:'View',
     hide:'Hide',
     viewAll:'View All',
-    hideAll:'Hide All'
+    hideAll:'Hide All',
+    types:'Types'
   },
   crossChain:{
     title:'Cross Chain',
index 0a0a8f5..ca7e3d5 100644 (file)
@@ -100,6 +100,10 @@ export default class Bytomdapp {
     return _send(MsgTypes.SIGNMESSAGE, params)
   }
 
+  signTransaction(params) {
+    return _send(MsgTypes.SIGNTRANSACTION, params)
+  }
+
 
   //v1.0.0
   send_transaction(params) {
index c112acd..0579a12 100644 (file)
@@ -116,6 +116,20 @@ transaction.signTransaction = function(guid, transaction, password) {
   return retPromise;
 };
 
+transaction.decodeTransaction = function(rawTx) {
+  let retPromise = new Promise((resolve, reject) => {
+    bytom.transaction
+      .decodeTransaction(rawTx)
+      .then(res => {
+        resolve(res);
+      })
+      .catch(error => {
+        reject(error);
+      });
+  });
+  return retPromise;
+};
+
 transaction.transfer = function(guid, transaction, password, address) {
   let retPromise = new Promise((resolve, reject) => {
     bytom.transaction
index 1433b2c..29efecc 100644 (file)
@@ -29,3 +29,23 @@ export const networksJS = {
   mainnetvapor: `${networks['mainnet']}/vapor/v3/` ,
 }
 
+
+export const decimalsMap = {
+  mainnet:{
+    '184e1cc4ee4845023888810a79eed7a42c02c544cf2c61ceac05e176d575bd46':6,
+    '25f2069140fa3ff4d6e0dc1d0fcaa11ace01eb721f115f0f1a5a3782db597fb1':6,
+    '78de44ffa1bce37b757c9eae8925b5f199dc4621b412ef0f3f46168865284a93':9,
+    'bda946b3110fa46fd94346ce3f05f0760f1b9de72e238835bc4d19f9d64f1742':8,
+    'c4644dd6643475d57ed624f63129ab815f282b61f4bb07646d73423a6e1a1563':6,
+    'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff':8
+  },
+  tesetnet:{
+    '328d60d8192425beeaea53dbbc7900778ec06f4ca2e4499f92aea6ca17645d4c':8,
+    '4483893ef7d9aac788c0e9e49a12398c1d35a2172adb1c8dc551fd169a6f5703':8,
+    '57e5b3a3040e1c0162f64019838fd93b5f33dcde7069fa72aee48a054143443f':8,
+    'a0889e1080999e59ed552865a1d3ee677202796222141ccc3552041708aad76c':8,
+    'babb18008ba6cf533fed6bc2bb8e98b3978c324f221aa679ba4c2b558a8fa97d':8,
+    'd50a426bdaaf1458d161aba4d8c3ebdd095eac7e1bbeb4a0252a3737ccf2d492':8,
+    'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff':8
+  }
+}
index 78b276b..f8fa456 100644 (file)
@@ -46,3 +46,7 @@ export const snakeize = (object) => {
 
   return object
 }
+
+export function removeFromArray(original, remove) {
+  return original.filter(value => !remove.includes(value));
+}
index fee1dc6..fd19f7b 100644 (file)
           </tr>
 
           <div class="divider"></div>
+          <tr class="row">
+            <td class="col label">{{ $t('transfer.types') }}</td>
+            <td class="col value">{{transaction.types}}</td>
+          </tr>
 
           <tr v-for="(amountInput, index) in transaction.amounts" :key="index" class="row">
             <td class="col label">{{index ==0 && $t('transfer.transferAmount') }}</td>
 <script>
 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'
 import { mapActions, mapGetters, mapState } from 'vuex'
 import _ from 'lodash';
 import account from "@/models/account";
-import { Number as Num } from "@/utils/Number"
-
+import { camelize, removeFromArray } from "@/utils/utils";
+import { decimalsMap } from "@/utils/constants"
+import BigNumber from "bignumber.js"
 
 
 export default {
@@ -162,7 +166,8 @@ export default {
                 args: "",
                 fee: "",
                 confirmations:1,
-                amounts: []
+                amounts: [],
+                types:[]
             },
             password:'',
             prompt:''
@@ -230,45 +235,66 @@ export default {
       }
     }, mounted() {
           this.prompt = window.data || apis.extension.getBackgroundPage().notification || null;
-
           if(this.prompt.data !== undefined){
-              const param = Array.isArray(this.prompt.data)? this.prompt.data[0].tx : this.prompt.data.tx
-              if(param.inputs !== undefined){
-                 this.transaction.input = param.inputs
-              }
-              if(param.outputs !== undefined){
-                  this.transaction.output = param.outputs
-              }
-              if(param.fee !== undefined){
-                 this.transaction.fee = param.fee
-              }
-
-              const array = param.inputs.filter(action => action.type ==='spend')
+              const param = Array.isArray(this.prompt.data)? this.prompt.data[0] : this.prompt.data
+              const tx = camelize(param)
+              const rawTransaction = tx.rawTransaction
+              const currentAddress = this.netType === 'vapor'?
+                 this.currentAccount.vpAddress:
+                 this.currentAccount.address
 
-              if(array.length>0){
                 account.setupNet(`${this.net}${this.netType}`)
-              const promise =
-                _(array)
-                  .groupBy('asset')
-                  .map((objs, key) => {
-                    return this.queryAsset(key).then(resp =>{
-                      return {
-                        'asset': key,
-                        'alias':resp.alias,
-                        'amount':Num.formatNue( _.sumBy(objs, 'amount'), resp.decimals)
-                      }
-                    })
+                transaction.decodeTransaction(rawTransaction).then((tx)=>{
+                  this.transaction.fee = tx.fee/100000000
+                  this.transaction.input = tx.inputs
+                  this.transaction.output = tx.outputs
+
+                  const inputs = tx.inputs.filter(i => i.address === currentAddress)
+                  const outputs = tx.outputs.filter(i => i.address === currentAddress)
+                  const inputAsset = inputs.map(i => i.asset);
+                  const outputAsset = outputs.map(i => i.asset);
+
+                  const asset = _.union(inputAsset, outputAsset)
+
+                  let types = ["transfer"]
+                  const promise =
+                    asset
+                      .map((assetId) => {
+                        return this.queryAsset(assetId).then(resp =>{
+                          const assetInput = inputs.filter(i => i.asset ===assetId)
+                          const assetOutput = outputs.filter(o => o.asset ===assetId)
+                          const inputAmount = new BigNumber(_.sumBy(assetInput, 'amount'))
+                          const outputAmount = new BigNumber(_.sumBy(assetOutput, 'amount'))
+
+                          const decimals = decimalsMap[this.net][assetId]
+                          const amount = inputAmount.minus(outputAmount).shiftedBy(-decimals)
+
+                          return {
+                            'asset': assetId,
+                            'alias': resp.symbol,
+                            'amount': amount.toString()
+                          }
+                        })
+                      })
+
+                  let that = this;
+                  Promise.all(promise).then(function(output) {
+                    that.transaction.amounts = output
                   })
 
-                let that = this;
-                Promise.all(promise).then(function(output) {
-                  that.transaction.amounts = output
-                })
+                  const inputType = inputs.map(i => i.type);
+                  const outputType = outputs.map(o => o.type);
+                  types = _.union(inputType, outputType, types);
 
-              }
+                  const remove = ['spend','control'];
+                  types = removeFromArray(types, remove);
+                  types = types.map(ty => this.$t(`common.${ty}`)).join(', ');
+
+                  this.transaction.types = types
 
+                })
 
-          }
+              }
       }
 };
 </script>
index 59e9bb3..b1f54fa 100644 (file)
Binary files a/static/wasm/main.wasm and b/static/wasm/main.wasm differ