@binance/w3w-multichain-connector 1.0.17 → 1.0.18

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @binance/w3w-multichain-connector
2
2
 
3
- The `@binance/w3w-multichain-connector` package provides a wrapper for the `Binance Wallet` extension multichain connector
3
+ The `@binance/w3w-multichain-connector` package provides a wrapper for the `Binance Wallet` extension multichain connector.
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,27 +18,228 @@ npm install @binance/w3w-multichain-connector
18
18
  import { getMultichainWallet } from '@binance/w3w-multichain-connector'
19
19
  ```
20
20
 
21
- ### Call methods
21
+ ### Get Wallet Instance
22
22
 
23
23
  ```ts
24
24
  const wallet = getMultichainWallet()
25
+ if (!wallet) {
26
+ console.log('Binance Wallet extension not installed')
27
+ }
28
+ ```
29
+
30
+ ### Check Extension Installed
31
+
32
+ ```ts
33
+ import { checkExtensionInstalled } from '@binance/w3w-multichain-connector'
34
+
35
+ const isInstalled = checkExtensionInstalled()
36
+ ```
37
+
38
+ ### Check Extension Feature Support
39
+
40
+ ```ts
41
+ import { checkExtensionSupported, Features } from '@binance/w3w-multichain-connector'
42
+
43
+ const isSupported = checkExtensionSupported(Features.requestConnect)
44
+ ```
45
+
46
+ ### Connect Wallet
47
+
48
+ ```ts
25
49
  const connectedWallets = await wallet.requestConnect(['56', 'CT_501'])
26
50
  ```
27
51
 
28
- ### Handle events
52
+ ### Connect with SAS
53
+
54
+ ```ts
55
+ // Single wallet SAS
56
+ const { wallet: connectedWallet, aggreeEnableSAS } = await wallet.requestConnectWithSAS({
57
+ binanceChainIds: ['56'],
58
+ userId: 'user-id',
59
+ enabledWalletInfo: [
60
+ { walletType: 'SEED_PHRASE', chain: 'evm', userPublicKeyHex: '0x...' }
61
+ ]
62
+ })
63
+
64
+ // Multi wallet SAS
65
+ const { walletList, enabledWalletIds } = await wallet.requestConnectWithMultiWalletSAS({
66
+ binanceChainIds: ['56'],
67
+ userId: 'user-id'
68
+ })
69
+ ```
70
+
71
+ ### Check Connected Wallets
72
+
73
+ ```ts
74
+ const wallets = await wallet.checkConnectedWallet()
75
+ // The user's active wallet will be the first element in the array
76
+ ```
77
+
78
+ ### Get Wallet Info
79
+
80
+ ```ts
81
+ const walletInfo = await wallet.getWalletInfo('0x...address', '56')
82
+ ```
83
+
84
+ ### Switch Chain
85
+
86
+ ```ts
87
+ await wallet.switchChain('56')
88
+ ```
89
+
90
+ ### Disconnect
91
+
92
+ ```ts
93
+ await wallet.disconnect()
94
+ ```
95
+
96
+ ### Sign Order
97
+
98
+ ```ts
99
+ const signature = await wallet.signOrder({
100
+ walletId: 'wallet-id',
101
+ walletAddress: '0x...',
102
+ binanceChainId: '56',
103
+ txData: '...',
104
+ feeAmount: '0.001',
105
+ feeAmountLevel: 'Medium', // 'Low' | 'Medium' | 'High' | 'Custom'
106
+ digest: '...',
107
+ digestSign: '...'
108
+ })
109
+ ```
110
+
111
+ ### Auto Sign
112
+
113
+ ```ts
114
+ await wallet.requestAutoSign()
115
+ console.log('Auto sign enabled:', wallet.autoSignEnabled)
116
+ ```
117
+
118
+ ### SAS Operations
119
+
120
+ ```ts
121
+ // Enable SAS
122
+ const { aggreeEnableSAS } = await wallet.requestEnableSAS({
123
+ walletId: 'wallet-id',
124
+ userId: 'user-id',
125
+ userEmail: 'user@example.com'
126
+ })
127
+
128
+ // Enable multi-wallet SAS
129
+ const { enabledWalletIds } = await wallet.requestEnableMultiWalletSAS({
130
+ walletIds: ['wallet-1', 'wallet-2'],
131
+ userId: 'user-id',
132
+ userEmail: 'user@example.com'
133
+ })
134
+
135
+ // Get SAS Keyshare
136
+ const keyshares = await wallet.getSASKeyshare({
137
+ walletIds: ['wallet-id'],
138
+ userId: 'user-id',
139
+ spsVersion: '1.0',
140
+ isSilent: false // if true, won't prompt unlock when wallet is locked
141
+ })
142
+
143
+ // Sync SAS Status
144
+ await wallet.syncSASStatus({
145
+ userId: 'user-id',
146
+ statusList: [
147
+ { walletId: 'wallet-id', enabled: true }
148
+ ]
149
+ })
150
+
151
+ // Get ProofKey Signature (before placing order)
152
+ const { timeStamp, proofKeyPublicKey, timestampSignature } = await wallet.getSASProofKeySignature({
153
+ walletId: 'wallet-id',
154
+ userId: 'user-id'
155
+ })
156
+
157
+ // Get Connected Wallets with ProofKey
158
+ const walletsWithProofKey = await wallet.getConnectedWalletAndProofKey({
159
+ userId: 'user-id'
160
+ })
161
+ ```
162
+
163
+ ### Handle Events
29
164
 
30
165
  ```ts
31
166
  const wallet = getMultichainWallet()
167
+
168
+ // Wallet lock status changed
32
169
  wallet.on('walletLockedChanged', (isLocked) => {
33
- console.log('Wallet is locked', isLocked)
170
+ console.log('Wallet locked:', isLocked)
171
+ })
172
+
173
+ // Auto sign status changed
174
+ wallet.on('autoSignEnabledChanged', (isEnabled) => {
175
+ console.log('Auto sign enabled:', isEnabled)
176
+ })
177
+
178
+ // Connected wallets changed
179
+ wallet.on('connectedWalletsChanged', (connectedWallets) => {
180
+ console.log('Connected wallets:', connectedWallets)
181
+ })
182
+
183
+ // Connected chain changed
184
+ wallet.on('connectedChainIdChanged', (binanceChainId) => {
185
+ console.log('Chain changed:', binanceChainId)
34
186
  })
35
187
  ```
36
188
 
37
- ### Check extension support features
189
+ ### Properties
38
190
 
39
- ```ts
40
- import { checkExtensionSupported, Features } from '@binance/w3w-multichain-connector'
191
+ | Property | Type | Description |
192
+ |---|---|---|
193
+ | `version` | `string` | Current extension version |
194
+ | `autoSignEnabled` | `boolean` | Whether auto sign is enabled |
195
+ | `walletLocked` | `boolean` | Whether the wallet is locked |
196
+ | `extensionId` | `string` | Extension unique identifier |
41
197
 
42
- const isSupported = checkExtensionSupported(Features.requestConnect)
198
+ ## Types
199
+
200
+ ### `IConnectedWallet`
201
+
202
+ | Field | Type | Description |
203
+ |---|---|---|
204
+ | `walletIcon` | `string` | Wallet icon (base64) |
205
+ | `walletName` | `string` | Wallet name |
206
+ | `walletId` | `string` | Wallet unique identifier |
207
+ | `walletType` | `WalletType` | Wallet type (privateKey / seedPhrase) |
208
+ | `addressList` | `Array<{ address, binanceChainId, publicKeyHex }>` | Wallet address list |
209
+ | `groupID?` | `string` | Wallet group ID |
210
+ | `groupName?` | `string` | Wallet group name |
211
+
212
+ ### `WalletType`
213
+
214
+ | Value | Description |
215
+ |---|---|
216
+ | `privateKey` | Private key wallet |
217
+ | `seedPhrase` (`hd`) | Seed phrase (mnemonic) wallet |
218
+
219
+ ### `Features`
43
220
 
44
- ```
221
+ | Value | Description |
222
+ |---|---|
223
+ | **Connect** | |
224
+ | `requestConnect` | Connect wallet |
225
+ | `requestConnectWithSAS` | Connect wallet with single-wallet SAS |
226
+ | `requestConnectWithMultiWalletSAS` | Connect wallet with multi-wallet SAS |
227
+ | `checkConnectedWallet` | Check connected wallets |
228
+ | `getWalletInfo` | Get wallet info by address and chain |
229
+ | `disconnect` | Disconnect all wallets |
230
+ | `switchChain` | Switch chain |
231
+ | **SAS** | |
232
+ | `requestEnableSAS` | Request enable SAS (single wallet) |
233
+ | `requestEnableMultiWalletSAS` | Request enable SAS (multi wallet) |
234
+ | `getSASKeyshare` | Get SAS keyshare |
235
+ | `syncSASStatus` | Sync SAS status |
236
+ | `getConnectedWalletAndProofKey` | Get connected wallets with ProofKey |
237
+ | `getSASProofKeySignature` | Get ProofKey signature before placing order |
238
+ | **Signing** | |
239
+ | `requestAutoSign` | Request auto sign |
240
+ | `signOrder` | Sign transaction order |
241
+ | **Events** | |
242
+ | `eventWalletLockedChanged` | Wallet lock status changed event |
243
+ | `eventAutoSignEnabledChanged` | Auto sign status changed event |
244
+ | `eventConnectedWalletsChanged` | Connected wallets changed event |
245
+ | `eventConnectedChainIdChanged` | Connected chain changed event |
package/dist/index.d.ts CHANGED
@@ -12,64 +12,151 @@ declare enum WalletType {
12
12
  privateKey = "privateKey",
13
13
  seedPhrase = "hd"
14
14
  }
15
+ /**
16
+ * 插件支持的功能特性枚举,用于 checkExtensionSupported 检测
17
+ */
15
18
  declare enum Features {
19
+ /** 请求连接钱包 */
16
20
  requestConnect = "requestConnect",
21
+ /** 连接钱包并发起单钱包SAS请求 */
22
+ requestConnectWithSAS = "requestConnectWithSAS",
23
+ /** 连接钱包并发起多钱包SAS请求 */
24
+ requestConnectWithMultiWalletSAS = "requestConnectWithMultiWalletSAS",
25
+ /** 检查当前已连接的钱包 */
17
26
  checkConnectedWallet = "checkConnectedWallet",
27
+ /** 根据地址和链ID获取钱包信息 */
28
+ getWalletInfo = "getWalletInfo",
29
+ /** 断开所有已连接的钱包 */
18
30
  disconnect = "disconnect",
31
+ /** 切换网络 */
19
32
  switchChain = "switchChain",
33
+ /** 弹出开启SAS的请求弹窗(单钱包) */
34
+ requestEnableSAS = "requestEnableSAS",
35
+ /** 弹出开启SAS的请求弹窗(多钱包) */
36
+ requestEnableMultiWalletSAS = "requestEnableMultiWalletSAS",
37
+ /** 获取SAS Keyshare */
38
+ getSASKeyshare = "getSASKeyshare",
39
+ /** 同步已连接钱包的SAS状态 */
40
+ syncSASStatus = "syncSASStatus",
41
+ /** 获取已连接钱包及其ProofKey */
42
+ getConnectedWalletAndProofKey = "getConnectedWalletAndProofKey",
43
+ /** 下单前获取ProofKey签名 */
44
+ getSASProofKeySignature = "getSASProofKeySignature",
45
+ /** 请求开启自动签名 */
20
46
  requestAutoSign = "requestAutoSign",
47
+ /** 签署交易订单 */
48
+ signOrder = "signOrder",
49
+ /** 钱包锁定状态变更事件 */
21
50
  eventWalletLockedChanged = "eventWalletLockedChanged",
51
+ /** 自动签名开关状态变更事件 */
22
52
  eventAutoSignEnabledChanged = "eventAutoSignEnabledChanged",
53
+ /** 已连接钱包列表变更事件 */
23
54
  eventConnectedWalletsChanged = "eventConnectedWalletsChanged",
55
+ /** 当前连接的链ID变更事件 */
24
56
  eventConnectedChainIdChanged = "eventConnectedChainIdChanged"
25
57
  }
26
58
 
27
59
  /**
28
- * 链接的钱包
60
+ * 已连接的钱包信息
29
61
  */
30
62
  interface IConnectedWallet {
31
63
  /**
32
64
  * base64编码的钱包图标
33
65
  */
34
66
  walletIcon: string;
67
+ /**
68
+ * 钱包名称
69
+ */
35
70
  walletName: string;
71
+ /**
72
+ * 钱包唯一标识
73
+ */
36
74
  walletId: string;
75
+ /**
76
+ * 钱包类型(私钥钱包 / 助记词钱包)
77
+ */
37
78
  walletType: WalletType;
79
+ /**
80
+ * 钱包关联的地址列表
81
+ */
38
82
  addressList: {
83
+ /** 链上地址 */
39
84
  address: string;
85
+ /** Binance 链ID */
40
86
  binanceChainId: string;
87
+ /** 公钥(hex 编码) */
41
88
  publicKeyHex: string;
42
89
  }[];
90
+ /**
91
+ * 钱包分组ID(可选)
92
+ */
43
93
  groupID?: string;
94
+ /**
95
+ * 钱包分组名称(可选)
96
+ */
44
97
  groupName?: string;
45
98
  }
99
+ /**
100
+ * SAS(Smart Account Signing)授权信息
101
+ */
46
102
  interface SASAuthorizeInfo {
103
+ /** 钱包类型:助记词 / 私钥 */
47
104
  walletType: 'SEED_PHRASE' | 'PRIVATE_KEY';
105
+ /** ProofKey 公钥 */
48
106
  proofKeyPublicKey: string;
107
+ /** 授权过期时间戳 */
49
108
  expireAt: number;
109
+ /** 钱包唯一标识 */
50
110
  walletId: string;
111
+ /** 插件唯一标识 */
51
112
  extensionId: string;
113
+ /** 授权链列表 */
52
114
  authorizeList: SASAuthorizeInfoItem[];
53
115
  }
116
+ /**
117
+ * SAS 授权中单条链的详细信息
118
+ */
54
119
  interface SASAuthorizeInfoItem {
120
+ /** 链类型:evm 或 sol */
55
121
  chain: 'evm' | 'sol';
122
+ /** Binance 链ID */
56
123
  binanceChainId: string;
124
+ /** 加密后的 KeyShare B */
57
125
  encryptedKeyShareB: string;
126
+ /** 加密后的 KeyShare A 数据 */
58
127
  encryptedKeyShareAData: string;
128
+ /** 公钥 */
59
129
  publicKey: string;
130
+ /** SPS 版本号 */
60
131
  spsVersion: string;
132
+ /** 用户公钥(hex 编码) */
61
133
  userPublicKeyHex: string;
62
134
  }
135
+ /**
136
+ * 钱包事件定义
137
+ */
63
138
  interface WalletEvents {
139
+ /** 钱包锁定状态变更 */
64
140
  walletLockedChanged: (isLocked: boolean) => void;
141
+ /** 自动签名开关状态变更 */
65
142
  autoSignEnabledChanged: (isEnabled: boolean) => void;
143
+ /** 已连接钱包列表变更 */
66
144
  connectedWalletsChanged: (connectedWallets: IConnectedWallet[]) => void;
145
+ /** 当前连接的链ID变更 */
67
146
  connectedChainIdChanged: (binanceChainId: string) => void;
68
147
  }
148
+ /**
149
+ * 多链钱包接口,继承 EventEmitter 以支持事件监听
150
+ */
69
151
  interface IWallet extends EventEmitter<WalletEvents> {
152
+ /**
153
+ * 请求连接钱包
154
+ * @param binanceChainIds 需要连接的 Binance 链ID列表
155
+ * @returns 已连接的钱包列表
156
+ */
70
157
  requestConnect(binanceChainIds: string[]): Promise<IConnectedWallet[]>;
71
158
  /**
72
- * 链接插件的时候同时发起开启SAS的请求
159
+ * 连接插件的同时发起开启SAS的请求
73
160
  * 如果用户同意,插件会记录下当前用户同意开启的SAS
74
161
  */
75
162
  requestConnectWithSAS(args: {
@@ -84,6 +171,17 @@ interface IWallet extends EventEmitter<WalletEvents> {
84
171
  wallet: IConnectedWallet;
85
172
  aggreeEnableSAS: boolean;
86
173
  }>;
174
+ /**
175
+ * 连接插件的同时发起多钱包SAS请求
176
+ * @returns walletList 已连接钱包列表,enabledWalletIds 同意开启SAS的钱包ID列表
177
+ */
178
+ requestConnectWithMultiWalletSAS(args: {
179
+ binanceChainIds: string[];
180
+ userId: string;
181
+ }): Promise<{
182
+ walletList: IConnectedWallet[];
183
+ enabledWalletIds: string[];
184
+ }>;
87
185
  /**
88
186
  * 弹出要求用户同意开启SAS的弹窗
89
187
  */
@@ -94,6 +192,17 @@ interface IWallet extends EventEmitter<WalletEvents> {
94
192
  }): Promise<{
95
193
  aggreeEnableSAS: boolean;
96
194
  }>;
195
+ /**
196
+ * 弹出多钱包SAS开启请求弹窗
197
+ * @returns enabledWalletIds 用户同意开启SAS的钱包ID列表
198
+ */
199
+ requestEnableMultiWalletSAS(arg: {
200
+ walletIds: string[];
201
+ userId: string;
202
+ userEmail: string;
203
+ }): Promise<{
204
+ enabledWalletIds: string[];
205
+ }>;
97
206
  /**
98
207
  * 用户同意开启SAS后,获取SAS的keyshare
99
208
  * 由于同一个钱包可能在app端被用户开启,所以插件不会校验用户有没有在插件端同意过
@@ -116,6 +225,9 @@ interface IWallet extends EventEmitter<WalletEvents> {
116
225
  enabled: boolean;
117
226
  }[];
118
227
  }): Promise<void>;
228
+ /**
229
+ * 获取已连接钱包及其 ProofKey 公钥
230
+ */
119
231
  getConnectedWalletAndProofKey(arg: {
120
232
  userId: string;
121
233
  }): Promise<{
@@ -138,6 +250,12 @@ interface IWallet extends EventEmitter<WalletEvents> {
138
250
  * 用户的当前活跃钱包会出现在数组的第一个
139
251
  */
140
252
  checkConnectedWallet(): Promise<IConnectedWallet[]>;
253
+ /**
254
+ * 根据地址和链ID获取钱包信息
255
+ * @param address 链上地址
256
+ * @param binanceChainId Binance 链ID
257
+ * @returns 匹配的钱包信息,未找到则返回 null
258
+ */
141
259
  getWalletInfo(address: string, binanceChainId: string): Promise<IConnectedWallet | null>;
142
260
  /**
143
261
  * 断开所有连接的钱包
@@ -153,14 +271,26 @@ interface IWallet extends EventEmitter<WalletEvents> {
153
271
  * 若已链接,则直接返回
154
272
  */
155
273
  requestAutoSign(): Promise<void>;
274
+ /**
275
+ * 签署交易订单
276
+ * @returns 签名结果字符串
277
+ */
156
278
  signOrder(arg: {
279
+ /** 钱包唯一标识 */
157
280
  walletId: string;
281
+ /** 钱包地址 */
158
282
  walletAddress: string;
283
+ /** Binance 链ID */
159
284
  binanceChainId: string;
285
+ /** 交易数据 */
160
286
  txData: string;
287
+ /** 手续费金额 */
161
288
  feeAmount: string;
289
+ /** 手续费等级(可选) */
162
290
  feeAmountLevel?: 'Low' | 'Medium' | 'High' | 'Custom';
291
+ /** 摘要 */
163
292
  digest: string;
293
+ /** 摘要签名 */
164
294
  digestSign: string;
165
295
  }): Promise<string>;
166
296
  /**
@@ -171,7 +301,13 @@ interface IWallet extends EventEmitter<WalletEvents> {
171
301
  * 是否启用自动签名
172
302
  */
173
303
  autoSignEnabled: boolean;
304
+ /**
305
+ * 钱包是否锁定
306
+ */
174
307
  walletLocked: boolean;
308
+ /**
309
+ * 插件唯一标识
310
+ */
175
311
  extensionId: string;
176
312
  }
177
313
 
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- var r=(n=>(n.EVM="EVM",n.SOL="SOL",n))(r||{}),l=(n=>(n.privateKey="privateKey",n.seedPhrase="hd",n))(l||{}),a=(e=>(e.requestConnect="requestConnect",e.checkConnectedWallet="checkConnectedWallet",e.disconnect="disconnect",e.switchChain="switchChain",e.requestAutoSign="requestAutoSign",e.eventWalletLockedChanged="eventWalletLockedChanged",e.eventAutoSignEnabledChanged="eventAutoSignEnabledChanged",e.eventConnectedWalletsChanged="eventConnectedWalletsChanged",e.eventConnectedChainIdChanged="eventConnectedChainIdChanged",e))(a||{});var d=()=>{try{return!!window?.binancew3w?.isExtension}catch{return!1}},s=t=>{let o=c();return o?o.checkSupportFeature(t):!1},c=()=>d()?window.binancew3w?.wallet:null,h=()=>c();export{r as ChainType,a as Features,l as WalletType,d as checkExtensionInstalled,s as checkExtensionSupported,h as getMultichainWallet};
1
+ var r=(t=>(t.EVM="EVM",t.SOL="SOL",t))(r||{}),c=(t=>(t.privateKey="privateKey",t.seedPhrase="hd",t))(c||{}),a=(e=>(e.requestConnect="requestConnect",e.requestConnectWithSAS="requestConnectWithSAS",e.requestConnectWithMultiWalletSAS="requestConnectWithMultiWalletSAS",e.checkConnectedWallet="checkConnectedWallet",e.getWalletInfo="getWalletInfo",e.disconnect="disconnect",e.switchChain="switchChain",e.requestEnableSAS="requestEnableSAS",e.requestEnableMultiWalletSAS="requestEnableMultiWalletSAS",e.getSASKeyshare="getSASKeyshare",e.syncSASStatus="syncSASStatus",e.getConnectedWalletAndProofKey="getConnectedWalletAndProofKey",e.getSASProofKeySignature="getSASProofKeySignature",e.requestAutoSign="requestAutoSign",e.signOrder="signOrder",e.eventWalletLockedChanged="eventWalletLockedChanged",e.eventAutoSignEnabledChanged="eventAutoSignEnabledChanged",e.eventConnectedWalletsChanged="eventConnectedWalletsChanged",e.eventConnectedChainIdChanged="eventConnectedChainIdChanged",e))(a||{});var i=()=>{try{return!!window?.binancew3w?.isExtension}catch{return!1}},d=n=>{let o=l();return o?o.checkSupportFeature(n):!1},l=()=>i()?window.binancew3w?.wallet:null,s=()=>l();export{r as ChainType,a as Features,c as WalletType,i as checkExtensionInstalled,d as checkExtensionSupported,s as getMultichainWallet};
2
2
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/enum.ts","../src/getMultichainWallet.ts"],"sourcesContent":["export enum ChainType {\n EVM = 'EVM',\n SOL = 'SOL',\n}\n\n/**\n * privateKey:私钥钱包\n * hd: 助记词钱包\n */\nexport enum WalletType {\n privateKey = 'privateKey',\n seedPhrase = 'hd',\n}\n\nexport enum Features {\n requestConnect = 'requestConnect',\n checkConnectedWallet = 'checkConnectedWallet',\n disconnect = 'disconnect',\n switchChain = 'switchChain',\n requestAutoSign = 'requestAutoSign',\n eventWalletLockedChanged = 'eventWalletLockedChanged',\n eventAutoSignEnabledChanged = 'eventAutoSignEnabledChanged',\n eventConnectedWalletsChanged = 'eventConnectedWalletsChanged',\n eventConnectedChainIdChanged = 'eventConnectedChainIdChanged',\n}\n","import { Features } from './enum'\nimport type { IWallet } from './type'\n\nexport const checkExtensionInstalled = () => {\n try {\n return !!(window as any)?.binancew3w?.isExtension\n } catch (error) {\n return false\n }\n}\n\n/**\n * 检查当前用户安装的插件是否支持\n */\nexport const checkExtensionSupported = (feature: Features) => {\n const wallet = getInjectedWallet()\n if (!wallet) {\n return false\n }\n return wallet.checkSupportFeature(feature)\n}\n\nconst getInjectedWallet = () => {\n const isInstalled = checkExtensionInstalled()\n if (!isInstalled) {\n return null\n }\n return (window as any).binancew3w?.wallet\n}\n\nexport const getMultichainWallet = (): IWallet | null => {\n return getInjectedWallet()\n}\n"],"mappings":"AAAO,IAAKA,OACVA,EAAA,IAAM,MACNA,EAAA,IAAM,MAFIA,OAAA,IASAC,OACVA,EAAA,WAAa,aACbA,EAAA,WAAa,KAFHA,OAAA,IAKAC,OACVA,EAAA,eAAiB,iBACjBA,EAAA,qBAAuB,uBACvBA,EAAA,WAAa,aACbA,EAAA,YAAc,cACdA,EAAA,gBAAkB,kBAClBA,EAAA,yBAA2B,2BAC3BA,EAAA,4BAA8B,8BAC9BA,EAAA,6BAA+B,+BAC/BA,EAAA,6BAA+B,+BATrBA,OAAA,ICXL,IAAMC,EAA0B,IAAM,CAC3C,GAAI,CACF,MAAO,CAAC,CAAE,QAAgB,YAAY,WACxC,MAAE,CACA,MAAO,EACT,CACF,EAKaC,EAA2BC,GAAsB,CAC5D,IAAMC,EAASC,EAAkB,EACjC,OAAKD,EAGEA,EAAO,oBAAoBD,CAAO,EAFhC,EAGX,EAEME,EAAoB,IACJJ,EAAwB,EAIpC,OAAe,YAAY,OAF1B,KAKEK,EAAsB,IAC1BD,EAAkB","names":["ChainType","WalletType","Features","checkExtensionInstalled","checkExtensionSupported","feature","wallet","getInjectedWallet","getMultichainWallet"]}
1
+ {"version":3,"sources":["../src/enum.ts","../src/getMultichainWallet.ts"],"sourcesContent":["export enum ChainType {\n EVM = 'EVM',\n SOL = 'SOL',\n}\n\n/**\n * privateKey:私钥钱包\n * hd: 助记词钱包\n */\nexport enum WalletType {\n privateKey = 'privateKey',\n seedPhrase = 'hd',\n}\n\n/**\n * 插件支持的功能特性枚举,用于 checkExtensionSupported 检测\n */\nexport enum Features {\n // ---- 连接相关 ----\n /** 请求连接钱包 */\n requestConnect = 'requestConnect',\n /** 连接钱包并发起单钱包SAS请求 */\n requestConnectWithSAS = 'requestConnectWithSAS',\n /** 连接钱包并发起多钱包SAS请求 */\n requestConnectWithMultiWalletSAS = 'requestConnectWithMultiWalletSAS',\n /** 检查当前已连接的钱包 */\n checkConnectedWallet = 'checkConnectedWallet',\n /** 根据地址和链ID获取钱包信息 */\n getWalletInfo = 'getWalletInfo',\n /** 断开所有已连接的钱包 */\n disconnect = 'disconnect',\n /** 切换网络 */\n switchChain = 'switchChain',\n\n // ---- SAS 相关 ----\n /** 弹出开启SAS的请求弹窗(单钱包) */\n requestEnableSAS = 'requestEnableSAS',\n /** 弹出开启SAS的请求弹窗(多钱包) */\n requestEnableMultiWalletSAS = 'requestEnableMultiWalletSAS',\n /** 获取SAS Keyshare */\n getSASKeyshare = 'getSASKeyshare',\n /** 同步已连接钱包的SAS状态 */\n syncSASStatus = 'syncSASStatus',\n /** 获取已连接钱包及其ProofKey */\n getConnectedWalletAndProofKey = 'getConnectedWalletAndProofKey',\n /** 下单前获取ProofKey签名 */\n getSASProofKeySignature = 'getSASProofKeySignature',\n\n // ---- 签名相关 ----\n /** 请求开启自动签名 */\n requestAutoSign = 'requestAutoSign',\n /** 签署交易订单 */\n signOrder = 'signOrder',\n\n // ---- 事件相关 ----\n /** 钱包锁定状态变更事件 */\n eventWalletLockedChanged = 'eventWalletLockedChanged',\n /** 自动签名开关状态变更事件 */\n eventAutoSignEnabledChanged = 'eventAutoSignEnabledChanged',\n /** 已连接钱包列表变更事件 */\n eventConnectedWalletsChanged = 'eventConnectedWalletsChanged',\n /** 当前连接的链ID变更事件 */\n eventConnectedChainIdChanged = 'eventConnectedChainIdChanged',\n}\n","import { Features } from './enum'\nimport type { IWallet } from './type'\n\nexport const checkExtensionInstalled = () => {\n try {\n return !!(window as any)?.binancew3w?.isExtension\n } catch (error) {\n return false\n }\n}\n\n/**\n * 检查当前用户安装的插件是否支持\n */\nexport const checkExtensionSupported = (feature: Features) => {\n const wallet = getInjectedWallet()\n if (!wallet) {\n return false\n }\n return wallet.checkSupportFeature(feature)\n}\n\nconst getInjectedWallet = () => {\n const isInstalled = checkExtensionInstalled()\n if (!isInstalled) {\n return null\n }\n return (window as any).binancew3w?.wallet\n}\n\nexport const getMultichainWallet = (): IWallet | null => {\n return getInjectedWallet()\n}\n"],"mappings":"AAAO,IAAKA,OACVA,EAAA,IAAM,MACNA,EAAA,IAAM,MAFIA,OAAA,IASAC,OACVA,EAAA,WAAa,aACbA,EAAA,WAAa,KAFHA,OAAA,IAQAC,OAGVA,EAAA,eAAiB,iBAEjBA,EAAA,sBAAwB,wBAExBA,EAAA,iCAAmC,mCAEnCA,EAAA,qBAAuB,uBAEvBA,EAAA,cAAgB,gBAEhBA,EAAA,WAAa,aAEbA,EAAA,YAAc,cAIdA,EAAA,iBAAmB,mBAEnBA,EAAA,4BAA8B,8BAE9BA,EAAA,eAAiB,iBAEjBA,EAAA,cAAgB,gBAEhBA,EAAA,8BAAgC,gCAEhCA,EAAA,wBAA0B,0BAI1BA,EAAA,gBAAkB,kBAElBA,EAAA,UAAY,YAIZA,EAAA,yBAA2B,2BAE3BA,EAAA,4BAA8B,8BAE9BA,EAAA,6BAA+B,+BAE/BA,EAAA,6BAA+B,+BA7CrBA,OAAA,ICdL,IAAMC,EAA0B,IAAM,CAC3C,GAAI,CACF,MAAO,CAAC,CAAE,QAAgB,YAAY,WACxC,MAAE,CACA,MAAO,EACT,CACF,EAKaC,EAA2BC,GAAsB,CAC5D,IAAMC,EAASC,EAAkB,EACjC,OAAKD,EAGEA,EAAO,oBAAoBD,CAAO,EAFhC,EAGX,EAEME,EAAoB,IACJJ,EAAwB,EAIpC,OAAe,YAAY,OAF1B,KAKEK,EAAsB,IAC1BD,EAAkB","names":["ChainType","WalletType","Features","checkExtensionInstalled","checkExtensionSupported","feature","wallet","getInjectedWallet","getMultichainWallet"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@binance/w3w-multichain-connector",
3
- "version": "1.0.17",
3
+ "version": "1.0.18",
4
4
  "description": "A multichain connector for Binance Wallet",
5
5
  "main": "dist/index.mjs",
6
6
  "types": "dist/index.d.ts",
package/src/enum.ts CHANGED
@@ -12,14 +12,53 @@ export enum WalletType {
12
12
  seedPhrase = 'hd',
13
13
  }
14
14
 
15
+ /**
16
+ * 插件支持的功能特性枚举,用于 checkExtensionSupported 检测
17
+ */
15
18
  export enum Features {
19
+ // ---- 连接相关 ----
20
+ /** 请求连接钱包 */
16
21
  requestConnect = 'requestConnect',
22
+ /** 连接钱包并发起单钱包SAS请求 */
23
+ requestConnectWithSAS = 'requestConnectWithSAS',
24
+ /** 连接钱包并发起多钱包SAS请求 */
25
+ requestConnectWithMultiWalletSAS = 'requestConnectWithMultiWalletSAS',
26
+ /** 检查当前已连接的钱包 */
17
27
  checkConnectedWallet = 'checkConnectedWallet',
28
+ /** 根据地址和链ID获取钱包信息 */
29
+ getWalletInfo = 'getWalletInfo',
30
+ /** 断开所有已连接的钱包 */
18
31
  disconnect = 'disconnect',
32
+ /** 切换网络 */
19
33
  switchChain = 'switchChain',
34
+
35
+ // ---- SAS 相关 ----
36
+ /** 弹出开启SAS的请求弹窗(单钱包) */
37
+ requestEnableSAS = 'requestEnableSAS',
38
+ /** 弹出开启SAS的请求弹窗(多钱包) */
39
+ requestEnableMultiWalletSAS = 'requestEnableMultiWalletSAS',
40
+ /** 获取SAS Keyshare */
41
+ getSASKeyshare = 'getSASKeyshare',
42
+ /** 同步已连接钱包的SAS状态 */
43
+ syncSASStatus = 'syncSASStatus',
44
+ /** 获取已连接钱包及其ProofKey */
45
+ getConnectedWalletAndProofKey = 'getConnectedWalletAndProofKey',
46
+ /** 下单前获取ProofKey签名 */
47
+ getSASProofKeySignature = 'getSASProofKeySignature',
48
+
49
+ // ---- 签名相关 ----
50
+ /** 请求开启自动签名 */
20
51
  requestAutoSign = 'requestAutoSign',
52
+ /** 签署交易订单 */
53
+ signOrder = 'signOrder',
54
+
55
+ // ---- 事件相关 ----
56
+ /** 钱包锁定状态变更事件 */
21
57
  eventWalletLockedChanged = 'eventWalletLockedChanged',
58
+ /** 自动签名开关状态变更事件 */
22
59
  eventAutoSignEnabledChanged = 'eventAutoSignEnabledChanged',
60
+ /** 已连接钱包列表变更事件 */
23
61
  eventConnectedWalletsChanged = 'eventConnectedWalletsChanged',
62
+ /** 当前连接的链ID变更事件 */
24
63
  eventConnectedChainIdChanged = 'eventConnectedChainIdChanged',
25
64
  }
package/src/type.ts CHANGED
@@ -3,7 +3,7 @@ import type EventEmitter from 'eventemitter3'
3
3
  import type { WalletType } from './enum'
4
4
 
5
5
  /**
6
- * 链接的钱包
6
+ * 已连接的钱包信息
7
7
  */
8
8
  export interface IConnectedWallet {
9
9
  /**
@@ -11,53 +11,108 @@ export interface IConnectedWallet {
11
11
  */
12
12
  walletIcon: string
13
13
 
14
+ /**
15
+ * 钱包名称
16
+ */
14
17
  walletName: string
15
18
 
19
+ /**
20
+ * 钱包唯一标识
21
+ */
16
22
  walletId: string
17
23
 
24
+ /**
25
+ * 钱包类型(私钥钱包 / 助记词钱包)
26
+ */
18
27
  walletType: WalletType
19
28
 
29
+ /**
30
+ * 钱包关联的地址列表
31
+ */
20
32
  addressList: {
33
+ /** 链上地址 */
21
34
  address: string
35
+ /** Binance 链ID */
22
36
  binanceChainId: string
37
+ /** 公钥(hex 编码) */
23
38
  publicKeyHex: string
24
39
  }[]
25
40
 
41
+ /**
42
+ * 钱包分组ID(可选)
43
+ */
26
44
  groupID?: string
45
+ /**
46
+ * 钱包分组名称(可选)
47
+ */
27
48
  groupName?: string
28
49
  }
29
50
 
51
+ /**
52
+ * SAS(Smart Account Signing)授权信息
53
+ */
30
54
  export interface SASAuthorizeInfo {
55
+ /** 钱包类型:助记词 / 私钥 */
31
56
  walletType: 'SEED_PHRASE' | 'PRIVATE_KEY'
57
+ /** ProofKey 公钥 */
32
58
  proofKeyPublicKey: string
59
+ /** 授权过期时间戳 */
33
60
  expireAt: number
61
+ /** 钱包唯一标识 */
34
62
  walletId: string
63
+ /** 插件唯一标识 */
35
64
  extensionId: string
65
+ /** 授权链列表 */
36
66
  authorizeList: SASAuthorizeInfoItem[]
37
67
  }
38
68
 
69
+ /**
70
+ * SAS 授权中单条链的详细信息
71
+ */
39
72
  export interface SASAuthorizeInfoItem {
73
+ /** 链类型:evm 或 sol */
40
74
  chain: 'evm' | 'sol'
75
+ /** Binance 链ID */
41
76
  binanceChainId: string
77
+ /** 加密后的 KeyShare B */
42
78
  encryptedKeyShareB: string
79
+ /** 加密后的 KeyShare A 数据 */
43
80
  encryptedKeyShareAData: string
81
+ /** 公钥 */
44
82
  publicKey: string
83
+ /** SPS 版本号 */
45
84
  spsVersion: string
85
+ /** 用户公钥(hex 编码) */
46
86
  userPublicKeyHex: string
47
87
  }
48
88
 
89
+ /**
90
+ * 钱包事件定义
91
+ */
49
92
  export interface WalletEvents {
93
+ /** 钱包锁定状态变更 */
50
94
  walletLockedChanged: (isLocked: boolean) => void
95
+ /** 自动签名开关状态变更 */
51
96
  autoSignEnabledChanged: (isEnabled: boolean) => void
97
+ /** 已连接钱包列表变更 */
52
98
  connectedWalletsChanged: (connectedWallets: IConnectedWallet[]) => void
99
+ /** 当前连接的链ID变更 */
53
100
  connectedChainIdChanged: (binanceChainId: string) => void
54
101
  }
55
102
 
103
+ /**
104
+ * 多链钱包接口,继承 EventEmitter 以支持事件监听
105
+ */
56
106
  export interface IWallet extends EventEmitter<WalletEvents> {
107
+ /**
108
+ * 请求连接钱包
109
+ * @param binanceChainIds 需要连接的 Binance 链ID列表
110
+ * @returns 已连接的钱包列表
111
+ */
57
112
  requestConnect(binanceChainIds: string[]): Promise<IConnectedWallet[]>
58
113
 
59
114
  /**
60
- * 链接插件的时候同时发起开启SAS的请求
115
+ * 连接插件的同时发起开启SAS的请求
61
116
  * 如果用户同意,插件会记录下当前用户同意开启的SAS
62
117
  */
63
118
  requestConnectWithSAS(args: {
@@ -73,6 +128,18 @@ export interface IWallet extends EventEmitter<WalletEvents> {
73
128
  aggreeEnableSAS: boolean
74
129
  }>
75
130
 
131
+ /**
132
+ * 连接插件的同时发起多钱包SAS请求
133
+ * @returns walletList 已连接钱包列表,enabledWalletIds 同意开启SAS的钱包ID列表
134
+ */
135
+ requestConnectWithMultiWalletSAS(args: {
136
+ binanceChainIds: string[]
137
+ userId: string
138
+ }): Promise<{
139
+ walletList: IConnectedWallet[]
140
+ enabledWalletIds: string[]
141
+ }>
142
+
76
143
  /**
77
144
  * 弹出要求用户同意开启SAS的弹窗
78
145
  */
@@ -84,6 +151,18 @@ export interface IWallet extends EventEmitter<WalletEvents> {
84
151
  aggreeEnableSAS: boolean
85
152
  }>
86
153
 
154
+ /**
155
+ * 弹出多钱包SAS开启请求弹窗
156
+ * @returns enabledWalletIds 用户同意开启SAS的钱包ID列表
157
+ */
158
+ requestEnableMultiWalletSAS(arg: {
159
+ walletIds: string[]
160
+ userId: string
161
+ userEmail: string
162
+ }): Promise<{
163
+ enabledWalletIds: string[]
164
+ }>
165
+
87
166
  /**
88
167
  * 用户同意开启SAS后,获取SAS的keyshare
89
168
  * 由于同一个钱包可能在app端被用户开启,所以插件不会校验用户有没有在插件端同意过
@@ -108,6 +187,9 @@ export interface IWallet extends EventEmitter<WalletEvents> {
108
187
  }[]
109
188
  }): Promise<void>
110
189
 
190
+ /**
191
+ * 获取已连接钱包及其 ProofKey 公钥
192
+ */
111
193
  getConnectedWalletAndProofKey(arg: { userId: string }): Promise<
112
194
  {
113
195
  wallet: IConnectedWallet
@@ -130,6 +212,12 @@ export interface IWallet extends EventEmitter<WalletEvents> {
130
212
  */
131
213
  checkConnectedWallet(): Promise<IConnectedWallet[]>
132
214
 
215
+ /**
216
+ * 根据地址和链ID获取钱包信息
217
+ * @param address 链上地址
218
+ * @param binanceChainId Binance 链ID
219
+ * @returns 匹配的钱包信息,未找到则返回 null
220
+ */
133
221
  getWalletInfo(
134
222
  address: string,
135
223
  binanceChainId: string
@@ -152,14 +240,26 @@ export interface IWallet extends EventEmitter<WalletEvents> {
152
240
  */
153
241
  requestAutoSign(): Promise<void>
154
242
 
243
+ /**
244
+ * 签署交易订单
245
+ * @returns 签名结果字符串
246
+ */
155
247
  signOrder(arg: {
248
+ /** 钱包唯一标识 */
156
249
  walletId: string
250
+ /** 钱包地址 */
157
251
  walletAddress: string
252
+ /** Binance 链ID */
158
253
  binanceChainId: string
254
+ /** 交易数据 */
159
255
  txData: string
256
+ /** 手续费金额 */
160
257
  feeAmount: string
258
+ /** 手续费等级(可选) */
161
259
  feeAmountLevel?: 'Low' | 'Medium' | 'High' | 'Custom'
260
+ /** 摘要 */
162
261
  digest: string
262
+ /** 摘要签名 */
163
263
  digestSign: string
164
264
  }): Promise<string>
165
265
 
@@ -172,6 +272,14 @@ export interface IWallet extends EventEmitter<WalletEvents> {
172
272
  * 是否启用自动签名
173
273
  */
174
274
  autoSignEnabled: boolean
275
+
276
+ /**
277
+ * 钱包是否锁定
278
+ */
175
279
  walletLocked: boolean
280
+
281
+ /**
282
+ * 插件唯一标识
283
+ */
176
284
  extensionId: string
177
285
  }