miniwallet 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +158 -0
- package/dist/miniwallet.css +3 -0
- package/dist/miniwallet.d.ts +42 -0
- package/dist/miniwallet.js +318 -0
- package/package.json +73 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 RowanDuan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# MiniWallet
|
|
2
|
+
|
|
3
|
+
React 钱包连接按钮组件:一键连接浏览器扩展钱包,展示账户、余额,并支持切换网络。
|
|
4
|
+
|
|
5
|
+
## 功能
|
|
6
|
+
|
|
7
|
+
- 连接 / 断开钱包
|
|
8
|
+
- 展示地址、余额、当前网络
|
|
9
|
+
- 切换网络(Ethereum / Sepolia / Polygon,可自定义)
|
|
10
|
+
- 多钱包支持(EIP-6963 发现):MetaMask、OKX、Phantom、Coinbase
|
|
11
|
+
- 刷新后自动恢复上次连接(`localStorage` + `eth_accounts`)
|
|
12
|
+
|
|
13
|
+
## 快速开始
|
|
14
|
+
|
|
15
|
+
### 本地 Demo
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pnpm install
|
|
19
|
+
pnpm dev
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### 在项目中使用
|
|
23
|
+
|
|
24
|
+
用 `MiniWalletProvider` 包裹应用,再放置 `MiniWalletButton`:
|
|
25
|
+
|
|
26
|
+
```tsx
|
|
27
|
+
import { MiniWalletProvider, MiniWalletButton } from "miniwallet"
|
|
28
|
+
|
|
29
|
+
function App() {
|
|
30
|
+
return (
|
|
31
|
+
<MiniWalletProvider>
|
|
32
|
+
<MiniWalletButton label="Connect Wallet" />
|
|
33
|
+
</MiniWalletProvider>
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
自定义支持的链(可选,不传则使用内置默认链):
|
|
39
|
+
|
|
40
|
+
```tsx
|
|
41
|
+
<MiniWalletProvider
|
|
42
|
+
chains={[
|
|
43
|
+
{ id: 1, name: "Ethereum", symbol: "ETH", decimals: 18 },
|
|
44
|
+
{ id: 137, name: "Polygon", symbol: "MATIC", decimals: 18 },
|
|
45
|
+
]}
|
|
46
|
+
>
|
|
47
|
+
<MiniWalletButton />
|
|
48
|
+
</MiniWalletProvider>
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### 使用 Hook
|
|
52
|
+
|
|
53
|
+
需要自定义 UI 时,可直接使用 `useMiniWallet`(必须在 `MiniWalletProvider` 内):
|
|
54
|
+
|
|
55
|
+
```tsx
|
|
56
|
+
import { useMiniWallet } from "miniwallet"
|
|
57
|
+
|
|
58
|
+
function CustomWalletUI() {
|
|
59
|
+
const {
|
|
60
|
+
account,
|
|
61
|
+
balance,
|
|
62
|
+
isConnected,
|
|
63
|
+
selectedChain,
|
|
64
|
+
wallet,
|
|
65
|
+
chains,
|
|
66
|
+
connect,
|
|
67
|
+
disconnect,
|
|
68
|
+
switchChain,
|
|
69
|
+
} = useMiniWallet()
|
|
70
|
+
|
|
71
|
+
// 自定义渲染...
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## API
|
|
76
|
+
|
|
77
|
+
### `MiniWalletProvider`
|
|
78
|
+
|
|
79
|
+
| Prop | 类型 | 默认值 | 说明 |
|
|
80
|
+
|------|------|--------|------|
|
|
81
|
+
| `children` | `ReactNode` | — | 子节点 |
|
|
82
|
+
| `chains` | `ChainConfig[]` | 内置默认链 | 可选网络列表 |
|
|
83
|
+
|
|
84
|
+
### `MiniWalletButton`
|
|
85
|
+
|
|
86
|
+
| Prop | 类型 | 默认值 | 说明 |
|
|
87
|
+
|------|------|--------|------|
|
|
88
|
+
| `label` | `string \| null` | `"Connect Wallet"` | 未连接时按钮文案 |
|
|
89
|
+
|
|
90
|
+
### `useMiniWallet()` 返回值
|
|
91
|
+
|
|
92
|
+
| 字段 | 类型 | 说明 |
|
|
93
|
+
|------|------|------|
|
|
94
|
+
| `account` | `string` | 当前账户地址 |
|
|
95
|
+
| `balance` | `string` | 格式化后的余额 |
|
|
96
|
+
| `isConnected` | `boolean` | 是否已连接 |
|
|
97
|
+
| `selectedChain` | `ChainConfig \| null` | 当前网络 |
|
|
98
|
+
| `wallet` | `WalletProvider \| undefined` | 当前钱包信息 |
|
|
99
|
+
| `chains` | `ChainConfig[]` | 可用网络列表 |
|
|
100
|
+
| `connect` | `(wallet) => Promise<void>` | 连接指定钱包 |
|
|
101
|
+
| `disconnect` | `() => Promise<void>` | 断开连接 |
|
|
102
|
+
| `switchChain` | `(chain) => Promise<void>` | 切换网络 |
|
|
103
|
+
|
|
104
|
+
### 类型
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
type ChainConfig = {
|
|
108
|
+
id: number // 链 ID,如 1、11155111、137
|
|
109
|
+
name: string
|
|
110
|
+
symbol: string
|
|
111
|
+
decimals: number
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
type WalletProvider = {
|
|
115
|
+
id: string
|
|
116
|
+
name: string
|
|
117
|
+
icon: string
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## 支持的钱包
|
|
122
|
+
|
|
123
|
+
| 钱包 | EIP-6963 rdns |
|
|
124
|
+
|------|----------------|
|
|
125
|
+
| MetaMask | `io.metamask` |
|
|
126
|
+
| OKX | `com.okx.wallet` |
|
|
127
|
+
| Phantom | `app.phantom` |
|
|
128
|
+
| Coinbase | `com.coinbase.wallet` |
|
|
129
|
+
|
|
130
|
+
需安装对应**浏览器扩展**;未检测到时会提示「请安装钱包」。
|
|
131
|
+
|
|
132
|
+
## 默认网络
|
|
133
|
+
|
|
134
|
+
| 网络 | id | symbol |
|
|
135
|
+
|------|-----|--------|
|
|
136
|
+
| Ethereum | `1` | ETH |
|
|
137
|
+
| Sepolia | `11155111` | SepoliaETH |
|
|
138
|
+
| Polygon | `137` | MATIC |
|
|
139
|
+
|
|
140
|
+
## 项目结构
|
|
141
|
+
|
|
142
|
+
```
|
|
143
|
+
src/
|
|
144
|
+
MiniWalletProvider.tsx # Context + 连接 / 切链 / 余额 / 自动恢复
|
|
145
|
+
MiniWalletButton.tsx # 连接按钮 UI
|
|
146
|
+
useMiniWallet.ts # Hook
|
|
147
|
+
constants.ts # 钱包列表、默认链、rdns 映射
|
|
148
|
+
types.ts # 类型
|
|
149
|
+
utils/formatUnits.ts # wei → 可读余额
|
|
150
|
+
icons/ # 钱包与断开图标
|
|
151
|
+
dev/ # 本地 Demo(不随包发布)
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## 说明
|
|
155
|
+
|
|
156
|
+
- 钱包发现基于 [EIP-6963](https://eips.ethereum.org/EIPS/eip-6963),避免多扩展抢占 `window.ethereum`。
|
|
157
|
+
- 主动连接使用 `eth_requestAccounts`;刷新恢复使用 `eth_accounts`(不弹窗)。
|
|
158
|
+
- 上次连接的钱包 id 保存在 `localStorage` 的 `miniwallet:lastWalletId`。
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/*! tailwindcss v4.3.2 | MIT License | https://tailwindcss.com */
|
|
2
|
+
@layer properties{@supports (((-webkit-hyphens:none)) and (not (margin-trim:inline))) or ((-moz-orient:inline) and (not (color:rgb(from red r g b)))){*,:before,:after,::backdrop{--tw-translate-x:0;--tw-translate-y:0;--tw-translate-z:0;--tw-rotate-x:initial;--tw-rotate-y:initial;--tw-rotate-z:initial;--tw-skew-x:initial;--tw-skew-y:initial;--tw-space-x-reverse:0;--tw-border-style:solid;--tw-font-weight:initial;--tw-shadow:0 0 #0000;--tw-shadow-color:initial;--tw-shadow-alpha:100%;--tw-inset-shadow:0 0 #0000;--tw-inset-shadow-color:initial;--tw-inset-shadow-alpha:100%;--tw-ring-color:initial;--tw-ring-shadow:0 0 #0000;--tw-inset-ring-color:initial;--tw-inset-ring-shadow:0 0 #0000;--tw-ring-inset:initial;--tw-ring-offset-width:0px;--tw-ring-offset-color:#fff;--tw-ring-offset-shadow:0 0 #0000;--tw-blur:initial;--tw-brightness:initial;--tw-contrast:initial;--tw-grayscale:initial;--tw-hue-rotate:initial;--tw-invert:initial;--tw-opacity:initial;--tw-saturate:initial;--tw-sepia:initial;--tw-drop-shadow:initial;--tw-drop-shadow-color:initial;--tw-drop-shadow-alpha:100%;--tw-drop-shadow-size:initial}}}@layer theme{:root,:host{--color-blue-500:oklch(62.3% .214 259.815);--color-gray-100:oklch(96.7% .003 264.542);--color-gray-200:oklch(92.8% .006 264.531);--color-gray-300:oklch(87.2% .01 258.338);--color-gray-400:oklch(70.7% .022 261.325);--color-gray-500:oklch(55.1% .027 264.364);--color-gray-600:oklch(44.6% .03 256.802);--color-white:#fff;--spacing:.25rem;--text-xs:.75rem;--text-xs--line-height:calc(1 / .75);--text-sm:.875rem;--text-sm--line-height:calc(1.25 / .875);--text-2xl:1.5rem;--text-2xl--line-height:calc(2 / 1.5);--font-weight-bold:700;--radius-md:.375rem;--radius-xl:.75rem}}@layer base,components;@layer utilities{.absolute{position:absolute}.fixed{position:fixed}.top-0{top:0}.top-1\/2{top:50%}.right-0{right:0}.bottom-0{bottom:0}.left-0{left:0}.left-1\/2{left:50%}.mr-1{margin-right:var(--spacing)}.mr-4{margin-right:calc(var(--spacing) * 4)}.mb-2{margin-bottom:calc(var(--spacing) * 2)}.flex{display:flex}.inline-flex{display:inline-flex}.h-5{height:calc(var(--spacing) * 5)}.h-6{height:calc(var(--spacing) * 6)}.h-8{height:calc(var(--spacing) * 8)}.h-\[22px\]{height:22px}.h-auto{height:auto}.w-5{width:calc(var(--spacing) * 5)}.w-6{width:calc(var(--spacing) * 6)}.w-8{width:calc(var(--spacing) * 8)}.w-\[460px\]{width:460px}.-translate-x-1\/2{--tw-translate-x:calc(calc(1 / 2 * 100%) * -1);translate:var(--tw-translate-x) var(--tw-translate-y)}.-translate-y-1\/2{--tw-translate-y:calc(calc(1 / 2 * 100%) * -1);translate:var(--tw-translate-x) var(--tw-translate-y)}.transform{transform:var(--tw-rotate-x,) var(--tw-rotate-y,) var(--tw-rotate-z,) var(--tw-skew-x,) var(--tw-skew-y,)}.cursor-pointer{cursor:pointer}.flex-col{flex-direction:column}.items-center{align-items:center}.justify-between{justify-content:space-between}.justify-center{justify-content:center}.gap-2{gap:calc(var(--spacing) * 2)}:where(.space-x-2>:not(:last-child)){--tw-space-x-reverse:0;margin-inline-start:calc(calc(var(--spacing) * 2) * var(--tw-space-x-reverse));margin-inline-end:calc(calc(var(--spacing) * 2) * calc(1 - var(--tw-space-x-reverse)))}.rounded-full{border-radius:3.40282e38px}.rounded-md{border-radius:var(--radius-md)}.rounded-xl{border-radius:var(--radius-xl)}.border{border-style:var(--tw-border-style);border-width:1px}.border-gray-300{border-color:var(--color-gray-300)}.bg-\[rgba\(0\,0\,0\,0\.5\)\]{background-color:#00000080}.bg-blue-500{background-color:var(--color-blue-500)}.bg-white{background-color:var(--color-white)}.p-1{padding:var(--spacing)}.p-6{padding:calc(var(--spacing) * 6)}.px-2{padding-inline:calc(var(--spacing) * 2)}.px-4{padding-inline:calc(var(--spacing) * 4)}.py-2{padding-block:calc(var(--spacing) * 2)}.py-2\.5{padding-block:calc(var(--spacing) * 2.5)}.pt-2{padding-top:calc(var(--spacing) * 2)}.text-center{text-align:center}.text-left{text-align:left}.text-2xl{font-size:var(--text-2xl);line-height:var(--tw-leading,var(--text-2xl--line-height))}.text-sm{font-size:var(--text-sm);line-height:var(--tw-leading,var(--text-sm--line-height))}.text-xs{font-size:var(--text-xs);line-height:var(--tw-leading,var(--text-xs--line-height))}.font-bold{--tw-font-weight:var(--font-weight-bold);font-weight:var(--font-weight-bold)}.text-gray-400{color:var(--color-gray-400)}.text-gray-500{color:var(--color-gray-500)}.text-gray-600{color:var(--color-gray-600)}.text-white{color:var(--color-white)}.shadow-lg{--tw-shadow:0 10px 15px -3px var(--tw-shadow-color,#0000001a), 0 4px 6px -4px var(--tw-shadow-color,#0000001a);box-shadow:var(--tw-inset-shadow), var(--tw-inset-ring-shadow), var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow)}.shadow-blue-500\/50{--tw-shadow-color:#3080ff80}@supports (color:color-mix(in lab, red, red)){.shadow-blue-500\/50{--tw-shadow-color:color-mix(in oklab, color-mix(in oklab, var(--color-blue-500) 50%, transparent) var(--tw-shadow-alpha), transparent)}}.shadow-gray-200{--tw-shadow-color:oklch(92.8% .006 264.531)}@supports (color:color-mix(in lab, red, red)){.shadow-gray-200{--tw-shadow-color:color-mix(in oklab, var(--color-gray-200) var(--tw-shadow-alpha), transparent)}}.filter{filter:var(--tw-blur,) var(--tw-brightness,) var(--tw-contrast,) var(--tw-grayscale,) var(--tw-hue-rotate,) var(--tw-invert,) var(--tw-saturate,) var(--tw-sepia,) var(--tw-drop-shadow,)}@media (hover:hover){.hover\:bg-gray-100:hover{background-color:var(--color-gray-100)}}}@property --tw-translate-x{syntax:"*";inherits:false;initial-value:0}@property --tw-translate-y{syntax:"*";inherits:false;initial-value:0}@property --tw-translate-z{syntax:"*";inherits:false;initial-value:0}@property --tw-rotate-x{syntax:"*";inherits:false}@property --tw-rotate-y{syntax:"*";inherits:false}@property --tw-rotate-z{syntax:"*";inherits:false}@property --tw-skew-x{syntax:"*";inherits:false}@property --tw-skew-y{syntax:"*";inherits:false}@property --tw-space-x-reverse{syntax:"*";inherits:false;initial-value:0}@property --tw-border-style{syntax:"*";inherits:false;initial-value:solid}@property --tw-font-weight{syntax:"*";inherits:false}@property --tw-shadow{syntax:"*";inherits:false;initial-value:0 0 #0000}@property --tw-shadow-color{syntax:"*";inherits:false}@property --tw-shadow-alpha{syntax:"<percentage>";inherits:false;initial-value:100%}@property --tw-inset-shadow{syntax:"*";inherits:false;initial-value:0 0 #0000}@property --tw-inset-shadow-color{syntax:"*";inherits:false}@property --tw-inset-shadow-alpha{syntax:"<percentage>";inherits:false;initial-value:100%}@property --tw-ring-color{syntax:"*";inherits:false}@property --tw-ring-shadow{syntax:"*";inherits:false;initial-value:0 0 #0000}@property --tw-inset-ring-color{syntax:"*";inherits:false}@property --tw-inset-ring-shadow{syntax:"*";inherits:false;initial-value:0 0 #0000}@property --tw-ring-inset{syntax:"*";inherits:false}@property --tw-ring-offset-width{syntax:"<length>";inherits:false;initial-value:0}@property --tw-ring-offset-color{syntax:"*";inherits:false;initial-value:#fff}@property --tw-ring-offset-shadow{syntax:"*";inherits:false;initial-value:0 0 #0000}@property --tw-blur{syntax:"*";inherits:false}@property --tw-brightness{syntax:"*";inherits:false}@property --tw-contrast{syntax:"*";inherits:false}@property --tw-grayscale{syntax:"*";inherits:false}@property --tw-hue-rotate{syntax:"*";inherits:false}@property --tw-invert{syntax:"*";inherits:false}@property --tw-opacity{syntax:"*";inherits:false}@property --tw-saturate{syntax:"*";inherits:false}@property --tw-sepia{syntax:"*";inherits:false}@property --tw-drop-shadow{syntax:"*";inherits:false}@property --tw-drop-shadow-color{syntax:"*";inherits:false}@property --tw-drop-shadow-alpha{syntax:"<percentage>";inherits:false;initial-value:100%}@property --tw-drop-shadow-size{syntax:"*";inherits:false}
|
|
3
|
+
/*$vite$:1*/
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { JSX } from 'react';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
|
|
4
|
+
export declare type ChainConfig = {
|
|
5
|
+
id: number;
|
|
6
|
+
name: string;
|
|
7
|
+
symbol: string;
|
|
8
|
+
decimals: number;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export declare function MiniWalletButton({ label, }: MiniWalletButtonProps): JSX.Element;
|
|
12
|
+
|
|
13
|
+
export declare type MiniWalletButtonProps = {
|
|
14
|
+
label?: string | null;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export declare function MiniWalletProvider({ children, chains, }: {
|
|
18
|
+
children: ReactNode;
|
|
19
|
+
chains?: ChainConfig[];
|
|
20
|
+
}): JSX.Element;
|
|
21
|
+
|
|
22
|
+
export declare function useMiniWallet(): WalletSate;
|
|
23
|
+
|
|
24
|
+
export declare interface WalletProvider {
|
|
25
|
+
id: string;
|
|
26
|
+
name: string;
|
|
27
|
+
icon: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export declare type WalletSate = {
|
|
31
|
+
account: string;
|
|
32
|
+
balance: string;
|
|
33
|
+
isConnected: boolean;
|
|
34
|
+
selectedChain: ChainConfig | null;
|
|
35
|
+
wallet: WalletProvider | undefined;
|
|
36
|
+
chains: ChainConfig[];
|
|
37
|
+
connect: (provider: WalletProvider) => Promise<void>;
|
|
38
|
+
disconnect: () => Promise<void>;
|
|
39
|
+
switchChain: (chain: ChainConfig) => Promise<void>;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export { }
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import "./miniwallet.css";
|
|
2
|
+
import { createContext as e, useCallback as t, useContext as n, useEffect as r, useMemo as i, useState as a } from "react";
|
|
3
|
+
import { jsx as o, jsxs as s } from "react/jsx-runtime";
|
|
4
|
+
//#region src/MiniWalletContext.tsx
|
|
5
|
+
var c = e(null);
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region src/utils/formatUnits.ts
|
|
8
|
+
function l(e) {
|
|
9
|
+
if (typeof e == "bigint") return e;
|
|
10
|
+
let t = e.trim();
|
|
11
|
+
if (!t) throw Error("value must not be empty");
|
|
12
|
+
return BigInt(t);
|
|
13
|
+
}
|
|
14
|
+
function u(e, t) {
|
|
15
|
+
if (!Number.isInteger(t) || t < 0) throw Error("decimals must be a non-negative integer");
|
|
16
|
+
let n = l(e);
|
|
17
|
+
if (t === 0) return n.toString();
|
|
18
|
+
let r = 10n ** BigInt(t), i = n / r, a = n % r;
|
|
19
|
+
return a === 0n ? i.toString() : `${i}.${a.toString().padStart(t, "0").replace(/0+$/, "")}`;
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/constants.ts
|
|
23
|
+
var d = [
|
|
24
|
+
{
|
|
25
|
+
id: "metamask",
|
|
26
|
+
name: "MetaMask",
|
|
27
|
+
icon: "data:image/svg+xml,%3c?xml%20version='1.0'%20standalone='no'?%3e%3c!DOCTYPE%20svg%20PUBLIC%20'-//W3C//DTD%20SVG%201.1//EN'%20'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'%3e%3csvg%20t='1783438662820'%20class='icon'%20viewBox='0%200%201024%201024'%20version='1.1'%20xmlns='http://www.w3.org/2000/svg'%20p-id='9997'%20width='32'%20height='32'%20xmlns:xlink='http://www.w3.org/1999/xlink'%3e%3cpath%20d='M572.224%20361.19466667l-120.832%200.18773333-47.14666667-113.8688z'%20fill='%23F6851B'%20p-id='9998'%3e%3c/path%3e%3cpath%20d='M404.24533333%20247.5136l216.7488-0.45226667-48.77013333%20114.13333334z'%20fill='%23F6851B'%20p-id='9999'%3e%3c/path%3e%3cpath%20d='M451.392%20361.3824L147.26826667%20132.64213333l256.97706666%20114.87146667z'%20fill='%23E4761B'%20p-id='10000'%3e%3c/path%3e%3cpath%20d='M876.79786667%20130.70933333l-304.576%20230.48533334%2048.77226666-114.13333334z'%20fill='%23E2761B'%20p-id='10001'%3e%3c/path%3e%3cpath%20d='M270.64106667%20458.77973333l-137.8048-35.89973333L125.44%20390.74133333zM137.5488%20461.48906667l-4.71466667-38.60906667%20137.80693334%2035.89973333zM156.82773333%20517.2544l113.81333334-58.47466667%2059.17866666%2010.51093334z'%20fill='%23763D16'%20p-id='10002'%3e%3c/path%3e%3cpath%20d='M137.5488%20461.48906667l133.09226667-2.70933334-113.81333334%2058.47466667zM754.80106667%20458.26986667l145.38666666-68.60373334-7.45813333%2032.27733334zM887.9488%20460.70613333l-133.14773333-2.43626666%20137.92853333-36.32426667zM868.55466667%20516.70613333l-173.71733334-47.78026666%2059.96373334-10.656z'%20fill='%23763D16'%20p-id='10003'%3e%3c/path%3e%3cpath%20d='M887.9488%20460.70613333l-19.39413333%2056-113.7536-58.43626666zM276.50773333%20284.01066667l-5.86666666%20174.76906666L125.44%20390.74133333zM329.82186667%20469.29066667l-59.1808-10.51093334%205.86666666-174.76906666z'%20fill='%23763D16'%20p-id='10004'%3e%3c/path%3e%3cpath%20d='M333.26933333%20824.46933333l45.16266667-23.89973333%207.89973333-1.86026667zM639.24053333%20798.8608l6.97386667%201.87093333%2044.05333333%2023.99573334z'%20fill='%23E2761B'%20p-id='10005'%3e%3c/path%3e%3cpath%20d='M900.18773333%20389.66613333l-145.38666666%2068.60373334-7.04-175.15946667z'%20fill='%23763D16'%20p-id='10006'%3e%3c/path%3e%3cpath%20d='M747.76106667%20283.1104l7.04%20175.15946667-59.96586667%2010.656z'%20fill='%23763D16'%20p-id='10007'%3e%3c/path%3e%3cpath%20d='M333.26933333%20824.46933333l-193.26506666%2071.392L92.65066667%20702.528z'%20fill='%23E4761B'%20p-id='10008'%3e%3c/path%3e%3cpath%20d='M107.49013333%20402.36373333l17.952-11.6224%207.39413334%2032.13866667z'%20fill='%23763D16'%20p-id='10009'%3e%3c/path%3e%3cpath%20d='M933.73226667%20702.63893333l-47.40266667%20194.02666667-196.06186667-71.936z'%20fill='%23E4761B'%20p-id='10010'%3e%3c/path%3e%3cpath%20d='M276.50773333%20284.01066667l174.88426667%2077.37173333-121.57013333%20107.90826667zM137.5488%20461.48906667L112.46933333%20436.992l20.3648-14.11413333zM694.8352%20468.92586667L572.224%20361.19466667l175.53706667-78.0864zM918.1568%20401.2864l-25.4272%2020.6592%207.45813333-32.27733333zM125.44%20390.74133333l-22.35733333-134.1952%20173.42506666%2027.46453334zM156.82773333%20517.2544l-35.9424-45.024%2016.66133334-10.74133333z'%20fill='%23763D16'%20p-id='10011'%3e%3c/path%3e%3cpath%20d='M887.9488%20460.70613333l4.7808-38.76266666%2020.37973333%2014.12053333zM747.76106667%20283.1104l174.33813333-28.2368-21.91146667%20134.79253333z'%20fill='%23763D16'%20p-id='10012'%3e%3c/path%3e%3cpath%20d='M275.75253333%20529.63413333L92.65066667%20702.52586667l64.17493333-185.27146667z'%20fill='%23F6851B'%20p-id='10013'%3e%3c/path%3e%3cpath%20d='M868.55466667%20516.70613333l19.392-56%2016.6784%2010.74773334z'%20fill='%23763D16'%20p-id='10014'%3e%3c/path%3e%3cpath%20d='M246.9248%20680.10453333l-154.272%2022.42133334%20183.09973333-172.89173334z'%20fill='%23F6851B'%20p-id='10015'%3e%3c/path%3e%3cpath%20d='M92.6528%20702.528l154.272-22.42346667%2086.34666667%20144.36266667z'%20fill='%23E4761B'%20p-id='10016'%3e%3c/path%3e%3cpath%20d='M747.30453333%20529.2992l121.25013334-12.59306667%2065.1776%20185.9328zM747.30453333%20529.2992l186.42773334%20173.33973333-157.57653334-22.5216z'%20fill='%23F6851B'%20p-id='10017'%3e%3c/path%3e%3cpath%20d='M690.26773333%20824.72746667l85.888-144.61013334%20157.57653334%2022.5216z'%20fill='%23E4761B'%20p-id='10018'%3e%3c/path%3e%3cpath%20d='M156.82773333%20517.2544l172.992-47.96373333-54.0672%2060.34346666zM868.55466667%20516.70613333l-121.25013334%2012.59306667-52.46933333-60.37333333z'%20fill='%23F6851B'%20p-id='10019'%3e%3c/path%3e%3cpath%20d='M329.82186667%20469.29066667l121.57013333-107.90826667%209.1392%20169.20746667zM460.5312%20530.58986667l-184.77866667-0.95573334%2054.06933334-60.34346666zM694.8352%20468.92586667l52.46933333%2060.37333333-186.00746666%201.22026667zM561.29706667%20530.51733333l10.92693333-169.32266666%20122.6112%20107.7312z'%20fill='%23E4761B'%20p-id='10020'%3e%3c/path%3e%3cpath%20d='M147.26826667%20132.64213333L451.392%20361.3824l-174.88426667-77.37173333zM276.50773333%20284.01066667l-173.42293333-27.46453334%2044.18133333-123.904zM876.79786667%20130.70933333l45.30133333%20124.16426667-174.33813333%2028.2368zM747.76106667%20283.1104l-175.53706667%2078.0864L876.79786667%20130.70933333z'%20fill='%23763D16'%20p-id='10021'%3e%3c/path%3e%3cpath%20d='M451.392%20361.3824l120.832-0.1856-10.92693333%20169.32266667z'%20fill='%23F6851B'%20p-id='10022'%3e%3c/path%3e%3cpath%20d='M561.29706667%20530.51733333l-100.76586667%200.07253334-9.1392-169.20746667z'%20fill='%23F6851B'%20p-id='10023'%3e%3c/path%3e%3cpath%20d='M333.26933333%20824.46933333l-86.34453333-144.3648%20103.7504-4.9536zM690.26773333%20824.72746667l-18.93973333-149.57653334%20104.82773333%204.9664zM349.17546667%20600.82133333l-73.42293334-71.18933333%20184.77866667%200.96z'%20fill='%23CD6116'%20p-id='10024'%3e%3c/path%3e%3cpath%20d='M275.75253333%20529.63413333l74.92266667%20145.5168-103.7504%204.9536z'%20fill='%23F6851B'%20p-id='10025'%3e%3c/path%3e%3cpath%20d='M275.75253333%20529.63413333l73.42293334%2071.18933334%201.49973333%2074.32746666z'%20fill='%23E4751F'%20p-id='10026'%3e%3c/path%3e%3cpath%20d='M561.29706667%20530.51733333l186.00533333-1.21813333-74.21866667%2071.4048z'%20fill='%23CD6116'%20p-id='10027'%3e%3c/path%3e%3cpath%20d='M776.15573333%20680.11733333l-104.82773333-4.9664L747.30666667%20529.30133333z'%20fill='%23F6851B'%20p-id='10028'%3e%3c/path%3e%3cpath%20d='M747.30453333%20529.2992l-75.97653333%20145.85173333%201.75573333-74.44693333z'%20fill='%23E4751F'%20p-id='10029'%3e%3c/path%3e%3cpath%20d='M369.67466667%20845.5488l-36.4032-21.0816%20104.52266666%207.49226667zM580.97066667%20832.06826667l109.29706666-7.33866667-37.54453333%2021.056z'%20fill='%23C0AD9E'%20p-id='10030'%3e%3c/path%3e%3cpath%20d='M460.5312%20530.58986667l-44.83413333%2039.21493333-66.5216%2031.01866667z'%20fill='%23CD6116'%20p-id='10031'%3e%3c/path%3e%3cpath%20d='M436.69546667%20790.58346667l1.09866666%2041.376-104.52266666-7.49226667z'%20fill='%23D7C1B3'%20p-id='10032'%3e%3c/path%3e%3cpath%20d='M673.08373333%20600.704l-66.77333333-30.99733333-45.01333333-39.18933334z'%20fill='%23CD6116'%20p-id='10033'%3e%3c/path%3e%3cpath%20d='M446.03733333%20713.14773333l-112.768%20111.31946667%2017.408-149.31626667z'%20fill='%23E4761B'%20p-id='10034'%3e%3c/path%3e%3cpath%20d='M333.26933333%20824.46933333l112.768-111.3216-9.34186666%2077.43573334zM581.1712%20790.66453333l109.09653333%2034.06293334-109.29493333%207.3408z'%20fill='%23D7C1B3'%20p-id='10035'%3e%3c/path%3e%3cpath%20d='M571.71413333%20713.1712l99.61386667-38.02026667%2018.93973333%20149.57653334z'%20fill='%23E4761B'%20p-id='10036'%3e%3c/path%3e%3cpath%20d='M690.26773333%20824.72746667l-109.09653333-34.06293334-9.45706667-77.49333333z'%20fill='%23D7C1B3'%20p-id='10037'%3e%3c/path%3e%3cpath%20d='M350.67733333%20675.15093333l-1.50186666-74.3296%2093.5104%2022.62826667z'%20fill='%23F6851B'%20p-id='10038'%3e%3c/path%3e%3cpath%20d='M442.68586667%20623.4496l-93.5104-22.62826667%2066.5216-31.01653333z'%20fill='%23233447'%20p-id='10039'%3e%3c/path%3e%3cpath%20d='M671.328%20675.15093333l-92.50133333-51.73333333%2094.25706666-22.7136z'%20fill='%23F6851B'%20p-id='10040'%3e%3c/path%3e%3cpath%20d='M578.82666667%20623.41546667l27.48586666-53.7088%2066.7712%2030.99733333z'%20fill='%23233447'%20p-id='10041'%3e%3c/path%3e%3cpath%20d='M415.69706667%20569.8048l44.83413333-39.21493333-17.84533333%2092.85973333zM606.31253333%20569.70666667l-27.48586666%2053.7088-17.5296-92.896z'%20fill='%23CD6116'%20p-id='10042'%3e%3c/path%3e%3cpath%20d='M464.1536%20583.94666667l-3.6224-53.35466667%20100.76586667-0.07253333z'%20fill='%23F6851B'%20p-id='10043'%3e%3c/path%3e%3cpath%20d='M442.68586667%20623.4496l17.84533333-92.85973333%203.6224%2053.3568z'%20fill='%23E4751F'%20p-id='10044'%3e%3c/path%3e%3cpath%20d='M561.29706667%20530.51733333l-4.3328%2053.3888-92.81066667%200.04266667z'%20fill='%23F6851B'%20p-id='10045'%3e%3c/path%3e%3cpath%20d='M578.82666667%20623.41546667l-21.8624-39.50933334%204.3328-53.38666666z'%20fill='%23E2761B'%20p-id='10046'%3e%3c/path%3e%3cpath%20d='M437.79413333%20831.95733333l6.09493334%2031.36853334-74.2144-17.77706667zM652.7232%20845.7856l-78.07146667%2017.66186667%206.32106667-31.3792z'%20fill='%23C0AD9E'%20p-id='10047'%3e%3c/path%3e%3cpath%20d='M446.03733333%20713.14773333l-95.36-37.99466666%2092.00853334-51.70346667zM578.82666667%20623.41546667l92.50133333%2051.73546666-99.61386667%2038.02026667z'%20fill='%23F6851B'%20p-id='10048'%3e%3c/path%3e%3cpath%20d='M464.1536%20583.94666667l-1.8944%20117.9136-19.57333333-78.41066667z'%20fill='%23E4751F'%20p-id='10049'%3e%3c/path%3e%3cpath%20d='M442.68586667%20623.4496l19.57333333%2078.41066667-16.224%2011.28533333z'%20fill='%23F6851B'%20p-id='10050'%3e%3c/path%3e%3cpath%20d='M556.96426667%20583.90613333l21.8624%2039.50933334-23.3472%2078.4576z'%20fill='%23E4751F'%20p-id='10051'%3e%3c/path%3e%3cpath%20d='M571.71413333%20713.1712l-16.23466666-11.29813333%2023.3472-78.4576z'%20fill='%23F6851B'%20p-id='10052'%3e%3c/path%3e%3cpath%20d='M464.1536%20583.94666667l92.81066667-0.04053334-1.4848%20117.96693334zM555.47733333%20701.87306667l-93.21813333-0.0128%201.8944-117.9136z'%20fill='%23F6851B'%20p-id='10053'%3e%3c/path%3e%3cpath%20d='M443.88906667%20863.32586667l-6.09493334-31.3664-1.09866666-41.376zM581.1712%20790.66453333l-0.1984%2041.40373334-6.32106667%2031.3792z'%20fill='%23C0AD9E'%20p-id='10054'%3e%3c/path%3e%3cpath%20d='M436.69546667%20790.58346667l8.18773333-12.04053334-0.99413333%2084.78293334zM443.88906667%20863.32586667l0.99413333-84.78293334%20127.66506667%200.064z'%20fill='%23C0AD9E'%20p-id='10055'%3e%3c/path%3e%3cpath%20d='M572.54826667%20778.60693333l2.10346666%2084.84053334-130.76266666-0.1216zM574.65173333%20863.44533333l-2.10346666-84.8384%208.62293333%2012.0576z'%20fill='%23C0AD9E'%20p-id='10056'%3e%3c/path%3e%3cpath%20d='M444.88533333%20778.54293333l-8.18986666%2012.04053334%209.33973333-77.43573334zM571.71413333%20713.1712l9.45706667%2077.49333333-8.62293333-12.0576zM460.928%20708.90026667l1.3312-7.04%2093.22026667%200.0128z'%20fill='%23161616'%20p-id='10057'%3e%3c/path%3e%3cpath%20d='M446.03733333%20713.14773333l16.22186667-11.28746666-1.32906667%207.04z'%20fill='%23161616'%20p-id='10058'%3e%3c/path%3e%3cpath%20d='M555.47733333%20701.87306667l0.46506667%207.04426666-95.01226667-0.01706666zM555.9424%20708.91733333l-0.46293333-7.04426666%2016.23466666%2011.29813333zM446.03733333%20713.14773333l4.50133334%207.4624-5.65333334%2057.9328z'%20fill='%23161616'%20p-id='10059'%3e%3c/path%3e%3cpath%20d='M460.928%20708.90026667l-10.38933333%2011.70986666-4.50133334-7.4624zM572.54826667%20778.60693333l-6.21013334-57.9712%205.376-7.46666666z'%20fill='%23161616'%20p-id='10060'%3e%3c/path%3e%3cpath%20d='M571.71413333%20713.1712l-5.376%207.46453333-10.39573333-11.7184zM572.54826667%20778.60693333l-127.66506667-0.064%205.65333333-57.9328zM450.53866667%20720.61013333l115.79733333%200.0256%206.21226667%2057.9712zM450.53866667%20720.61013333l10.38933333-11.70986666%2095.0144%200.01706666z'%20fill='%23161616'%20p-id='10061'%3e%3c/path%3e%3cpath%20d='M555.9424%20708.91733333l10.39573333%2011.7184-115.79946666-0.0256z'%20fill='%23161616'%20p-id='10062'%3e%3c/path%3e%3c/svg%3e"
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
id: "okx",
|
|
31
|
+
name: "OKX",
|
|
32
|
+
icon: "data:image/svg+xml,%3c?xml%20version='1.0'%20standalone='no'?%3e%3c!DOCTYPE%20svg%20PUBLIC%20'-//W3C//DTD%20SVG%201.1//EN'%20'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'%3e%3csvg%20t='1783439758863'%20class='icon'%20viewBox='0%200%201024%201024'%20version='1.1'%20xmlns='http://www.w3.org/2000/svg'%20p-id='11121'%20xmlns:xlink='http://www.w3.org/1999/xlink'%20width='32'%20height='32'%3e%3cpath%20d='M0%20182.857143a182.857143%20182.857143%200%200%201%20182.857143-182.857143h658.285714a182.857143%20182.857143%200%200%201%20182.857143%20182.857143v658.285714a182.857143%20182.857143%200%200%201-182.857143%20182.857143H182.857143a182.857143%20182.857143%200%200%201-182.857143-182.857143V182.857143z'%20fill='%23AFFB4B'%20p-id='11122'%3e%3c/path%3e%3cpath%20d='M188.964571%20213.76h215.369143v198.838857h-215.405714V213.76z%20m0%20397.641143h215.369143v198.802286h-215.405714v-198.802286z%20m215.369143-198.802286h215.369143v198.802286h-215.405714v-198.802286z%20m215.369143-198.838857h215.405714v198.838857h-215.405714V213.76z%20m0%20397.641143h215.405714v198.802286h-215.405714v-198.802286z'%20fill='%23000000'%20p-id='11123'%3e%3c/path%3e%3c/svg%3e"
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
id: "phantom",
|
|
36
|
+
name: "Phantom",
|
|
37
|
+
icon: "data:image/svg+xml,%3c?xml%20version='1.0'%20standalone='no'?%3e%3c!DOCTYPE%20svg%20PUBLIC%20'-//W3C//DTD%20SVG%201.1//EN'%20'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'%3e%3csvg%20t='1783439849461'%20class='icon'%20viewBox='0%200%201231%201024'%20version='1.1'%20xmlns='http://www.w3.org/2000/svg'%20p-id='16027'%20xmlns:xlink='http://www.w3.org/1999/xlink'%20width='38.46875'%20height='32'%3e%3cpath%20d='M145.509777%201024c156.921249%200%20274.849493-136.470329%20345.227164-244.310199-8.559643%2023.859408-13.316154%2047.716738-13.316154%2070.62069%200%2062.987424%2036.141176%20107.83987%20107.468073%20107.83987%2097.957128%200%20202.571294-85.889298%20256.780982-178.46056-3.805209%2013.36185-5.705736%2025.768243-5.705737%2037.219181%200%2043.899067%2024.72555%2071.576146%2075.132106%2071.576146%20158.823854%200%20318.596933-281.529379%20318.596933-527.746337C1229.693144%20168.916381%201132.687318%200%20889.222491%200%20461.25488%200%200%20522.975286%200%20860.808049%200%20993.460706%2071.326897%201024%20145.509777%201024zM741.808032%20339.743675c0-47.716738%2026.630231-81.118247%2065.621161-81.118247%2038.041704%200%2064.671935%2033.401509%2064.671935%2081.118247%200%2047.716738-26.630231%2082.071627-64.671935%2082.071627-38.990929%200-65.62116-34.354888-65.621161-82.071627z%20m203.522597%200c0-47.716738%2026.630231-81.118247%2065.62116-81.118247%2038.041704%200%2064.671935%2033.401509%2064.671935%2081.118247%200%2047.716738-26.630231%2082.071627-64.671935%2082.071627-38.990929%200-65.62116-34.354888-65.62116-82.071627z'%20fill='%23AB9FF2'%20p-id='16028'%3e%3c/path%3e%3c/svg%3e"
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
id: "coinbase",
|
|
41
|
+
name: "Coinbase",
|
|
42
|
+
icon: "data:image/svg+xml,%3c?xml%20version='1.0'%20standalone='no'?%3e%3c!DOCTYPE%20svg%20PUBLIC%20'-//W3C//DTD%20SVG%201.1//EN'%20'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'%3e%3csvg%20t='1783439807758'%20class='icon'%20viewBox='0%200%201024%201024'%20version='1.1'%20xmlns='http://www.w3.org/2000/svg'%20p-id='13209'%20xmlns:xlink='http://www.w3.org/1999/xlink'%20width='32'%20height='32'%3e%3cpath%20d='M1024%20186.181818a186.181818%20186.181818%200%200%200-186.181818-186.181818H186.181818a186.181818%20186.181818%200%200%200-186.181818%20186.181818v651.636364a186.181818%20186.181818%200%200%200%20186.181818%20186.181818h651.636364a186.181818%20186.181818%200%200%200%20186.181818-186.181818V186.181818z'%20fill='%232C5FF6'%20p-id='13210'%3e%3c/path%3e%3cpath%20d='M512%20870.4a358.4%20358.4%200%201%200%200-716.8%20358.4%20358.4%200%200%200%200%20716.8zM422.4%20394.984727h179.2a27.461818%2027.461818%200%200%201%2027.461818%2027.415273v179.2a27.461818%2027.461818%200%200%201-27.461818%2027.461818h-179.2a27.461818%2027.461818%200%200%201-27.461818-27.461818v-179.2a27.461818%2027.461818%200%200%201%2027.461818-27.461818z'%20fill='%23FFFFFF'%20p-id='13211'%3e%3c/path%3e%3c/svg%3e"
|
|
43
|
+
}
|
|
44
|
+
], f = {
|
|
45
|
+
"io.metamask": "metamask",
|
|
46
|
+
"com.okx.wallet": "okx",
|
|
47
|
+
"app.phantom": "phantom",
|
|
48
|
+
"com.coinbase.wallet": "coinbase"
|
|
49
|
+
}, p = [
|
|
50
|
+
{
|
|
51
|
+
id: 1,
|
|
52
|
+
name: "Ethereum",
|
|
53
|
+
symbol: "ETH",
|
|
54
|
+
decimals: 18
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
id: 11155111,
|
|
58
|
+
name: "Sepolia",
|
|
59
|
+
symbol: "SepoliaETH",
|
|
60
|
+
decimals: 18
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
id: 137,
|
|
64
|
+
name: "Polygon",
|
|
65
|
+
symbol: "MATIC",
|
|
66
|
+
decimals: 18
|
|
67
|
+
}
|
|
68
|
+
];
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region src/MiniWalletProvider.tsx
|
|
71
|
+
function m({ children: e, chains: n = [] }) {
|
|
72
|
+
let [s, l] = a(""), [m, h] = a(""), [g, _] = a(0), [v, y] = a(""), [b, x] = a({}), S = i(() => n.length > 0 ? n : p, [n]), C = i(() => b[m] ?? null, [b, m]), w = i(() => d.find((e) => e.id === m), [m]), T = i(() => !!s, [s]), E = i(() => S.find((e) => e.id === g) || null, [S, g]), D = i(() => !E || !v || !E?.decimals ? "0" : Number(u(v, E.decimals)).toFixed(3), [v, E]), O = t(async ({ id: e }) => {
|
|
73
|
+
let t = b[e];
|
|
74
|
+
if (!t) {
|
|
75
|
+
alert("请安装钱包");
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
console.log("provider: ", t);
|
|
80
|
+
let n = await t.request({ method: "eth_requestAccounts" });
|
|
81
|
+
console.log("accounts: ", n), console.log("provider.chainId: ", t.chainId), console.log("parseInt chainId: ", parseInt(t.chainId, 16));
|
|
82
|
+
let r = parseInt(t.chainId, 16);
|
|
83
|
+
l(n[0]), h(e), _(r), localStorage.setItem("miniwallet:lastWalletId", e);
|
|
84
|
+
} catch (e) {
|
|
85
|
+
console.error("连接失败:", e), alert("连接失败");
|
|
86
|
+
}
|
|
87
|
+
}, [b]), k = t(async () => {
|
|
88
|
+
try {
|
|
89
|
+
if (!C) {
|
|
90
|
+
alert("请安装钱包");
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
let e = await C.request({
|
|
94
|
+
method: "wallet_revokePermissions",
|
|
95
|
+
params: [{ eth_accounts: {} }]
|
|
96
|
+
});
|
|
97
|
+
console.log("res: ", e);
|
|
98
|
+
} catch (e) {
|
|
99
|
+
console.error(e);
|
|
100
|
+
} finally {
|
|
101
|
+
l(""), h(""), _(0), localStorage.removeItem("miniwallet:lastWalletId");
|
|
102
|
+
}
|
|
103
|
+
}, [C]), A = t(async (e) => {
|
|
104
|
+
if (!C) {
|
|
105
|
+
alert("钱包不存在");
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
try {
|
|
109
|
+
await C.request({
|
|
110
|
+
method: "wallet_switchEthereumChain",
|
|
111
|
+
params: [{ chainId: `0x${e.id.toString(16)}` }]
|
|
112
|
+
}), _(e.id);
|
|
113
|
+
} catch (e) {
|
|
114
|
+
console.error("切换失败:", e), alert("切换失败");
|
|
115
|
+
}
|
|
116
|
+
}, [C]), j = t(async () => {
|
|
117
|
+
try {
|
|
118
|
+
if (!s || !C) return;
|
|
119
|
+
let e = await C.request({
|
|
120
|
+
method: "eth_getBalance",
|
|
121
|
+
params: [s, "latest"]
|
|
122
|
+
});
|
|
123
|
+
console.log("balanceRes: ", Number(BigInt(e))), y(BigInt(e).toString());
|
|
124
|
+
} catch (e) {
|
|
125
|
+
console.error(e);
|
|
126
|
+
}
|
|
127
|
+
}, [s, C]), M = i(() => ({
|
|
128
|
+
account: s,
|
|
129
|
+
balance: D,
|
|
130
|
+
isConnected: T,
|
|
131
|
+
selectedChain: E,
|
|
132
|
+
wallet: w,
|
|
133
|
+
chains: S,
|
|
134
|
+
connect: O,
|
|
135
|
+
switchChain: A,
|
|
136
|
+
disconnect: k
|
|
137
|
+
}), [
|
|
138
|
+
s,
|
|
139
|
+
D,
|
|
140
|
+
T,
|
|
141
|
+
E,
|
|
142
|
+
w,
|
|
143
|
+
S,
|
|
144
|
+
O,
|
|
145
|
+
A,
|
|
146
|
+
k
|
|
147
|
+
]);
|
|
148
|
+
return r(() => {
|
|
149
|
+
let e = ((e) => {
|
|
150
|
+
let { info: t, provider: n } = e.detail ?? {}, r = f[t?.rdns];
|
|
151
|
+
!r || !n?.request || x((e) => e[r] === n ? e : {
|
|
152
|
+
...e,
|
|
153
|
+
[r]: n
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
return window.addEventListener("eip6963:announceProvider", e), window.dispatchEvent(new Event("eip6963:requestProvider")), () => {
|
|
157
|
+
window.removeEventListener("eip6963:announceProvider", e), x({});
|
|
158
|
+
};
|
|
159
|
+
}, []), r(() => {
|
|
160
|
+
g && j();
|
|
161
|
+
}, [j, g]), r(() => {
|
|
162
|
+
if (!T || !C?.on) return;
|
|
163
|
+
let e = (e) => {
|
|
164
|
+
console.log("ChainChanged chainId: ", e), _(parseInt(e, 16));
|
|
165
|
+
}, t = (e) => {
|
|
166
|
+
console.log("AccountsChanged accounts: ", e), l(e[0]);
|
|
167
|
+
};
|
|
168
|
+
return console.log("监听"), C.on("chainChanged", e), C.on("accountsChanged", t), () => {
|
|
169
|
+
console.log("取消监听"), C.removeListener("chainChanged", e), C.removeListener("accountsChanged", t);
|
|
170
|
+
};
|
|
171
|
+
}, [T, C]), r(() => {
|
|
172
|
+
if (!s) return;
|
|
173
|
+
let e = setInterval(j, 1e4);
|
|
174
|
+
return () => clearInterval(e);
|
|
175
|
+
}, [s, j]), r(() => {
|
|
176
|
+
let e = localStorage.getItem("miniwallet:lastWalletId");
|
|
177
|
+
if (!e) return;
|
|
178
|
+
let t = b[e];
|
|
179
|
+
if (!t) return;
|
|
180
|
+
let n = !1;
|
|
181
|
+
return (async () => {
|
|
182
|
+
let r = await t.request({ method: "eth_accounts" });
|
|
183
|
+
if (!n) {
|
|
184
|
+
if (!r?.length) {
|
|
185
|
+
localStorage.removeItem("miniwallet:lastWalletId");
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
l(r[0]), h(e), _(parseInt(t.chainId, 16));
|
|
189
|
+
}
|
|
190
|
+
})(), () => {
|
|
191
|
+
n = !0;
|
|
192
|
+
};
|
|
193
|
+
}, [b]), /* @__PURE__ */ o(c.Provider, {
|
|
194
|
+
value: M,
|
|
195
|
+
children: e
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
//#endregion
|
|
199
|
+
//#region src/useMiniWallet.ts
|
|
200
|
+
function h() {
|
|
201
|
+
let e = n(c);
|
|
202
|
+
if (!e) throw Error("useMiniWallet 必须在 MiniWalletProvider 内使用");
|
|
203
|
+
return e;
|
|
204
|
+
}
|
|
205
|
+
//#endregion
|
|
206
|
+
//#region src/icons/disconnect.svg
|
|
207
|
+
var g = "data:image/svg+xml,%3c?xml%20version='1.0'%20standalone='no'?%3e%3c!DOCTYPE%20svg%20PUBLIC%20'-//W3C//DTD%20SVG%201.1//EN'%20'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'%3e%3csvg%20t='1783575961451'%20class='icon'%20viewBox='0%200%201024%201024'%20version='1.1'%20xmlns='http://www.w3.org/2000/svg'%20p-id='5071'%20xmlns:xlink='http://www.w3.org/1999/xlink'%20width='32'%20height='32'%3e%3cpath%20d='M512.922%2063.583c248.335%200%20449.712%20201.384%20449.712%20449.71%200%20248.333-201.377%20449.702-449.712%20449.702-248.333%200-449.71-201.369-449.71-449.702%200-248.326%20201.377-449.71%20449.71-449.71z%20m148.683%20213.634l-35.351%2061.247a206.8%20206.8%200%200%201%2034.37%2027.739c37.359%2037.351%2060.475%2088.96%2060.475%20145.955%200%2057.004-23.117%20108.607-60.475%20145.965-37.344%2037.352-88.945%2060.469-145.949%2060.469-56.987%200-108.606-23.117-145.965-60.469-37.359-37.359-60.461-88.962-60.461-145.965%200-56.995%2023.117-108.605%2060.461-145.955a209.04%20209.04%200%200%201%2027.762-23.286l-35.43-61.359a278.69%20278.69%200%200%200-42.279%2034.69c-50.156%2050.148-81.18%20119.417-81.18%20195.91%200%2076.504%2031.041%20145.773%2081.18%20195.929%2050.139%2050.139%20119.408%2081.166%20195.912%2081.166%2076.502%200%20145.771-31.026%20195.91-81.166%2050.156-50.156%2081.182-119.425%2081.182-195.929%200-76.494-31.026-145.763-81.182-195.91a277.704%20277.704%200%200%200-48.98-39.031zM473.618%20128.865v337.849h75.458V128.865h-75.458z'%20fill='%23e6523f'%20p-id='5072'%3e%3c/path%3e%3c/svg%3e";
|
|
208
|
+
//#endregion
|
|
209
|
+
//#region src/MiniWalletButton.tsx
|
|
210
|
+
function _({ label: e = "Connect Wallet" }) {
|
|
211
|
+
let { account: n, balance: r, isConnected: i, selectedChain: c, wallet: l, switchChain: u, chains: f, connect: p, disconnect: m } = h(), [_, v] = a(!1), y = t(() => {
|
|
212
|
+
v(!0);
|
|
213
|
+
}, []), b = t(() => {
|
|
214
|
+
v(!1);
|
|
215
|
+
}, []);
|
|
216
|
+
return i ? /* @__PURE__ */ s("section", {
|
|
217
|
+
className: "inline-flex items-center justify-center text-center",
|
|
218
|
+
children: [/* @__PURE__ */ o("select", {
|
|
219
|
+
value: c?.id,
|
|
220
|
+
onChange: (e) => u(f.find((t) => t.id === Number(e.target.value))),
|
|
221
|
+
className: "mr-4 text-gray-600",
|
|
222
|
+
children: f.map((e) => /* @__PURE__ */ o("option", {
|
|
223
|
+
value: e.id,
|
|
224
|
+
children: e.name
|
|
225
|
+
}, e.id))
|
|
226
|
+
}), /* @__PURE__ */ s("div", {
|
|
227
|
+
className: "flex items-center justify-center space-x-2 rounded-md bg-white px-2 py-2.5 shadow-lg shadow-gray-200",
|
|
228
|
+
children: [
|
|
229
|
+
l && /* @__PURE__ */ o("img", {
|
|
230
|
+
src: l.icon,
|
|
231
|
+
alt: l.name,
|
|
232
|
+
className: "h-6 w-6"
|
|
233
|
+
}),
|
|
234
|
+
/* @__PURE__ */ s("div", {
|
|
235
|
+
className: "mr-1",
|
|
236
|
+
children: [
|
|
237
|
+
n.slice(0, 6),
|
|
238
|
+
"...",
|
|
239
|
+
n.slice(-4)
|
|
240
|
+
]
|
|
241
|
+
}),
|
|
242
|
+
/* @__PURE__ */ s("span", {
|
|
243
|
+
className: "text-sm text-gray-400",
|
|
244
|
+
children: [
|
|
245
|
+
"(",
|
|
246
|
+
r,
|
|
247
|
+
" ",
|
|
248
|
+
c?.symbol ?? "",
|
|
249
|
+
")"
|
|
250
|
+
]
|
|
251
|
+
}),
|
|
252
|
+
/* @__PURE__ */ o("button", {
|
|
253
|
+
className: "h-6 w-6 cursor-pointer rounded-full",
|
|
254
|
+
onClick: m,
|
|
255
|
+
children: /* @__PURE__ */ o("img", {
|
|
256
|
+
src: g,
|
|
257
|
+
alt: "disconnect",
|
|
258
|
+
className: "h-[22px] w-6"
|
|
259
|
+
})
|
|
260
|
+
})
|
|
261
|
+
]
|
|
262
|
+
})]
|
|
263
|
+
}) : /* @__PURE__ */ s("div", { children: [/* @__PURE__ */ o("button", {
|
|
264
|
+
type: "button",
|
|
265
|
+
onClick: y,
|
|
266
|
+
className: "cursor-pointer rounded-xl bg-blue-500 px-4 py-2 text-white shadow-lg shadow-blue-500/50",
|
|
267
|
+
children: e
|
|
268
|
+
}), _ && /* @__PURE__ */ o("div", {
|
|
269
|
+
className: "z-index-10 fixed top-0 right-0 bottom-0 left-0 bg-[rgba(0,0,0,0.5)]",
|
|
270
|
+
children: /* @__PURE__ */ s("div", {
|
|
271
|
+
className: "absolute top-1/2 left-1/2 flex h-auto w-[460px] -translate-x-1/2 -translate-y-1/2 flex-col gap-2 rounded-md bg-white p-6",
|
|
272
|
+
children: [/* @__PURE__ */ s("div", {
|
|
273
|
+
className: "mb-2 flex items-center justify-between",
|
|
274
|
+
children: [/* @__PURE__ */ o("span", {
|
|
275
|
+
className: "text-2xl font-bold",
|
|
276
|
+
children: "Connect Wallet"
|
|
277
|
+
}), /* @__PURE__ */ o("button", {
|
|
278
|
+
type: "button",
|
|
279
|
+
"aria-label": "Close",
|
|
280
|
+
className: "cursor-pointer rounded-full p-1 hover:bg-gray-100",
|
|
281
|
+
onClick: b,
|
|
282
|
+
children: /* @__PURE__ */ s("svg", {
|
|
283
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
284
|
+
viewBox: "0 0 24 24",
|
|
285
|
+
fill: "none",
|
|
286
|
+
stroke: "currentColor",
|
|
287
|
+
strokeWidth: "2",
|
|
288
|
+
className: "h-5 w-5",
|
|
289
|
+
children: [/* @__PURE__ */ o("path", { d: "M18 6 6 18" }), /* @__PURE__ */ o("path", { d: "m6 6 12 12" })]
|
|
290
|
+
})
|
|
291
|
+
})]
|
|
292
|
+
}), d.map((e) => /* @__PURE__ */ s("button", {
|
|
293
|
+
type: "button",
|
|
294
|
+
onClick: () => {
|
|
295
|
+
v(!1), p(e);
|
|
296
|
+
},
|
|
297
|
+
className: "flex cursor-pointer items-center gap-2 rounded-md border border-gray-300 px-4 py-2",
|
|
298
|
+
children: [/* @__PURE__ */ o("img", {
|
|
299
|
+
src: e.icon,
|
|
300
|
+
alt: e.name,
|
|
301
|
+
className: "h-8 w-8"
|
|
302
|
+
}), /* @__PURE__ */ s("div", {
|
|
303
|
+
className: "flex flex-col justify-between text-left",
|
|
304
|
+
children: [/* @__PURE__ */ o("p", { children: e.name }), /* @__PURE__ */ s("span", {
|
|
305
|
+
className: "text-xs text-gray-500",
|
|
306
|
+
children: [
|
|
307
|
+
"Connect your ",
|
|
308
|
+
e.name,
|
|
309
|
+
" wallet"
|
|
310
|
+
]
|
|
311
|
+
})]
|
|
312
|
+
})]
|
|
313
|
+
}, e.id))]
|
|
314
|
+
})
|
|
315
|
+
})] });
|
|
316
|
+
}
|
|
317
|
+
//#endregion
|
|
318
|
+
export { _ as MiniWalletButton, m as MiniWalletProvider, h as useMiniWallet };
|
package/package.json
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "miniwallet",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "React wallet connect button with multi-wallet support via EIP-6963",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/miniwallet.js",
|
|
7
|
+
"module": "./dist/miniwallet.js",
|
|
8
|
+
"types": "./dist/miniwallet.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/miniwallet.d.ts",
|
|
12
|
+
"import": "./dist/miniwallet.js"
|
|
13
|
+
},
|
|
14
|
+
"./style.css": "./dist/miniwallet.css"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"sideEffects": [
|
|
20
|
+
"**/*.css"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"dev": "vite",
|
|
24
|
+
"build": "tsc -b && vite build",
|
|
25
|
+
"lint": "eslint .",
|
|
26
|
+
"preview": "vite preview",
|
|
27
|
+
"prepublishOnly": "pnpm build"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"react": ">=18",
|
|
31
|
+
"react-dom": ">=18"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@eslint/js": "^10.0.1",
|
|
35
|
+
"@microsoft/api-extractor": "^7.58.9",
|
|
36
|
+
"@tailwindcss/vite": "^4.3.2",
|
|
37
|
+
"@types/node": "^24.13.2",
|
|
38
|
+
"@types/react": "^19.2.17",
|
|
39
|
+
"@types/react-dom": "^19.2.3",
|
|
40
|
+
"@vitejs/plugin-react": "^6.0.3",
|
|
41
|
+
"eslint": "^10.6.0",
|
|
42
|
+
"eslint-plugin-react-hooks": "^7.1.1",
|
|
43
|
+
"eslint-plugin-react-refresh": "^0.5.3",
|
|
44
|
+
"globals": "^17.7.0",
|
|
45
|
+
"prettier": "^3.9.4",
|
|
46
|
+
"prettier-plugin-tailwindcss": "^0.8.0",
|
|
47
|
+
"react": "^19.2.7",
|
|
48
|
+
"react-dom": "^19.2.7",
|
|
49
|
+
"tailwindcss": "^4.3.2",
|
|
50
|
+
"typescript": "~6.0.2",
|
|
51
|
+
"typescript-eslint": "^8.62.0",
|
|
52
|
+
"vite": "^8.1.1",
|
|
53
|
+
"vite-plugin-dts": "^5.0.3"
|
|
54
|
+
},
|
|
55
|
+
"keywords": [
|
|
56
|
+
"react",
|
|
57
|
+
"wallet",
|
|
58
|
+
"web3",
|
|
59
|
+
"eip-6963",
|
|
60
|
+
"metamask",
|
|
61
|
+
"ethereum"
|
|
62
|
+
],
|
|
63
|
+
"author": "RowanDuan",
|
|
64
|
+
"license": "MIT",
|
|
65
|
+
"repository": {
|
|
66
|
+
"type": "git",
|
|
67
|
+
"url": "git+https://github.com/RowanDuan/MiniWallet.git"
|
|
68
|
+
},
|
|
69
|
+
"bugs": {
|
|
70
|
+
"url": "https://github.com/RowanDuan/MiniWallet/issues"
|
|
71
|
+
},
|
|
72
|
+
"homepage": "https://github.com/RowanDuan/MiniWallet#readme"
|
|
73
|
+
}
|