toss-expo-sdk 0.1.2 → 1.0.1

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 (73) hide show
  1. package/README.md +368 -15
  2. package/lib/module/ble.js +59 -4
  3. package/lib/module/ble.js.map +1 -1
  4. package/lib/module/client/BLETransactionHandler.js +277 -0
  5. package/lib/module/client/BLETransactionHandler.js.map +1 -0
  6. package/lib/module/client/NonceAccountManager.js +364 -0
  7. package/lib/module/client/NonceAccountManager.js.map +1 -0
  8. package/lib/module/client/TossClient.js +1 -1
  9. package/lib/module/client/TossClient.js.map +1 -1
  10. package/lib/module/hooks/useOfflineBLETransactions.js +314 -0
  11. package/lib/module/hooks/useOfflineBLETransactions.js.map +1 -0
  12. package/lib/module/index.js +12 -8
  13. package/lib/module/index.js.map +1 -1
  14. package/lib/module/intent.js +129 -0
  15. package/lib/module/intent.js.map +1 -1
  16. package/lib/module/noise.js +175 -0
  17. package/lib/module/noise.js.map +1 -1
  18. package/lib/module/reconciliation.js +155 -0
  19. package/lib/module/reconciliation.js.map +1 -1
  20. package/lib/module/services/authService.js +164 -1
  21. package/lib/module/services/authService.js.map +1 -1
  22. package/lib/module/storage/secureStorage.js +102 -0
  23. package/lib/module/storage/secureStorage.js.map +1 -1
  24. package/lib/module/sync.js +25 -1
  25. package/lib/module/sync.js.map +1 -1
  26. package/lib/module/types/nonceAccount.js +2 -0
  27. package/lib/module/types/nonceAccount.js.map +1 -0
  28. package/lib/module/types/tossUser.js +16 -1
  29. package/lib/module/types/tossUser.js.map +1 -1
  30. package/lib/typescript/src/__tests__/solana-program-simple.test.d.ts +8 -0
  31. package/lib/typescript/src/__tests__/solana-program-simple.test.d.ts.map +1 -0
  32. package/lib/typescript/src/ble.d.ts +31 -2
  33. package/lib/typescript/src/ble.d.ts.map +1 -1
  34. package/lib/typescript/src/client/BLETransactionHandler.d.ts +98 -0
  35. package/lib/typescript/src/client/BLETransactionHandler.d.ts.map +1 -0
  36. package/lib/typescript/src/client/NonceAccountManager.d.ts +82 -0
  37. package/lib/typescript/src/client/NonceAccountManager.d.ts.map +1 -0
  38. package/lib/typescript/src/hooks/useOfflineBLETransactions.d.ts +91 -0
  39. package/lib/typescript/src/hooks/useOfflineBLETransactions.d.ts.map +1 -0
  40. package/lib/typescript/src/index.d.ts +9 -4
  41. package/lib/typescript/src/index.d.ts.map +1 -1
  42. package/lib/typescript/src/intent.d.ts +15 -0
  43. package/lib/typescript/src/intent.d.ts.map +1 -1
  44. package/lib/typescript/src/noise.d.ts +62 -0
  45. package/lib/typescript/src/noise.d.ts.map +1 -1
  46. package/lib/typescript/src/reconciliation.d.ts +6 -0
  47. package/lib/typescript/src/reconciliation.d.ts.map +1 -1
  48. package/lib/typescript/src/services/authService.d.ts +26 -1
  49. package/lib/typescript/src/services/authService.d.ts.map +1 -1
  50. package/lib/typescript/src/storage/secureStorage.d.ts +16 -0
  51. package/lib/typescript/src/storage/secureStorage.d.ts.map +1 -1
  52. package/lib/typescript/src/sync.d.ts +6 -1
  53. package/lib/typescript/src/sync.d.ts.map +1 -1
  54. package/lib/typescript/src/types/nonceAccount.d.ts +59 -0
  55. package/lib/typescript/src/types/nonceAccount.d.ts.map +1 -0
  56. package/lib/typescript/src/types/tossUser.d.ts +16 -0
  57. package/lib/typescript/src/types/tossUser.d.ts.map +1 -1
  58. package/package.json +1 -1
  59. package/src/__tests__/solana-program-simple.test.ts +256 -0
  60. package/src/ble.ts +105 -4
  61. package/src/client/BLETransactionHandler.ts +364 -0
  62. package/src/client/NonceAccountManager.ts +444 -0
  63. package/src/client/TossClient.ts +1 -1
  64. package/src/hooks/useOfflineBLETransactions.ts +438 -0
  65. package/src/index.tsx +40 -6
  66. package/src/intent.ts +166 -0
  67. package/src/noise.ts +238 -0
  68. package/src/reconciliation.ts +184 -0
  69. package/src/services/authService.ts +188 -1
  70. package/src/storage/secureStorage.ts +138 -0
  71. package/src/sync.ts +40 -0
  72. package/src/types/nonceAccount.ts +75 -0
  73. package/src/types/tossUser.ts +35 -2
@@ -0,0 +1,364 @@
1
+ import { Device } from 'react-native-ble-plx';
2
+ import type { SolanaIntent } from '../intent';
3
+ import type { OfflineTransaction } from '../types/nonceAccount';
4
+
5
+ /**
6
+ * BLE MTU Configuration for different device types
7
+ */
8
+ export interface BLEMTUConfig {
9
+ maxPayloadSize: number; // Actual data size (MTU - overhead)
10
+ chunkSize: number; // Size of each fragment
11
+ maxRetries: number; // Max retries per chunk
12
+ timeout: number; // Timeout in ms
13
+ }
14
+
15
+ /**
16
+ * Default MTU configurations
17
+ */
18
+ const DEFAULT_MTU_CONFIGS: Record<string, BLEMTUConfig> = {
19
+ android: {
20
+ maxPayloadSize: 512, // Typical Android BLE MTU
21
+ chunkSize: 480, // Conservative chunk size
22
+ maxRetries: 3,
23
+ timeout: 5000,
24
+ },
25
+ ios: {
26
+ maxPayloadSize: 512, // iOS BLE MTU
27
+ chunkSize: 480,
28
+ maxRetries: 3,
29
+ timeout: 5000,
30
+ },
31
+ };
32
+
33
+ /**
34
+ * Represents a fragmented message with header information
35
+ */
36
+ export interface BLEFragment {
37
+ messageId: string; // Unique identifier for the message
38
+ sequenceNumber: number; // Fragment index
39
+ totalFragments: number; // Total number of fragments
40
+ checksumValue: number; // CRC32 checksum of fragment
41
+ payload: Uint8Array; // Actual data
42
+ }
43
+
44
+ /**
45
+ * Represents a Noise-encrypted BLE message
46
+ */
47
+ export interface EncryptedBLEMessage {
48
+ version: number; // Protocol version
49
+ ciphertext: Uint8Array; // Encrypted payload
50
+ nonce: Uint8Array; // Encryption nonce
51
+ tag: Uint8Array; // Authentication tag
52
+ }
53
+
54
+ /**
55
+ * BLETransactionHandler
56
+ * Manages secure, fragmented BLE transmission of offline transactions
57
+ * with Noise Protocol encryption
58
+ */
59
+ export class BLETransactionHandler {
60
+ private mtuConfig!: BLEMTUConfig;
61
+ private fragmentCache: Map<string, BLEFragment[]> = new Map();
62
+ private messageIdMap: Map<string, OfflineTransaction | SolanaIntent> =
63
+ new Map();
64
+
65
+ constructor(platform: 'android' | 'ios' = 'android') {
66
+ this.mtuConfig =
67
+ DEFAULT_MTU_CONFIGS[platform] || DEFAULT_MTU_CONFIGS.android!;
68
+ }
69
+
70
+ /**
71
+ * Fragment a large transaction/intent into BLE-safe chunks
72
+ * Respects MTU limitations and adds framing information
73
+ */
74
+ fragmentTransaction(
75
+ transaction: OfflineTransaction | SolanaIntent,
76
+ isIntent: boolean = false
77
+ ): BLEFragment[] {
78
+ const payload = Buffer.from(JSON.stringify(transaction), 'utf-8');
79
+ const messageId = `msg_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
80
+ const totalFragments = Math.ceil(payload.length / this.mtuConfig.chunkSize);
81
+ const fragments: BLEFragment[] = [];
82
+
83
+ for (let i = 0; i < totalFragments; i++) {
84
+ const start = i * this.mtuConfig.chunkSize;
85
+ const end = Math.min(start + this.mtuConfig.chunkSize, payload.length);
86
+ const chunk = payload.slice(start, end);
87
+
88
+ const fragment: BLEFragment = {
89
+ messageId,
90
+ sequenceNumber: i,
91
+ totalFragments,
92
+ checksumValue: this.calculateCRC32(chunk),
93
+ payload: chunk,
94
+ };
95
+
96
+ fragments.push(fragment);
97
+ }
98
+
99
+ // Store fragments for reassembly on receiver end
100
+ this.fragmentCache.set(messageId, fragments);
101
+ this.messageIdMap.set(
102
+ messageId,
103
+ isIntent
104
+ ? (transaction as SolanaIntent)
105
+ : (transaction as OfflineTransaction)
106
+ );
107
+
108
+ return fragments;
109
+ }
110
+
111
+ /**
112
+ * Prepare encrypted BLE message for transmission
113
+ * Uses Noise Protocol for end-to-end encryption
114
+ */
115
+ async prepareEncryptedMessage(
116
+ fragment: BLEFragment,
117
+ noiseEncryptFn: (data: Uint8Array) => Promise<EncryptedBLEMessage>
118
+ ): Promise<EncryptedBLEMessage> {
119
+ // Serialize fragment
120
+ const fragmentData = this.serializeFragment(fragment);
121
+
122
+ // Encrypt using Noise Protocol
123
+ const encrypted = await noiseEncryptFn(fragmentData);
124
+
125
+ return encrypted;
126
+ }
127
+
128
+ /**
129
+ * Send fragmented transaction over BLE with encryption
130
+ * Handles retries and verification
131
+ */
132
+ async sendFragmentedTransactionBLE(
133
+ device: Device,
134
+ transaction: OfflineTransaction | SolanaIntent,
135
+ sendFn: (
136
+ deviceId: string,
137
+ characteristicUUID: string,
138
+ data: Buffer
139
+ ) => Promise<void>,
140
+ noiseEncryptFn?: (data: Uint8Array) => Promise<EncryptedBLEMessage>,
141
+ isIntent: boolean = false
142
+ ): Promise<{
143
+ success: boolean;
144
+ sentFragments: number;
145
+ failedFragments: number[];
146
+ messageId: string;
147
+ }> {
148
+ const fragments = this.fragmentTransaction(transaction, isIntent);
149
+ const messageId = fragments[0]?.messageId;
150
+ const failedFragments: number[] = [];
151
+
152
+ if (!messageId) {
153
+ throw new Error('Failed to generate message ID for transaction');
154
+ }
155
+
156
+ const CHARACTERISTIC_UUID = '0000ff02-0000-1000-8000-00805f9b34fb'; // Intent characteristic
157
+
158
+ for (const fragment of fragments) {
159
+ let retries = 0;
160
+ let sent = false;
161
+
162
+ while (retries < this.mtuConfig.maxRetries && !sent) {
163
+ try {
164
+ let messageData: Buffer;
165
+
166
+ if (noiseEncryptFn) {
167
+ // Encrypt fragment using Noise Protocol
168
+ const encrypted = await this.prepareEncryptedMessage(
169
+ fragment,
170
+ noiseEncryptFn
171
+ );
172
+ messageData = Buffer.from(JSON.stringify(encrypted), 'utf-8');
173
+ } else {
174
+ // Send unencrypted (not recommended)
175
+ messageData = Buffer.from(JSON.stringify(fragment), 'utf-8');
176
+ }
177
+
178
+ // Send via BLE
179
+ await sendFn(device.id, CHARACTERISTIC_UUID, messageData);
180
+
181
+ sent = true;
182
+ } catch (error) {
183
+ retries++;
184
+ console.warn(
185
+ `Failed to send fragment ${fragment.sequenceNumber}, retry ${retries}:`,
186
+ error
187
+ );
188
+
189
+ if (retries >= this.mtuConfig.maxRetries) {
190
+ failedFragments.push(fragment.sequenceNumber);
191
+ } else {
192
+ // Exponential backoff
193
+ await this.delay(Math.pow(2, retries) * 100);
194
+ }
195
+ }
196
+ }
197
+ }
198
+
199
+ return {
200
+ success: failedFragments.length === 0,
201
+ sentFragments: fragments.length - failedFragments.length,
202
+ failedFragments,
203
+ messageId,
204
+ };
205
+ }
206
+
207
+ /**
208
+ * Receive and reassemble fragmented messages
209
+ */
210
+ async receiveFragmentedMessage(
211
+ fragment: BLEFragment,
212
+ _noiseDecryptFn?: (encrypted: EncryptedBLEMessage) => Promise<Uint8Array>
213
+ ): Promise<{
214
+ complete: boolean;
215
+ transaction?: OfflineTransaction | SolanaIntent;
216
+ progress: {
217
+ received: number;
218
+ total: number;
219
+ };
220
+ }> {
221
+ const messageId = fragment.messageId;
222
+
223
+ // Initialize or retrieve fragment cache
224
+ if (!this.fragmentCache.has(messageId)) {
225
+ this.fragmentCache.set(messageId, []);
226
+ }
227
+
228
+ const cachedFragments = this.fragmentCache.get(messageId)!;
229
+ cachedFragments[fragment.sequenceNumber] = fragment;
230
+
231
+ const progress = {
232
+ received: cachedFragments.filter((f) => f !== undefined).length,
233
+ total: fragment.totalFragments,
234
+ };
235
+
236
+ // Check if all fragments received
237
+ if (progress.received < fragment.totalFragments) {
238
+ return {
239
+ complete: false,
240
+ progress,
241
+ };
242
+ }
243
+
244
+ // Reassemble message
245
+ const reassembled = this.reassembleMessage(cachedFragments);
246
+
247
+ if (!reassembled) {
248
+ return {
249
+ complete: false,
250
+ progress,
251
+ };
252
+ }
253
+
254
+ try {
255
+ // Parse transaction
256
+ const transactionData = JSON.parse(reassembled);
257
+ const transaction: OfflineTransaction | SolanaIntent = transactionData;
258
+
259
+ // Cleanup cache
260
+ this.fragmentCache.delete(messageId);
261
+ this.messageIdMap.delete(messageId);
262
+
263
+ return {
264
+ complete: true,
265
+ transaction,
266
+ progress,
267
+ };
268
+ } catch (error) {
269
+ console.error('Failed to parse reassembled message:', error);
270
+ return {
271
+ complete: false,
272
+ progress,
273
+ };
274
+ }
275
+ }
276
+
277
+ /**
278
+ * Reassemble fragments into original message
279
+ */
280
+ private reassembleMessage(fragments: BLEFragment[]): string | null {
281
+ try {
282
+ // Sort by sequence number
283
+ const sorted = fragments.sort(
284
+ (a, b) => a.sequenceNumber - b.sequenceNumber
285
+ );
286
+
287
+ // Verify all fragments present and checksums
288
+ for (const fragment of sorted) {
289
+ const calculatedChecksum = this.calculateCRC32(fragment.payload);
290
+ if (calculatedChecksum !== fragment.checksumValue) {
291
+ console.warn(
292
+ `Checksum mismatch for fragment ${fragment.sequenceNumber}`
293
+ );
294
+ return null;
295
+ }
296
+ }
297
+
298
+ // Concatenate payloads
299
+ const combined = Buffer.concat(sorted.map((f) => Buffer.from(f.payload)));
300
+ return combined.toString('utf-8');
301
+ } catch (error) {
302
+ console.error('Failed to reassemble message:', error);
303
+ return null;
304
+ }
305
+ }
306
+
307
+ /**
308
+ * Serialize BLE fragment for transmission
309
+ */
310
+ private serializeFragment(fragment: BLEFragment): Uint8Array {
311
+ const data = {
312
+ messageId: fragment.messageId,
313
+ sequenceNumber: fragment.sequenceNumber,
314
+ totalFragments: fragment.totalFragments,
315
+ checksumValue: fragment.checksumValue,
316
+ payload: Array.from(fragment.payload),
317
+ };
318
+
319
+ return new Uint8Array(Buffer.from(JSON.stringify(data), 'utf-8'));
320
+ }
321
+
322
+ /**
323
+ * Calculate CRC32 checksum for fragment verification
324
+ */
325
+ private calculateCRC32(data: Uint8Array | Buffer): number {
326
+ let crc = 0xffffffff;
327
+
328
+ for (let i = 0; i < data.length; i++) {
329
+ const byte = data[i];
330
+ if (byte !== undefined) {
331
+ crc = crc ^ byte;
332
+ for (let j = 0; j < 8; j++) {
333
+ crc = (crc >>> 1) ^ (crc & 1 ? 0xedb88320 : 0);
334
+ }
335
+ }
336
+ }
337
+
338
+ return (crc ^ 0xffffffff) >>> 0;
339
+ }
340
+
341
+ /**
342
+ * Delay utility for retries
343
+ */
344
+ private delay(ms: number): Promise<void> {
345
+ return new Promise((resolve) => setTimeout(resolve, ms));
346
+ }
347
+
348
+ /**
349
+ * Get MTU configuration
350
+ */
351
+ getMTUConfig(): BLEMTUConfig {
352
+ return this.mtuConfig;
353
+ }
354
+
355
+ /**
356
+ * Set custom MTU configuration
357
+ */
358
+ setMTUConfig(config: Partial<BLEMTUConfig>): void {
359
+ this.mtuConfig = {
360
+ ...this.mtuConfig,
361
+ ...config,
362
+ };
363
+ }
364
+ }