|
@@ -79,14 +79,26 @@ export const useWalletStore = defineStore("useWalletStore", {
|
|
|
|
|
|
// 助记词路径
|
|
// 助记词路径
|
|
if (isMnemonic) {
|
|
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个单词");
|
|
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;
|
|
this.privateKey = hdwallet[0].privateKey;
|
|
- }
|
|
|
|
|
|
+ }
|
|
if (!/^0x[0-9a-fA-F]{64}$/.test(this.privateKey)) {
|
|
if (!/^0x[0-9a-fA-F]{64}$/.test(this.privateKey)) {
|
|
throw new Error("私钥格式不正确");
|
|
throw new Error("私钥格式不正确");
|
|
}
|
|
}
|