Bläddra i källkod

fix: 添加钱包

liming 3 veckor sedan
förälder
incheckning
7b0e87674b
1 ändrade filer med 24 tillägg och 11 borttagningar
  1. 24 11
      src/stores/modules/walletStore.js

+ 24 - 11
src/stores/modules/walletStore.js

@@ -1,7 +1,7 @@
 import { defineStore } from "pinia";
 import Web3 from "web3";
 import CryptoJS from "crypto-js";
-
+import { cloneDeep } from "lodash";
 
 
 export const useWalletStore = defineStore("useWalletStore", {
@@ -67,15 +67,6 @@ export const useWalletStore = defineStore("useWalletStore", {
         if (!/^0x[0-9a-fA-F]{64}$/.test(this.privateKey)) {
           throw new Error("私钥格式不正确");
         }
- 
-
-        // if (password) {
-        //   this.encryptedKey = CryptoJS.AES.encrypt(
-        //     privateKey,
-        //     password
-        //   ).toString();
-        //   localStorage.setItem("encryptedKey", this.encryptedKey);
-        // }
 
         const account = await web3.eth.accounts.privateKeyToAccount(this.privateKey);
         const code = await web3.eth.getCode(account.address);
@@ -84,7 +75,8 @@ export const useWalletStore = defineStore("useWalletStore", {
         this.account = account.address;
         this.isAuthenticated = true;
         this.chainId =  `${await web3.eth.getChainId()}n`;
-        await this.getBalance();
+        await this.getBalance(); 
+        this.addWallet()
         return {
           address: account.address,
           chainId: this.chainId,
@@ -99,5 +91,26 @@ export const useWalletStore = defineStore("useWalletStore", {
         this.loading = false;
       }
     },
+
+    // 添加钱包
+    addWallet() { 
+      this.id = `${this.account}@${this.accountName}`
+      const data = cloneDeep(this.$state)
+      Reflect.deleteProperty(data, "walletList");
+      if(this.walletList.length = 0) {
+        this.walletList.push(data)
+        return 
+      } 
+      const findIndex = this.walletList.findIndex(item =>  item.id === this.id);
+      if(findIndex != -1){
+        this.walletList[walletList] = data
+      }else{
+        this.walletList.push(data)
+      }
+    },
+
+    // 切换钱包
+    switchWallet(account) { 
+    },
   },
 });