@oobit/react-native-sdk 1.0.3 → 1.0.5

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/src/config.ts CHANGED
@@ -3,14 +3,14 @@
3
3
  * Environment-based configuration for the widget URL
4
4
  */
5
5
 
6
- import { WidgetEnvironment } from './types';
6
+ import { WidgetEnvironment } from "./types";
7
7
 
8
8
  /**
9
9
  * Widget URLs by environment
10
10
  */
11
11
  export const WIDGET_URLS = {
12
12
  development: "https://oobit-widget-dev.web.app",
13
- production: "https://widget.oobit.com",
13
+ production: "https://oobit-widget.web.app",
14
14
  } as const;
15
15
 
16
16
  /**
@@ -18,6 +18,8 @@ export const WIDGET_URLS = {
18
18
  * @param environment - The environment to use (defaults to 'production')
19
19
  * @returns The widget URL for the specified environment
20
20
  */
21
- export function getWidgetUrl(environment: WidgetEnvironment = 'production'): string {
21
+ export function getWidgetUrl(
22
+ environment: WidgetEnvironment = "production"
23
+ ): string {
22
24
  return WIDGET_URLS[environment];
23
25
  }
@@ -0,0 +1,160 @@
1
+ /**
2
+ * Crypto Utilities for Card Details Session Generation
3
+ *
4
+ * CRITICAL: This implementation MUST match Android's PublicKeyCryptoHelper.kt exactly:
5
+ * - RSA Algorithm: RSA/ECB/OAEPWithSHA-1AndMGF1Padding
6
+ * - Secret Key: UUID without dashes (32 hex chars = 16 bytes = 128-bit)
7
+ * - Input to RSA: secretKey as Base64 string
8
+ * - Output: encrypted sessionId as Base64 string
9
+ *
10
+ * Uses react-native-quick-crypto for native RSA-OAEP encryption
11
+ * (OpenSSL on Android, CommonCrypto on iOS)
12
+ *
13
+ * @requires react-native-quick-crypto - npm install react-native-quick-crypto
14
+ */
15
+
16
+ import QuickCrypto from 'react-native-quick-crypto';
17
+
18
+ /**
19
+ * Generates a random hex key matching Android's generateRandomHexKey()
20
+ *
21
+ * Creates a UUID v4 without dashes = 32 hex characters = 16 bytes = 128-bit key
22
+ * This matches the format used in Android's PublicKeyCryptoHelper
23
+ *
24
+ * @returns 32-character hexadecimal string
25
+ */
26
+ export function generateRandomHexKey(): string {
27
+ // Generate UUID v4 pattern and remove dashes
28
+ // This matches Android's java.util.UUID.randomUUID().toString().replace("-", "")
29
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
30
+ .replace(/[xy]/g, (c) => {
31
+ const r = (Math.random() * 16) | 0;
32
+ const v = c === 'x' ? r : (r & 0x3) | 0x8;
33
+ return v.toString(16);
34
+ })
35
+ .replace(/-/g, '');
36
+ }
37
+
38
+ /**
39
+ * Converts a hex string to Base64 encoding
40
+ * Matches Android's hexToBase64() method
41
+ *
42
+ * @param hexString - Hexadecimal string (must have even length)
43
+ * @returns Base64 encoded string
44
+ */
45
+ export function hexToBase64(hexString: string): string {
46
+ // Convert hex string to binary string
47
+ let binaryString = '';
48
+ for (let i = 0; i < hexString.length; i += 2) {
49
+ const byte = parseInt(hexString.substring(i, i + 2), 16);
50
+ binaryString += String.fromCharCode(byte);
51
+ }
52
+
53
+ // Use btoa for Base64 encoding (available in React Native with Hermes)
54
+ if (typeof btoa === 'function') {
55
+ return btoa(binaryString);
56
+ }
57
+
58
+ // Fallback: manual Base64 encoding
59
+ const base64Chars =
60
+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
61
+ let result = '';
62
+ let i = 0;
63
+
64
+ while (i < binaryString.length) {
65
+ const a = binaryString.charCodeAt(i++);
66
+ const b = binaryString.charCodeAt(i++);
67
+ const c = binaryString.charCodeAt(i++);
68
+
69
+ const triplet = (a << 16) | ((b || 0) << 8) | (c || 0);
70
+
71
+ result +=
72
+ base64Chars[(triplet >> 18) & 0x3f] +
73
+ base64Chars[(triplet >> 12) & 0x3f] +
74
+ (isNaN(b) ? '=' : base64Chars[(triplet >> 6) & 0x3f]) +
75
+ (isNaN(c) ? '=' : base64Chars[triplet & 0x3f]);
76
+ }
77
+
78
+ return result;
79
+ }
80
+
81
+ /**
82
+ * Encrypts data using RSA with OAEP-SHA1 padding (native implementation)
83
+ *
84
+ * MUST match Android's RSA/ECB/OAEPWithSHA-1AndMGF1Padding algorithm
85
+ * Uses native crypto (OpenSSL on Android, CommonCrypto on iOS)
86
+ *
87
+ * @param data - Plain text data to encrypt (Base64 string of the secret key)
88
+ * @param publicKeyPem - RSA public key in PEM format
89
+ * @returns Encrypted data as Base64 string
90
+ * @throws Error if encryption fails
91
+ */
92
+ export function encryptWithRSA(data: string, publicKeyPem: string): string {
93
+ try {
94
+ // Convert string to Buffer
95
+ const dataBuffer = Buffer.from(data, 'utf8');
96
+
97
+ // Encrypt using RSA-OAEP with SHA-1 (matches Android's OAEPWithSHA-1AndMGF1Padding)
98
+ const encrypted = QuickCrypto.publicEncrypt(
99
+ {
100
+ key: publicKeyPem,
101
+ padding: QuickCrypto.constants.RSA_PKCS1_OAEP_PADDING,
102
+ oaepHash: 'sha1',
103
+ },
104
+ dataBuffer
105
+ );
106
+
107
+ // Return as Base64 string
108
+ return encrypted.toString('base64');
109
+ } catch (error) {
110
+ console.error('[CryptoUtils] RSA encryption failed:', error);
111
+ throw new Error('Failed to encrypt session data');
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Generates session credentials for card details API request
117
+ *
118
+ * This is the main function that creates the cryptographically linked
119
+ * sessionId + secretKey pair, matching Android's PublicKeyCryptoHelper.generateSessionId()
120
+ *
121
+ * Flow:
122
+ * 1. Generate random secretKeyHex (UUID without dashes)
123
+ * 2. Convert secretKeyHex to Base64
124
+ * 3. Encrypt Base64 key with RSA public key → sessionId
125
+ * 4. Return both secretKeyHex (for client-side decryption) and sessionId (for API header)
126
+ *
127
+ * @param publicKeyPem - RSA public key in PEM format (from widget)
128
+ * @returns Object containing:
129
+ * - secretKeyHex: Keep on device for AES-GCM decryption (32 hex chars)
130
+ * - sessionId: Send to API as SessionId header (RSA-encrypted, Base64)
131
+ * @throws Error if key generation or encryption fails
132
+ */
133
+ export function generateSessionCredentials(publicKeyPem: string): SessionCredentials {
134
+ // Validate public key
135
+ if (!publicKeyPem || !publicKeyPem.includes('BEGIN PUBLIC KEY')) {
136
+ throw new Error('Invalid RSA public key format');
137
+ }
138
+
139
+ // Step 1: Generate random secret key (matches Android's generateRandomHexKey)
140
+ const secretKeyHex = generateRandomHexKey();
141
+
142
+ // Step 2: Convert to Base64 (matches Android's hexToBase64)
143
+ const secretKeyBase64 = hexToBase64(secretKeyHex);
144
+
145
+ // Step 3: Encrypt with RSA public key (matches Android's encryptWithPublicKey)
146
+ const sessionId = encryptWithRSA(secretKeyBase64, publicKeyPem);
147
+
148
+ console.log('[CryptoUtils] Session credentials generated successfully');
149
+
150
+ return {
151
+ secretKeyHex,
152
+ sessionId,
153
+ };
154
+ }
155
+
156
+ // Export types for type safety
157
+ export interface SessionCredentials {
158
+ secretKeyHex: string;
159
+ sessionId: string;
160
+ }
package/src/index.ts CHANGED
@@ -23,6 +23,7 @@ export type {
23
23
  WidgetErrorMessage,
24
24
  WidgetCloseMessage,
25
25
  TransactionRequestedMessage,
26
+ RequestCardDetailsSessionMessage,
26
27
 
27
28
  // Native → Widget message types
28
29
  NativeMessageType,
@@ -31,6 +32,8 @@ export type {
31
32
  NativeNavigateBackMessage,
32
33
  NativePlatformInfoMessage,
33
34
  NativeWalletOpenedMessage,
35
+ NativeCardDetailsSessionMessage,
36
+ NativeBiometricFailedMessage,
34
37
  } from './types';
35
38
 
36
39
  // Export constants
@@ -39,3 +42,20 @@ export { WIDGET_URLS, getWidgetUrl } from './config';
39
42
 
40
43
  // Export wallet utilities
41
44
  export { openNativeWallet, isWalletAvailable } from './walletUtils';
45
+
46
+ // Export biometric utilities
47
+ export {
48
+ authenticateWithBiometrics,
49
+ isBiometricAvailable,
50
+ getBiometryTypeLabel,
51
+ BiometryTypes,
52
+ } from './biometricUtils';
53
+ export type { BiometricResult, BiometricAvailability } from './biometricUtils';
54
+
55
+ // Export crypto utilities (for advanced usage)
56
+ export {
57
+ generateSessionCredentials,
58
+ generateRandomHexKey,
59
+ hexToBase64,
60
+ } from './cryptoUtils';
61
+ export type { SessionCredentials } from './cryptoUtils';
package/src/types.ts CHANGED
@@ -15,36 +15,39 @@ export interface DepositToken {
15
15
  }
16
16
 
17
17
  export type WidgetMessageType =
18
- | 'widget:ready'
19
- | 'widget:open-wallet'
20
- | 'widget:card-created'
21
- | 'widget:error'
22
- | 'widget:close'
23
- | 'widget:transaction-requested'
24
- | 'widget:token-expired';
18
+ | "widget:ready"
19
+ | "widget:open-wallet"
20
+ | "widget:card-created"
21
+ | "widget:error"
22
+ | "widget:close"
23
+ | "widget:transaction-requested"
24
+ | "widget:token-expired"
25
+ | "widget:request-card-details-session";
25
26
 
26
27
  /**
27
28
  * Native Message Types
28
29
  * Messages sent from the native app to the web widget
29
30
  */
30
31
  export type NativeMessageType =
31
- | 'native:platform-info'
32
- | 'native:wallet-opened'
33
- | 'native:back-pressed'
34
- | 'native:navigate-back';
32
+ | "native:platform-info"
33
+ | "native:wallet-opened"
34
+ | "native:back-pressed"
35
+ | "native:navigate-back"
36
+ | "native:card-details-session"
37
+ | "native:biometric-failed";
35
38
 
36
39
  export interface NativeBackPressedMessage {
37
- type: 'native:back-pressed';
40
+ type: "native:back-pressed";
38
41
  timestamp: number;
39
42
  }
40
43
 
41
44
  export interface NativeNavigateBackMessage {
42
- type: 'native:navigate-back';
45
+ type: "native:navigate-back";
43
46
  timestamp: number;
44
47
  }
45
48
 
46
49
  export interface NativePlatformInfoMessage {
47
- type: 'native:platform-info';
50
+ type: "native:platform-info";
48
51
  payload: {
49
52
  platform: string;
50
53
  walletAvailable: boolean;
@@ -52,17 +55,35 @@ export interface NativePlatformInfoMessage {
52
55
  }
53
56
 
54
57
  export interface NativeWalletOpenedMessage {
55
- type: 'native:wallet-opened';
58
+ type: "native:wallet-opened";
56
59
  payload: {
57
60
  success: boolean;
58
61
  };
59
62
  }
60
63
 
64
+ export interface NativeCardDetailsSessionMessage {
65
+ type: "native:card-details-session";
66
+ payload: {
67
+ sessionId: string; // RSA-encrypted session ID (Base64)
68
+ secretKey: string; // Secret key in hex format (32 chars)
69
+ };
70
+ }
71
+
72
+ export interface NativeBiometricFailedMessage {
73
+ type: "native:biometric-failed";
74
+ payload: {
75
+ reason: "cancelled" | "failed" | "not_available" | "not_enrolled";
76
+ message?: string;
77
+ };
78
+ }
79
+
61
80
  export type NativeMessage =
62
81
  | NativeBackPressedMessage
63
82
  | NativeNavigateBackMessage
64
83
  | NativePlatformInfoMessage
65
- | NativeWalletOpenedMessage;
84
+ | NativeWalletOpenedMessage
85
+ | NativeCardDetailsSessionMessage
86
+ | NativeBiometricFailedMessage;
66
87
 
67
88
  export interface BaseWidgetMessage {
68
89
  type: WidgetMessageType;
@@ -70,27 +91,27 @@ export interface BaseWidgetMessage {
70
91
  }
71
92
 
72
93
  export interface WidgetReadyMessage extends BaseWidgetMessage {
73
- type: 'widget:ready';
94
+ type: "widget:ready";
74
95
  }
75
96
 
76
97
  export interface OpenWalletMessage extends BaseWidgetMessage {
77
- type: 'widget:open-wallet';
98
+ type: "widget:open-wallet";
78
99
  payload: {
79
- platform: 'ios' | 'android';
100
+ platform: "ios" | "android";
80
101
  };
81
102
  }
82
103
 
83
104
  export interface CardCreatedMessage extends BaseWidgetMessage {
84
- type: 'widget:card-created';
105
+ type: "widget:card-created";
85
106
  payload: {
86
107
  cardId: string;
87
- cardType: 'virtual' | 'physical';
108
+ cardType: "virtual" | "physical";
88
109
  last4: string;
89
110
  };
90
111
  }
91
112
 
92
113
  export interface WidgetErrorMessage extends BaseWidgetMessage {
93
- type: 'widget:error';
114
+ type: "widget:error";
94
115
  payload: {
95
116
  code: string;
96
117
  message: string;
@@ -98,11 +119,11 @@ export interface WidgetErrorMessage extends BaseWidgetMessage {
98
119
  }
99
120
 
100
121
  export interface WidgetCloseMessage extends BaseWidgetMessage {
101
- type: 'widget:close';
122
+ type: "widget:close";
102
123
  }
103
124
 
104
125
  export interface TransactionRequestedMessage extends BaseWidgetMessage {
105
- type: 'widget:transaction-requested';
126
+ type: "widget:transaction-requested";
106
127
  payload: {
107
128
  token: DepositToken;
108
129
  cryptoAmount: string;
@@ -112,12 +133,19 @@ export interface TransactionRequestedMessage extends BaseWidgetMessage {
112
133
  }
113
134
 
114
135
  export interface TokenExpiredMessage extends BaseWidgetMessage {
115
- type: 'widget:token-expired';
136
+ type: "widget:token-expired";
116
137
  payload?: {
117
138
  reason?: string;
118
139
  };
119
140
  }
120
141
 
142
+ export interface RequestCardDetailsSessionMessage extends BaseWidgetMessage {
143
+ type: "widget:request-card-details-session";
144
+ payload: {
145
+ publicKey: string; // RSA public key in PEM format
146
+ };
147
+ }
148
+
121
149
  export type WidgetMessage =
122
150
  | WidgetReadyMessage
123
151
  | OpenWalletMessage
@@ -125,14 +153,15 @@ export type WidgetMessage =
125
153
  | WidgetErrorMessage
126
154
  | WidgetCloseMessage
127
155
  | TransactionRequestedMessage
128
- | TokenExpiredMessage;
156
+ | TokenExpiredMessage
157
+ | RequestCardDetailsSessionMessage;
129
158
 
130
159
  /**
131
160
  * Widget environment configuration
132
161
  * - 'development': Uses development/staging widget URL
133
162
  * - 'production': Uses production widget URL
134
163
  */
135
- export type WidgetEnvironment = 'development' | 'production';
164
+ export type WidgetEnvironment = "development" | "production";
136
165
 
137
166
  /**
138
167
  * SDK Configuration
@@ -151,7 +180,12 @@ export interface WidgetSDKConfig {
151
180
  onCardCreated?: (cardId: string, cardType: string, last4: string) => void;
152
181
  onError?: (code: string, message: string) => void;
153
182
  onClose?: () => void;
154
- onTransactionRequested?: (token: DepositToken, cryptoAmount: string, depositAddress: string, depositAddressTag: string | null) => void;
183
+ onTransactionRequested?: (
184
+ token: DepositToken,
185
+ cryptoAmount: string,
186
+ depositAddress: string,
187
+ depositAddressTag: string | null
188
+ ) => void;
155
189
  }
156
190
 
157
191
  /**
@@ -159,12 +193,12 @@ export interface WidgetSDKConfig {
159
193
  */
160
194
  export const WALLET_URLS = {
161
195
  ios: {
162
- passkit: 'shoebox://', // Apple Wallet deep link
163
- fallback: 'https://wallet.apple.com',
196
+ passkit: "shoebox://", // Apple Wallet deep link
197
+ fallback: "https://wallet.apple.com",
164
198
  },
165
199
  android: {
166
- googlePay: 'https://pay.google.com/gp/w/home/wallet',
167
- fallback: 'https://wallet.google.com',
200
+ googlePay: "https://pay.google.com/gp/w/home/wallet",
201
+ fallback: "https://wallet.google.com",
168
202
  },
169
203
  } as const;
170
204
 
@@ -177,17 +211,19 @@ export const WALLET_URLS = {
177
211
  */
178
212
  export const MessageTypes = {
179
213
  // Widget → Native
180
- READY: 'widget:ready',
181
- OPEN_WALLET: 'widget:open-wallet',
182
- CARD_CREATED: 'widget:card-created',
183
- ERROR: 'widget:error',
184
- CLOSE: 'widget:close',
185
- TRANSACTION_REQUESTED: 'widget:transaction-requested',
186
- TOKEN_EXPIRED: 'widget:token-expired',
214
+ READY: "widget:ready",
215
+ OPEN_WALLET: "widget:open-wallet",
216
+ CARD_CREATED: "widget:card-created",
217
+ ERROR: "widget:error",
218
+ CLOSE: "widget:close",
219
+ TRANSACTION_REQUESTED: "widget:transaction-requested",
220
+ TOKEN_EXPIRED: "widget:token-expired",
221
+ REQUEST_CARD_DETAILS_SESSION: "widget:request-card-details-session",
187
222
  // Native → Widget
188
- PLATFORM_INFO: 'native:platform-info',
189
- WALLET_OPENED: 'native:wallet-opened',
190
- BACK_PRESSED: 'native:back-pressed',
191
- NAVIGATE_BACK: 'native:navigate-back',
223
+ PLATFORM_INFO: "native:platform-info",
224
+ WALLET_OPENED: "native:wallet-opened",
225
+ BACK_PRESSED: "native:back-pressed",
226
+ NAVIGATE_BACK: "native:navigate-back",
227
+ CARD_DETAILS_SESSION: "native:card-details-session",
228
+ BIOMETRIC_FAILED: "native:biometric-failed",
192
229
  } as const;
193
-