OSDN Git Service

add the Advanced build transaction's function
authorZhiting Lin <zlin035@uottawa.ca>
Wed, 23 Jan 2019 02:23:23 +0000 (10:23 +0800)
committerZhiting Lin <zlin035@uottawa.ca>
Wed, 23 Jan 2019 02:23:23 +0000 (10:23 +0800)
src/background.js
src/content.js
src/dapp.js
src/messages/types.js
src/models/transaction.js
src/router.js
src/views/advancedTransfer.vue [new file with mode: 0644]

index 4e844c8..9f99837 100644 (file)
@@ -21,6 +21,9 @@ export default class Background {
       case MsgTypes.TRANSFER:
         this.transfer(sendResponse, message.payload)
         break
+      case MsgTypes.ADVTRANSFER:
+        this.advancedTransfer(sendResponse, message.payload)
+        break
     }
   }
 
@@ -42,6 +45,25 @@ 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)
+    chrome.windows.create(
+      {
+        url: `${promptURL}#advancedTransfer?${queryString}`,
+        type: 'popup',
+        width: 350,
+        height: 623,
+        top: 0,
+        left: 0
+      },
+      () => {
+        sendResponse(true)
+      }
+    )
+  }
 }
 
 new Background()
index a78875d..b2b5634 100644 (file)
@@ -58,7 +58,8 @@ class Content {
         this.sync(msg)
         break
       case MsgTypes.TRANSFER:
-        this.transfer(networkMessage)
+      case MsgTypes.ADVTRANSFER:
+        this.transfer(msg.type, networkMessage)
         break
       default:
         stream.send(networkMessage.error('errtest'), EventNames.INJECT)
@@ -85,10 +86,10 @@ class Content {
     stream.synced = true
   }
 
-  transfer(message) {
+  transfer(type, message) {
     if (!isReady) return
 
-    InternalMessage.payload(MsgTypes.TRANSFER, message.payload)
+    InternalMessage.payload(type, message.payload)
       .send()
       .then(res => this.respond(message, res))
   }
index 6c01db9..c096e1e 100644 (file)
@@ -72,4 +72,11 @@ export default class Bytomdapp {
       amount: amount
     })
   }
+
+  advancedTransfer(input, output) {
+    return _send(MsgTypes.ADVTRANSFER, {
+      input,
+      output
+    })
+  }
 }
index 3405e62..afa22c9 100644 (file)
@@ -3,3 +3,4 @@ export const ERROR = 'error'
 export const PUSH_BYTOM = 'pushBytom'
 export const AUTHENTICATE = 'authenticate'
 export const TRANSFER = 'transfer'
+export const ADVTRANSFER = 'advTransfer'
index 39490df..e507de7 100644 (file)
@@ -28,6 +28,20 @@ transaction.build = function(guid, to, asset, amount, fee) {
   return retPromise;
 };
 
+transaction.buildTransaction = function(guid, inputs, outputs) {
+  let retPromise = new Promise((resolve, reject) => {
+    bytom.transaction
+      .buildTransaction(guid, inputs, outputs)
+      .then(res => {
+        resolve(res);
+      })
+      .catch(error => {
+        reject(error);
+      });
+  });
+  return retPromise;
+};
+
 transaction.transfer = function(guid, transaction, password) {
   let retPromise = new Promise((resolve, reject) => {
     bytom.transaction
@@ -50,4 +64,28 @@ transaction.transfer = function(guid, transaction, password) {
   return retPromise;
 };
 
+transaction.advancedTransfer = function(guid, transaction, password, arrayData) {
+  let retPromise = new Promise((resolve, reject) => {
+    bytom.transaction
+      .signTransaction(guid, JSON.stringify(transaction), password)
+      .then(ret => {
+        let signatures = ret.signatures
+        signatures[0] = arrayData
+        bytom.transaction
+          .submitPayment(guid, ret.raw_transaction, signatures)
+          .then(res3 => {
+            resolve(res3);
+          })
+          .catch(error => {
+            reject(error);
+          });
+      })
+      .catch(error => {
+        reject(error);
+      });
+  });
+
+  return retPromise;
+};
+
 export default transaction;
index 8b7b711..d1c53b1 100644 (file)
@@ -18,6 +18,14 @@ const routers = [
         }
       },
       {
+        path: '/advancedTransfer',
+        name: 'advanced-transfer',
+        meta: { title: '高级转账' },
+        component: resolve => {
+          require(['@/views/advancedTransfer.vue'], resolve)
+        }
+      },
+      {
         path: '/transfer/info',
         name: 'transfer-info',
         meta: { title: '交易详情' },
diff --git a/src/views/advancedTransfer.vue b/src/views/advancedTransfer.vue
new file mode 100644 (file)
index 0000000..a94545a
--- /dev/null
@@ -0,0 +1,177 @@
+<style lang="" scoped>
+.header {
+    height: 150px;
+}
+.balance {
+    text-align: center;
+    margin-top: 15px;
+}
+.balance .token-icon {
+    height: 38px;
+    width: 38px;
+    margin-right: 5px;
+}
+.balance .token-amount {
+    display: inline-block;
+    font-size: 45px;
+    line-height: 45px;
+}
+.balance .token-amount .asset {
+    font-size: 18px;
+    margin-left: 2px;
+}
+.form {
+    padding: 10px 22px;
+}
+.form-item-group {
+    display: flex;
+}
+.form-item-group .form-item {
+    width: 40%;
+}
+
+.btn-inline {
+    display: flex;
+    padding: 0;
+}
+.btn-inline .btn {
+    margin: 10px 15px;
+}
+</style>
+
+<template>
+    <div class="warp-chlid bg-gray">
+        <section class="header bg-green">
+            <i class="iconfont icon-back" @click="close"></i>
+            <div class="balance">
+                <img src="@/assets/logo.png" class="token-icon">
+                <div class="token-amount">
+                    {{accountBalance}}
+                    <span class="asset">BTM</span>
+                </div>
+            </div>
+        </section>
+
+        <section class="form">
+            <div class="form-item-group">
+                <div class="form-item">
+                    <!-- <label>账户</label> -->
+                    <v-select :clearable="false" v-model="account" style="height: 32px;width: 125px;" label="alias" :options="accounts"></v-select>
+                </div>
+            </div>
+            <br>
+            <div style="width: 200px; margin: 0 auto;">
+              <div class="btn bg-green" @click="$refs.modalPasswd.open()">{{ $t('transfer.confirm') }}</div>
+            </div>
+        </section>
+
+        <!-- modal -->
+        <modalPasswd ref="modalPasswd" @confirm="send"></modalPasswd>
+    </div>
+</template>
+
+<script>
+import account from "@/models/account";
+import transaction from "@/models/transaction";
+import getLang from "@/assets/language/sdk";
+import modalPasswd from "@/components/modal-passwd";
+
+export default {
+    components: {
+        modalPasswd
+    },
+    data() {
+        const ASSET_BTM =
+            "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
+        return {
+            show: false,
+            accounts: [],
+            guid: null,
+            account: {},
+            accountBalance: 0.00,
+            transaction: {
+                to: "",
+                asset: ASSET_BTM,
+                amount: "",
+                fee: "",
+                cost: ""
+            }
+        };
+    },
+    computed: {
+        unit() {
+            return this.assets[this.transaction.asset];
+        }
+    },
+    watch: {
+        account: function (newAccount) {
+            this.guid = newAccount.guid;
+        },
+        guid: function (newGuid) {
+            this.accounts.forEach(account => {
+                if (account.guid == newGuid.guid) {
+                    this.account = account;
+                    return;
+                }
+            });
+
+            account.balance(newGuid).then(balance => {
+                this.accountBalance = balance;
+            }).catch(error => {
+                console.log(error);
+            });
+        }
+    },
+    methods: {
+        close: function () {
+            this.$router.go(-1)
+        },
+        send: function (passwd) {
+            let loader = this.$loading.show({
+                // Optional parameters
+                container: null,
+                canCancel: true,
+                onCancel: this.onCancel
+            });
+            const inout = JSON.parse(this.$route.query.object)
+
+            transaction.buildTransaction(this.account.guid,  inout.input, inout.output).then(ret => {
+                const arrayData = ret.result.data.signing_instructions[0].data
+                return transaction.advancedTransfer(this.account.guid, ret.result.data, passwd, arrayData)
+                  .then(() => {
+                    loader.hide();
+                    this.$dialog.show({
+                      body: this.$t("transfer.success")
+                    });
+                    this.$router.push('/')
+                  })
+                  .catch(error => {
+                    throw error
+                  });
+            }).catch(error => {
+                loader.hide();
+                this.$dialog.show({
+                    body: getLang(error.message)
+                });
+            });
+        }
+    }, mounted() {
+        if (this.$route.params.account != undefined) {
+            this.account = this.$route.params.account;
+        }
+
+        account.setupNet(localStorage.bytomNet);
+        account.list().then(accounts => {
+            this.accounts = accounts;
+            this.accounts.forEach(function (ele) {
+                ele.label = ele.alias
+                ele.value = ele.guid
+            });
+
+            if (Object.keys(this.account).length == 0) {
+                this.account = accounts[0]
+            }
+        });
+    }
+};
+</script>