OSDN Git Service

fix: remove bytomjslib
[bytom/Byone.git] / src / views / sendTransaction / signTransaction.vue
1 <style lang="" scoped>
2   .warp {
3     position: absolute;
4     top: 0;
5     left: 0;
6     right: 0;
7     height: 600px;
8     z-index: 2;
9     overflow: scroll;
10   }
11   .header {
12     display: flex;
13   }
14   .header p{
15     text-align: center;
16     width: 280px;
17     padding-top: 17px;
18   }
19
20   .content {
21     margin: 20px;
22     padding: 20px;
23     overflow: hidden;
24     border-radius:4px;
25     width: 280px;
26   }
27   .divider {
28     margin: 12px 0;
29   }
30
31   .value .uint {
32     font-size: 12px;
33     margin-left: 3px;
34   }
35   .btn-inline .btn {
36     margin: 10px 15px;
37   }
38   .row{
39     word-break: break-all;
40   }
41   .col{
42     font-size: 14px;
43     width: 35%;
44   }
45   .label{
46     color: #7B7B7B;
47   }
48   .value{
49     color: #282828;
50     width: 60%;
51   }
52   table{
53     width: 100%;
54   }
55   .form-item{
56     padding:0;
57     margin:0;
58     margin-bottom: 10px;
59   }
60   .hide{
61     width: 175px;
62     overflow: hidden;
63     text-overflow: ellipsis;
64     white-space: nowrap;
65   }
66   .view-link{
67     font-size: 14px;
68     color: #035BD4;
69     width: 275px;
70     display: block;
71   }
72 </style>
73
74 <template>
75   <div class="warp bg-gray">
76     <section class="header bg-header">
77       <i class="iconfont icon-back" @click="close"></i>
78       <p>{{ $t('transfer.confirmTransaction') }}</p>
79     </section>
80
81     <section class="content bg-white">
82       <table>
83         <tbody>
84           <tr class="row">
85             <td class="col label">{{ $t('transfer.from') }}</td>
86             <td class="col value">{{currentAccount.alias}}</td>
87           </tr>
88           <div class="divider"></div>
89           <tr class="row">
90             <td class="col label">Input</td>
91             <td class="col value" v-bind:class="{ hide: !full }" >{{transaction.input}}</td>
92           </tr>
93           <tr class="row">
94             <td class="col label">Output</td>
95             <td class="col value" v-bind:class="{ hide: !full }" >{{transaction.output}}</td>
96           </tr>
97           <tr v-if="transaction.args" class="row">
98             <td class="col label">Args</td>
99             <td class="col value" v-bind:class="{ hide: !full }" >{{transaction.args}}</td>
100           </tr>
101           <tr class="row">
102             <td colspan="2" class="center-text">
103               <a v-on:click="full = !full"  class="view-link">
104                 {{ full? $t('transfer.hideAll'): $t('transfer.viewAll') }} >>
105               </a>
106             </td>
107           </tr>
108
109           <div class="divider"></div>
110           <tr class="row">
111             <td class="col label">{{ $t('transfer.types') }}</td>
112             <td class="col value">{{transaction.types}}</td>
113           </tr>
114
115           <tr v-for="(amountInput, index) in transaction.amounts" :key="index" class="row">
116             <td class="col label">{{index ==0 && $t('transfer.transferAmount') }}</td>
117             <td class="col value">{{amountInput.amount}}<span class="uint uppercase">{{amountInput.alias || amountInput.asset}}</span></td>
118           </tr>
119
120           <tr class="row">
121             <td class="col label">{{ $t('transfer.fee') }}</td>
122             <td class="col value">{{transaction.fee}}<span class="uint">BTM</span></td>
123           </tr>
124
125         </tbody>
126       </table>
127     </section>
128     <section class="content bg-white">
129       <div class="form">
130         <div class="form-item">
131           <label class="form-item-label">{{ $t('transfer.confirmPassword') }}</label>
132           <div class="form-item-content">
133             <input type="password"  v-model="password" autofocus>
134           </div>
135         </div>
136       </div>
137     </section>
138
139     <div class="row" style="margin: 20px;">
140       <div class="btn bg-green" @click="transfer">{{ $t('transfer.confirm') }}</div>
141     </div>
142
143   </div>
144 </template>
145
146 <script>
147 import transaction from "@/models/transaction";
148 import getLang from "@/assets/language/sdk";
149 import {apis} from '@/utils/BrowserApis';
150 import NotificationService from '../../services/NotificationService'
151 import { mapActions, mapGetters, mapState } from 'vuex'
152 import _ from 'lodash';
153 import account from "@/models/account";
154 import { camelize, removeFromArray } from "@/utils/utils";
155 import { decimalsMap } from "@/utils/constants"
156 import BigNumber from "bignumber.js"
157 // import bytomjslib from 'bytomjs-lib'
158
159
160 export default {
161     data() {
162         return {
163             full: false,
164             transaction: {
165                 input: "",
166                 output: "",
167                 args: "",
168                 fee: "",
169                 confirmations:1,
170                 amounts: [],
171                 types:[]
172             },
173             password:'',
174             prompt:''
175         };
176     },
177     computed: {
178       ...mapState([
179         'bytom'
180       ]),
181       ...mapGetters([
182         'currentAccount',
183         'net',
184         'netType',
185       ])
186     },
187     watch: {
188     },
189     methods: {
190         close: function () {
191             NotificationService.close();
192         },
193         transfer: function () {
194             let loader = this.$loading.show({
195                 // Optional parameters
196                 container: null,
197                 canCancel: true,
198                 onCancel: this.onCancel
199             });
200
201             if(Array.isArray(this.prompt.data)){
202                 Promise.all(this.prompt.data.map( (rawdata) => transaction.signTransaction(this.currentAccount.guid, rawdata, this.password, this)))
203                     .then( (result) => {
204                         loader.hide();
205                         this.prompt.responder(result);
206                         this.$dialog.show({
207                             type: 'success',
208                             body: this.$t("transfer.success")
209                         });
210                       NotificationService.close();
211                     }).catch(error => {
212                         loader.hide();
213
214                         this.$dialog.show({
215                             body: getLang(error.message)
216                         });
217                     });
218             }else{
219                 transaction.signTransaction(this.currentAccount.guid,  this.prompt.data,  this.password, this).then( (result) => {
220                     loader.hide();
221                     this.prompt.responder(result);
222                     this.$dialog.show({
223                         type: 'success',
224                         body: this.$t("transfer.success")
225                     });
226                     NotificationService.close();
227                 }).catch(error => {
228                     loader.hide();
229
230                     this.$dialog.show({
231                         body: getLang(error.message)
232                     });
233                 });
234             }
235
236         },
237       queryAsset: function(assetID){
238         return transaction.asset(assetID)
239       }
240     }, mounted() {
241           this.prompt = window.data || apis.extension.getBackgroundPage().notification || null;
242           if(this.prompt.data !== undefined){
243               const param = Array.isArray(this.prompt.data)? this.prompt.data[0] : this.prompt.data
244               const _tx = camelize(param)
245               const rawTransaction = _tx.rawTransaction
246               const currentAddress = this.netType === 'vapor'?
247                   this.currentAccount.vpAddress:
248                   this.currentAccount.address
249
250               account.setupNet(`${this.net}${this.netType}`)
251               const tx = _tx.tx
252               // const tx = this.netType === 'vapor'?
253               // bytomjslib.vapor.Transaction.decodeRawTransaction(rawTransaction):
254               // bytomjslib.bytom.Transaction.decodeRawTransaction(rawTransaction)
255               this.transaction.fee = tx.fee/100000000
256               this.transaction.input = tx.inputs
257               this.transaction.output = tx.outputs
258
259               const inputs = tx.inputs.filter(i => i.address === currentAddress)
260               const outputs = tx.outputs.filter(i => i.address === currentAddress)
261               const inputAsset = inputs.map(i => i.assetID);
262               const outputAsset = outputs.map(i => i.assetID);
263
264               const asset = _.union(inputAsset, outputAsset)
265
266               let types = ["transfer"]
267               const promise =
268                 asset
269                   .map((assetId) => {
270                     return this.queryAsset(assetId).then(resp =>{
271                       const assetInput = inputs.filter(i => i.assetID ===assetId)
272                       const assetOutput = outputs.filter(o => o.assetID ===assetId)
273                       const inputAmount = new BigNumber(_.sumBy(assetInput, 'amount'))
274                       const outputAmount = new BigNumber(_.sumBy(assetOutput, 'amount'))
275
276                       const decimals = decimalsMap[this.net][assetId]
277                       const amount = inputAmount.minus(outputAmount).shiftedBy(-decimals)
278
279                       return {
280                         'asset': assetId,
281                         'alias': resp.symbol,
282                         'amount': amount.toString()
283                       }
284                     })
285                   })
286
287               let that = this;
288               Promise.all(promise).then(function(output) {
289                 that.transaction.amounts = output
290               })
291
292               const inputType = inputs.map(i => i.type);
293               const outputType = outputs.map(o => o.type);
294               types = _.union(inputType, outputType, types);
295
296               const remove = ['spend','control'];
297               types = removeFromArray(types, remove);
298               types = types.map(ty => this.$t(`common.${ty}`)).join(', ');
299
300               this.transaction.types = types
301
302           }
303       }
304 };
305 </script>