toss-expo-sdk 0.1.1 → 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.
- package/README.md +490 -81
- 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 +27 -44
- package/lib/module/client/TossClient.js.map +1 -1
- package/lib/module/contexts/WalletContext.js +4 -4
- package/lib/module/contexts/WalletContext.js.map +1 -1
- package/lib/module/discovery.js +35 -8
- package/lib/module/discovery.js.map +1 -1
- package/lib/module/examples/offlinePaymentFlow.js +27 -2
- 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 +13 -8
- package/lib/module/index.js.map +1 -1
- package/lib/module/intent.js +198 -0
- package/lib/module/intent.js.map +1 -1
- package/lib/module/nfc.js +1 -1
- package/lib/module/noise.js +176 -1
- package/lib/module/noise.js.map +1 -1
- package/lib/module/reconciliation.js +155 -0
- package/lib/module/reconciliation.js.map +1 -1
- package/lib/module/services/authService.js +164 -1
- 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/storage.js +4 -4
- 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/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/client/TossClient.d.ts +10 -12
- package/lib/typescript/src/client/TossClient.d.ts.map +1 -1
- package/lib/typescript/src/discovery.d.ts +8 -2
- package/lib/typescript/src/discovery.d.ts.map +1 -1
- package/lib/typescript/src/examples/offlinePaymentFlow.d.ts +9 -1
- package/lib/typescript/src/examples/offlinePaymentFlow.d.ts.map +1 -1
- 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 +26 -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/package.json +12 -1
- package/src/__tests__/reconciliation.test.tsx +7 -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 +36 -49
- package/src/contexts/WalletContext.tsx +4 -4
- package/src/discovery.ts +46 -8
- package/src/examples/offlinePaymentFlow.ts +48 -2
- package/src/hooks/useOfflineBLETransactions.ts +438 -0
- package/src/index.tsx +49 -7
- package/src/intent.ts +254 -0
- package/src/nfc.ts +4 -4
- package/src/noise.ts +239 -1
- package/src/reconciliation.ts +184 -0
- package/src/services/authService.ts +188 -1
- package/src/storage/secureStorage.ts +142 -4
- package/src/storage.ts +4 -4
- package/src/sync.ts +40 -0
- package/src/types/nonceAccount.ts +75 -0
- package/src/types/tossUser.ts +35 -2
package/README.md
CHANGED
|
@@ -1,50 +1,83 @@
|
|
|
1
1
|
# TOSS Expo SDK
|
|
2
2
|
|
|
3
|
-
**TOSS (The Offline Solana Stack)**
|
|
3
|
+
**TOSS (The Offline Solana Stack)** implements the complete [TOSS Technical Paper](./TOSS_PAPER_IMPLEMENTATION_VERIFICATION.md) — a protocol-correct approach to building offline-first Solana applications without compromising security or finality guarantees.
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/toss-expo-sdk)
|
|
6
|
-
[](LICENSE)
|
|
7
|
+
|
|
8
|
+
**Solana, extended.** TOSS introduces deterministic separation between transaction intent creation and onchain settlement, enabling applications to function seamlessly in disconnected environments while preserving Solana's security model.
|
|
7
9
|
|
|
8
10
|
## 🚀 Features
|
|
9
11
|
|
|
10
12
|
### Core Capabilities
|
|
11
13
|
|
|
12
14
|
- **Offline-First Architecture**: Create and process transactions without immediate network access
|
|
13
|
-
- **
|
|
14
|
-
- **
|
|
15
|
+
- **Durable Nonce Accounts**: Replay-protected offline transactions with automatic expiry handling
|
|
16
|
+
- **Secure Wallet Integration**: Built-in wallet management with biometric protection
|
|
17
|
+
- **BLE Transaction Transmission**: MTU-aware message fragmentation with automatic retry
|
|
18
|
+
- **End-to-End Encryption**: Noise Protocol integration for secure device-to-device communication
|
|
15
19
|
- **TypeScript Support**: Full TypeScript definitions for better developer experience
|
|
16
20
|
|
|
17
21
|
### Security
|
|
18
22
|
|
|
19
|
-
-
|
|
20
|
-
-
|
|
21
|
-
-
|
|
23
|
+
- ✅ Biometric-protected nonce accounts (mandatory)
|
|
24
|
+
- ✅ Hardware-backed secure key storage (Secure Enclave / Keymaster)
|
|
25
|
+
- ✅ Automatic replay protection with durable nonces
|
|
26
|
+
- ✅ Noise Protocol encryption for BLE transmission
|
|
27
|
+
- ✅ CRC32 checksums for fragment verification
|
|
28
|
+
- ✅ Non-custodial by design - users maintain control of their keys
|
|
22
29
|
|
|
23
30
|
### Developer Experience
|
|
24
31
|
|
|
25
|
-
- Simple, intuitive API for
|
|
26
|
-
-
|
|
32
|
+
- Simple, intuitive API for offline transactions
|
|
33
|
+
- Custom React hooks for easy integration (`useOfflineTransaction`, `useBLETransactionTransmission`)
|
|
34
|
+
- Comprehensive error handling with detailed messages
|
|
27
35
|
- Built-in retry mechanisms with exponential backoff
|
|
28
|
-
-
|
|
36
|
+
- Automatic MTU negotiation and fragmentation
|
|
29
37
|
|
|
30
38
|
## 📦 Installation
|
|
31
39
|
|
|
32
40
|
```bash
|
|
33
41
|
# Using npm
|
|
34
|
-
npm install toss-expo-sdk
|
|
42
|
+
npm install toss-expo-sdk
|
|
35
43
|
|
|
36
44
|
# Using yarn
|
|
37
|
-
yarn add toss-expo-sdk
|
|
45
|
+
yarn add toss-expo-sdk
|
|
38
46
|
```
|
|
39
47
|
|
|
48
|
+
All dependencies (Solana Web3.js, Arcium, Noise Protocol, etc.) are automatically included.
|
|
49
|
+
|
|
50
|
+
## 📋 System Model & Design Principles
|
|
51
|
+
|
|
52
|
+
**TOSS operates under these assumptions:**
|
|
53
|
+
|
|
54
|
+
- Devices may be **offline for arbitrary durations**
|
|
55
|
+
- Transport channels are **unreliable and potentially adversarial**
|
|
56
|
+
- Devices are **not mutually trusted**
|
|
57
|
+
- **Onchain state is the sole authority** for settlement
|
|
58
|
+
- Offline execution is limited to **cryptographically verifiable intent generation only**
|
|
59
|
+
|
|
60
|
+
**TOSS maintains these invariants:**
|
|
61
|
+
|
|
62
|
+
- ✅ Onchain state is canonical
|
|
63
|
+
- ✅ Offline execution never mutates global state
|
|
64
|
+
- ✅ All offline artifacts are cryptographically verifiable onchain
|
|
65
|
+
- ✅ No trusted relayers or delegated signing
|
|
66
|
+
- ✅ Failure is deterministic and safe
|
|
67
|
+
- ✅ Privacy is preserved prior to settlement
|
|
68
|
+
|
|
69
|
+
**Violation of any invariant invalidates the offline model.**
|
|
70
|
+
|
|
40
71
|
## 🏁 Quick Start
|
|
41
72
|
|
|
42
73
|
### Initialize the Client
|
|
43
74
|
|
|
75
|
+
Recommended: use the `createClient` helper for concise initialization.
|
|
76
|
+
|
|
44
77
|
```typescript
|
|
45
|
-
import {
|
|
78
|
+
import { createClient } from 'toss-expo-sdk';
|
|
46
79
|
|
|
47
|
-
const client =
|
|
80
|
+
const client = createClient({
|
|
48
81
|
projectId: 'your-project-id',
|
|
49
82
|
mode: 'devnet', // or 'testnet' | 'mainnet-beta'
|
|
50
83
|
privateTransactions: true,
|
|
@@ -53,28 +86,65 @@ const client = new TossClient({
|
|
|
53
86
|
});
|
|
54
87
|
```
|
|
55
88
|
|
|
56
|
-
|
|
89
|
+
Or, you may call the static constructor directly if you prefer:
|
|
57
90
|
|
|
58
91
|
```typescript
|
|
59
|
-
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
92
|
+
import { TossClient } from 'toss-expo-sdk';
|
|
93
|
+
const client = TossClient.createClient({ projectId: 'your-project-id' });
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Create and Sign an Intent
|
|
97
|
+
|
|
98
|
+
Prefer the user-centric API which accepts `TossUser` objects and validates user features.
|
|
99
|
+
|
|
100
|
+
> Note: There are two common ways to create a user intent:
|
|
101
|
+
>
|
|
102
|
+
> - `createUserIntent(senderUser, senderKeypair, recipientUser, amount, connection, options)` — top-level helper suitable for scripts or when you have both user objects and the signing `Keypair` available.
|
|
103
|
+
> - `TossClient.createUserIntent(senderKeypair, recipient, amount, options)` — an instance method used when working with a `TossClient`. This requires an explicit `Keypair` for signing (TossClient is framework-agnostic). For React apps, prefer unlocking the wallet via `WalletProvider` and calling the top-level `createUserIntent` with the unlocked `Keypair`.
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
import {
|
|
107
|
+
createUserIntent,
|
|
108
|
+
createSignedIntent, // legacy: accepts Keypair + PublicKey
|
|
109
|
+
secureStoreIntent,
|
|
110
|
+
syncToChain,
|
|
111
|
+
} from 'toss-expo-sdk';
|
|
112
|
+
import { Connection } from '@solana/web3.js';
|
|
113
|
+
import type { TossUser } from 'toss-expo-sdk';
|
|
114
|
+
|
|
115
|
+
const connection = new Connection('https://api.devnet.solana.com');
|
|
116
|
+
|
|
117
|
+
// Example using TossUser objects
|
|
118
|
+
const intent = await createUserIntent(
|
|
119
|
+
senderUser, // TossUser (includes wallet.publicKey and features)
|
|
120
|
+
senderKeypair, // Keypair object for signing (must match senderUser.wallet)
|
|
121
|
+
recipientUser, // TossUser
|
|
63
122
|
amountInLamports,
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
useDurableNonce: true,
|
|
67
|
-
}
|
|
123
|
+
connection,
|
|
124
|
+
{ expiresIn: 60 * 60, privateTransaction: true }
|
|
68
125
|
);
|
|
69
126
|
|
|
70
|
-
//
|
|
71
|
-
|
|
127
|
+
// Store intent locally
|
|
128
|
+
await secureStoreIntent(intent);
|
|
129
|
+
|
|
130
|
+
// Later, when online, sync to Solana
|
|
131
|
+
const syncResult = await syncToChain(connection);
|
|
132
|
+
console.log(`Settled: ${syncResult.successfulSettlements.length}`);
|
|
133
|
+
|
|
134
|
+
// Legacy: If you already have an address and Keypair, use createSignedIntent
|
|
135
|
+
const legacyIntent = await createSignedIntent(
|
|
136
|
+
senderKeypair,
|
|
137
|
+
recipientPublicKey,
|
|
138
|
+
amountInLamports,
|
|
139
|
+
connection
|
|
140
|
+
);
|
|
72
141
|
```
|
|
73
142
|
|
|
74
143
|
### Using with React Native
|
|
75
144
|
|
|
76
145
|
```typescript
|
|
77
|
-
import { WalletProvider, useWallet } from 'toss-expo-sdk';
|
|
146
|
+
import { WalletProvider, useWallet, createUserIntent, secureStoreIntent, syncToChain } from 'toss-expo-sdk';
|
|
147
|
+
import { View, Button } from 'react-native';
|
|
78
148
|
|
|
79
149
|
function App() {
|
|
80
150
|
return (
|
|
@@ -86,34 +156,332 @@ function App() {
|
|
|
86
156
|
|
|
87
157
|
function PaymentScreen() {
|
|
88
158
|
const {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
159
|
+
isUnlocked,
|
|
160
|
+
unlockWallet,
|
|
161
|
+
lockWallet,
|
|
162
|
+
keypair,
|
|
163
|
+
user,
|
|
94
164
|
} = useWallet();
|
|
95
165
|
|
|
96
|
-
|
|
166
|
+
const handlePay = async (recipientUser, amountLamports, connection) => {
|
|
167
|
+
if (!isUnlocked || !keypair || !user) {
|
|
168
|
+
// Ensure wallet is unlocked and keypair is available
|
|
169
|
+
const ok = await unlockWallet();
|
|
170
|
+
if (!ok) throw new Error('Wallet is locked');
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Create intent using unlocked keypair and TossUser context
|
|
174
|
+
const intent = await createUserIntent(user, keypair, recipientUser, amountLamports, connection);
|
|
175
|
+
await secureStoreIntent(intent);
|
|
176
|
+
|
|
177
|
+
// Optionally trigger sync when online
|
|
178
|
+
// await syncToChain(connection);
|
|
179
|
+
|
|
180
|
+
return intent;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
return (
|
|
184
|
+
<View>
|
|
185
|
+
<Button
|
|
186
|
+
title={isUnlocked ? 'Lock Wallet' : 'Unlock Wallet'}
|
|
187
|
+
onPress={() => (isUnlocked ? lockWallet() : unlockWallet())}
|
|
188
|
+
/>
|
|
189
|
+
</View>
|
|
190
|
+
);
|
|
97
191
|
}
|
|
98
192
|
```
|
|
99
193
|
|
|
100
194
|
## Core Idea: Intent, Not Transaction
|
|
101
195
|
|
|
102
|
-
TOSS uses an **intent-based model
|
|
196
|
+
TOSS uses an **intent-based model** that fundamentally separates cryptographic commitment from onchain execution.
|
|
103
197
|
|
|
104
198
|
An **intent** is:
|
|
105
199
|
|
|
106
|
-
-
|
|
107
|
-
-
|
|
108
|
-
-
|
|
109
|
-
-
|
|
200
|
+
- A **signed declaration** of what a user wants to do
|
|
201
|
+
- **Transferable offline** via BLE, NFC, QR, or local mesh
|
|
202
|
+
- **Verifiable locally** without network access (signature, expiry, nonce only)
|
|
203
|
+
- **Settled later on-chain** where Solana enforces final state transitions
|
|
204
|
+
|
|
205
|
+
This separation allows applications to function **without connectivity** while preserving Solana's security guarantees.
|
|
110
206
|
|
|
111
|
-
|
|
207
|
+
### The 4-Phase Intent Lifecycle
|
|
208
|
+
|
|
209
|
+
TOSS follows a deterministic 4-phase model (Section 4.3 of Technical Paper):
|
|
210
|
+
|
|
211
|
+
```
|
|
212
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
213
|
+
│ Phase 1: CREATION (Offline) │
|
|
214
|
+
│ • Sender constructs intent locally │
|
|
215
|
+
│ • Signs with native Solana keypair (Ed25519) │
|
|
216
|
+
│ • No network required │
|
|
217
|
+
└──────────────────────────┬──────────────────────────────────────┘
|
|
218
|
+
↓
|
|
219
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
220
|
+
│ Phase 2: EXCHANGE (Offline) │
|
|
221
|
+
│ • Signed intent transmitted via proximity (BLE/NFC/QR) │
|
|
222
|
+
│ • Transport integrity not trusted │
|
|
223
|
+
│ • Peer receives intent in untrusted environment │
|
|
224
|
+
└──────────────────────────┬──────────────────────────────────────┘
|
|
225
|
+
↓
|
|
226
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
227
|
+
│ Phase 3: OFFLINE VERIFICATION (Offline) │
|
|
228
|
+
│ • Verify signature correctness (Ed25519) │
|
|
229
|
+
│ • Check expiry bounds │
|
|
230
|
+
│ • Validate nonce (replay protection) │
|
|
231
|
+
│ • DEFERRED: Balance, program constraints (onchain only) │
|
|
232
|
+
└──────────────────────────┬──────────────────────────────────────┘
|
|
233
|
+
↓
|
|
234
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
235
|
+
│ Phase 4: SETTLEMENT (Online) │
|
|
236
|
+
│ • Connectivity restored │
|
|
237
|
+
│ • Intent submitted to Solana │
|
|
238
|
+
│ • Program verifies signature + state │
|
|
239
|
+
│ • Settlement succeeds or fails deterministically │
|
|
240
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
**Key Invariant:** At no stage does offline execution bypass Solana's runtime or programs. Final state transitions occur **exclusively on Solana**.
|
|
244
|
+
|
|
245
|
+
---
|
|
246
|
+
|
|
247
|
+
## 💾 Local State Management & Reconciliation
|
|
248
|
+
|
|
249
|
+
TOSS maintains an **encrypted, append-only local intent store** that enables offline operation while waiting for network connectivity.
|
|
250
|
+
|
|
251
|
+
### What Gets Stored Locally
|
|
252
|
+
|
|
253
|
+
Each device maintains:
|
|
254
|
+
|
|
255
|
+
- **Outbound pending intents** - Intents created but not yet settled
|
|
256
|
+
- **Inbound received intents** - Intents from peers, awaiting verification
|
|
257
|
+
- **Synchronization status** - Track which intents have been submitted
|
|
258
|
+
- **Expiry metadata** - Automatic cleanup of expired intents
|
|
259
|
+
|
|
260
|
+
```typescript
|
|
261
|
+
import {
|
|
262
|
+
secureStoreIntent,
|
|
263
|
+
getPendingIntents,
|
|
264
|
+
getAllSecureIntents,
|
|
265
|
+
} from 'toss-expo-sdk';
|
|
266
|
+
|
|
267
|
+
// Store an intent locally (encrypted in hardware secure enclave)
|
|
268
|
+
await secureStoreIntent(intent);
|
|
269
|
+
|
|
270
|
+
// Get all pending intents
|
|
271
|
+
const pending = await getPendingIntents();
|
|
272
|
+
console.log(`${pending.length} intents waiting for settlement`);
|
|
273
|
+
|
|
274
|
+
// Later, when online...
|
|
275
|
+
const allIntents = await getAllSecureIntents();
|
|
276
|
+
console.log(`Stored intents: ${allIntents.length}`);
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
### Automatic Reconciliation
|
|
280
|
+
|
|
281
|
+
When connectivity is restored, TOSS initiates **deterministic reconciliation**:
|
|
282
|
+
|
|
283
|
+
```typescript
|
|
284
|
+
import { syncToChain, reconcilePendingIntents } from 'toss-expo-sdk';
|
|
285
|
+
|
|
286
|
+
// Sync all pending intents to Solana
|
|
287
|
+
const syncResult = await syncToChain(connection);
|
|
288
|
+
|
|
289
|
+
console.log(`Settled: ${syncResult.successfulSettlements.length}`);
|
|
290
|
+
console.log(`Failed: ${syncResult.failedSettlements.length}`);
|
|
291
|
+
console.log(`Conflicts detected: ${syncResult.detectedConflicts.length}`);
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
**Reconciliation Steps:**
|
|
295
|
+
|
|
296
|
+
1. ✅ Retrieve all pending intents from local storage
|
|
297
|
+
2. ✅ Submit to Solana
|
|
298
|
+
3. ✅ Verify signatures and state constraints onchain
|
|
299
|
+
4. ✅ Deterministically reject invalid or conflicting intents
|
|
300
|
+
5. ✅ Update local state with finality
|
|
301
|
+
|
|
302
|
+
**No offline state is treated as final.** Solana's verdict is authoritative.
|
|
303
|
+
|
|
304
|
+
---
|
|
305
|
+
|
|
306
|
+
## 🔐 Confidential Execution via Arcium (Optional)
|
|
307
|
+
|
|
308
|
+
TOSS integrates **Arcium** for confidential computation in pre-settlement stages, protecting sensitive transaction parameters before onchain execution.
|
|
309
|
+
|
|
310
|
+
### When to Use Private Transactions
|
|
311
|
+
|
|
312
|
+
By default, intents are created as regular transactions. Use `privateTransaction: true` when:
|
|
313
|
+
|
|
314
|
+
- You want to hide **transaction amounts** from peers during exchange
|
|
315
|
+
- You need to hide **recipient information** before settlement
|
|
316
|
+
- You're working with **sensitive business logic** (payments, transfers)
|
|
317
|
+
- You need **pre-settlement confidentiality** without revealing intent details
|
|
318
|
+
|
|
319
|
+
### Enabling Confidential Execution
|
|
320
|
+
|
|
321
|
+
```typescript
|
|
322
|
+
import { createUserIntent, createIntent } from 'toss-expo-sdk';
|
|
323
|
+
import { Provider } from '@project-serum/anchor';
|
|
324
|
+
|
|
325
|
+
// Confidential intent creation
|
|
326
|
+
const confidentialIntent = await createUserIntent(
|
|
327
|
+
senderUser,
|
|
328
|
+
senderKeypair,
|
|
329
|
+
recipientUser,
|
|
330
|
+
amountInLamports,
|
|
331
|
+
connection,
|
|
332
|
+
{
|
|
333
|
+
privateTransaction: true,
|
|
334
|
+
mxeProgramId: new PublicKey('YOUR_MXE_PROGRAM_ID'),
|
|
335
|
+
provider: anchorProvider, // Anchor provider for MXE operations
|
|
336
|
+
expiresIn: 60 * 60, // 1 hour expiry
|
|
337
|
+
}
|
|
338
|
+
);
|
|
339
|
+
|
|
340
|
+
console.log('Intent created with Arcium encryption');
|
|
341
|
+
console.log(`Encrypted data: ${confidentialIntent.encrypted?.ciphertext}`);
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
### How It Works
|
|
345
|
+
|
|
346
|
+
1. **Pre-Creation:** Construct intent with plaintext values
|
|
347
|
+
2. **Encryption:** Amount and metadata encrypted with Arcium
|
|
348
|
+
3. **Storage:** Encrypted intent stored locally
|
|
349
|
+
4. **Exchange:** Encrypted parameters transmitted (peer cannot decrypt)
|
|
350
|
+
5. **Settlement:** Solana program decrypts and executes (onchain verification)
|
|
351
|
+
|
|
352
|
+
### Security Model
|
|
353
|
+
|
|
354
|
+
- **Encryption scope:** Optional (amount, metadata)
|
|
355
|
+
- **Key management:** Arcium handles encryption/decryption
|
|
356
|
+
- **Signature:** Intent signature covers plaintext (authenticity verified before decryption)
|
|
357
|
+
- **Onchain verification:** Only Solana MXE program can decrypt and validate
|
|
358
|
+
|
|
359
|
+
**Important:** Arcium encryption is **optional** and operates strictly before onchain execution. It does **not** alter Solana's trust model — the blockchain remains the final authority.
|
|
360
|
+
|
|
361
|
+
---
|
|
362
|
+
|
|
363
|
+
TOSS now includes **production-ready offline transaction support** using Solana's durable nonce accounts with biometric protection.
|
|
364
|
+
|
|
365
|
+
### Quick Start: Offline Transactions
|
|
366
|
+
|
|
367
|
+
```typescript
|
|
368
|
+
import {
|
|
369
|
+
useOfflineTransaction,
|
|
370
|
+
useBLETransactionTransmission,
|
|
371
|
+
useNonceAccountManagement,
|
|
372
|
+
createOfflineIntent,
|
|
373
|
+
AuthService,
|
|
374
|
+
} from 'toss-expo-sdk';
|
|
375
|
+
import { SystemProgram } from '@solana/web3.js';
|
|
376
|
+
|
|
377
|
+
// Step 1: Create nonce account (requires biometric)
|
|
378
|
+
const updatedUser = await AuthService.createSecureNonceAccount(
|
|
379
|
+
user,
|
|
380
|
+
connection,
|
|
381
|
+
userKeypair
|
|
382
|
+
);
|
|
383
|
+
|
|
384
|
+
// Step 2: Create offline transaction using custom hook
|
|
385
|
+
const { createOfflineTransaction } = useOfflineTransaction(user, connection);
|
|
386
|
+
|
|
387
|
+
const offlineTx = await createOfflineTransaction([
|
|
388
|
+
SystemProgram.transfer({
|
|
389
|
+
fromPubkey: user.wallet.publicKey,
|
|
390
|
+
toPubkey: recipientAddress,
|
|
391
|
+
lamports: amount,
|
|
392
|
+
}),
|
|
393
|
+
]);
|
|
394
|
+
|
|
395
|
+
// Step 3: Send via BLE with automatic fragmentation
|
|
396
|
+
const { sendTransactionBLE } = useBLETransactionTransmission('ios');
|
|
397
|
+
|
|
398
|
+
const result = await sendTransactionBLE(
|
|
399
|
+
bleDevice,
|
|
400
|
+
offlineTx,
|
|
401
|
+
noiseEncryptFn, // Optional Noise Protocol encryption
|
|
402
|
+
false // This is a transaction, not an intent
|
|
403
|
+
);
|
|
404
|
+
|
|
405
|
+
console.log(`Sent ${result.sentFragments}/${result.totalFragments} fragments`);
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
### Key Features
|
|
409
|
+
|
|
410
|
+
**Biometric Protection**
|
|
411
|
+
|
|
412
|
+
- ✅ All nonce operations require biometric authentication
|
|
413
|
+
- ✅ Private keys stored in device's secure enclave (iOS Secure Enclave / Android Keymaster)
|
|
414
|
+
- ✅ User cannot export or backup private keys
|
|
415
|
+
|
|
416
|
+
**Replay Protection**
|
|
417
|
+
|
|
418
|
+
- ✅ Each transaction uses a unique, incrementing nonce from the blockchain
|
|
419
|
+
- ✅ Nonce values automatically validated before transaction execution
|
|
420
|
+
- ✅ Expired nonces automatically detected and rejected
|
|
421
|
+
|
|
422
|
+
**BLE Transmission**
|
|
423
|
+
|
|
424
|
+
- ✅ Automatic MTU-aware message fragmentation
|
|
425
|
+
- ✅ CRC32 checksum verification for each fragment
|
|
426
|
+
- ✅ Automatic retry with exponential backoff
|
|
427
|
+
- ✅ Noise Protocol encryption support
|
|
428
|
+
|
|
429
|
+
**Storage & Renewal**
|
|
430
|
+
|
|
431
|
+
- ✅ Nonce accounts cached efficiently in memory
|
|
432
|
+
- ✅ Automatic renewal from blockchain
|
|
433
|
+
- ✅ Graceful expiry handling with status tracking
|
|
434
|
+
|
|
435
|
+
### Detailed Guide
|
|
436
|
+
|
|
437
|
+
For comprehensive documentation, guides, and advanced usage examples, see [OFFLINE_TRANSACTIONS_GUIDE.md](./OFFLINE_TRANSACTIONS_GUIDE.md).
|
|
438
|
+
|
|
439
|
+
Topics covered:
|
|
440
|
+
|
|
441
|
+
- Architecture overview
|
|
442
|
+
- Setup guide with step-by-step examples
|
|
443
|
+
- Custom React hooks API reference
|
|
444
|
+
- Noise Protocol integration
|
|
445
|
+
- Security considerations
|
|
446
|
+
- Error handling patterns
|
|
447
|
+
- Testing strategies
|
|
448
|
+
- Migration guide from previous versions
|
|
112
449
|
|
|
113
450
|
---
|
|
114
451
|
|
|
115
452
|
## 🔒 Security
|
|
116
453
|
|
|
454
|
+
### TOSS Security Guarantees
|
|
455
|
+
|
|
456
|
+
TOSS guarantees (per Technical Paper, Section 13):
|
|
457
|
+
|
|
458
|
+
1. **No Unauthorized Signing**
|
|
459
|
+
- Only users with access to their private keys can create intents
|
|
460
|
+
- Ed25519 signatures cannot be forged
|
|
461
|
+
- Biometric protection for nonce accounts (mandatory)
|
|
462
|
+
|
|
463
|
+
2. **No Offline State Mutation**
|
|
464
|
+
- Offline operations only create local intents (append-only store)
|
|
465
|
+
- No global state changes until onchain settlement
|
|
466
|
+
- Solana remains the sole authority for state transitions
|
|
467
|
+
|
|
468
|
+
3. **No Forced Execution**
|
|
469
|
+
- Signers can choose whether to submit intents
|
|
470
|
+
- Intents can be rejected before settlement
|
|
471
|
+
- Both parties must verify before committing to offline transfer
|
|
472
|
+
|
|
473
|
+
4. **Deterministic Settlement**
|
|
474
|
+
- Settlement outcomes are deterministic based on onchain state
|
|
475
|
+
- Conflict resolution is deterministic (nonce + timestamp)
|
|
476
|
+
- Failures are safe and recoverable
|
|
477
|
+
|
|
478
|
+
5. **Confidential Pre-Settlement Handling**
|
|
479
|
+
- Optional Arcium encryption before submission
|
|
480
|
+
- Private transaction metadata protected from peers
|
|
481
|
+
- Onchain verification ensures integrity
|
|
482
|
+
|
|
483
|
+
**Important:** Offline capability does **not** expand Solana's attack surface. All security guarantees come from cryptography and onchain verification, not the offline layer.
|
|
484
|
+
|
|
117
485
|
### Best Practices
|
|
118
486
|
|
|
119
487
|
1. Always verify transaction details before signing
|
|
@@ -140,23 +508,28 @@ The SDK provides detailed error codes for handling various scenarios. All errors
|
|
|
140
508
|
#### Error Handling Example
|
|
141
509
|
|
|
142
510
|
```typescript
|
|
143
|
-
import {
|
|
511
|
+
import { TossError } from 'toss-expo-sdk';
|
|
144
512
|
|
|
145
513
|
try {
|
|
146
|
-
await
|
|
514
|
+
const intent = await createSignedIntent(...);
|
|
515
|
+
await secureStoreIntent(intent);
|
|
147
516
|
} catch (error) {
|
|
148
|
-
if (error
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
517
|
+
if (error instanceof TossError) {
|
|
518
|
+
switch (error.code) {
|
|
519
|
+
case 'INSUFFICIENT_FUNDS':
|
|
520
|
+
console.error('Insufficient funds for this transaction');
|
|
521
|
+
break;
|
|
522
|
+
case 'INTENT_EXPIRED':
|
|
523
|
+
console.error('This transaction intent has expired');
|
|
524
|
+
break;
|
|
525
|
+
case 'NETWORK_ERROR':
|
|
526
|
+
console.error('Network error occurred. Please check your connection.');
|
|
527
|
+
break;
|
|
528
|
+
default:
|
|
529
|
+
console.error(`Error [${error.code}]: ${error.message}`);
|
|
530
|
+
}
|
|
157
531
|
} else {
|
|
158
|
-
|
|
159
|
-
console.error('An error occurred:', error.message);
|
|
532
|
+
console.error('An unexpected error occurred:', error);
|
|
160
533
|
}
|
|
161
534
|
}
|
|
162
535
|
```
|
|
@@ -234,36 +607,82 @@ console.log(`Losers: ${resolution.losers.map((i) => i.id)}`);
|
|
|
234
607
|
Devices can discover and exchange intents with nearby peers:
|
|
235
608
|
|
|
236
609
|
```typescript
|
|
237
|
-
import {
|
|
238
|
-
import { startTossScan } from 'toss-expo-sdk';
|
|
610
|
+
import { DeviceDiscoveryService, startTossScan } from 'toss-expo-sdk';
|
|
239
611
|
|
|
240
|
-
|
|
612
|
+
const discovery = new DeviceDiscoveryService();
|
|
613
|
+
|
|
614
|
+
// Scan for nearby TOSS devices via BLE
|
|
241
615
|
startTossScan(
|
|
242
616
|
(user, device) => {
|
|
243
617
|
// Register discovered peer
|
|
244
|
-
|
|
618
|
+
discovery.registerPeer({
|
|
245
619
|
id: device.id,
|
|
246
620
|
user,
|
|
247
621
|
lastSeen: Date.now(),
|
|
248
622
|
transport: 'ble',
|
|
249
623
|
});
|
|
250
|
-
|
|
251
|
-
// Create intent exchange request
|
|
252
|
-
const request = intentExchange.createRequest(
|
|
253
|
-
myIntent,
|
|
254
|
-
'my-device-id',
|
|
255
|
-
undefined,
|
|
256
|
-
5 * 60 // 5 minute expiry
|
|
257
|
-
);
|
|
258
|
-
|
|
259
|
-
console.log(`Exchange request created: ${request.requestId}`);
|
|
624
|
+
console.log(`Peer discovered: ${device.id}`);
|
|
260
625
|
},
|
|
261
626
|
(intent, device) => {
|
|
262
627
|
console.log(`Received intent from ${device.id}`);
|
|
263
628
|
}
|
|
264
629
|
);
|
|
630
|
+
|
|
631
|
+
// Get active peers
|
|
632
|
+
const activePeers = discovery.getActivePeers();
|
|
633
|
+
console.log(`${activePeers.length} active peers nearby`);
|
|
265
634
|
```
|
|
266
635
|
|
|
636
|
+
## ⚠️ Understanding TOSS Limitations
|
|
637
|
+
|
|
638
|
+
TOSS is designed to fail safely. The following limitations are **by design** and fundamental to any offline system:
|
|
639
|
+
|
|
640
|
+
### What TOSS Does NOT Do
|
|
641
|
+
|
|
642
|
+
❌ **Does not resolve offline double-spend**
|
|
643
|
+
|
|
644
|
+
- Multiple devices can create conflicting intents offline
|
|
645
|
+
- Conflicts are resolved deterministically onchain (first valid intent wins)
|
|
646
|
+
- You must verify transaction details before committing to offline transfer
|
|
647
|
+
|
|
648
|
+
❌ **Does not guarantee settlement success**
|
|
649
|
+
|
|
650
|
+
- Intents may fail onchain due to insufficient balance, nonce violations, or expired intents
|
|
651
|
+
- Settlement is deterministic but failures can occur
|
|
652
|
+
- Always check sync results for failed intents
|
|
653
|
+
|
|
654
|
+
❌ **Does not replace Solana consensus**
|
|
655
|
+
|
|
656
|
+
- TOSS extends operational boundaries but does not modify Solana's execution semantics
|
|
657
|
+
- All final state transitions occur exclusively on Solana
|
|
658
|
+
- Transaction validity is determined by Solana's runtime, not TOSS
|
|
659
|
+
|
|
660
|
+
❌ **Does not eliminate finality dependency on network**
|
|
661
|
+
|
|
662
|
+
- Finality still requires network connectivity to Solana
|
|
663
|
+
- Offline operation defers settlement, not finality
|
|
664
|
+
- You cannot validate final state without connectivity
|
|
665
|
+
|
|
666
|
+
### Settlement Guarantees
|
|
667
|
+
|
|
668
|
+
TOSS **guarantees**:
|
|
669
|
+
|
|
670
|
+
- ✅ Signatures cannot be forged (Ed25519 cryptography)
|
|
671
|
+
- ✅ Offline state never mutates global state (local-only storage)
|
|
672
|
+
- ✅ All artifacts are verifiable onchain (deterministic settlement)
|
|
673
|
+
- ✅ Conflicts resolve deterministically (based on nonce and timestamp)
|
|
674
|
+
- ✅ Pre-settlement confidentiality via Arcium (optional)
|
|
675
|
+
|
|
676
|
+
But TOSS **does not guarantee**:
|
|
677
|
+
|
|
678
|
+
- ❌ That your intent will successfully settle (dependent on onchain state)
|
|
679
|
+
- ❌ Prevention of offline double-spend (conflicts detected onchain)
|
|
680
|
+
- ❌ Immediate finality (requires network connectivity)
|
|
681
|
+
|
|
682
|
+
**The distinction:** TOSS guarantees **protocol correctness**, not **settlement success**. These are different requirements.
|
|
683
|
+
|
|
684
|
+
---
|
|
685
|
+
|
|
267
686
|
## 🌐 Network Support
|
|
268
687
|
|
|
269
688
|
| Network | Status | RPC Endpoint |
|
|
@@ -272,21 +691,11 @@ startTossScan(
|
|
|
272
691
|
| Testnet | ✅ Live | https://api.testnet.solana.com |
|
|
273
692
|
| Mainnet | ✅ Live | https://api.mainnet-beta.solana.com |
|
|
274
693
|
|
|
275
|
-
## 📚
|
|
694
|
+
## 📚 Complete Documentation
|
|
276
695
|
|
|
277
|
-
For
|
|
278
|
-
|
|
279
|
-
- Full API reference
|
|
280
|
-
- Architecture diagrams
|
|
281
|
-
- Performance considerations
|
|
282
|
-
- Future improvements
|
|
283
|
-
|
|
284
|
-
See [IMPLEMENTATION.md](./IMPLEMENTATION.md)
|
|
285
|
-
|
|
286
|
-
## 🌐 Network Support
|
|
696
|
+
For comprehensive information, see:
|
|
287
697
|
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
| Mainnet | ✅ Live | https://api.mainnet.solana.com |
|
|
698
|
+
- [DOCUMENTATION_INDEX.md](./DOCUMENTATION_INDEX.md) - Complete documentation guide
|
|
699
|
+
- [TOSS_PAPER_IMPLEMENTATION_VERIFICATION.md](./TOSS_PAPER_IMPLEMENTATION_VERIFICATION.md) - TOSS paper compliance
|
|
700
|
+
- [PAPER_VS_IMPLEMENTATION_AUDIT.md](./PAPER_VS_IMPLEMENTATION_AUDIT.md) - Detailed technical audit
|
|
701
|
+
- [USER_SECURITY_AND_KEY_MANAGEMENT.md](./USER_SECURITY_AND_KEY_MANAGEMENT.md) - Security model and biometric protection
|