Forráskód Böngészése

Merge branch 'master' of https://git.nanodreamtech.com/wkw/wallet_app

wkw 3 hete
szülő
commit
12dff0cabb

+ 4 - 0
src/api/path/login.api.js

@@ -18,4 +18,8 @@ export function createAccent(param) {
 // 获取代币地址
 export function hotTokens(param) {
   return service.get('hot-tokens', {params: param});
+}
+// 登录
+export function login(param) {
+  return service.post('login/walletLogin', param );
 }

+ 19 - 0
src/common/login.js

@@ -0,0 +1,19 @@
+import { login } from "@/api/path/login.api";
+import { useSystemStore } from "@/stores/modules/systemStore";
+
+
+
+const systemStore = useSystemStore();
+export async function sysLogin(address) {
+
+  const { data, code } = await login({ wallet_address:address }); 
+  if(code !== 1){
+    $msg($t("form.LoginFailed"));
+    return
+  }
+  systemStore.setStateValue({
+    key: "token",
+    value: data.access_token,
+    localStorage: true,
+  }) 
+}

+ 2 - 0
src/i18n/zhHk/form.js

@@ -13,4 +13,6 @@ export default {
   PrivateKey:"私鑰",
   MemoryAidWord:"助記詞",
   PleaseFillIn: "請填寫",
+
+  LoginFailed: "登陸失敗",
 };

+ 1 - 0
src/stores/modules/systemStore.js

@@ -7,6 +7,7 @@ export const useSystemStore = defineStore("useSystemStore", {
     local_loading: false,
     routeSate: "",
     user_login_information: "",
+    token: getLocalStorage("token") || "",
     stsClientInfo: {},
   }),
   persist: true,

+ 23 - 8
src/stores/modules/walletStore.js

@@ -2,20 +2,24 @@ import { defineStore } from "pinia";
 import Web3 from "web3";
 import CryptoJS from "crypto-js";
 import { cloneDeep } from "lodash";
+import { sysLogin } from "@/common/login.js";
 
 
 export const useWalletStore = defineStore("useWalletStore", {
   state: () => ({
     id: "", // name + @ + account
     isAuthenticated: false, // 登录状态
-    account: null, // 钱包地址
+    account: null, // 钱包地址 
+    
     privateKey: null, // 私钥
     encryptedKey: localStorage.getItem("encryptedKey") || null, // 加密后的密钥
     loading: false, // 加载状态
     error: null, // 错误信息
     chainId: null, // 链ID
+
     balance: "0", // 账户余额(ETH)
-    rpcUrl: "", // 您的私有链RPC
+
+    rpcUrl: "", // ACC私有链RPC
 
     username: "", // 用户名
     accountName: "", //网络昵称
@@ -57,26 +61,38 @@ export const useWalletStore = defineStore("useWalletStore", {
         return "0";
       }
     },
-    async loginWithPrivateKey() {
+    async loginWithPrivateKey(isMnemonic = false) {
       this.loading = true;
       this.error = null;
       try {
         const web3 = window.$web3 || (await this.initWeb3());
         if (!web3) throw new Error("区块链连接失败");
 
+        // 助记词路径
+        if (isMnemonic) {
+          // 验证助记词格式 (12或24个单词)
+          if (![12, 24].includes(this.words.length)) {
+            throw new Error("助记词应为12或24个单词");
+          }
+          // 从助记词派生私钥 (使用HD钱包)
+          const hdwallet = web3.eth.accounts.wallet.create(1, this.words);
+          this.privateKey = hdwallet[0].privateKey; 
+        }
         if (!/^0x[0-9a-fA-F]{64}$/.test(this.privateKey)) {
           throw new Error("私钥格式不正确");
-        }
-
+        } 
+        // 创建账户对象
         const account = await web3.eth.accounts.privateKeyToAccount(this.privateKey);
+        // 验证是否为合约地址
         const code = await web3.eth.getCode(account.address);
-              
         if (code !== "0x") throw new Error("该地址是合约地址,不支持登录");
+
         this.account = account.address;
         this.isAuthenticated = true;
         this.chainId =  `${await web3.eth.getChainId()}n`;
-        await this.getBalance(); 
+        this.id = `${this.account}@${this.accountName}`
         this.addWallet()
+        await sysLogin(this.account)
         return {
           address: account.address,
           chainId: this.chainId,
@@ -94,7 +110,6 @@ export const useWalletStore = defineStore("useWalletStore", {
 
     // 添加钱包
     addWallet() { 
-      this.id = `${this.account}@${this.accountName}`
       const data = cloneDeep(this.$state)
       Reflect.deleteProperty(data, "walletList");
       if(this.walletList.length = 0) {

+ 6 - 2
src/views/login/importWallet/index.vue

@@ -89,11 +89,15 @@ const onSubmit = async () => {
     $msg($t("form.InconsistentPasswords"));
     return;
   }
+  if(type == 0){
+    walletStore.privateKey = form.value.userKey;
+  }else{
+    walletStore.words = form.value.userKey.trim().split(',');
+  }
   walletStore.username = form.value.username;
-  walletStore.privateKey = form.value.userKey;
   walletStore.accountPassword = form.value.password;
   walletStore.accountHint = form.value.promptMessage;
-  await walletStore.loginWithPrivateKey();
+  await walletStore.loginWithPrivateKey(type != 0);
   router.push({
     path: "/wallet",
   });