OSDN Git Service

update and compact the keystore restore for v2
authorZhiting Lin <zlin035@uottawa.ca>
Wed, 26 Aug 2020 05:59:33 +0000 (13:59 +0800)
committerZhiting Lin <zlin035@uottawa.ca>
Wed, 26 Aug 2020 05:59:33 +0000 (13:59 +0800)
src/assets/language/cn.js
src/assets/language/en.js
src/models/account.js
src/views/welcome/import.vue

index 744a0db..a2bbf03 100644 (file)
@@ -255,7 +255,10 @@ const cn = {
     BTM0006:'输入有效的地址格式。',
     BTM0007:'钱包名称错误,请输入正确钱包名称。',
     BTM0008:'密码不能为空',
-    BTM0009:'目前钱包已为账户'
+    BTM0009:'目前钱包已为账户',
+    BTM0010:'Keystore导入不支持V2多账户导入,请拆分为单个Keystore',
+    BTM0011:'Keystore文件格式错误。',
+    BTM0012:'Keystore已存在请勿重复导入。'
   },
   successMsg:{
     createWallet:{
index 8b41085..4ab8d3b 100644 (file)
@@ -249,7 +249,10 @@ const en = {
     BTM0006:'Address format error',
     BTM0007:'Wallet name error',
     BTM0008:'Empty password',
-    BTM0009:'Current wallet is already an account'
+    BTM0009:'Current wallet is already an account',
+    BTM0010:'Keystore restore dose not support V2 multiple account. Please split into single keystore.',
+    BTM0011:'Keystore file format error.',
+    BTM0012:'Keystore already exist.'
   },
   successMsg:{
     createWallet:{
index c1e3c4c..4208425 100644 (file)
@@ -2,7 +2,7 @@ import bytom from './bytom'
 import uuid from 'uuid'
 import * as Actions from '../store/constants';
 import _ from 'lodash'
-import {getDomains} from '@/utils/utils.js'
+import {getDomains, camelize} from '@/utils/utils.js'
 
 let account = {
   setupNet: bytom.setupNet
@@ -259,8 +259,30 @@ account.isValidMnemonic = function(mnemonic) {
   return bytom.keys.isValidMnemonic(mnemonic)
 }
 
-account.isValidKeystore = function(keystore) {
-  return bytom.keys.isValidKeystore(keystore)
+account.isValidKeystore = function(keystore, context) {
+  const walletImage = camelize(JSON.parse(keystore));
+  //V2
+  if(walletImage['accounts-server']){
+    const account = walletImage['accounts-server'].filter(a => a.net === context.net)
+    if(account.length>1){
+      throw(context.$t('error.BTM0010'))
+    }else if(account.length===0){
+      throw(context.$t('error.BTM0011'))
+    }else{
+      const xpub = account[0].rootXPub
+      if(context.bytom.keychain.findIdentity(xpub)){
+        throw(context.$t('error.BTM0012'))
+      }else{
+        const key = walletImage["keys"].find(key => key.xpub === xpub)
+        return key.key
+      }
+    }
+  }
+  //invalid format
+  else if(!walletImage['crypto']){
+    throw(context.$t('error.BTM0011'))
+  }
+  return walletImage
 }
 
 account.decryptMnemonic = function(vault,password, context) {
@@ -274,11 +296,9 @@ account.createOld = function(accountAlias, keyAlias, passwd, success, error) {
     if(!keyAlias){
       keyAlias = `${accountAlias}-key-${uuid.v4()}`
     }
-    debugger
     bytom.keys
       .create(keyAlias, passwd)
       .then(res => {
-        debugger
         bytom.accounts
           .createAccountUseServer(res.xpub, accountAlias)
           .then(ret => {
index d0830a4..915922b 100644 (file)
             }
 
             try{
-              const keystore = account.isValidKeystore(this.formItem.keystoreF)
+              const keystore = account.isValidKeystore(this.formItem.keystoreF, this)
               this[Actions.SET_DATA]({
                 keystore: keystore
               }).then(() => {
             }
 
             try{
-              const keystore = account.isValidKeystore(this.formItem.keystore)
+              const keystore = account.isValidKeystore(this.formItem.keystore, this)
               this[Actions.SET_DATA]({
                 keystore: keystore
               }).then(() => {