@explorins/pers-sdk-react-native 1.5.8 → 1.5.11

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@explorins/pers-sdk-react-native",
3
- "version": "1.5.8",
3
+ "version": "1.5.11",
4
4
  "description": "React Native SDK for PERS Platform - Tourism Loyalty System with Manager Pattern Architecture",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -142,20 +142,55 @@ export const useTransactionSigner = () => {
142
142
  if (isInitialized && isAuthenticated && user && !isSignerInitialized && sdk && authProvider) {
143
143
  console.log('[useTransactionSigner] Auto-initializing PERS transaction signer...');
144
144
 
145
- // Get configuration from the PERS SDK
146
- const sdkConfig = (sdk as any).config || {};
147
- console.log('[useTransactionSigner] SDK config:', sdkConfig);
148
- const apiProjectKey = sdkConfig.apiProjectKey || 'demo-project-key';
149
- console.log('[useTransactionSigner] Extracted API project key:', apiProjectKey);
150
- const tenantId = sdkConfig.tenantId || 'vq-demo';
145
+ // Async auto-initialization function
146
+ const autoInitialize = async () => {
147
+ try {
148
+ // Get configuration from the PERS SDK
149
+ const sdkConfig = (sdk as any).config || {};
150
+ console.log('[useTransactionSigner] SDK config:', sdkConfig);
151
+
152
+ // Try to extract tenantId from JWT token if available
153
+ let tenantIdFromJWT: string | undefined;
154
+ if (authProvider.getToken) {
155
+ try {
156
+ const token = await authProvider.getToken();
157
+ if (token) {
158
+ // Decode JWT to get tenant information
159
+ const tokenParts = token.split('.');
160
+ if (tokenParts.length >= 2) {
161
+ const payload = JSON.parse(atob(tokenParts[1]));
162
+ console.log('[useTransactionSigner] JWT payload decoded:', payload);
163
+
164
+ // Look for tenant ID in various possible fields
165
+ tenantIdFromJWT = payload.tenantId || payload.tenant_id || payload.tenant;
166
+ if (tenantIdFromJWT) {
167
+ console.log('[useTransactionSigner] Found tenant ID in JWT:', tenantIdFromJWT);
168
+ }
169
+ }
170
+ }
171
+ } catch (e) {
172
+ console.warn('[useTransactionSigner] Failed to decode JWT for tenant ID:', e);
173
+ }
174
+ }
175
+
176
+ // For React Native, we should get tenantId from JWT, then user/auth context, then config
177
+ const userTenantId = (user as any).tenantId || (user as any).tenant_id;
178
+ const configTenantId = sdkConfig.tenantId || 'vq-demo';
179
+ const tenantId = tenantIdFromJWT || userTenantId || configTenantId;
180
+
181
+ console.log('[useTransactionSigner] Extracted tenant ID:', tenantId, 'from JWT:', tenantIdFromJWT, 'user:', userTenantId, 'or config:', configTenantId);
182
+
183
+ // Don't use apiProjectKey since we'll get it via tenant initialization
184
+ await initializeSigner({
185
+ tenantId: tenantId,
186
+ ethersProviderUrl: DEFAULT_ETHERS_PROVIDER
187
+ });
188
+ } catch (error) {
189
+ console.error('[useTransactionSigner] Auto-initialization failed:', error);
190
+ }
191
+ };
151
192
 
152
- initializeSigner({
153
- tenantId: tenantId,
154
- projectKey: apiProjectKey,
155
- ethersProviderUrl: DEFAULT_ETHERS_PROVIDER
156
- }).catch((error) => {
157
- console.error('[useTransactionSigner] Auto-initialization failed:', error);
158
- });
193
+ autoInitialize();
159
194
  }
160
195
  }, [isInitialized, isAuthenticated, user, isSignerInitialized, sdk, authProvider]);
161
196
 
@@ -188,7 +223,7 @@ export const useTransactionSigner = () => {
188
223
  try {
189
224
  console.log('[useTransactionSigner] Initializing PERS transaction signer...');
190
225
 
191
- // Configure the PERS service with the project key before creating SDK
226
+ // Configure the PERS service before creating SDK
192
227
  if (config?.projectKey) {
193
228
  console.log('[useTransactionSigner] Configuring PERS service with project key:', config.projectKey);
194
229
  try {
@@ -206,6 +241,37 @@ export const useTransactionSigner = () => {
206
241
  } catch (configError) {
207
242
  console.error('[useTransactionSigner] Failed to configure PERS service:', configError);
208
243
  }
244
+ } else if (config?.tenantId) {
245
+ console.log('[useTransactionSigner] No project key provided, will initialize tenant with tenantId:', config.tenantId);
246
+ try {
247
+ // Import and initialize tenant environment to get project key
248
+ const { PersService } = await import('@explorins/pers-signer/react-native');
249
+ console.log('[useTransactionSigner] Initializing tenant for tenantId:', config.tenantId);
250
+
251
+ // We need to set the auth token first for tenant initialization
252
+ if (authProvider) {
253
+ try {
254
+ const token = await authProvider.getToken();
255
+ if (token) {
256
+ console.log('[useTransactionSigner] Setting auth token for tenant initialization');
257
+ (PersService as any).configure({
258
+ token: token
259
+ });
260
+ }
261
+ } catch (tokenError) {
262
+ console.warn('[useTransactionSigner] Could not get auth token for tenant initialization:', tokenError);
263
+ }
264
+ }
265
+
266
+ await (PersService as any).initializeTenant(config.tenantId);
267
+ console.log('[useTransactionSigner] Tenant initialized successfully');
268
+
269
+ // Verify configuration
270
+ const persConfig = (PersService as any).getConfig();
271
+ console.log('[useTransactionSigner] PERS service config after tenant initialization:', persConfig);
272
+ } catch (configError) {
273
+ console.error('[useTransactionSigner] Failed to initialize tenant:', configError);
274
+ }
209
275
  }
210
276
 
211
277
  const signerSDK = await createPersSignerSDK({
@@ -221,7 +287,7 @@ export const useTransactionSigner = () => {
221
287
  console.error('[useTransactionSigner] Failed to initialize transaction signer:', error);
222
288
  throw new Error(`Signer initialization failed: ${error}`);
223
289
  }
224
- }, []);
290
+ }, [authProvider]);
225
291
 
226
292
  /**
227
293
  * Sign a blockchain transaction using WebAuthn authentication