wkw 1 hari lalu
induk
melakukan
fce2ad529c
1 mengubah file dengan 17 tambahan dan 5 penghapusan
  1. 17 5
      src/stores/modules/walletStore.js

+ 17 - 5
src/stores/modules/walletStore.js

@@ -79,14 +79,26 @@ export const useWalletStore = defineStore("useWalletStore", {
 
         // 助记词路径
         if (isMnemonic) {
-          // 验证助记词格式 (12或24个单词)
-          if (![12, 24].includes(this.words.length)) {
+          let wordArray;
+          if (Array.isArray(this.words)) {
+            if (this.words.length === 1 && typeof this.words[0] === "string") {
+              wordArray = this.words[0].trim().split(/[\s,]+/).filter(Boolean);
+            } else {
+              wordArray = this.words.flat().map(w => String(w).trim()).filter(Boolean);
+            }
+          } else {
+            wordArray = String(this.words)
+              .trim()
+              .split(/[\s,]+/)
+              .filter(Boolean);
+          }
+          if (![12, 24].includes(wordArray.length)) {
             throw new Error("助记词应为12或24个单词");
           }
-          // 从助记词派生私钥 (使用HD钱包)
-          const hdwallet = web3.eth.accounts.wallet.create(1, this.words);
+          const mnemonic = wordArray.join(" ");
+          const hdwallet = web3.eth.accounts.wallet.create(1, mnemonic);
           this.privateKey = hdwallet[0].privateKey;
-        }
+        }        
         if (!/^0x[0-9a-fA-F]{64}$/.test(this.privateKey)) {
           throw new Error("私钥格式不正确");
         }