OSDN Git Service

update the keys retrived logic for sign Transactions.
authorZhiting Lin <zlin035@uottawa.ca>
Wed, 22 Jul 2020 05:44:22 +0000 (13:44 +0800)
committerZhiting Lin <zlin035@uottawa.ca>
Wed, 22 Jul 2020 05:44:22 +0000 (13:44 +0800)
src/models/account.js
src/models/transaction.js
src/store/store.js
src/utils/Bytom.js
src/utils/Keychain.js [new file with mode: 0644]
src/views/sendTransaction/transferConfirm.vue
src/views/sideMenu/menuCreation.vue
src/views/welcome/creation.vue

index bd4eda1..86471f0 100644 (file)
@@ -1,32 +1,39 @@
 import bytom from './bytom'
 import uuid from 'uuid'
-import address from "../utils/address";
+import * as Actions from '../store/constants';
 
 
 let account = {
   setupNet: bytom.setupNet
 }
 
-account.create = function(accountAlias, keyAlias, passwd, success, error) {
+account.create = function(accountAlias, keyAlias, passwd, context) {
   let retPromise = new Promise((resolve, reject) => {
     if(!keyAlias){
       keyAlias = `${accountAlias}-key-${uuid.v4()}`
     }
+
+    const _bytom = context.bytom.clone();
     bytom.keys
-      .create(keyAlias, passwd)
+      .createKey(keyAlias, passwd)
       .then(res => {
-        bytom.accounts
-          .createAccountUseServer(res.xpub, accountAlias)
-          .then(ret => {
-            resolve(ret)
+        _bytom.keychain.keys[keyAlias] = res
+        context[Actions.UPDATE_STORED_BYTOM](_bytom).then(() => {
+            bytom.accounts
+              .createAccountUseServer(res.xpub, accountAlias)
+              .then(ret => {
+                resolve(ret)
+              })
+              .catch(error => {
+                reject(error)
+              })
           })
           .catch(error => {
             reject(error)
           })
-      })
-      .catch(error => {
-        reject(error)
-      })
+
+        });
+
   })
   return retPromise
 }
index 0579a12..6613148 100644 (file)
@@ -102,10 +102,14 @@ transaction.buildTransaction = function(address, inputs, outputs, gas, confirmat
   return retPromise;
 };
 
-transaction.signTransaction = function(guid, transaction, password) {
+transaction.signTransaction = function(guid, transaction, password, context) {
   let retPromise = new Promise((resolve, reject) => {
-    bytom.transaction
-      .signTransaction(guid, JSON.stringify(snakeize(transaction)), password)
+      signTx(
+        guid,
+        JSON.stringify(snakeize(transaction)),
+        password,
+        context
+      )
       .then(res => {
         resolve(res);
       })
@@ -130,10 +134,14 @@ transaction.decodeTransaction = function(rawTx) {
   return retPromise;
 };
 
-transaction.transfer = function(guid, transaction, password, address) {
+transaction.transfer = function(guid, transaction, password, address, context) {
   let retPromise = new Promise((resolve, reject) => {
-    bytom.transaction
-      .signTransaction(guid, JSON.stringify(snakeize(transaction)), password)
+      signTx(
+        guid,
+        JSON.stringify(snakeize(transaction)),
+        password,
+        context
+      )
       .then(ret => {
         bytom.transaction
           .submitPayment(address, ret.raw_transaction, ret.signatures)
@@ -160,10 +168,14 @@ transaction.signMessage = function(message, password, address) {
   return bytom.keys.signMessage(message, password,address);
 };
 
-transaction.advancedTransfer = function(guid, transaction, password, arrayData, address) {
+transaction.advancedTransfer = function(guid, transaction, password, arrayData, address, context) {
   let retPromise = new Promise((resolve, reject) => {
-    bytom.transaction
-      .signTransaction(guid, JSON.stringify(snakeize(transaction)), password)
+      signTx(
+        guid,
+        JSON.stringify(snakeize(transaction)),
+        password,
+        context
+      )
       .then(ret => {
         let signatures = ret.signatures
         if(arrayData){
@@ -189,4 +201,23 @@ transaction.advancedTransfer = function(guid, transaction, password, arrayData,
   return retPromise;
 };
 
+
+
+function signTx(guid, transaction, password, context){
+  return bytom.accounts.getAccountXpub(guid).then((xpub)=>{
+    const keyArray = context.bytom.keychain.findIdentity(xpub);
+    if(!keyArray){
+      throw 'xpub not found'
+    }else{
+      const key = keyArray.key
+      return bytom.transaction
+        ._signTransaction(
+          transaction,
+          password,
+          key
+        )
+    }
+  })
+}
+
 export default transaction;
index 40f4622..8526a19 100644 (file)
@@ -26,6 +26,7 @@ const getters = {
     language:state => state.bytom.settings.language,
     login:state => state.bytom.settings.login,
     currency:state => state.bytom.settings.currency,
+    keys: state => state.bytom.keychain.keys,
 
     // FOR PROMPTS ONLY
     messages:state => state.prompt.data.messages || [],
index f464e93..299a4ec 100644 (file)
@@ -1,15 +1,22 @@
 import Settings from './Settings';
+import Keychain from './Keychain';
 
 export default class BytomObj {
 
     constructor(){
         this.settings = Settings.placeholder();
+      this.keychain = Keychain.placeholder();
+      this.histories = [];
     }
 
     static placeholder(){ return new BytomObj(); }
     static fromJson(json){
         let p = Object.assign(this.placeholder(), json);
         if(json.hasOwnProperty('settings')) p.settings = Settings.fromJson(json.settings);
+        if(json.hasOwnProperty('keychain'))
+          p.keychain = (typeof json.keychain === 'string')
+            ? json.keychain : Keychain.fromJson(json.keychain);
+
         return p;
     }
 
diff --git a/src/utils/Keychain.js b/src/utils/Keychain.js
new file mode 100644 (file)
index 0000000..6189cca
--- /dev/null
@@ -0,0 +1,15 @@
+export default class Keychain {
+
+  constructor(){
+    this.keys = {};
+  }
+
+  static placeholder(){ return new Keychain(); }
+  static fromJson(json){
+    let p = Object.assign(this.placeholder(), json);
+    if(json.hasOwnProperty('keys')) p.keys = json.keys;
+    return p;
+  }
+
+  findIdentity(publicKey){ return Object.values(this.keys).find(id => id.xpub === publicKey); }
+}
index 02ecb14..b526d35 100644 (file)
 import modalPasswd from "@/components/modal-passwd";
 import getLang from "@/assets/language/sdk";
 import { LocalStream } from 'extension-streams';
-import { mapGetters } from 'vuex'
+import { mapGetters , mapState} from 'vuex'
 
 export default {
     components: {
@@ -225,6 +225,9 @@ export default {
           return Number(this.transaction.amount)
         }
       },
+      ...mapState([
+        'bytom'
+      ]),
       ...mapGetters([
         'language',
         'net',
@@ -258,7 +261,7 @@ export default {
             address = this.netType === 'vapor'?  this.account.vpAddress: this.account.address;
           }
 
-          Promise.all(this.rawData.map( (rawdata) => transaction.transfer(this.account.guid, rawdata, this.password, address)))
+          Promise.all(this.rawData.map( (rawdata) => transaction.transfer(this.account.guid, rawdata, this.password, address, this)))
                 .then(ret => {
                     loader.hide();
                     if(this.$route.params.type == 'popup'){
index 299d7db..8290fab 100644 (file)
@@ -108,7 +108,7 @@ export default {
 
             account.setupNet(`${this.net}`);
             if(this.netType === 'vapor'){
-              account.create(this.formItem.accAlias, null, this.formItem.passwd1).then((resp) => {
+              account.create(this.formItem.accAlias, null, this.formItem.passwd1, this).then((resp) => {
                 account.setupNet(`${this.net}vapor`);
                 return account.copy(resp.guid).then((currentRespAccount)=>{
                   this[Actions.CREATE_NEW_BYTOM_ACCOUNT](currentRespAccount).then(()=>{
@@ -124,7 +124,7 @@ export default {
                 });
               });
             }else{
-              account.create(this.formItem.accAlias, null, this.formItem.passwd1).then(account => {
+              account.create(this.formItem.accAlias, null, this.formItem.passwd1,this).then(account => {
                 this[Actions.CREATE_NEW_BYTOM_ACCOUNT](account).then(()=>{
                   loader.hide();
                   this.$router.push('/');
@@ -141,6 +141,7 @@ export default {
         },
         ...mapActions([
           Actions.CREATE_NEW_BYTOM_ACCOUNT,
+          Actions.UPDATE_STORED_BYTOM
         ])
     },
     mounted() { }
index 2920562..101c815 100644 (file)
@@ -279,7 +279,7 @@ export default {
                 canCancel: true,
                 onCancel: this.onCancel
             });
-            account.create(this.formItem.accAlias, null, this.formItem.passwd1).then(currentAccount => {
+            account.create(this.formItem.accAlias, null, this.formItem.passwd1, this).then(currentAccount => {
               this[Actions.CREATE_NEW_BYTOM](this.selected.value).then(() =>{
                 loader.hide();
                 this.formItem = {};
@@ -321,6 +321,7 @@ export default {
         ...mapActions([
           Actions.CREATE_NEW_BYTOM,
           Actions.IMPORT_BYTOM,
+          Actions.UPDATE_STORED_BYTOM
         ])
     },
     watch: {