@yeying-community/web3-bs 1.0.16 → 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.
@@ -2157,6 +2157,111 @@
2157
2157
  }
2158
2158
  }
2159
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
+
2160
2265
  function normalizeBaseUrl(baseUrl) {
2161
2266
  return baseUrl.replace(/\/+$/, '');
2162
2267
  }
@@ -2717,6 +2822,7 @@
2717
2822
  exports.clearAccessToken = clearAccessToken;
2718
2823
  exports.clearCentralSessionToken = clearCentralSessionToken;
2719
2824
  exports.clearUcanSession = clearUcanSession;
2825
+ exports.connectAndGetWalletProfile = connectAndGetWalletProfile;
2720
2826
  exports.createAndIssueCentralUcan = createAndIssueCentralUcan;
2721
2827
  exports.createCentralSession = createCentralSession;
2722
2828
  exports.createDelegationUcan = createDelegationUcan;
@@ -2738,6 +2844,7 @@
2738
2844
  exports.getCentralIssuerInfo = getCentralIssuerInfo;
2739
2845
  exports.getCentralSessionToken = getCentralSessionToken;
2740
2846
  exports.getChainId = getChainId;
2847
+ exports.getGrantedProfileFields = getGrantedProfileFields;
2741
2848
  exports.getOrCreateInvocationUcan = getOrCreateInvocationUcan;
2742
2849
  exports.getOrCreateUcanRoot = getOrCreateUcanRoot;
2743
2850
  exports.getPreferredAccount = getPreferredAccount;
@@ -2748,6 +2855,8 @@
2748
2855
  exports.getUcanTokenTiming = getUcanTokenTiming;
2749
2856
  exports.getWalletErrorCode = getWalletErrorCode;
2750
2857
  exports.getWalletErrorMessage = getWalletErrorMessage;
2858
+ exports.getWalletProfile = getWalletProfile;
2859
+ exports.getWalletProfilePermission = getWalletProfilePermission;
2751
2860
  exports.initDappSession = initDappSession;
2752
2861
  exports.initWebDavStorage = initWebDavStorage;
2753
2862
  exports.isUcanTokenFresh = isUcanTokenFresh;
@@ -2766,9 +2875,11 @@
2766
2875
  exports.parseUcanAccountFromIssuer = parseUcanAccountFromIssuer;
2767
2876
  exports.refreshAccessToken = refreshAccessToken;
2768
2877
  exports.requestAccounts = requestAccounts;
2878
+ exports.requestWalletProfilePermission = requestWalletProfilePermission;
2769
2879
  exports.requireProvider = requireProvider;
2770
2880
  exports.resolveUcanAuthorization = resolveUcanAuthorization;
2771
2881
  exports.resolveWalletAccount = resolveWalletAccount;
2882
+ exports.revokeWalletProfilePermission = revokeWalletProfilePermission;
2772
2883
  exports.setAccessToken = setAccessToken;
2773
2884
  exports.setCentralSessionToken = setCentralSessionToken;
2774
2885
  exports.signMessage = signMessage;