@yeying-community/web3-bs 1.0.13 → 1.0.14

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
@@ -22,7 +22,7 @@ npm install @yeying-community/web3-bs
22
22
 
23
23
  - `getProvider` / `requireProvider`
24
24
  - `watchProvider`
25
- - `requestAccounts` / `focusPendingApproval` / `getAccounts` / `getPreferredAccount` / `watchAccounts`
25
+ - `requestAccounts` / `focusPendingApproval` / `getAccounts` / `getPreferredAccount` / `resolveWalletAccount` / `watchAccounts`
26
26
  - `getChainId` / `getBalance`
27
27
  - `onAccountsChanged` / `onChainChanged`
28
28
  - `classifyWalletError` / `isUserRejectedWalletAction` / `isWalletReconnectError`
@@ -30,6 +30,14 @@ npm install @yeying-community/web3-bs
30
30
  `requestAccounts` 默认会复用同一 provider 上尚未完成的连接请求,避免用户重复点击时触发多个钱包授权弹窗。
31
31
  当钱包已经存在待确认的连接、签名或解锁窗口时,可调用 `focusPendingApproval` 将该窗口重新拉到前台,而不是再发起一次新的请求。
32
32
 
33
+ `resolveWalletAccount` 用于处理“应用传入期望地址”和“钱包当前地址”之间的关系:
34
+ - 未传 `expectedAccount`:直接采用钱包当前地址,返回 `status: "wallet"`
35
+ - 传了且与钱包当前地址一致:返回 `status: "matched"`
36
+ - 传了但与钱包当前地址不一致:返回 `status: "mismatch"`
37
+ - 钱包当前没有可用地址:返回 `status: "unavailable"`
38
+
39
+ 它只返回结构化决策结果,不负责弹窗、toast 或业务状态写入,适合在登录、授权、签名前由应用自行决定是继续使用当前钱包地址,还是要求用户切换钱包账户。
40
+
33
41
  ### 2) SIWE + JWT
34
42
 
35
43
  - `signMessage`
@@ -1,4 +1,4 @@
1
- import { Eip1193Provider, ProviderDiscoveryOptions, ProviderInfo, RequestAccountsOptions, AccountSelection, PreferredAccountOptions, WatchAccountsOptions, AccountsChangedHandler, WatchProviderOptions, ProviderChangedHandler, WalletErrorInfo, FocusPendingApprovalResult } from './types';
1
+ import { Eip1193Provider, ProviderDiscoveryOptions, ProviderInfo, RequestAccountsOptions, AccountSelection, PreferredAccountOptions, ResolveWalletAccountOptions, WalletAccountResolution, WatchAccountsOptions, AccountsChangedHandler, WatchProviderOptions, ProviderChangedHandler, WalletErrorInfo, FocusPendingApprovalResult } from './types';
2
2
  export declare function isYeYingProvider(provider?: Eip1193Provider | null, info?: ProviderInfo): boolean;
3
3
  export declare function getProvider(options?: ProviderDiscoveryOptions): Promise<Eip1193Provider | null>;
4
4
  export declare function watchProvider(handler: ProviderChangedHandler, options?: WatchProviderOptions): () => void;
@@ -13,6 +13,7 @@ export declare function focusPendingApproval(provider?: Eip1193Provider): Promis
13
13
  export declare function getAccounts(provider?: Eip1193Provider): Promise<string[]>;
14
14
  export declare function getChainId(provider?: Eip1193Provider): Promise<string | null>;
15
15
  export declare function getPreferredAccount(options?: PreferredAccountOptions): Promise<AccountSelection>;
16
+ export declare function resolveWalletAccount(options?: ResolveWalletAccountOptions): Promise<WalletAccountResolution>;
16
17
  export declare function watchAccounts(provider: Eip1193Provider, handler: AccountsChangedHandler, options?: WatchAccountsOptions): () => void;
17
18
  export declare function getBalance(provider?: Eip1193Provider, address?: string, blockTag?: string): Promise<string>;
18
19
  export declare function onAccountsChanged(provider: Eip1193Provider, handler: (accounts: string[]) => void): () => void;
@@ -67,6 +67,35 @@ export interface PreferredAccountOptions extends RequestAccountsOptions {
67
67
  autoConnect?: boolean;
68
68
  preferStored?: boolean;
69
69
  }
70
+ export interface ResolveWalletAccountOptions extends RequestAccountsOptions {
71
+ expectedAccount?: string | null;
72
+ autoConnect?: boolean;
73
+ }
74
+ export type WalletAccountResolution = {
75
+ status: 'wallet';
76
+ account: string;
77
+ walletAccount: string;
78
+ expectedAccount: null;
79
+ accounts: string[];
80
+ } | {
81
+ status: 'matched';
82
+ account: string;
83
+ walletAccount: string;
84
+ expectedAccount: string;
85
+ accounts: string[];
86
+ } | {
87
+ status: 'mismatch';
88
+ account: null;
89
+ walletAccount: string;
90
+ expectedAccount: string;
91
+ accounts: string[];
92
+ } | {
93
+ status: 'unavailable';
94
+ account: null;
95
+ walletAccount: null;
96
+ expectedAccount: string | null;
97
+ accounts: string[];
98
+ };
70
99
  export interface WatchAccountsOptions {
71
100
  storageKey?: string;
72
101
  preferStored?: boolean;
@@ -73,6 +73,17 @@ function selectPreferredAccount(accounts, stored, preferStored) {
73
73
  }
74
74
  return accounts[0] || null;
75
75
  }
76
+ function normalizeAccount(account) {
77
+ const normalized = (account || '').trim();
78
+ return normalized || null;
79
+ }
80
+ function isSameAccount(left, right) {
81
+ const normalizedLeft = normalizeAccount(left);
82
+ const normalizedRight = normalizeAccount(right);
83
+ return Boolean(normalizedLeft &&
84
+ normalizedRight &&
85
+ normalizedLeft.toLowerCase() === normalizedRight.toLowerCase());
86
+ }
76
87
  function isYeYingProvider(provider, info) {
77
88
  if (!provider)
78
89
  return false;
@@ -336,6 +347,49 @@ async function getPreferredAccount(options = {}) {
336
347
  writeStoredAccount(storageKey, account);
337
348
  return { account, accounts };
338
349
  }
350
+ async function resolveWalletAccount(options = {}) {
351
+ const provider = options.provider || (await requireProvider());
352
+ let accounts = await getAccounts(provider);
353
+ if (accounts.length === 0 && options.autoConnect) {
354
+ accounts = await requestAccounts({ provider });
355
+ }
356
+ const expectedAccount = normalizeAccount(options.expectedAccount);
357
+ const walletAccount = normalizeAccount(accounts[0]);
358
+ if (!walletAccount) {
359
+ return {
360
+ status: 'unavailable',
361
+ account: null,
362
+ walletAccount: null,
363
+ expectedAccount,
364
+ accounts,
365
+ };
366
+ }
367
+ if (!expectedAccount) {
368
+ return {
369
+ status: 'wallet',
370
+ account: walletAccount,
371
+ walletAccount,
372
+ expectedAccount: null,
373
+ accounts,
374
+ };
375
+ }
376
+ if (isSameAccount(expectedAccount, walletAccount)) {
377
+ return {
378
+ status: 'matched',
379
+ account: walletAccount,
380
+ walletAccount,
381
+ expectedAccount,
382
+ accounts,
383
+ };
384
+ }
385
+ return {
386
+ status: 'mismatch',
387
+ account: null,
388
+ walletAccount,
389
+ expectedAccount,
390
+ accounts,
391
+ };
392
+ }
339
393
  function watchAccounts(provider, handler, options = {}) {
340
394
  const storageKey = options.storageKey || DEFAULT_ACCOUNT_STORAGE_KEY;
341
395
  const preferStored = options.preferStored !== false;
@@ -2471,5 +2525,5 @@ async function initDappSession(options) {
2471
2525
  return result;
2472
2526
  }
2473
2527
 
2474
- export { CipherError, DEFAULT_UCAN_SESSION_TTL_MS, DEFAULT_UCAN_TOKEN_SKEW_MS, DEFAULT_UCAN_TOKEN_TTL_MS, WebDavClient, authCentralUcanFetch, authFetch, authUcanFetch, base64ToBytes, bytesToBase64, classifyUcanAuthError, classifyWalletError, clearAccessToken, clearCentralSessionToken, clearUcanSession, createAndIssueCentralUcan, createCentralSession, createDelegationUcan, createInvocationUcan, createRootUcan, createUcanSession, createWebDavClient, decodeUcanPayload, decrypt, deriveAppIdFromHost, deriveAppIdFromLocation, encrypt, focusPendingApproval, getAccessToken, getAccounts, getBalance, getCapabilityAction, getCapabilityResource, getCentralIssuerInfo, getCentralSessionToken, getChainId, getOrCreateInvocationUcan, getOrCreateUcanRoot, getPreferredAccount, getProvider, getStoredUcanRoot, getSupportedCipherSuites, getUcanSession, getUcanTokenTiming, getWalletErrorCode, getWalletErrorMessage, initDappSession, initWebDavStorage, isUcanTokenFresh, isUserRejectedWalletAction, isWalletReconnectError, isYeYingProvider, issueCentralUcan, loginWithChallenge, logout, normalizeAppHostnameForAppId, normalizeUcanCapabilities, normalizeUcanCapability, normalizeUcanExpiry, onAccountsChanged, onChainChanged, refreshAccessToken, requestAccounts, requireProvider, setAccessToken, setCentralSessionToken, signMessage, storeUcanRoot, watchAccounts, watchProvider };
2528
+ export { CipherError, DEFAULT_UCAN_SESSION_TTL_MS, DEFAULT_UCAN_TOKEN_SKEW_MS, DEFAULT_UCAN_TOKEN_TTL_MS, WebDavClient, authCentralUcanFetch, authFetch, authUcanFetch, base64ToBytes, bytesToBase64, classifyUcanAuthError, classifyWalletError, clearAccessToken, clearCentralSessionToken, clearUcanSession, createAndIssueCentralUcan, createCentralSession, createDelegationUcan, createInvocationUcan, createRootUcan, createUcanSession, createWebDavClient, decodeUcanPayload, decrypt, deriveAppIdFromHost, deriveAppIdFromLocation, encrypt, focusPendingApproval, getAccessToken, getAccounts, getBalance, getCapabilityAction, getCapabilityResource, getCentralIssuerInfo, getCentralSessionToken, getChainId, getOrCreateInvocationUcan, getOrCreateUcanRoot, getPreferredAccount, getProvider, getStoredUcanRoot, getSupportedCipherSuites, getUcanSession, getUcanTokenTiming, getWalletErrorCode, getWalletErrorMessage, initDappSession, initWebDavStorage, isUcanTokenFresh, isUserRejectedWalletAction, isWalletReconnectError, isYeYingProvider, issueCentralUcan, loginWithChallenge, logout, normalizeAppHostnameForAppId, normalizeUcanCapabilities, normalizeUcanCapability, normalizeUcanExpiry, onAccountsChanged, onChainChanged, refreshAccessToken, requestAccounts, requireProvider, resolveWalletAccount, setAccessToken, setCentralSessionToken, signMessage, storeUcanRoot, watchAccounts, watchProvider };
2475
2529
  //# sourceMappingURL=web3-bs.esm.js.map