@explorins/pers-sdk-react-native 2.1.13 → 2.1.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.
Files changed (68) hide show
  1. package/README.md +108 -14
  2. package/dist/hooks/index.d.ts +3 -2
  3. package/dist/hooks/index.d.ts.map +1 -1
  4. package/dist/hooks/useAnalytics.d.ts +1 -1
  5. package/dist/hooks/useAnalytics.d.ts.map +1 -1
  6. package/dist/hooks/useAuth.d.ts +3 -3
  7. package/dist/hooks/useAuth.d.ts.map +1 -1
  8. package/dist/hooks/useAuth.js +5 -17
  9. package/dist/hooks/useBusiness.d.ts +3 -2
  10. package/dist/hooks/useBusiness.d.ts.map +1 -1
  11. package/dist/hooks/useBusiness.js +28 -2
  12. package/dist/hooks/useCampaigns.d.ts +12 -4
  13. package/dist/hooks/useCampaigns.d.ts.map +1 -1
  14. package/dist/hooks/useCampaigns.js +36 -8
  15. package/dist/hooks/useDonations.d.ts +1 -1
  16. package/dist/hooks/useDonations.d.ts.map +1 -1
  17. package/dist/hooks/usePurchases.d.ts +2 -2
  18. package/dist/hooks/usePurchases.d.ts.map +1 -1
  19. package/dist/hooks/useRedemptions.d.ts +28 -5
  20. package/dist/hooks/useRedemptions.d.ts.map +1 -1
  21. package/dist/hooks/useRedemptions.js +58 -1
  22. package/dist/hooks/useTenants.d.ts +1 -1
  23. package/dist/hooks/useTenants.d.ts.map +1 -1
  24. package/dist/hooks/useTokenBalances.d.ts +1 -1
  25. package/dist/hooks/useTokenBalances.d.ts.map +1 -1
  26. package/dist/hooks/useTokenBalances.js +1 -1
  27. package/dist/hooks/useTokens.d.ts +1 -1
  28. package/dist/hooks/useTokens.d.ts.map +1 -1
  29. package/dist/hooks/useTransactionSigner.d.ts.map +1 -1
  30. package/dist/hooks/useTransactionSigner.js +38 -11
  31. package/dist/hooks/useTransactions.d.ts +1 -1
  32. package/dist/hooks/useTransactions.d.ts.map +1 -1
  33. package/dist/hooks/useTriggerSources.d.ts +1 -1
  34. package/dist/hooks/useTriggerSources.d.ts.map +1 -1
  35. package/dist/hooks/useUserStatus.d.ts +1 -1
  36. package/dist/hooks/useUserStatus.d.ts.map +1 -1
  37. package/dist/hooks/useUsers.d.ts +1 -1
  38. package/dist/hooks/useUsers.d.ts.map +1 -1
  39. package/dist/index-CzLxqePZ.js +7717 -0
  40. package/dist/index-CzLxqePZ.js.map +1 -0
  41. package/dist/index.d.ts +4 -3
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/index.js +1896 -317
  44. package/dist/index.js.map +1 -1
  45. package/dist/providers/PersSDKProvider.d.ts.map +1 -1
  46. package/dist/providers/PersSDKProvider.js +7 -17
  47. package/dist/providers/rn-dpop-provider.d.ts.map +1 -1
  48. package/dist/providers/rn-dpop-provider.js +48 -12
  49. package/package.json +3 -3
  50. package/src/hooks/index.ts +5 -2
  51. package/src/hooks/useAnalytics.ts +1 -1
  52. package/src/hooks/useAuth.ts +6 -20
  53. package/src/hooks/useBusiness.ts +32 -3
  54. package/src/hooks/useCampaigns.ts +49 -12
  55. package/src/hooks/useDonations.ts +1 -1
  56. package/src/hooks/usePurchases.ts +1 -1
  57. package/src/hooks/useRedemptions.ts +91 -6
  58. package/src/hooks/useTenants.ts +1 -1
  59. package/src/hooks/useTokenBalances.ts +1 -1
  60. package/src/hooks/useTokens.ts +1 -1
  61. package/src/hooks/useTransactionSigner.ts +41 -10
  62. package/src/hooks/useTransactions.ts +1 -1
  63. package/src/hooks/useTriggerSources.ts +1 -1
  64. package/src/hooks/useUserStatus.ts +1 -1
  65. package/src/hooks/useUsers.ts +1 -1
  66. package/src/index.ts +6 -3
  67. package/src/providers/PersSDKProvider.tsx +8 -17
  68. package/src/providers/rn-dpop-provider.ts +57 -11
@@ -19,14 +19,44 @@ export type { StatusUpdateData };
19
19
  export type OnStatusUpdateFn = (status: SigningStatus, message: string, data?: StatusUpdateData) => void;
20
20
 
21
21
 
22
- // Dynamic import the signer SDK to avoid build issues with static dependencies
23
- let createPersSignerSDK: ((config: PersSignerConfig) => Promise<PersSignerSDK>) | null = null;
24
- try {
25
- const signerModule = require('@explorins/pers-signer/react-native');
26
- createPersSignerSDK = signerModule.createPersSignerSDK;
27
- } catch (error: unknown) {
28
- console.warn('[useTransactionSigner] PERS Signer SDK not available:', (error as Error).message);
29
- console.warn('[useTransactionSigner] Real blockchain signing will not be available');
22
+ // Lazy-loaded signer SDK factory - initialized on first use to avoid native module timing issues
23
+ let _createPersSignerSDK: ((config: PersSignerConfig) => Promise<PersSignerSDK>) | null = null;
24
+ let _signerInitAttempted = false;
25
+
26
+ /**
27
+ * Lazily load the PERS Signer SDK.
28
+ *
29
+ * Supports customer pre-initialization via global.__PersSigner for environments
30
+ * where native module timing is unpredictable (e.g., certain React Native setups).
31
+ *
32
+ * @returns The createPersSignerSDK function or null if unavailable
33
+ */
34
+ function getSignerSDKFactory(): ((config: PersSignerConfig) => Promise<PersSignerSDK>) | null {
35
+ if (_signerInitAttempted) {
36
+ // Already tried - return cached result (could be null if failed)
37
+ return _createPersSignerSDK;
38
+ }
39
+
40
+ _signerInitAttempted = true;
41
+
42
+ // Check for customer pre-initialized global (for native module timing issues)
43
+ if (typeof global !== 'undefined' && (global as any).__PersSigner?.createPersSignerSDK) {
44
+ _createPersSignerSDK = (global as any).__PersSigner.createPersSignerSDK;
45
+ return _createPersSignerSDK;
46
+ }
47
+
48
+ // Standard lazy load
49
+ try {
50
+ const signerModule = require('@explorins/pers-signer/react-native');
51
+ _createPersSignerSDK = signerModule.createPersSignerSDK;
52
+ return _createPersSignerSDK;
53
+ } catch (error: unknown) {
54
+ console.warn('[useTransactionSigner] PERS Signer SDK not available:', (error as Error).message);
55
+ console.warn('[useTransactionSigner] Real blockchain signing will not be available');
56
+ console.warn('[useTransactionSigner] Tip: If timing issues, pre-initialize in index.js: global.__PersSigner = require("@explorins/pers-signer/react-native")');
57
+ _createPersSignerSDK = null; // Mark as unavailable
58
+ return null;
59
+ }
30
60
  }
31
61
 
32
62
 
@@ -243,6 +273,7 @@ export const useTransactionSigner = (): TransactionSignerHook => {
243
273
  // Auto-initialize signer SDK when PERS SDK is ready
244
274
  useEffect(() => {
245
275
  const initializeSignerSDK = async () => {
276
+ const createPersSignerSDK = getSignerSDKFactory();
246
277
  if (!createPersSignerSDK) {
247
278
  console.warn('[useTransactionSigner] Signer SDK not available');
248
279
  return;
@@ -361,7 +392,7 @@ export const useTransactionSigner = (): TransactionSignerHook => {
361
392
  if (!isSignerInitialized || !signerSDKRef.current) {
362
393
  throw new Error('Transaction signer not initialized');
363
394
  }
364
- if (!createPersSignerSDK) {
395
+ if (!getSignerSDKFactory()) {
365
396
  throw new Error('PERS Signer SDK not available. Blockchain signing is not supported.');
366
397
  }
367
398
 
@@ -514,7 +545,7 @@ export const useTransactionSigner = (): TransactionSignerHook => {
514
545
  * );
515
546
  * ```
516
547
  */
517
- isSignerAvailable: isSignerInitialized && !!createPersSignerSDK,
548
+ isSignerAvailable: isSignerInitialized && !!getSignerSDKFactory(),
518
549
 
519
550
  /**
520
551
  * Current signing status for UI feedback
@@ -8,7 +8,7 @@ import type {
8
8
  TransactionPaginationRequestDTO,
9
9
  PaginatedResponseDTO,
10
10
  TransactionIncludeRelation
11
- } from '@explorins/pers-shared';
11
+ } from '@explorins/pers-sdk';
12
12
  import type { TransactionQueryOptions } from '@explorins/pers-sdk/transaction';
13
13
 
14
14
  // Re-export for consumers
@@ -4,7 +4,7 @@ import type {
4
4
  TriggerSourceDTO,
5
5
  TriggerSourceCreateRequestDTO,
6
6
  PaginatedResponseDTO
7
- } from '@explorins/pers-shared';
7
+ } from '@explorins/pers-sdk';
8
8
  import type { TriggerSourceQueryOptions } from '@explorins/pers-sdk/trigger-source';
9
9
 
10
10
  // Re-export for consumers
@@ -1,6 +1,6 @@
1
1
  import { useCallback } from 'react';
2
2
  import { usePersSDK } from '../providers/PersSDKProvider';
3
- import type { UserStatusTypeDTO, PaginatedResponseDTO } from '@explorins/pers-shared';
3
+ import type { UserStatusTypeDTO, PaginatedResponseDTO } from '@explorins/pers-sdk';
4
4
 
5
5
  export const useUserStatus = () => {
6
6
  const { sdk, isInitialized, isAuthenticated } = usePersSDK();
@@ -1,6 +1,6 @@
1
1
  import { useCallback } from 'react';
2
2
  import { usePersSDK } from '../providers/PersSDKProvider';
3
- import type { UserDTO, UserCreateRequestDTO, PaginatedResponseDTO } from '@explorins/pers-shared';
3
+ import type { UserDTO, UserCreateRequestDTO, PaginatedResponseDTO } from '@explorins/pers-sdk';
4
4
  import type { UserPublicProfileDTO, UserQueryOptions } from '@explorins/pers-sdk/user';
5
5
 
6
6
  // Re-export for consumers
package/src/index.ts CHANGED
@@ -284,10 +284,13 @@ export type { EventsHook, PersEvent, EventHandler, EventFilter, Unsubscribe } fr
284
284
  export type { TokenBalanceWithToken, UseTokenBalancesOptions, UseTokenBalancesResult } from './hooks';
285
285
 
286
286
  // Re-export campaign types for convenience
287
- export type { CampaignClaimFilters, CampaignHook, GetCampaignsOptions } from './hooks';
287
+ export type { CampaignClaimFilters, CampaignFilterOptions, CampaignHook, GetCampaignsOptions } from './hooks';
288
288
 
289
289
  // Re-export redemption types for convenience
290
- export type { RedemptionRedeemFilters, RedemptionHook } from './hooks';
290
+ export type { RedemptionRedeemFilters, RedemptionFilterOptions, RedemptionHook, GetRedemptionsOptions } from './hooks';
291
+
292
+ // Re-export business types for convenience
293
+ export type { BusinessFilterOptions, BusinessHook } from './hooks';
291
294
 
292
295
  // Re-export transaction types for convenience
293
296
  export type { TransactionQueryOptions, TransactionHook } from './hooks';
@@ -357,7 +360,7 @@ export {
357
360
  *
358
361
  * @see {@link https://github.com/eXplorins/pers-shared} - Shared library documentation
359
362
  */
360
- export * from '@explorins/pers-shared';
363
+ export * from '@explorins/pers-sdk';
361
364
 
362
365
  /**
363
366
  * Token utilities re-exported from token module
@@ -228,22 +228,13 @@ export const PersSDKProvider: React.FC<{
228
228
  if (!sdk) return;
229
229
 
230
230
  const unsubscribe = sdk.events.subscribe((event) => {
231
- if (event.domain !== 'authentication') return;
232
-
233
- // Session restored successfully - sync React state
234
- if (event.type === 'session_restored') {
235
- console.log('[PersSDK] Session restoration event received, syncing state...');
236
- // Read user from event details if available, otherwise fetch
237
- const userId = event.details?.userId;
238
- if (userId) {
239
- // User ID available, fetch user data
240
- sdk.users.getCurrentUser()
241
- .then(userData => {
242
- setAuthenticationState(userData, true);
243
- })
244
- .catch(error => {
245
- console.error('[PersSDK] Failed to sync restored session:', error);
246
- });
231
+ // Handle both session restoration and fresh login
232
+ if (event.type === 'session_restored' || event.type === 'login_success') {
233
+ const details = event.details as { user?: UserDTO | AdminDTO };
234
+
235
+ if (details?.user) {
236
+ console.log(`[PersSDK] ${event.type}: syncing user state`);
237
+ setAuthenticationState(details.user, true);
247
238
  }
248
239
  }
249
240
 
@@ -252,7 +243,7 @@ export const PersSDKProvider: React.FC<{
252
243
  console.log('[PersSDK] Authentication failed - clearing session');
253
244
  setAuthenticationState(null, false);
254
245
  }
255
- }, { domains: ['authentication'] });
246
+ }, { domains: ['authentication'], replay: true });
256
247
 
257
248
  return () => {
258
249
  unsubscribe();
@@ -2,19 +2,50 @@ import { Platform } from 'react-native';
2
2
  import { Buffer } from 'buffer';
3
3
  import { DPoPCryptoProvider, DPoPKeyPair } from '@explorins/pers-sdk/core';
4
4
 
5
- // Conditionally require quick-crypto for Native platforms only
6
- let crypto: any;
7
- if (Platform.OS !== 'web') {
5
+ // Lazy-loaded crypto module - initialized on first use to avoid native module timing issues
6
+ let _crypto: any = null;
7
+ let _cryptoInitialized = false;
8
+
9
+ /**
10
+ * Lazily load the crypto module.
11
+ *
12
+ * Supports customer pre-initialization via global.__QuickCrypto for environments
13
+ * where native module timing is unpredictable (e.g., certain React Native setups).
14
+ *
15
+ * @returns The crypto module or null if unavailable
16
+ */
17
+ function getCrypto(): any {
18
+ if (_cryptoInitialized) {
19
+ return _crypto;
20
+ }
21
+
22
+ _cryptoInitialized = true;
23
+
24
+ if (Platform.OS === 'web') {
25
+ // On Web, we shouldn't be using this provider (Core SDK has WebDPoPProvider)
26
+ _crypto = {
27
+ generateKeyPair: () => { throw new Error('ReactNativeDPoPProvider not supported on Web'); },
28
+ randomUUID: () => globalThis.crypto?.randomUUID?.() || `${Date.now()}-${Math.random()}`
29
+ };
30
+ return _crypto;
31
+ }
32
+
33
+ // Check for customer pre-initialized global (for native module timing issues)
34
+ if (typeof global !== 'undefined' && (global as any).__QuickCrypto) {
35
+ _crypto = (global as any).__QuickCrypto;
36
+ return _crypto;
37
+ }
38
+
39
+ // Standard lazy load
8
40
  try {
9
- crypto = require('react-native-quick-crypto').default || require('react-native-quick-crypto');
41
+ _crypto = require('react-native-quick-crypto').default || require('react-native-quick-crypto');
10
42
  } catch (e) {
11
- console.warn('ReactNativeDPoPProvider: Failed to load react-native-quick-crypto', e);
43
+ console.warn('ReactNativeDPoPProvider: Failed to load react-native-quick-crypto.', e);
44
+ console.warn('Tip: If you encounter timing issues, pre-initialize in index.js: global.__QuickCrypto = require("react-native-quick-crypto")');
45
+ _crypto = null;
12
46
  }
13
- } else {
14
- // on Web, we shouldn't be using this provider anyway (Core SDK has WebDPoPProvider)
15
- crypto = {
16
- generateKeyPair: () => { throw new Error('ReactNativeDPoPProvider not supported on Web'); }
17
- };
47
+
48
+ return _crypto;
18
49
  }
19
50
 
20
51
  export class ReactNativeDPoPProvider implements DPoPCryptoProvider {
@@ -25,6 +56,11 @@ export class ReactNativeDPoPProvider implements DPoPCryptoProvider {
25
56
  * Falls back to Node.js crypto API on iOS if needed.
26
57
  */
27
58
  async generateKeyPair(options?: { extractable?: boolean }): Promise<DPoPKeyPair> {
59
+ const crypto = getCrypto();
60
+ if (!crypto) {
61
+ throw new Error('Crypto module not available. Install react-native-quick-crypto or pre-initialize global.__QuickCrypto');
62
+ }
63
+
28
64
  // Try WebCrypto API first (works on both iOS and Android)
29
65
  if (crypto.subtle) {
30
66
  try {
@@ -62,6 +98,11 @@ export class ReactNativeDPoPProvider implements DPoPCryptoProvider {
62
98
  }
63
99
 
64
100
  async signProof(payload: Record<string, any>, keyPair: DPoPKeyPair): Promise<string> {
101
+ const crypto = getCrypto();
102
+ if (!crypto) {
103
+ throw new Error('Crypto module not available for signing');
104
+ }
105
+
65
106
  // 1. Construct Header
66
107
  const header = {
67
108
  typ: 'dpop+jwt',
@@ -73,7 +114,7 @@ export class ReactNativeDPoPProvider implements DPoPCryptoProvider {
73
114
  const finalPayload = {
74
115
  ...payload,
75
116
  iat: payload.iat || Math.floor(Date.now() / 1000),
76
- jti: payload.jti || crypto.randomUUID()
117
+ jti: payload.jti || (crypto.randomUUID?.() || `${Date.now()}-${Math.random()}`)
77
118
  };
78
119
 
79
120
  // 3. Encode
@@ -115,6 +156,11 @@ export class ReactNativeDPoPProvider implements DPoPCryptoProvider {
115
156
  }
116
157
 
117
158
  async hashAccessToken(accessToken: string): Promise<string> {
159
+ const crypto = getCrypto();
160
+ if (!crypto) {
161
+ throw new Error('Crypto module not available for hashing');
162
+ }
163
+
118
164
  // Try WebCrypto first
119
165
  if (crypto.subtle) {
120
166
  try {