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