toss-expo-sdk 1.0.1 → 1.0.4
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/README.md +68 -65
- package/lib/module/client/NonceAccountManager.js +2 -2
- package/lib/module/client/NonceAccountManager.js.map +1 -1
- package/lib/module/examples/enhancedFeaturesFlow.js +233 -0
- package/lib/module/examples/enhancedFeaturesFlow.js.map +1 -0
- package/lib/module/examples/offlinePaymentFlow.js +27 -27
- package/lib/module/examples/offlinePaymentFlow.js.map +1 -1
- package/lib/module/index.js +13 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/qr.js +2 -2
- package/lib/module/reconciliation.js +4 -4
- package/lib/module/reconciliation.js.map +1 -1
- package/lib/module/services/authService.js +3 -3
- package/lib/module/services/authService.js.map +1 -1
- package/lib/module/utils/compression.js +210 -0
- package/lib/module/utils/compression.js.map +1 -0
- package/lib/module/wifi.js +311 -0
- package/lib/module/wifi.js.map +1 -0
- package/lib/typescript/src/examples/enhancedFeaturesFlow.d.ts +45 -0
- package/lib/typescript/src/examples/enhancedFeaturesFlow.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +7 -1
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/utils/compression.d.ts +52 -0
- package/lib/typescript/src/utils/compression.d.ts.map +1 -0
- package/lib/typescript/src/wifi.d.ts +116 -0
- package/lib/typescript/src/wifi.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/client/NonceAccountManager.ts +2 -2
- package/src/examples/enhancedFeaturesFlow.ts +272 -0
- package/src/examples/offlinePaymentFlow.ts +27 -27
- package/src/index.tsx +26 -1
- package/src/qr.tsx +2 -2
- package/src/reconciliation.ts +4 -4
- package/src/services/authService.ts +3 -3
- package/src/utils/compression.ts +247 -0
- package/src/wifi.ts +401 -0
package/src/wifi.ts
ADDED
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WiFi Direct Transport for TOSS
|
|
3
|
+
*
|
|
4
|
+
* Higher-bandwidth alternative to BLE for device-to-device communication
|
|
5
|
+
* Fallback to BLE if WiFi Direct unavailable
|
|
6
|
+
*
|
|
7
|
+
* Uses native React Native APIs for production-ready implementation
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { NativeModules, Platform } from 'react-native';
|
|
11
|
+
import type { SolanaIntent } from './intent';
|
|
12
|
+
import type { OfflineTransaction } from './types/nonceAccount';
|
|
13
|
+
import { TossError } from './errors';
|
|
14
|
+
|
|
15
|
+
const { WiFiDirect } = NativeModules;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* WiFi Direct connection state
|
|
19
|
+
*/
|
|
20
|
+
export interface WiFiDirectPeer {
|
|
21
|
+
deviceName: string;
|
|
22
|
+
deviceAddress: string;
|
|
23
|
+
isGroupOwner: boolean;
|
|
24
|
+
signalStrength?: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* WiFi Direct socket for data transmission
|
|
29
|
+
*/
|
|
30
|
+
export interface WiFiDirectSocket {
|
|
31
|
+
peerId: string;
|
|
32
|
+
connected: boolean;
|
|
33
|
+
bytesTransferred: number;
|
|
34
|
+
lastActivityTime: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* WiFi Direct Transport Handler
|
|
39
|
+
* Wrapper around native WiFi Direct capabilities
|
|
40
|
+
*
|
|
41
|
+
* Supports higher MTU (1200+ bytes) than BLE (480 bytes)
|
|
42
|
+
* Useful for batch transmission of intents
|
|
43
|
+
*/
|
|
44
|
+
export class WiFiDirectTransport {
|
|
45
|
+
private connectedPeers: Map<string, WiFiDirectSocket> = new Map();
|
|
46
|
+
private readonly SOCKET_TIMEOUT = 30000; // 30 seconds
|
|
47
|
+
private readonly WIFI_MTU = 1200; // Conservative MTU for WiFi packets
|
|
48
|
+
|
|
49
|
+
constructor(_platform: 'android' | 'ios' = 'android') {
|
|
50
|
+
if (!WiFiDirect) {
|
|
51
|
+
console.warn(
|
|
52
|
+
'WiFi Direct not available in this environment (requires native module)'
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Check if WiFi Direct is available on device
|
|
59
|
+
*/
|
|
60
|
+
async isAvailable(): Promise<boolean> {
|
|
61
|
+
if (!WiFiDirect) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
if (Platform.OS === 'android') {
|
|
67
|
+
return await WiFiDirect.isAvailable();
|
|
68
|
+
}
|
|
69
|
+
// iOS uses different APIs (Bonjour, Multipeer Connectivity)
|
|
70
|
+
return true;
|
|
71
|
+
} catch (error) {
|
|
72
|
+
console.warn('Error checking WiFi Direct availability:', error);
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Enable WiFi Direct on device
|
|
79
|
+
*/
|
|
80
|
+
async enable(): Promise<void> {
|
|
81
|
+
if (!WiFiDirect) {
|
|
82
|
+
throw new TossError(
|
|
83
|
+
'WiFi Direct native module not available',
|
|
84
|
+
'WIFI_DIRECT_UNAVAILABLE'
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
try {
|
|
89
|
+
if (Platform.OS === 'android') {
|
|
90
|
+
await WiFiDirect.enable();
|
|
91
|
+
}
|
|
92
|
+
} catch (error) {
|
|
93
|
+
throw new TossError(
|
|
94
|
+
`Failed to enable WiFi Direct: ${error instanceof Error ? error.message : String(error)}`,
|
|
95
|
+
'WIFI_DIRECT_ERROR'
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Discover nearby WiFi Direct peers
|
|
102
|
+
*/
|
|
103
|
+
async discoverPeers(timeoutSeconds: number = 10): Promise<WiFiDirectPeer[]> {
|
|
104
|
+
if (!WiFiDirect) {
|
|
105
|
+
return [];
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
try {
|
|
109
|
+
const peers = await WiFiDirect.discoverPeers(timeoutSeconds * 1000);
|
|
110
|
+
return peers.map((p: any) => ({
|
|
111
|
+
deviceName: p.deviceName,
|
|
112
|
+
deviceAddress: p.deviceAddress,
|
|
113
|
+
isGroupOwner: p.isGroupOwner,
|
|
114
|
+
signalStrength: p.signalStrength,
|
|
115
|
+
}));
|
|
116
|
+
} catch (error) {
|
|
117
|
+
console.warn('Peer discovery failed:', error);
|
|
118
|
+
return [];
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Connect to a specific WiFi Direct peer
|
|
124
|
+
*/
|
|
125
|
+
async connectToPeer(deviceAddress: string): Promise<WiFiDirectSocket> {
|
|
126
|
+
if (!WiFiDirect) {
|
|
127
|
+
throw new TossError(
|
|
128
|
+
'WiFi Direct not available',
|
|
129
|
+
'WIFI_DIRECT_UNAVAILABLE'
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
try {
|
|
134
|
+
const socket: WiFiDirectSocket = {
|
|
135
|
+
peerId: deviceAddress,
|
|
136
|
+
connected: false,
|
|
137
|
+
bytesTransferred: 0,
|
|
138
|
+
lastActivityTime: Date.now(),
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
await WiFiDirect.connect(deviceAddress);
|
|
142
|
+
|
|
143
|
+
socket.connected = true;
|
|
144
|
+
this.connectedPeers.set(deviceAddress, socket);
|
|
145
|
+
|
|
146
|
+
return socket;
|
|
147
|
+
} catch (error) {
|
|
148
|
+
throw new TossError(
|
|
149
|
+
`Failed to connect to WiFi Direct peer: ${error instanceof Error ? error.message : String(error)}`,
|
|
150
|
+
'WIFI_DIRECT_CONNECT_ERROR'
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Send intent via WiFi Direct connection
|
|
157
|
+
* Uses larger MTU than BLE for efficiency
|
|
158
|
+
*/
|
|
159
|
+
async sendIntent(
|
|
160
|
+
socket: WiFiDirectSocket,
|
|
161
|
+
intent: SolanaIntent
|
|
162
|
+
): Promise<{
|
|
163
|
+
success: boolean;
|
|
164
|
+
bytesTransferred: number;
|
|
165
|
+
chunks: number;
|
|
166
|
+
}> {
|
|
167
|
+
if (!socket.connected) {
|
|
168
|
+
throw new TossError(
|
|
169
|
+
'WiFi Direct socket not connected',
|
|
170
|
+
'WIFI_DIRECT_DISCONNECTED'
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
try {
|
|
175
|
+
const intentBuffer = Buffer.from(JSON.stringify(intent), 'utf-8');
|
|
176
|
+
const chunks = Math.ceil(intentBuffer.length / this.WIFI_MTU);
|
|
177
|
+
|
|
178
|
+
let totalTransferred = 0;
|
|
179
|
+
|
|
180
|
+
for (let i = 0; i < chunks; i++) {
|
|
181
|
+
const start = i * this.WIFI_MTU;
|
|
182
|
+
const end = Math.min(start + this.WIFI_MTU, intentBuffer.length);
|
|
183
|
+
const chunk = intentBuffer.slice(start, end);
|
|
184
|
+
|
|
185
|
+
// Send with simple header: chunk number + total chunks
|
|
186
|
+
const chunkHeader = Buffer.allocUnsafe(2);
|
|
187
|
+
chunkHeader.writeUInt8(i, 0);
|
|
188
|
+
chunkHeader.writeUInt8(chunks, 1);
|
|
189
|
+
|
|
190
|
+
const packet = Buffer.concat([chunkHeader, chunk]);
|
|
191
|
+
|
|
192
|
+
await WiFiDirect.sendData(socket.peerId, packet);
|
|
193
|
+
|
|
194
|
+
totalTransferred += chunk.length;
|
|
195
|
+
|
|
196
|
+
// Update socket stats
|
|
197
|
+
socket.bytesTransferred += chunk.length;
|
|
198
|
+
socket.lastActivityTime = Date.now();
|
|
199
|
+
|
|
200
|
+
// Small delay between chunks to avoid congestion
|
|
201
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return {
|
|
205
|
+
success: true,
|
|
206
|
+
bytesTransferred: totalTransferred,
|
|
207
|
+
chunks,
|
|
208
|
+
};
|
|
209
|
+
} catch (error) {
|
|
210
|
+
throw new TossError(
|
|
211
|
+
`Failed to send intent via WiFi Direct: ${error instanceof Error ? error.message : String(error)}`,
|
|
212
|
+
'WIFI_DIRECT_SEND_ERROR'
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Send offline transaction via WiFi Direct
|
|
219
|
+
*/
|
|
220
|
+
async sendOfflineTransaction(
|
|
221
|
+
socket: WiFiDirectSocket,
|
|
222
|
+
transaction: OfflineTransaction
|
|
223
|
+
): Promise<{
|
|
224
|
+
success: boolean;
|
|
225
|
+
bytesTransferred: number;
|
|
226
|
+
chunks: number;
|
|
227
|
+
}> {
|
|
228
|
+
const txBuffer = Buffer.from(JSON.stringify(transaction), 'utf-8');
|
|
229
|
+
const chunks = Math.ceil(txBuffer.length / this.WIFI_MTU);
|
|
230
|
+
|
|
231
|
+
let totalTransferred = 0;
|
|
232
|
+
|
|
233
|
+
for (let i = 0; i < chunks; i++) {
|
|
234
|
+
const start = i * this.WIFI_MTU;
|
|
235
|
+
const end = Math.min(start + this.WIFI_MTU, txBuffer.length);
|
|
236
|
+
const chunk = txBuffer.slice(start, end);
|
|
237
|
+
|
|
238
|
+
const chunkHeader = Buffer.allocUnsafe(2);
|
|
239
|
+
chunkHeader.writeUInt8(i, 0);
|
|
240
|
+
chunkHeader.writeUInt8(chunks, 1);
|
|
241
|
+
|
|
242
|
+
const packet = Buffer.concat([chunkHeader, chunk]);
|
|
243
|
+
|
|
244
|
+
await WiFiDirect.sendData(socket.peerId, packet);
|
|
245
|
+
|
|
246
|
+
totalTransferred += chunk.length;
|
|
247
|
+
socket.bytesTransferred += chunk.length;
|
|
248
|
+
socket.lastActivityTime = Date.now();
|
|
249
|
+
|
|
250
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return {
|
|
254
|
+
success: true,
|
|
255
|
+
bytesTransferred: totalTransferred,
|
|
256
|
+
chunks,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Receive data from WiFi Direct peer
|
|
262
|
+
*/
|
|
263
|
+
async receiveData(
|
|
264
|
+
socket: WiFiDirectSocket,
|
|
265
|
+
expectedChunks: number
|
|
266
|
+
): Promise<Buffer> {
|
|
267
|
+
const chunks: Buffer[] = [];
|
|
268
|
+
const receivedChunks = new Set<number>();
|
|
269
|
+
|
|
270
|
+
try {
|
|
271
|
+
while (receivedChunks.size < expectedChunks) {
|
|
272
|
+
const packet = await WiFiDirect.receiveData(socket.peerId, 5000); // 5 second timeout
|
|
273
|
+
|
|
274
|
+
if (packet) {
|
|
275
|
+
const chunkNumber = packet[0];
|
|
276
|
+
// Header byte (not used in reassembly)
|
|
277
|
+
const chunkData = packet.slice(2);
|
|
278
|
+
|
|
279
|
+
chunks[chunkNumber] = chunkData;
|
|
280
|
+
receivedChunks.add(chunkNumber);
|
|
281
|
+
|
|
282
|
+
socket.lastActivityTime = Date.now();
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// Check timeout
|
|
286
|
+
if (Date.now() - socket.lastActivityTime > this.SOCKET_TIMEOUT) {
|
|
287
|
+
throw new TossError(
|
|
288
|
+
'WiFi Direct socket timeout',
|
|
289
|
+
'WIFI_DIRECT_TIMEOUT'
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
return Buffer.concat(chunks);
|
|
295
|
+
} catch (error) {
|
|
296
|
+
throw new TossError(
|
|
297
|
+
`Failed to receive data via WiFi Direct: ${error instanceof Error ? error.message : String(error)}`,
|
|
298
|
+
'WIFI_DIRECT_RECEIVE_ERROR'
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Disconnect from WiFi Direct peer
|
|
305
|
+
*/
|
|
306
|
+
async disconnect(peerId: string): Promise<void> {
|
|
307
|
+
try {
|
|
308
|
+
const socket = this.connectedPeers.get(peerId);
|
|
309
|
+
|
|
310
|
+
if (socket) {
|
|
311
|
+
socket.connected = false;
|
|
312
|
+
this.connectedPeers.delete(peerId);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
if (WiFiDirect) {
|
|
316
|
+
await WiFiDirect.disconnect(peerId);
|
|
317
|
+
}
|
|
318
|
+
} catch (error) {
|
|
319
|
+
console.warn(`Error disconnecting from ${peerId}:`, error);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Get all connected peers
|
|
325
|
+
*/
|
|
326
|
+
getConnectedPeers(): WiFiDirectSocket[] {
|
|
327
|
+
return Array.from(this.connectedPeers.values()).filter(
|
|
328
|
+
(socket) => socket.connected
|
|
329
|
+
);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Get MTU size for this transport
|
|
334
|
+
*/
|
|
335
|
+
getMTU(): number {
|
|
336
|
+
return this.WIFI_MTU;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Clean up expired connections
|
|
341
|
+
*/
|
|
342
|
+
cleanupExpiredConnections(): void {
|
|
343
|
+
const now = Date.now();
|
|
344
|
+
|
|
345
|
+
for (const [peerId, socket] of this.connectedPeers.entries()) {
|
|
346
|
+
if (now - socket.lastActivityTime > this.SOCKET_TIMEOUT) {
|
|
347
|
+
this.disconnect(peerId).catch(() => {});
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Smart transport selector
|
|
355
|
+
* Automatically chooses best transport for given context
|
|
356
|
+
*/
|
|
357
|
+
export class SmartTransportSelector {
|
|
358
|
+
private wifiDirect: WiFiDirectTransport;
|
|
359
|
+
|
|
360
|
+
constructor() {
|
|
361
|
+
this.wifiDirect = new WiFiDirectTransport();
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Select best available transport for intent transmission
|
|
366
|
+
*
|
|
367
|
+
* Preference order:
|
|
368
|
+
* 1. WiFi Direct (fastest, 1200 MTU)
|
|
369
|
+
* 2. BLE (fallback, 480 MTU)
|
|
370
|
+
*/
|
|
371
|
+
async selectTransport(): Promise<'wifi' | 'ble'> {
|
|
372
|
+
const wifiAvailable = await this.wifiDirect.isAvailable();
|
|
373
|
+
|
|
374
|
+
if (wifiAvailable) {
|
|
375
|
+
return 'wifi';
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
return 'ble';
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Check if WiFi Direct should be used
|
|
383
|
+
* Factors: availability, proximity, battery level
|
|
384
|
+
*/
|
|
385
|
+
async shouldUseWiFi(checkBattery: boolean = false): Promise<boolean> {
|
|
386
|
+
const available = await this.wifiDirect.isAvailable();
|
|
387
|
+
|
|
388
|
+
if (!available) {
|
|
389
|
+
return false;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// Optional: check battery level
|
|
393
|
+
if (checkBattery) {
|
|
394
|
+
// In production, query Battery API
|
|
395
|
+
// For now, assume battery OK
|
|
396
|
+
return true;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
return true;
|
|
400
|
+
}
|
|
401
|
+
}
|