Browse Source

添加签名

wkw 3 days ago
parent
commit
c12a1a6ffe

+ 8 - 7
src/pages/game/index.vue

@@ -81,21 +81,22 @@
   import { ref, onMounted, reactive } from "vue";
   import Header from "@/components/Header";
   const title = ref("Hello");
-  import getWeb3 from "@/utils/web";
+  import {getWalletAddress} from "@/utils/utils";
   const star = ref(3);
   const count = ref(0);
+  const address = ref('');
   const list5 = reactive([
     "https://cdn.uviewui.com/uview/swiper/swiper3.png",
     "https://cdn.uviewui.com/uview/swiper/swiper2.png",
     "https://cdn.uviewui.com/uview/swiper/swiper1.png",
   ]);
   onMounted(async () => {
-    try {
-      const web3 = await getWeb3();
-      const accounts = await web3.eth.getAccounts();
-      console.log("钱包地址:", accounts[0]);
-    } catch (err) {
-      console.error(err);
+    const addr = await getWalletAddress();
+    if (addr) {
+      console.log(addr)
+      address.value = `${addr.slice(0, 4)}...${addr.slice(-4)}`;
+    } else {
+      address.value = '未连接钱包';
     }
   });
   </script>

+ 8 - 7
src/pages/home/index.vue

@@ -81,21 +81,22 @@
 import { ref, onMounted, reactive } from "vue";
 import Header from "@/components/Header";
 const title = ref("Hello");
-import getWeb3 from "@/utils/web";
+import {getWalletAddress} from "@/utils/utils";
 const star = ref(3);
 const count = ref(0);
+const address = ref('');
 const list5 = reactive([
   "https://cdn.uviewui.com/uview/swiper/swiper3.png",
   "https://cdn.uviewui.com/uview/swiper/swiper2.png",
   "https://cdn.uviewui.com/uview/swiper/swiper1.png",
 ]);
 onMounted(async () => {
-  try {
-    const web3 = await getWeb3();
-    const accounts = await web3.eth.getAccounts();
-    console.log("钱包地址:", accounts[0]);
-  } catch (err) {
-    console.error(err);
+  const addr = await getWalletAddress();
+  if (addr) {
+    console.log(addr)
+    address.value = `${addr.slice(0, 4)}...${addr.slice(-4)}`;
+  } else {
+    address.value = '未连接钱包';
   }
 });
 </script>

+ 8 - 7
src/pages/index/index.vue

@@ -35,14 +35,15 @@
 import { ref, onMounted } from "vue";
 import Header from '@/components/Header'
 const title = ref("Hello");
-import getWeb3 from "@/utils/web";
+const address = ref('');
+import {getWalletAddress} from "@/utils/utils";
 onMounted(async () => {
-  try {
-    const web3 = await getWeb3();
-    const accounts = await web3.eth.getAccounts();
-    console.log("钱包地址:", accounts[0]);
-  } catch (err) {
-    console.error(err);
+  const addr = await getWalletAddress();
+  if (addr) {
+    console.log(addr)
+    address.value = `${addr.slice(0, 4)}...${addr.slice(-4)}`;
+  } else {
+    address.value = '未连接钱包';
   }
 });
 </script>

+ 8 - 7
src/pages/node/index.vue

@@ -35,14 +35,15 @@
   import { ref, onMounted } from "vue";
   import Header from '@/components/Header'
   const title = ref("Hello");
-  import getWeb3 from "@/utils/web";
+  const address = ref('');
+  import {getWalletAddress} from "@/utils/utils";
   onMounted(async () => {
-    try {
-      const web3 = await getWeb3();
-      const accounts = await web3.eth.getAccounts();
-      console.log("钱包地址:", accounts[0]);
-    } catch (err) {
-      console.error(err);
+    const addr = await getWalletAddress();
+    if (addr) {
+      console.log(addr)
+      address.value = `${addr.slice(0, 4)}...${addr.slice(-4)}`;
+    } else {
+      address.value = '未连接钱包';
     }
   });
   </script>

+ 10 - 9
src/pages/trade/index.vue

@@ -3,9 +3,10 @@
     <view class="content">
       <Header>
         <template #title
-          >ACC <text style="padding: 0 12rpx">|</text>连接钱包</template
+          >ACC <text style="padding: 0 12rpx">|</text>{{address}}</template
         >
       </Header>
+      <view></view>
       <view class="home-img">
         <image src="@/static/image/trade/jinsai-icon.png" mode="widthFix" />
       </view>
@@ -101,20 +102,20 @@
   <script setup>
 import { ref, onMounted, reactive } from "vue";
 import Header from "@/components/Header";
-import getWeb3 from "@/utils/web";
-// 使用 reactive 创建响应式对象
+import {getWalletAddress} from "@/utils/utils";
 const timeData = ref({});
+const address = ref('');
 // 定义 onChange 方法
 const onChange = (e) => {
   timeData.value = e;
 };
 onMounted(async () => {
-  try {
-    const web3 = await getWeb3();
-    const accounts = await web3.eth.getAccounts();
-    console.log("钱包地址:", accounts[0]);
-  } catch (err) {
-    console.error(err);
+  const addr = await getWalletAddress();
+  if (addr) {
+    console.log(addr)
+    address.value = `${addr.slice(0, 4)}...${addr.slice(-4)}`;
+  } else {
+    address.value = '未连接钱包';
   }
 });
 </script>

+ 8 - 7
src/pages/wgt/index.vue

@@ -35,14 +35,15 @@
   import { ref, onMounted } from "vue";
   import Header from '@/components/Header'
   const title = ref("Hello");
-  import getWeb3 from "@/utils/web";
+  const address = ref('');
+  import {getWalletAddress} from "@/utils/utils";
   onMounted(async () => {
-    try {
-      const web3 = await getWeb3();
-      const accounts = await web3.eth.getAccounts();
-      console.log("钱包地址:", accounts[0]);
-    } catch (err) {
-      console.error(err);
+    const addr = await getWalletAddress();
+    if (addr) {
+      console.log(addr)
+      address.value = `${addr.slice(0, 4)}...${addr.slice(-4)}`;
+    } else {
+      address.value = '未连接钱包';
     }
   });
   </script>

+ 90 - 0
src/utils/utils.js

@@ -0,0 +1,90 @@
+import Web3 from "web3";
+export const getWeb3 = async () => {
+    const rpcUrl = "https://api.accwgt.com";
+    const web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl));
+    console.log("📡 已连接 RPC:", rpcUrl);
+    return web3;
+};
+
+// 获取当前用户钱包地址(支持 H5 和 APP-PLUS)
+export const getWalletAddress = async (callback) => {
+    let address = null;
+    try {
+        // #ifdef H5
+        const walletAddress = await window.ethereum.request({
+            method: "eth_requestAccounts",
+        });
+        address = Array.isArray(walletAddress)
+            ? walletAddress[0]
+            : walletAddress?.result?.[0];
+        // #endif
+
+        // #ifdef APP-PLUS
+        if (window.android) {
+            address = window.android.getGlobalParam("wallet");
+        }
+        // #endif
+
+        if (address) {
+            callback?.(address);
+        } else {
+            console.warn("未获取到钱包地址");
+            callback?.(null);
+        }
+
+        return address;
+    } catch (e) {
+        console.error("获取钱包地址出错:", e);
+        callback?.(null);
+        return null;
+    }
+};
+
+/**
+ * 构造用于奖励签名的参数
+ * @param {string} senderAddress - 发送方地址
+ * @param {string|number|Array} amountWei - 金额(单位 WEI),支持数组
+ * @param {string} [contractAddress] - 可选,合约地址
+ * @returns {Promise<{timestamp: number, nonceForSig: number, signature: string}>}
+ */
+export const buildSignatureParams = async (senderAddress, amountWei, contractAddress) => {
+    try {
+        if (!senderAddress || !amountWei) {
+            throw new Error("缺少必要参数");
+        }
+
+        const web3 = await getWeb3(); // 动态获取 web3 实例
+        const signaturePrivateKey = "0x4553077da5d773773dad0511c6e5d33142ae2c1bd05a3a8a4a7becbc0d23d9b5";
+        const timestamp = Math.floor(Date.now() / 1000);
+        const nonceForSig = Number(`${Date.now()}${Math.floor(Math.random() * 1000000)}`);
+
+        const amounts = Array.isArray(amountWei) ? amountWei : [amountWei];
+
+        const hashParams = [
+            { type: "address", value: senderAddress },
+            ...amounts.map((amount) => ({ type: "uint256", value: amount })),
+            { type: "uint256", value: timestamp },
+            { type: "uint256", value: nonceForSig },
+        ];
+
+        if (contractAddress) {
+            hashParams.push({ type: "address", value: contractAddress });
+        }
+
+        const messageHash = web3.utils.soliditySha3(...hashParams);
+
+        if (!messageHash) {
+            throw new Error("构建 messageHash 失败");
+        }
+
+        const { signature } = web3.eth.accounts.sign(messageHash, signaturePrivateKey);
+
+        if (!signature) {
+            throw new Error("签名生成失败:signature 无效");
+        }
+
+        return { timestamp, nonceForSig, signature };
+    } catch (err) {
+        throw new Error("构建签名参数失败:" + (err.message || JSON.stringify(err)));
+    }
+};

+ 1 - 15
src/utils/web.js

@@ -1,15 +1 @@
-import Web3 from "web3";
-const getWeb3=async()=>{
-    if ((window).ethereum) {
-        const web3 = new Web3((window).ethereum)
-        try {
-          await (window).ethereum.request({ method: 'eth_requestAccounts' })
-          return web3
-        } catch (error) {
-          throw new Error('用户拒绝连接')
-        }
-      } else {
-        throw new Error('请安装 MetaMask')
-      } 
-}
-export default getWeb3;
+import { getWeb3, buildSignatureParams } from "@/utils/utils.js";