uvd-x402-sdk 2.14.1 → 2.15.2

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.
@@ -10,6 +10,17 @@
10
10
  * 3. SignedDelegateAction is sent to facilitator
11
11
  * 4. Facilitator wraps it and submits to NEAR, paying all gas
12
12
  *
13
+ * NEP-366 Wallet Support:
14
+ * For apps that need wallet selector integration, call setupWalletSelector() early:
15
+ *
16
+ * ```ts
17
+ * // In your app's initialization (e.g., App.tsx or main.ts)
18
+ * await NEARProvider.setupWalletSelector({
19
+ * walletUrl: 'https://mynearwallet.ultravioletadao.xyz',
20
+ * contractId: '17208628f84f5d6ad33f0da3bbbeb27ffcb398eac501a31bd6ad2011e36133a1',
21
+ * });
22
+ * ```
23
+ *
13
24
  * @example
14
25
  * ```ts
15
26
  * import { NEARProvider } from 'uvd-x402-sdk/near';
@@ -221,6 +232,18 @@ interface MyNearWallet {
221
232
  }) => Promise<{ signature: Uint8Array; publicKey: string }>;
222
233
  }
223
234
 
235
+ /**
236
+ * Options for configuring the NEAR wallet selector helper
237
+ */
238
+ export interface WalletSelectorOptions {
239
+ /** URL of the MyNearWallet instance (default: https://mynearwallet.ultravioletadao.xyz) */
240
+ walletUrl?: string;
241
+ /** USDC contract ID for NEAR (default: 17208628f84f5d6ad33f0da3bbbeb27ffcb398eac501a31bd6ad2011e36133a1) */
242
+ contractId?: string;
243
+ /** NEAR network ID (default: mainnet) */
244
+ networkId?: string;
245
+ }
246
+
224
247
  /**
225
248
  * NEARProvider - Wallet adapter for NEAR Protocol via MyNearWallet/Meteor
226
249
  */
@@ -233,6 +256,83 @@ export class NEARProvider implements WalletAdapter {
233
256
  private publicKey: Uint8Array | null = null;
234
257
  private rpcUrl: string = NEAR_CONFIG.nodeUrl;
235
258
 
259
+ /**
260
+ * Setup wallet selector for NEP-366 meta-transaction support.
261
+ *
262
+ * Call this once early in your app initialization (e.g., App.tsx or main.ts)
263
+ * to configure the NEAR wallet selector before using NEARProvider.
264
+ *
265
+ * @param options - Configuration options for wallet selector
266
+ * @example
267
+ * ```ts
268
+ * await NEARProvider.setupWalletSelector({
269
+ * walletUrl: 'https://mynearwallet.ultravioletadao.xyz',
270
+ * contractId: '17208628f84f5d6ad33f0da3bbbeb27ffcb398eac501a31bd6ad2011e36133a1',
271
+ * });
272
+ * ```
273
+ */
274
+ static async setupWalletSelector(options: WalletSelectorOptions = {}): Promise<void> {
275
+ const {
276
+ walletUrl = 'https://mynearwallet.ultravioletadao.xyz',
277
+ contractId = '17208628f84f5d6ad33f0da3bbbeb27ffcb398eac501a31bd6ad2011e36133a1',
278
+ networkId = 'mainnet',
279
+ } = options;
280
+
281
+ if (typeof window === 'undefined') {
282
+ throw new X402Error(
283
+ 'setupWalletSelector() must be called in a browser environment',
284
+ 'WALLET_NOT_FOUND'
285
+ );
286
+ }
287
+
288
+ try {
289
+ // Dynamic imports for wallet selector packages
290
+ const [{ setupWalletSelector }, { setupMyNearWallet }] = await Promise.all([
291
+ import('@near-wallet-selector/core'),
292
+ import('@near-wallet-selector/my-near-wallet'),
293
+ ]);
294
+
295
+ // Setup the wallet selector with MyNearWallet module
296
+ const selector = await setupWalletSelector({
297
+ network: networkId as 'mainnet' | 'testnet',
298
+ modules: [
299
+ setupMyNearWallet({
300
+ walletUrl,
301
+ }),
302
+ ],
303
+ });
304
+
305
+ // Expose to window for SDK compatibility
306
+ const win = window as Window & { nearWalletSelector?: NEARWalletSelector };
307
+ win.nearWalletSelector = {
308
+ isSignedIn: () => selector.isSignedIn(),
309
+ getAccountId: () => {
310
+ const state = selector.store.getState();
311
+ return state.accounts[0]?.accountId || null;
312
+ },
313
+ signIn: async (signInOptions?: { contractId?: string }) => {
314
+ const wallet = await selector.wallet('my-near-wallet');
315
+ // BrowserWallet only requires contractId, cast to bypass union type requirement
316
+ const signInFn = wallet.signIn as unknown as (params: { contractId: string }) => Promise<void>;
317
+ await signInFn({
318
+ contractId: signInOptions?.contractId || contractId,
319
+ });
320
+ },
321
+ signOut: async () => {
322
+ const wallet = await selector.wallet('my-near-wallet');
323
+ await wallet.signOut();
324
+ },
325
+ };
326
+ } catch (error: unknown) {
327
+ throw new X402Error(
328
+ `Failed to setup wallet selector: ${error instanceof Error ? error.message : 'Unknown error'}. ` +
329
+ 'Make sure @near-wallet-selector/core and @near-wallet-selector/my-near-wallet are installed.',
330
+ 'WALLET_NOT_FOUND',
331
+ error
332
+ );
333
+ }
334
+ }
335
+
236
336
  /**
237
337
  * Check if NEAR wallet is available
238
338
  */
@@ -300,7 +400,7 @@ export class NEARProvider implements WalletAdapter {
300
400
  }
301
401
 
302
402
  throw new X402Error(
303
- 'No NEAR wallet found. Please install MyNearWallet or Meteor.',
403
+ 'No NEAR wallet found. Call NEARProvider.setupWalletSelector() first, or install MyNearWallet/Meteor.',
304
404
  'WALLET_NOT_FOUND'
305
405
  );
306
406
  } catch (error: unknown) {
@@ -573,11 +673,38 @@ export class NEARProvider implements WalletAdapter {
573
673
  */
574
674
  private async signMessage(message: Uint8Array): Promise<Uint8Array> {
575
675
  const win = window as Window & {
576
- nearWalletSelector?: NEARWalletSelector;
676
+ nearWalletSelector?: NEARWalletSelector & {
677
+ wallet?: () => Promise<{
678
+ signMessage?: (params: {
679
+ message: Uint8Array;
680
+ recipient: string;
681
+ nonce: Uint8Array;
682
+ }) => Promise<{ signature: Uint8Array; publicKey: string }>;
683
+ }>;
684
+ };
577
685
  myNearWallet?: MyNearWallet;
578
686
  };
579
687
 
580
- // Try MyNearWallet signMessage
688
+ // Try wallet selector's wallet.signMessage() first (from setupWalletSelector)
689
+ if (win.nearWalletSelector?.wallet) {
690
+ try {
691
+ const wallet = await win.nearWalletSelector.wallet();
692
+ if (wallet?.signMessage) {
693
+ const nonce = crypto.getRandomValues(new Uint8Array(32));
694
+ const result = await wallet.signMessage({
695
+ message,
696
+ recipient: 'uvd-x402-sdk',
697
+ nonce,
698
+ });
699
+ return result.signature;
700
+ }
701
+ } catch (e) {
702
+ // Fall through to try other methods
703
+ console.debug('Wallet selector signMessage failed:', e);
704
+ }
705
+ }
706
+
707
+ // Try MyNearWallet browser extension signMessage
581
708
  if (win.myNearWallet?.signMessage) {
582
709
  const result = await win.myNearWallet.signMessage({
583
710
  message,