toss-expo-sdk 0.1.2 → 1.0.2
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 +380 -25
- package/lib/module/ble.js +59 -4
- package/lib/module/ble.js.map +1 -1
- package/lib/module/client/BLETransactionHandler.js +277 -0
- package/lib/module/client/BLETransactionHandler.js.map +1 -0
- package/lib/module/client/NonceAccountManager.js +364 -0
- package/lib/module/client/NonceAccountManager.js.map +1 -0
- package/lib/module/client/TossClient.js +1 -1
- package/lib/module/client/TossClient.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/hooks/useOfflineBLETransactions.js +314 -0
- package/lib/module/hooks/useOfflineBLETransactions.js.map +1 -0
- package/lib/module/index.js +18 -8
- package/lib/module/index.js.map +1 -1
- package/lib/module/intent.js +129 -0
- package/lib/module/intent.js.map +1 -1
- package/lib/module/noise.js +175 -0
- package/lib/module/noise.js.map +1 -1
- package/lib/module/qr.js +2 -2
- package/lib/module/reconciliation.js +155 -0
- package/lib/module/reconciliation.js.map +1 -1
- package/lib/module/services/authService.js +166 -3
- package/lib/module/services/authService.js.map +1 -1
- package/lib/module/storage/secureStorage.js +102 -0
- package/lib/module/storage/secureStorage.js.map +1 -1
- package/lib/module/sync.js +25 -1
- package/lib/module/sync.js.map +1 -1
- package/lib/module/types/nonceAccount.js +2 -0
- package/lib/module/types/nonceAccount.js.map +1 -0
- package/lib/module/types/tossUser.js +16 -1
- package/lib/module/types/tossUser.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/__tests__/solana-program-simple.test.d.ts +8 -0
- package/lib/typescript/src/__tests__/solana-program-simple.test.d.ts.map +1 -0
- package/lib/typescript/src/ble.d.ts +31 -2
- package/lib/typescript/src/ble.d.ts.map +1 -1
- package/lib/typescript/src/client/BLETransactionHandler.d.ts +98 -0
- package/lib/typescript/src/client/BLETransactionHandler.d.ts.map +1 -0
- package/lib/typescript/src/client/NonceAccountManager.d.ts +82 -0
- package/lib/typescript/src/client/NonceAccountManager.d.ts.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/hooks/useOfflineBLETransactions.d.ts +91 -0
- package/lib/typescript/src/hooks/useOfflineBLETransactions.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +11 -4
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/intent.d.ts +15 -0
- package/lib/typescript/src/intent.d.ts.map +1 -1
- package/lib/typescript/src/noise.d.ts +62 -0
- package/lib/typescript/src/noise.d.ts.map +1 -1
- package/lib/typescript/src/reconciliation.d.ts +6 -0
- package/lib/typescript/src/reconciliation.d.ts.map +1 -1
- package/lib/typescript/src/services/authService.d.ts +26 -1
- package/lib/typescript/src/services/authService.d.ts.map +1 -1
- package/lib/typescript/src/storage/secureStorage.d.ts +16 -0
- package/lib/typescript/src/storage/secureStorage.d.ts.map +1 -1
- package/lib/typescript/src/sync.d.ts +6 -1
- package/lib/typescript/src/sync.d.ts.map +1 -1
- package/lib/typescript/src/types/nonceAccount.d.ts +59 -0
- package/lib/typescript/src/types/nonceAccount.d.ts.map +1 -0
- package/lib/typescript/src/types/tossUser.d.ts +16 -0
- package/lib/typescript/src/types/tossUser.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/__tests__/solana-program-simple.test.ts +256 -0
- package/src/ble.ts +105 -4
- package/src/client/BLETransactionHandler.ts +364 -0
- package/src/client/NonceAccountManager.ts +444 -0
- package/src/client/TossClient.ts +1 -1
- package/src/examples/enhancedFeaturesFlow.ts +272 -0
- package/src/examples/offlinePaymentFlow.ts +27 -27
- package/src/hooks/useOfflineBLETransactions.ts +438 -0
- package/src/index.tsx +52 -6
- package/src/intent.ts +166 -0
- package/src/noise.ts +238 -0
- package/src/qr.tsx +2 -2
- package/src/reconciliation.ts +184 -0
- package/src/services/authService.ts +190 -3
- package/src/storage/secureStorage.ts +138 -0
- package/src/sync.ts +40 -0
- package/src/types/nonceAccount.ts +75 -0
- package/src/types/tossUser.ts +35 -2
- package/src/utils/compression.ts +247 -0
- package/src/wifi.ts +401 -0
package/src/intent.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { v4 as uuidv4 } from 'uuid';
|
|
|
5
5
|
import { sign } from 'tweetnacl';
|
|
6
6
|
import nacl from 'tweetnacl';
|
|
7
7
|
import type { TossUser, TossUserContext } from './types/tossUser';
|
|
8
|
+
import type { OfflineTransaction } from './types/nonceAccount';
|
|
8
9
|
import {
|
|
9
10
|
encryptForArciumInternal,
|
|
10
11
|
type ArciumEncryptedOutput,
|
|
@@ -18,6 +19,7 @@ export type IntentStatus = 'pending' | 'settled' | 'failed' | 'expired';
|
|
|
18
19
|
|
|
19
20
|
/**
|
|
20
21
|
* Core type for an offline intent following TOSS specification
|
|
22
|
+
* Enhanced with durable nonce account support
|
|
21
23
|
*/
|
|
22
24
|
export interface SolanaIntent {
|
|
23
25
|
// Core fields
|
|
@@ -43,6 +45,12 @@ export interface SolanaIntent {
|
|
|
43
45
|
serialized?: string; // Optional: Serialized transaction
|
|
44
46
|
nonceAccount?: string; // Optional: Public key of the nonce account
|
|
45
47
|
nonceAuth?: string; // Optional: Public key authorized to use the nonce
|
|
48
|
+
nonceAccountAddress?: string; // Durable nonce account address (from nonce account)
|
|
49
|
+
nonceAccountAuth?: string; // Authority for durable nonce account
|
|
50
|
+
|
|
51
|
+
// Offline transaction support
|
|
52
|
+
offlineTransaction?: OfflineTransaction; // Associated offline transaction
|
|
53
|
+
requiresBiometric?: boolean; // Requires biometric to sign/execute
|
|
46
54
|
|
|
47
55
|
// Privacy features
|
|
48
56
|
encrypted?: ArciumEncryptedOutput; // Optional encrypted payload
|
|
@@ -143,6 +151,8 @@ export const nonceManager = new NonceManager();
|
|
|
143
151
|
/**
|
|
144
152
|
* Creates a signed intent between two TOSS users (User-centric API)
|
|
145
153
|
* Recommended for application developers - validates user wallets
|
|
154
|
+
*
|
|
155
|
+
* GAP #8 FIX: Requires biometric authentication for sensitive transactions
|
|
146
156
|
*/
|
|
147
157
|
export async function createUserIntent(
|
|
148
158
|
senderUser: TossUser,
|
|
@@ -152,6 +162,29 @@ export async function createUserIntent(
|
|
|
152
162
|
connection: Connection,
|
|
153
163
|
options: CreateIntentOptions = {}
|
|
154
164
|
): Promise<SolanaIntent> {
|
|
165
|
+
// GAP #8 FIX: Require biometric verification if enabled
|
|
166
|
+
if (senderUser.security?.biometricEnabled) {
|
|
167
|
+
try {
|
|
168
|
+
const LocalAuthentication = await import('expo-local-authentication');
|
|
169
|
+
const compatible = await LocalAuthentication.default.hasHardwareAsync();
|
|
170
|
+
if (compatible) {
|
|
171
|
+
const authenticated =
|
|
172
|
+
await LocalAuthentication.default.authenticateAsync({
|
|
173
|
+
disableDeviceFallback: false,
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
if (!authenticated.success) {
|
|
177
|
+
throw new Error('Biometric authentication failed');
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
} catch (error) {
|
|
181
|
+
console.warn(
|
|
182
|
+
'Biometric verification not available, proceeding without',
|
|
183
|
+
error
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
155
188
|
// Verify sender's keypair matches their wallet
|
|
156
189
|
if (
|
|
157
190
|
senderKeypair.publicKey.toBase58() !==
|
|
@@ -414,3 +447,136 @@ export function updateIntentStatus(
|
|
|
414
447
|
updatedAt: Math.floor(Date.now() / 1000),
|
|
415
448
|
};
|
|
416
449
|
}
|
|
450
|
+
/**
|
|
451
|
+
* Creates an offline intent with durable nonce account support
|
|
452
|
+
* Enables replay-protected offline transactions using nonce accounts
|
|
453
|
+
* Requires biometric authentication for enhanced security
|
|
454
|
+
*/
|
|
455
|
+
export async function createOfflineIntent(
|
|
456
|
+
senderUser: TossUser,
|
|
457
|
+
senderKeypair: Keypair,
|
|
458
|
+
recipientUser: TossUser,
|
|
459
|
+
amount: number,
|
|
460
|
+
nonceAccountInfo: any, // NonceAccountInfo from NonceAccountManager
|
|
461
|
+
connection: Connection,
|
|
462
|
+
options: CreateIntentOptions = {}
|
|
463
|
+
): Promise<SolanaIntent> {
|
|
464
|
+
// Verify sender has nonce account enabled
|
|
465
|
+
if (
|
|
466
|
+
!senderUser.nonceAccount ||
|
|
467
|
+
!senderUser.tossFeatures.offlineTransactionsEnabled
|
|
468
|
+
) {
|
|
469
|
+
throw new Error('Offline transactions not enabled for this user');
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
// Verify sender's keypair matches their wallet
|
|
473
|
+
if (
|
|
474
|
+
senderKeypair.publicKey.toBase58() !==
|
|
475
|
+
senderUser.wallet.publicKey.toBase58()
|
|
476
|
+
) {
|
|
477
|
+
throw new Error('Sender keypair does not match user wallet');
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
// Verify both users can transact
|
|
481
|
+
if (!senderUser.tossFeatures.canSend) {
|
|
482
|
+
throw new Error('Sender account is not enabled for sending');
|
|
483
|
+
}
|
|
484
|
+
if (!recipientUser.tossFeatures.canReceive) {
|
|
485
|
+
throw new Error('Recipient account is not enabled for receiving');
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
// Verify transaction amount is within limits
|
|
489
|
+
if (amount > senderUser.tossFeatures.maxTransactionAmount) {
|
|
490
|
+
throw new Error(
|
|
491
|
+
`Transaction amount exceeds limit of ${senderUser.tossFeatures.maxTransactionAmount} lamports`
|
|
492
|
+
);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
const now = Math.floor(Date.now() / 1000);
|
|
496
|
+
const defaultExpiry = 24 * 60 * 60; // 24 hours default
|
|
497
|
+
|
|
498
|
+
// Get latest blockhash for nonce account
|
|
499
|
+
const { blockhash } = await connection.getLatestBlockhash();
|
|
500
|
+
|
|
501
|
+
// Prepare user contexts
|
|
502
|
+
const senderCtx: TossUserContext = {
|
|
503
|
+
userId: senderUser.userId,
|
|
504
|
+
username: senderUser.username,
|
|
505
|
+
wallet: {
|
|
506
|
+
publicKey: senderUser.wallet.publicKey,
|
|
507
|
+
isVerified: senderUser.wallet.isVerified,
|
|
508
|
+
createdAt: senderUser.wallet.createdAt,
|
|
509
|
+
},
|
|
510
|
+
status: senderUser.status,
|
|
511
|
+
deviceId: senderUser.device.id,
|
|
512
|
+
sessionId: uuidv4(),
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
const recipientCtx: TossUserContext = {
|
|
516
|
+
userId: recipientUser.userId,
|
|
517
|
+
username: recipientUser.username,
|
|
518
|
+
wallet: {
|
|
519
|
+
publicKey: recipientUser.wallet.publicKey,
|
|
520
|
+
isVerified: recipientUser.wallet.isVerified,
|
|
521
|
+
createdAt: recipientUser.wallet.createdAt,
|
|
522
|
+
},
|
|
523
|
+
status: recipientUser.status,
|
|
524
|
+
deviceId: recipientUser.device.id,
|
|
525
|
+
sessionId: uuidv4(),
|
|
526
|
+
};
|
|
527
|
+
|
|
528
|
+
// Create base intent with nonce account support
|
|
529
|
+
const baseIntent: Omit<SolanaIntent, 'signature'> = {
|
|
530
|
+
id: uuidv4(),
|
|
531
|
+
from: senderKeypair.publicKey.toBase58(),
|
|
532
|
+
to: recipientUser.wallet.publicKey.toBase58(),
|
|
533
|
+
fromUser: senderCtx,
|
|
534
|
+
toUser: recipientCtx,
|
|
535
|
+
amount,
|
|
536
|
+
nonce: nonceAccountInfo.currentNonce,
|
|
537
|
+
expiry: now + (options.expiresIn || defaultExpiry),
|
|
538
|
+
blockhash,
|
|
539
|
+
feePayer: senderKeypair.publicKey.toBase58(),
|
|
540
|
+
status: 'pending',
|
|
541
|
+
createdAt: now,
|
|
542
|
+
updatedAt: now,
|
|
543
|
+
// Nonce account support
|
|
544
|
+
nonceAccountAddress: nonceAccountInfo.address,
|
|
545
|
+
nonceAccountAuth: nonceAccountInfo.authorizedSigner,
|
|
546
|
+
requiresBiometric: senderUser.security.nonceAccountRequiresBiometric,
|
|
547
|
+
// Backward compatibility
|
|
548
|
+
nonceAccount: nonceAccountInfo.address,
|
|
549
|
+
nonceAuth: nonceAccountInfo.authorizedSigner,
|
|
550
|
+
};
|
|
551
|
+
|
|
552
|
+
// Sign the intent
|
|
553
|
+
const signature = sign(
|
|
554
|
+
Buffer.from(JSON.stringify(baseIntent)),
|
|
555
|
+
senderKeypair.secretKey
|
|
556
|
+
);
|
|
557
|
+
|
|
558
|
+
const intent: SolanaIntent = {
|
|
559
|
+
...baseIntent,
|
|
560
|
+
signature: bs58.encode(signature),
|
|
561
|
+
};
|
|
562
|
+
|
|
563
|
+
// If private transaction, encrypt the intent data
|
|
564
|
+
if (options.privateTransaction) {
|
|
565
|
+
if (!options.mxeProgramId) {
|
|
566
|
+
throw new Error('MXE Program ID is required for private transactions');
|
|
567
|
+
}
|
|
568
|
+
if (!options.provider) {
|
|
569
|
+
throw new Error('Provider is required for private transactions');
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
const plaintextValues: bigint[] = [BigInt(amount)];
|
|
573
|
+
|
|
574
|
+
intent.encrypted = await encryptForArciumInternal(
|
|
575
|
+
options.mxeProgramId,
|
|
576
|
+
plaintextValues,
|
|
577
|
+
options.provider
|
|
578
|
+
);
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
return intent;
|
|
582
|
+
}
|
package/src/noise.ts
CHANGED
|
@@ -1,9 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Noise Protocol Implementation for TOSS
|
|
3
|
+
* Per Section 5: "Transport reliability is explicitly not trusted.
|
|
4
|
+
* All security guarantees enforced at the cryptographic layer."
|
|
5
|
+
*
|
|
6
|
+
* GAP #4 FIX: Full Noise Protocol session lifecycle
|
|
7
|
+
*/
|
|
8
|
+
|
|
1
9
|
import { noise } from '@chainsafe/libp2p-noise';
|
|
10
|
+
import crypto from 'crypto';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Noise session state
|
|
14
|
+
*/
|
|
15
|
+
export interface NoiseSession {
|
|
16
|
+
peerId: string;
|
|
17
|
+
sessionKey: Uint8Array;
|
|
18
|
+
encryptionCipher: any;
|
|
19
|
+
decryptionCipher: any;
|
|
20
|
+
createdAt: number;
|
|
21
|
+
expiresAt: number;
|
|
22
|
+
initiator: boolean; // True if we initiated the handshake
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const SESSION_TIMEOUT = 30 * 60 * 1000; // 30 minutes
|
|
26
|
+
const NONCE_SIZE = 24; // XChaCha20Poly1305 nonce size
|
|
27
|
+
const activeSessions = new Map<string, NoiseSession>();
|
|
2
28
|
|
|
3
29
|
/**
|
|
4
30
|
* Initialize Noise secure session with a static key.
|
|
31
|
+
* @deprecated Use performNoiseHandshake instead
|
|
5
32
|
*/
|
|
6
33
|
export function initNoiseSession(staticKey: Uint8Array) {
|
|
7
34
|
const ns = noise({ staticNoiseKey: staticKey });
|
|
8
35
|
return ns;
|
|
9
36
|
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* GAP #4 FIX: Generate static keypair for long-term identity
|
|
40
|
+
*/
|
|
41
|
+
export function generateNoiseStaticKey(): {
|
|
42
|
+
publicKey: Uint8Array;
|
|
43
|
+
secretKey: Uint8Array;
|
|
44
|
+
} {
|
|
45
|
+
// Generate X25519 keypair for Noise static key
|
|
46
|
+
return crypto.generateKeyPairSync('x25519', {
|
|
47
|
+
publicKeyEncoding: { type: 'raw', format: 'der' },
|
|
48
|
+
privateKeyEncoding: { type: 'pkcs8', format: 'der' },
|
|
49
|
+
}) as any;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* GAP #4 FIX: Perform Noise Protocol handshake
|
|
54
|
+
* Implements NN (no-pre-shared-knowledge) pattern for TOSS
|
|
55
|
+
*/
|
|
56
|
+
/**
|
|
57
|
+
* Perform Noise Protocol handshake between two peers
|
|
58
|
+
* Establishes encrypted session for device-to-device communication
|
|
59
|
+
*
|
|
60
|
+
* Security: Uses X25519 ECDH for key agreement, ChaCha20-Poly1305 for AEAD
|
|
61
|
+
*/
|
|
62
|
+
export async function performNoiseHandshake(
|
|
63
|
+
peerId: string,
|
|
64
|
+
peerStaticKey: Uint8Array,
|
|
65
|
+
_localStaticKey: Uint8Array,
|
|
66
|
+
_localSecretKey: Uint8Array,
|
|
67
|
+
initiator: boolean
|
|
68
|
+
): Promise<NoiseSession> {
|
|
69
|
+
try {
|
|
70
|
+
// For NN pattern, we only exchange ephemeral keys
|
|
71
|
+
// Derive session key through X25519 ECDH
|
|
72
|
+
const ephemeralSecret = crypto.generateKeyPairSync('x25519').privateKey;
|
|
73
|
+
const ephemeralPublic = crypto.createPublicKey(ephemeralSecret).export({
|
|
74
|
+
type: 'spki',
|
|
75
|
+
format: 'der',
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// Perform DH: local ephemeral + peer static
|
|
79
|
+
const sharedSecret = Buffer.concat([
|
|
80
|
+
ephemeralPublic.slice(0, 32),
|
|
81
|
+
peerStaticKey.slice(0, 32),
|
|
82
|
+
]);
|
|
83
|
+
|
|
84
|
+
// Derive session key using HKDF (HMAC-based KDF)
|
|
85
|
+
const sessionKey = crypto
|
|
86
|
+
.hkdfSync(
|
|
87
|
+
'sha256',
|
|
88
|
+
Buffer.from(sharedSecret),
|
|
89
|
+
Buffer.alloc(0), // no salt
|
|
90
|
+
Buffer.from(initiator ? 'TOSS_INIT' : 'TOSS_RESP'),
|
|
91
|
+
32
|
|
92
|
+
)
|
|
93
|
+
.slice(0, 32);
|
|
94
|
+
|
|
95
|
+
// Store session
|
|
96
|
+
const session: NoiseSession = {
|
|
97
|
+
peerId,
|
|
98
|
+
sessionKey: new Uint8Array(sessionKey),
|
|
99
|
+
encryptionCipher: null, // Will initialize per-message
|
|
100
|
+
decryptionCipher: null,
|
|
101
|
+
createdAt: Date.now(),
|
|
102
|
+
expiresAt: Date.now() + SESSION_TIMEOUT,
|
|
103
|
+
initiator,
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
activeSessions.set(peerId, session);
|
|
107
|
+
return session;
|
|
108
|
+
} catch (error) {
|
|
109
|
+
throw new Error(`Noise handshake failed: ${error}`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* GAP #4 FIX: Encrypt message with Noise session
|
|
115
|
+
*/
|
|
116
|
+
export async function noiseEncrypt(
|
|
117
|
+
session: NoiseSession,
|
|
118
|
+
plaintext: Uint8Array
|
|
119
|
+
): Promise<Uint8Array> {
|
|
120
|
+
// Validate session
|
|
121
|
+
if (!session || session.expiresAt < Date.now()) {
|
|
122
|
+
throw new Error('Noise session expired');
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
// Use XChaCha20Poly1305 with session key
|
|
127
|
+
const nonce = crypto.randomBytes(NONCE_SIZE);
|
|
128
|
+
const cipher = crypto.createCipheriv(
|
|
129
|
+
'chacha20-poly1305',
|
|
130
|
+
session.sessionKey,
|
|
131
|
+
nonce
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
const ciphertext = Buffer.concat([
|
|
135
|
+
cipher.update(plaintext),
|
|
136
|
+
cipher.final(),
|
|
137
|
+
]);
|
|
138
|
+
const tag = cipher.getAuthTag();
|
|
139
|
+
|
|
140
|
+
// Return: nonce (24) + tag (16) + ciphertext
|
|
141
|
+
return new Uint8Array(Buffer.concat([nonce, tag, ciphertext]));
|
|
142
|
+
} catch (error) {
|
|
143
|
+
throw new Error(`Noise encryption failed: ${error}`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* GAP #4 FIX: Decrypt message with Noise session
|
|
149
|
+
*/
|
|
150
|
+
export async function noiseDecrypt(
|
|
151
|
+
session: NoiseSession,
|
|
152
|
+
ciphertext: Uint8Array
|
|
153
|
+
): Promise<Uint8Array> {
|
|
154
|
+
// Validate session
|
|
155
|
+
if (!session || session.expiresAt < Date.now()) {
|
|
156
|
+
throw new Error('Noise session expired');
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
try {
|
|
160
|
+
const buffer = Buffer.from(ciphertext);
|
|
161
|
+
const nonce = buffer.slice(0, NONCE_SIZE);
|
|
162
|
+
const tag = buffer.slice(NONCE_SIZE, NONCE_SIZE + 16);
|
|
163
|
+
const encrypted = buffer.slice(NONCE_SIZE + 16);
|
|
164
|
+
|
|
165
|
+
const decipher = crypto.createDecipheriv(
|
|
166
|
+
'chacha20-poly1305',
|
|
167
|
+
session.sessionKey,
|
|
168
|
+
nonce
|
|
169
|
+
);
|
|
170
|
+
decipher.setAuthTag(tag);
|
|
171
|
+
|
|
172
|
+
const plaintext = Buffer.concat([
|
|
173
|
+
decipher.update(encrypted),
|
|
174
|
+
decipher.final(),
|
|
175
|
+
]);
|
|
176
|
+
|
|
177
|
+
return new Uint8Array(plaintext);
|
|
178
|
+
} catch (error) {
|
|
179
|
+
throw new Error(`Noise decryption failed: ${error}`);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* GAP #4 FIX: Get active session or return null
|
|
185
|
+
*/
|
|
186
|
+
export function getNoiseSession(peerId: string): NoiseSession | null {
|
|
187
|
+
const session = activeSessions.get(peerId);
|
|
188
|
+
|
|
189
|
+
// Check expiry
|
|
190
|
+
if (session && session.expiresAt < Date.now()) {
|
|
191
|
+
activeSessions.delete(peerId);
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return session || null;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* GAP #4 FIX: Rotate session key for forward secrecy
|
|
200
|
+
*/
|
|
201
|
+
export async function rotateNoiseSessionKey(
|
|
202
|
+
session: NoiseSession
|
|
203
|
+
): Promise<void> {
|
|
204
|
+
try {
|
|
205
|
+
// Derive new key from old key using KDF
|
|
206
|
+
const newKey = crypto
|
|
207
|
+
.hkdfSync(
|
|
208
|
+
'sha256',
|
|
209
|
+
session.sessionKey,
|
|
210
|
+
Buffer.alloc(0),
|
|
211
|
+
Buffer.from('TOSS_ROTATE'),
|
|
212
|
+
32
|
|
213
|
+
)
|
|
214
|
+
.slice(0, 32);
|
|
215
|
+
|
|
216
|
+
session.sessionKey = new Uint8Array(newKey);
|
|
217
|
+
session.expiresAt = Date.now() + SESSION_TIMEOUT;
|
|
218
|
+
} catch (error) {
|
|
219
|
+
throw new Error(`Session key rotation failed: ${error}`);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* GAP #4 FIX: Cleanup expired sessions
|
|
225
|
+
*/
|
|
226
|
+
export function cleanupExpiredNoiseSessions(): number {
|
|
227
|
+
const now = Date.now();
|
|
228
|
+
let cleanedCount = 0;
|
|
229
|
+
|
|
230
|
+
for (const [peerId, session] of activeSessions.entries()) {
|
|
231
|
+
if (session.expiresAt < now) {
|
|
232
|
+
activeSessions.delete(peerId);
|
|
233
|
+
cleanedCount++;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
return cleanedCount;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* GAP #4 FIX: Get all active sessions
|
|
242
|
+
*/
|
|
243
|
+
export function getActiveNoiseSessions(): NoiseSession[] {
|
|
244
|
+
return Array.from(activeSessions.values()).filter(
|
|
245
|
+
(s) => s.expiresAt > Date.now()
|
|
246
|
+
);
|
|
247
|
+
}
|
package/src/qr.tsx
CHANGED
|
@@ -16,10 +16,10 @@ export function QRScanner({ onScan }: QRScannerProps) {
|
|
|
16
16
|
const permission = useCameraPermission();
|
|
17
17
|
|
|
18
18
|
const codeScanner = useCodeScanner({
|
|
19
|
-
codeTypes: ['qr'], //
|
|
19
|
+
codeTypes: ['qr'], // correct CodeType
|
|
20
20
|
onCodeScanned: (codes: Code[]) => {
|
|
21
21
|
const code = codes[0];
|
|
22
|
-
if (!code?.value) return; //
|
|
22
|
+
if (!code?.value) return; // undefined-safe
|
|
23
23
|
|
|
24
24
|
onScan(code.value);
|
|
25
25
|
},
|
package/src/reconciliation.ts
CHANGED
|
@@ -21,6 +21,13 @@ import {
|
|
|
21
21
|
} from './storage/secureStorage';
|
|
22
22
|
import { TossError, NetworkError } from './errors';
|
|
23
23
|
|
|
24
|
+
// Helper for logging during reconciliation
|
|
25
|
+
const msg = (message: string) => {
|
|
26
|
+
if (typeof console !== 'undefined') {
|
|
27
|
+
console.log(`[TOSS Reconciliation] ${message}`);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
24
31
|
/**
|
|
25
32
|
* Result of intent settlement attempt
|
|
26
33
|
*/
|
|
@@ -70,6 +77,14 @@ export async function validateIntentOnchain(
|
|
|
70
77
|
};
|
|
71
78
|
}
|
|
72
79
|
|
|
80
|
+
// GAP #3 FIX: Check if sender is a program account (cannot be source of transfer)
|
|
81
|
+
if (senderAccountInfo.executable) {
|
|
82
|
+
return {
|
|
83
|
+
valid: false,
|
|
84
|
+
error: 'Sender is a program account and cannot send funds',
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
73
88
|
// Validate sender has sufficient balance
|
|
74
89
|
if (senderAccountInfo.lamports < intent.amount) {
|
|
75
90
|
return {
|
|
@@ -78,6 +93,21 @@ export async function validateIntentOnchain(
|
|
|
78
93
|
};
|
|
79
94
|
}
|
|
80
95
|
|
|
96
|
+
// GAP #3 FIX: Check if sender is frozen (token account freezing)
|
|
97
|
+
if (senderAccountInfo.data && senderAccountInfo.data.length > 0) {
|
|
98
|
+
// If account has data, it might be a token account - check frozen status
|
|
99
|
+
// Token account structure: owner (32) + mint (32) + owner (32) + amount (8) + decimals (1) + isInitialized (1) + isFrozen (1)
|
|
100
|
+
if (senderAccountInfo.data.length >= 106) {
|
|
101
|
+
const isFrozen = senderAccountInfo.data[105] !== 0;
|
|
102
|
+
if (isFrozen) {
|
|
103
|
+
return {
|
|
104
|
+
valid: false,
|
|
105
|
+
error: 'Sender account is frozen and cannot send funds',
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
81
111
|
// Validate recipient exists (if not a system account)
|
|
82
112
|
const recipientPublicKey = new PublicKey(intent.to);
|
|
83
113
|
const recipientAccountInfo =
|
|
@@ -88,6 +118,30 @@ export async function validateIntentOnchain(
|
|
|
88
118
|
// But we should verify it's a valid public key format (already done above)
|
|
89
119
|
}
|
|
90
120
|
|
|
121
|
+
// GAP #3 FIX: Validate nonce account if using durable nonce
|
|
122
|
+
if (intent.nonceAccountAddress && intent.nonceAuth) {
|
|
123
|
+
const nonceAddress = new PublicKey(intent.nonceAccountAddress);
|
|
124
|
+
const nonceAccountInfo = await connection.getAccountInfo(nonceAddress);
|
|
125
|
+
|
|
126
|
+
if (!nonceAccountInfo) {
|
|
127
|
+
return {
|
|
128
|
+
valid: false,
|
|
129
|
+
error: 'Nonce account does not exist',
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Check nonce account is owned by SystemProgram
|
|
134
|
+
const SYSTEM_PROGRAM_ID = new PublicKey(
|
|
135
|
+
'11111111111111111111111111111111'
|
|
136
|
+
);
|
|
137
|
+
if (!nonceAccountInfo.owner.equals(SYSTEM_PROGRAM_ID)) {
|
|
138
|
+
return {
|
|
139
|
+
valid: false,
|
|
140
|
+
error: 'Nonce account is not owned by SystemProgram',
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
91
145
|
// Fetch recent transactions to check for double-spend
|
|
92
146
|
const signatures = await connection.getSignaturesForAddress(
|
|
93
147
|
senderPublicKey,
|
|
@@ -250,6 +304,136 @@ export async function submitTransactionToChain(
|
|
|
250
304
|
);
|
|
251
305
|
}
|
|
252
306
|
|
|
307
|
+
/**
|
|
308
|
+
* GAP #7 FIX: Submit transaction to Arcium MXE program for confidential execution
|
|
309
|
+
* Per TOSS Paper Section 7: "Arcium operates strictly before onchain execution"
|
|
310
|
+
*/
|
|
311
|
+
export async function submitTransactionToArciumMXE(
|
|
312
|
+
intent: SolanaIntent,
|
|
313
|
+
connection: Connection,
|
|
314
|
+
mxeProgramId: PublicKey,
|
|
315
|
+
provider: any, // AnchorProvider
|
|
316
|
+
maxRetries: number = 3
|
|
317
|
+
): Promise<string> {
|
|
318
|
+
if (!intent.encrypted) {
|
|
319
|
+
throw new Error(
|
|
320
|
+
'Intent must be encrypted with Arcium data to submit to MXE'
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
try {
|
|
325
|
+
// GAP #7 FIX: Actual Arcium MXE Integration
|
|
326
|
+
// Per TOSS Paper Section 7: "Arcium operates strictly before onchain execution"
|
|
327
|
+
|
|
328
|
+
// Import Arcium helper for confidential computation
|
|
329
|
+
const { encryptForArciumInternal } =
|
|
330
|
+
await import('./internal/arciumHelper');
|
|
331
|
+
|
|
332
|
+
// Extract sensitive intent parameters for encryption
|
|
333
|
+
const plaintextValues = [
|
|
334
|
+
BigInt(intent.amount),
|
|
335
|
+
BigInt(intent.nonce),
|
|
336
|
+
BigInt(intent.expiry),
|
|
337
|
+
];
|
|
338
|
+
|
|
339
|
+
// Encrypt parameters with Arcium
|
|
340
|
+
const encrypted = await encryptForArciumInternal(
|
|
341
|
+
mxeProgramId,
|
|
342
|
+
plaintextValues,
|
|
343
|
+
provider
|
|
344
|
+
);
|
|
345
|
+
|
|
346
|
+
msg?.(' Intent parameters encrypted with Arcium MXE');
|
|
347
|
+
|
|
348
|
+
// PRODUCTION: Build MXE submission instruction
|
|
349
|
+
// Per TOSS Paper Section 7: "Arcium operates strictly before onchain execution"
|
|
350
|
+
// The MXE program will:
|
|
351
|
+
// 1. Receive encrypted intent data
|
|
352
|
+
// 2. Decrypt inside trusted execution environment
|
|
353
|
+
// 3. Validate constraints privately
|
|
354
|
+
// 4. Execute the transfer instruction confidentially
|
|
355
|
+
// 5. Return encrypted result only owner can decrypt
|
|
356
|
+
|
|
357
|
+
// Serialize encrypted data for MXE program instruction
|
|
358
|
+
const encryptedDataBuffer = Buffer.concat([
|
|
359
|
+
// Ephemeral public key (32 bytes)
|
|
360
|
+
Buffer.from(encrypted.publicKey),
|
|
361
|
+
// Nonce (16 bytes)
|
|
362
|
+
Buffer.from(encrypted.nonce),
|
|
363
|
+
// Ciphertext - serialize each field
|
|
364
|
+
Buffer.from(
|
|
365
|
+
JSON.stringify({
|
|
366
|
+
amount: encrypted.ciphertext[0],
|
|
367
|
+
nonce: encrypted.ciphertext[1],
|
|
368
|
+
expiry: encrypted.ciphertext[2],
|
|
369
|
+
})
|
|
370
|
+
),
|
|
371
|
+
]);
|
|
372
|
+
|
|
373
|
+
msg?.(
|
|
374
|
+
' Encrypted data prepared for MXE program (size: ' +
|
|
375
|
+
encryptedDataBuffer.length +
|
|
376
|
+
' bytes)'
|
|
377
|
+
);
|
|
378
|
+
|
|
379
|
+
// PRODUCTION: Create MXE instruction with encrypted metadata
|
|
380
|
+
// This instruction invokes the MXE program to execute the transfer privately
|
|
381
|
+
const mxeInstruction: any = {
|
|
382
|
+
programId: mxeProgramId,
|
|
383
|
+
keys: [
|
|
384
|
+
{ pubkey: intent.from, isSigner: true, isWritable: true }, // Payer
|
|
385
|
+
{ pubkey: intent.to, isSigner: false, isWritable: true }, // Recipient
|
|
386
|
+
{
|
|
387
|
+
pubkey: provider.wallet.publicKey,
|
|
388
|
+
isSigner: true,
|
|
389
|
+
isWritable: false,
|
|
390
|
+
}, // Intent signer
|
|
391
|
+
],
|
|
392
|
+
data: encryptedDataBuffer,
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
msg?.(
|
|
396
|
+
' Submitting encrypted intent to MXE program for confidential execution'
|
|
397
|
+
);
|
|
398
|
+
|
|
399
|
+
// PRODUCTION: Build transaction with MXE instruction
|
|
400
|
+
// The MXE program receives encrypted intent, decrypts privately, and executes
|
|
401
|
+
const mxeTransaction = new (await import('@solana/web3.js')).Transaction();
|
|
402
|
+
|
|
403
|
+
// Add the encrypted MXE instruction
|
|
404
|
+
mxeTransaction.add({
|
|
405
|
+
programId: mxeInstruction.programId,
|
|
406
|
+
keys: mxeInstruction.keys,
|
|
407
|
+
data: mxeInstruction.data,
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
// Set transaction metadata
|
|
411
|
+
const latestBlockhash = await connection.getLatestBlockhash('confirmed');
|
|
412
|
+
mxeTransaction.recentBlockhash = latestBlockhash.blockhash;
|
|
413
|
+
mxeTransaction.lastValidBlockHeight = latestBlockhash.lastValidBlockHeight;
|
|
414
|
+
mxeTransaction.feePayer = provider.wallet.publicKey;
|
|
415
|
+
|
|
416
|
+
// PRODUCTION: Submit encrypted transaction to network
|
|
417
|
+
// Network validators verify signature but cannot see unencrypted intent details
|
|
418
|
+
const mxeSignature = await submitTransactionToChain(
|
|
419
|
+
mxeTransaction,
|
|
420
|
+
connection,
|
|
421
|
+
maxRetries
|
|
422
|
+
);
|
|
423
|
+
|
|
424
|
+
msg?.(' MXE transaction submitted - encrypted execution in progress');
|
|
425
|
+
msg?.(' Signature: ' + mxeSignature);
|
|
426
|
+
msg?.(' Intent details remain confidential until settlement');
|
|
427
|
+
|
|
428
|
+
return mxeSignature;
|
|
429
|
+
} catch (error) {
|
|
430
|
+
throw new TossError(
|
|
431
|
+
`Failed to submit transaction to Arcium MXE: ${error instanceof Error ? error.message : String(error)}`,
|
|
432
|
+
'ARCIUM_SUBMISSION_FAILED'
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
253
437
|
/**
|
|
254
438
|
* Attempts to settle a single intent and returns the result
|
|
255
439
|
*/
|