@yeying-community/web3-bs 1.0.15 → 1.0.17

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.
@@ -2042,7 +2042,8 @@
2042
2042
  * 调用钱包插件的 `yeying_encrypt` / `yeying_decrypt` / `yeying_getCipherSuites`
2043
2043
  * EIP-1193 自定义方法。命名安全套件 + 静默执行 + 站点授权模型同 UCAN。
2044
2044
  *
2045
- * 数据密码(password 参数)由 DApp 自行管理与钱包密码独立;
2045
+ * 默认数据密码(password 参数)由 DApp 自行管理与钱包密码独立;
2046
+ * 也可通过 passwordSource 请求钱包插件在钱包内部派生加密密码。
2046
2047
  * plaintext 走 base64 字符串跨 message 边界传输。
2047
2048
  */
2048
2049
  class CipherError extends Error {
@@ -2093,6 +2094,8 @@
2093
2094
  params: [{
2094
2095
  data: options.data,
2095
2096
  password: options.password,
2097
+ passwordSource: options.passwordSource,
2098
+ passwordContext: options.passwordContext,
2096
2099
  suite: options.suite
2097
2100
  }]
2098
2101
  });
@@ -2116,7 +2119,9 @@
2116
2119
  method: 'yeying_decrypt',
2117
2120
  params: [{
2118
2121
  ciphertext: options.ciphertext,
2119
- password: options.password
2122
+ password: options.password,
2123
+ passwordSource: options.passwordSource,
2124
+ passwordContext: options.passwordContext
2120
2125
  }]
2121
2126
  });
2122
2127
  if (!result || typeof result !== 'object' || typeof result.plaintext !== 'string') {
@@ -2152,6 +2157,111 @@
2152
2157
  }
2153
2158
  }
2154
2159
 
2160
+ const SUPPORTED_FIELDS = new Set(['username', 'email']);
2161
+ function normalizeFields(fields) {
2162
+ if (!Array.isArray(fields) || fields.length === 0) {
2163
+ throw new Error('Profile fields must contain username and/or email');
2164
+ }
2165
+ const normalized = Array.from(new Set(fields.map(field => String(field))));
2166
+ if (normalized.some(field => !SUPPORTED_FIELDS.has(field))) {
2167
+ throw new Error('Unsupported wallet profile field');
2168
+ }
2169
+ return normalized;
2170
+ }
2171
+ function parsePermission(value) {
2172
+ if (!value || typeof value !== 'object')
2173
+ return null;
2174
+ const permission = value;
2175
+ return permission.parentCapability === 'yeying_profile' ? permission : null;
2176
+ }
2177
+ function getGrantedProfileFields(permission) {
2178
+ if (!permission || !Array.isArray(permission.caveats))
2179
+ return [];
2180
+ const fields = permission.caveats.flatMap(caveat => {
2181
+ if (Array.isArray(caveat?.value))
2182
+ return caveat.value;
2183
+ return Array.isArray(caveat?.value?.fields) ? caveat.value.fields : [];
2184
+ });
2185
+ return Array.from(new Set(fields.filter(field => SUPPORTED_FIELDS.has(field))));
2186
+ }
2187
+ async function getWalletProfilePermission(provider) {
2188
+ const target = provider || (await requireProvider());
2189
+ const permissions = await target.request({ method: 'wallet_getPermissions' });
2190
+ if (!Array.isArray(permissions))
2191
+ return null;
2192
+ return permissions.map(parsePermission).find(Boolean) || null;
2193
+ }
2194
+ async function requestWalletProfilePermission(options) {
2195
+ const fields = normalizeFields(options.fields);
2196
+ const provider = options.provider || (await requireProvider());
2197
+ const result = await provider.request({
2198
+ method: 'wallet_requestPermissions',
2199
+ params: [{ yeying_profile: { fields } }],
2200
+ });
2201
+ if (!Array.isArray(result)) {
2202
+ throw new Error('Wallet returned an invalid profile permission response');
2203
+ }
2204
+ const permission = result.map(parsePermission).find(Boolean);
2205
+ if (!permission)
2206
+ throw new Error('Wallet did not grant profile permission');
2207
+ const granted = new Set(getGrantedProfileFields(permission));
2208
+ if (fields.some(field => !granted.has(field))) {
2209
+ throw new Error('Wallet did not grant all requested profile fields');
2210
+ }
2211
+ return permission;
2212
+ }
2213
+ async function getWalletProfile(options) {
2214
+ const fields = normalizeFields(options.fields);
2215
+ const provider = options.provider || (await requireProvider());
2216
+ const result = await provider.request({
2217
+ method: 'yeying_getProfile',
2218
+ params: [{ fields }],
2219
+ });
2220
+ if (!result || typeof result !== 'object') {
2221
+ throw new Error('Wallet returned an invalid profile response');
2222
+ }
2223
+ const value = result;
2224
+ const profileSource = value.profile;
2225
+ if (typeof value.address !== 'string' || !profileSource || typeof profileSource !== 'object') {
2226
+ throw new Error('Wallet returned an invalid profile response');
2227
+ }
2228
+ const profile = {};
2229
+ for (const field of fields) {
2230
+ const fieldValue = profileSource[field];
2231
+ if (typeof fieldValue === 'string')
2232
+ profile[field] = fieldValue;
2233
+ }
2234
+ return {
2235
+ address: value.address,
2236
+ chainId: typeof value.chainId === 'string' ? value.chainId : null,
2237
+ profile,
2238
+ };
2239
+ }
2240
+ async function revokeWalletProfilePermission(provider) {
2241
+ const target = provider || (await requireProvider());
2242
+ await target.request({
2243
+ method: 'wallet_revokePermissions',
2244
+ params: [{ yeying_profile: {} }],
2245
+ });
2246
+ }
2247
+ async function connectAndGetWalletProfile(options) {
2248
+ const fields = normalizeFields(options.fields);
2249
+ const provider = options.provider || (await requireProvider());
2250
+ const accounts = await provider.request({ method: 'eth_requestAccounts' });
2251
+ if (!Array.isArray(accounts) || typeof accounts[0] !== 'string') {
2252
+ throw new Error('Wallet did not return an account');
2253
+ }
2254
+ if (options.requestPermission !== false) {
2255
+ const permission = await getWalletProfilePermission(provider);
2256
+ const granted = new Set(getGrantedProfileFields(permission));
2257
+ const missing = fields.filter(field => !granted.has(field));
2258
+ if (missing.length > 0) {
2259
+ await requestWalletProfilePermission({ provider, fields: missing });
2260
+ }
2261
+ }
2262
+ return getWalletProfile({ provider, fields });
2263
+ }
2264
+
2155
2265
  function normalizeBaseUrl(baseUrl) {
2156
2266
  return baseUrl.replace(/\/+$/, '');
2157
2267
  }
@@ -2712,6 +2822,7 @@
2712
2822
  exports.clearAccessToken = clearAccessToken;
2713
2823
  exports.clearCentralSessionToken = clearCentralSessionToken;
2714
2824
  exports.clearUcanSession = clearUcanSession;
2825
+ exports.connectAndGetWalletProfile = connectAndGetWalletProfile;
2715
2826
  exports.createAndIssueCentralUcan = createAndIssueCentralUcan;
2716
2827
  exports.createCentralSession = createCentralSession;
2717
2828
  exports.createDelegationUcan = createDelegationUcan;
@@ -2733,6 +2844,7 @@
2733
2844
  exports.getCentralIssuerInfo = getCentralIssuerInfo;
2734
2845
  exports.getCentralSessionToken = getCentralSessionToken;
2735
2846
  exports.getChainId = getChainId;
2847
+ exports.getGrantedProfileFields = getGrantedProfileFields;
2736
2848
  exports.getOrCreateInvocationUcan = getOrCreateInvocationUcan;
2737
2849
  exports.getOrCreateUcanRoot = getOrCreateUcanRoot;
2738
2850
  exports.getPreferredAccount = getPreferredAccount;
@@ -2743,6 +2855,8 @@
2743
2855
  exports.getUcanTokenTiming = getUcanTokenTiming;
2744
2856
  exports.getWalletErrorCode = getWalletErrorCode;
2745
2857
  exports.getWalletErrorMessage = getWalletErrorMessage;
2858
+ exports.getWalletProfile = getWalletProfile;
2859
+ exports.getWalletProfilePermission = getWalletProfilePermission;
2746
2860
  exports.initDappSession = initDappSession;
2747
2861
  exports.initWebDavStorage = initWebDavStorage;
2748
2862
  exports.isUcanTokenFresh = isUcanTokenFresh;
@@ -2761,9 +2875,11 @@
2761
2875
  exports.parseUcanAccountFromIssuer = parseUcanAccountFromIssuer;
2762
2876
  exports.refreshAccessToken = refreshAccessToken;
2763
2877
  exports.requestAccounts = requestAccounts;
2878
+ exports.requestWalletProfilePermission = requestWalletProfilePermission;
2764
2879
  exports.requireProvider = requireProvider;
2765
2880
  exports.resolveUcanAuthorization = resolveUcanAuthorization;
2766
2881
  exports.resolveWalletAccount = resolveWalletAccount;
2882
+ exports.revokeWalletProfilePermission = revokeWalletProfilePermission;
2767
2883
  exports.setAccessToken = setAccessToken;
2768
2884
  exports.setCentralSessionToken = setCentralSessionToken;
2769
2885
  exports.signMessage = signMessage;