@aztec/wallet-sdk 0.0.1-commit.7d4e6cd β 0.0.1-commit.9372f48
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 +218 -355
- package/dest/base-wallet/base_wallet.d.ts +33 -10
- package/dest/base-wallet/base_wallet.d.ts.map +1 -1
- package/dest/base-wallet/base_wallet.js +53 -16
- package/dest/crypto.d.ts +73 -27
- package/dest/crypto.d.ts.map +1 -1
- package/dest/crypto.js +219 -41
- package/dest/emoji_alphabet.d.ts +35 -0
- package/dest/emoji_alphabet.d.ts.map +1 -0
- package/dest/emoji_alphabet.js +299 -0
- package/dest/extension/handlers/background_connection_handler.d.ts +158 -0
- package/dest/extension/handlers/background_connection_handler.d.ts.map +1 -0
- package/dest/extension/handlers/background_connection_handler.js +258 -0
- package/dest/extension/handlers/content_script_connection_handler.d.ts +56 -0
- package/dest/extension/handlers/content_script_connection_handler.d.ts.map +1 -0
- package/dest/extension/handlers/content_script_connection_handler.js +174 -0
- package/dest/extension/handlers/index.d.ts +12 -0
- package/dest/extension/handlers/index.d.ts.map +1 -0
- package/dest/extension/handlers/index.js +10 -0
- package/dest/extension/handlers/internal_message_types.d.ts +63 -0
- package/dest/extension/handlers/internal_message_types.d.ts.map +1 -0
- package/dest/extension/handlers/internal_message_types.js +22 -0
- package/dest/extension/provider/extension_provider.d.ts +107 -0
- package/dest/extension/provider/extension_provider.d.ts.map +1 -0
- package/dest/extension/provider/extension_provider.js +160 -0
- package/dest/extension/provider/extension_wallet.d.ts +131 -0
- package/dest/extension/provider/extension_wallet.d.ts.map +1 -0
- package/dest/extension/provider/extension_wallet.js +271 -0
- package/dest/extension/provider/index.d.ts +3 -0
- package/dest/extension/provider/index.d.ts.map +1 -0
- package/dest/{providers/extension β extension/provider}/index.js +0 -1
- package/dest/manager/index.d.ts +2 -7
- package/dest/manager/index.d.ts.map +1 -1
- package/dest/manager/index.js +0 -4
- package/dest/manager/types.d.ts +108 -5
- package/dest/manager/types.d.ts.map +1 -1
- package/dest/manager/types.js +17 -1
- package/dest/manager/wallet_manager.d.ts +50 -7
- package/dest/manager/wallet_manager.d.ts.map +1 -1
- package/dest/manager/wallet_manager.js +178 -29
- package/dest/types.d.ts +55 -15
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +10 -2
- package/package.json +11 -10
- package/src/base-wallet/base_wallet.ts +74 -28
- package/src/crypto.ts +263 -47
- package/src/emoji_alphabet.ts +317 -0
- package/src/extension/handlers/background_connection_handler.ts +423 -0
- package/src/extension/handlers/content_script_connection_handler.ts +246 -0
- package/src/extension/handlers/index.ts +25 -0
- package/src/extension/handlers/internal_message_types.ts +69 -0
- package/src/extension/provider/extension_provider.ts +233 -0
- package/src/extension/provider/extension_wallet.ts +321 -0
- package/src/extension/provider/index.ts +7 -0
- package/src/manager/index.ts +3 -9
- package/src/manager/types.ts +112 -4
- package/src/manager/wallet_manager.ts +204 -31
- package/src/types.ts +57 -14
- package/dest/providers/extension/extension_provider.d.ts +0 -17
- package/dest/providers/extension/extension_provider.d.ts.map +0 -1
- package/dest/providers/extension/extension_provider.js +0 -56
- package/dest/providers/extension/extension_wallet.d.ts +0 -95
- package/dest/providers/extension/extension_wallet.d.ts.map +0 -1
- package/dest/providers/extension/extension_wallet.js +0 -225
- package/dest/providers/extension/index.d.ts +0 -5
- package/dest/providers/extension/index.d.ts.map +0 -1
- package/src/providers/extension/extension_provider.ts +0 -72
- package/src/providers/extension/extension_wallet.ts +0 -275
- package/src/providers/extension/index.ts +0 -11
package/src/crypto.ts
CHANGED
|
@@ -4,34 +4,61 @@
|
|
|
4
4
|
* This module provides ECDH key exchange and AES-GCM encryption primitives
|
|
5
5
|
* for establishing secure communication channels between dApps and wallet extensions.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
7
|
+
* ## Security Model
|
|
8
|
+
*
|
|
9
|
+
* The crypto flow uses HKDF for key derivation with domain separation:
|
|
10
|
+
*
|
|
8
11
|
* 1. Both parties generate ECDH key pairs using {@link generateKeyPair}
|
|
9
12
|
* 2. Public keys are exchanged (exported via {@link exportPublicKey}, imported via {@link importPublicKey})
|
|
10
|
-
* 3. Both parties derive
|
|
11
|
-
*
|
|
13
|
+
* 3. Both parties derive keys using {@link deriveSessionKeys}:
|
|
14
|
+
* - ECDH produces raw shared secret
|
|
15
|
+
* - HKDF expands the secret into 512 bits using concatenated public keys as salt
|
|
16
|
+
* - The 512 bits are split: first 256 bits for AES-GCM, second 256 bits for HMAC
|
|
17
|
+
* 4. Fingerprint is computed as HMAC(HMAC_KEY, "aztec-wallet-verification-verificationHash")
|
|
18
|
+
* 5. Messages are encrypted/decrypted using {@link encrypt} and {@link decrypt}
|
|
19
|
+
*
|
|
20
|
+
* This design ensures:
|
|
21
|
+
* - The encryption key is never exposed (verificationHash uses separate HMAC key)
|
|
22
|
+
* - Public keys are bound to the derived keys via HKDF salt
|
|
23
|
+
* - Single HKDF derivation with domain-separated output splitting
|
|
24
|
+
*
|
|
25
|
+
* ## Curve Choice
|
|
26
|
+
*
|
|
27
|
+
* We use P-256 (secp256r1) because it's the only ECDH curve with broad Web Crypto API
|
|
28
|
+
* support across all browsers. X25519 would be preferable for its simplicity and
|
|
29
|
+
* resistance to implementation errors, but it lacks universal browser support.
|
|
12
30
|
*
|
|
13
31
|
* @example
|
|
14
32
|
* ```typescript
|
|
15
|
-
* // Party A
|
|
33
|
+
* // Party A (dApp)
|
|
16
34
|
* const keyPairA = await generateKeyPair();
|
|
17
35
|
* const publicKeyA = await exportPublicKey(keyPairA.publicKey);
|
|
18
36
|
*
|
|
19
|
-
* // Party B
|
|
37
|
+
* // Party B (wallet)
|
|
20
38
|
* const keyPairB = await generateKeyPair();
|
|
21
39
|
* const publicKeyB = await exportPublicKey(keyPairB.publicKey);
|
|
22
40
|
*
|
|
23
|
-
* // Exchange public keys, then derive
|
|
41
|
+
* // Exchange public keys, then derive session keys
|
|
42
|
+
* // App side: isApp = true
|
|
24
43
|
* const importedB = await importPublicKey(publicKeyB);
|
|
25
|
-
* const
|
|
44
|
+
* const sessionA = await deriveSessionKeys(keyPairA, importedB, true);
|
|
45
|
+
*
|
|
46
|
+
* // Wallet side: isApp = false
|
|
47
|
+
* const importedA = await importPublicKey(publicKeyA);
|
|
48
|
+
* const sessionB = await deriveSessionKeys(keyPairB, importedA, false);
|
|
49
|
+
*
|
|
50
|
+
* // Both parties compute the same verificationHash for verification
|
|
51
|
+
* const verificationHashA = sessionA.verificationHash;
|
|
52
|
+
* const emojiA = hashToEmoji(verificationHashA);
|
|
26
53
|
*
|
|
27
54
|
* // Encrypt and decrypt
|
|
28
|
-
* const encrypted = await encrypt(
|
|
29
|
-
* const decrypted = await decrypt(
|
|
55
|
+
* const encrypted = await encrypt(sessionA.encryptionKey, JSON.stringify({ message: 'hello' }));
|
|
56
|
+
* const decrypted = await decrypt(sessionB.encryptionKey, encrypted);
|
|
30
57
|
* ```
|
|
31
58
|
*
|
|
32
59
|
* @packageDocumentation
|
|
33
60
|
*/
|
|
34
|
-
import {
|
|
61
|
+
import { EMOJI_ALPHABET, EMOJI_ALPHABET_SIZE } from './emoji_alphabet.js';
|
|
35
62
|
|
|
36
63
|
/**
|
|
37
64
|
* Exported public key in JWK format for transmission over untrusted channels.
|
|
@@ -74,11 +101,31 @@ export interface SecureKeyPair {
|
|
|
74
101
|
privateKey: CryptoKey;
|
|
75
102
|
}
|
|
76
103
|
|
|
104
|
+
/**
|
|
105
|
+
* Session keys derived from ECDH key exchange.
|
|
106
|
+
*
|
|
107
|
+
* Contains both the encryption key and the verification hash (verificationHash)
|
|
108
|
+
* computed from a separate HMAC key.
|
|
109
|
+
*/
|
|
110
|
+
export interface SessionKeys {
|
|
111
|
+
/** AES-256-GCM key for message encryption/decryption */
|
|
112
|
+
encryptionKey: CryptoKey;
|
|
113
|
+
/** Hex-encoded verificationHash for verification */
|
|
114
|
+
verificationHash: string;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** P-256 coordinate size in bytes */
|
|
118
|
+
const P256_COORDINATE_SIZE = 32;
|
|
119
|
+
|
|
120
|
+
// HKDF info string for key derivation
|
|
121
|
+
const HKDF_INFO = new TextEncoder().encode('Aztec Wallet DAPP Key derivation');
|
|
122
|
+
const FINGERPRINT_DATA = new TextEncoder().encode('aztec-wallet-verification-verificationHash');
|
|
123
|
+
|
|
77
124
|
/**
|
|
78
125
|
* Generates an ECDH P-256 key pair for key exchange.
|
|
79
126
|
*
|
|
80
|
-
* The generated key pair can be used to derive
|
|
81
|
-
* party's public key using {@link
|
|
127
|
+
* The generated key pair can be used to derive session keys with another
|
|
128
|
+
* party's public key using {@link deriveSessionKeys}.
|
|
82
129
|
*
|
|
83
130
|
* @returns A new ECDH key pair
|
|
84
131
|
*
|
|
@@ -95,8 +142,8 @@ export async function generateKeyPair(): Promise<SecureKeyPair> {
|
|
|
95
142
|
name: 'ECDH',
|
|
96
143
|
namedCurve: 'P-256',
|
|
97
144
|
},
|
|
98
|
-
true, // extractable (needed to export public key)
|
|
99
|
-
['
|
|
145
|
+
true, // extractable (needed to export public key and derive bits)
|
|
146
|
+
['deriveBits'],
|
|
100
147
|
);
|
|
101
148
|
return {
|
|
102
149
|
publicKey: keyPair.publicKey,
|
|
@@ -133,16 +180,16 @@ export async function exportPublicKey(publicKey: CryptoKey): Promise<ExportedPub
|
|
|
133
180
|
/**
|
|
134
181
|
* Imports a public key from JWK format.
|
|
135
182
|
*
|
|
136
|
-
* Used to import the other party's public key for deriving
|
|
183
|
+
* Used to import the other party's public key for deriving session keys.
|
|
137
184
|
*
|
|
138
185
|
* @param exported - The public key in JWK format
|
|
139
|
-
* @returns A CryptoKey that can be used with {@link
|
|
186
|
+
* @returns A CryptoKey that can be used with {@link deriveSessionKeys}
|
|
140
187
|
*
|
|
141
188
|
* @example
|
|
142
189
|
* ```typescript
|
|
143
|
-
* //
|
|
144
|
-
* const
|
|
145
|
-
* const
|
|
190
|
+
* // App side: receive wallet's public key and derive session
|
|
191
|
+
* const walletPublicKey = await importPublicKey(receivedWalletKey);
|
|
192
|
+
* const session = await deriveSessionKeys(appKeyPair, walletPublicKey, true);
|
|
146
193
|
* ```
|
|
147
194
|
*/
|
|
148
195
|
export function importPublicKey(exported: ExportedPublicKey): Promise<CryptoKey> {
|
|
@@ -158,67 +205,171 @@ export function importPublicKey(exported: ExportedPublicKey): Promise<CryptoKey>
|
|
|
158
205
|
name: 'ECDH',
|
|
159
206
|
namedCurve: 'P-256',
|
|
160
207
|
},
|
|
161
|
-
|
|
208
|
+
true, // extractable - needed for deriveSessionKeys to export for salt. Safe for public keys.
|
|
162
209
|
[],
|
|
163
210
|
);
|
|
164
211
|
}
|
|
165
212
|
|
|
166
213
|
/**
|
|
167
|
-
*
|
|
214
|
+
* Decodes a base64url-encoded coordinate to fixed-size bytes.
|
|
215
|
+
*
|
|
216
|
+
* For P-256, coordinates are always 32 bytes. This function ensures
|
|
217
|
+
* consistent serialization regardless of leading zeros.
|
|
218
|
+
*
|
|
219
|
+
* @param base64url - Base64url-encoded coordinate
|
|
220
|
+
* @param size - Expected size in bytes (32 for P-256)
|
|
221
|
+
* @returns Fixed-size Uint8Array, left-padded with zeros if needed
|
|
222
|
+
*/
|
|
223
|
+
function decodeCoordinateFixedSize(base64url: string, size: number): Uint8Array {
|
|
224
|
+
const decoded = base64UrlToBytes(base64url);
|
|
225
|
+
if (decoded.length === size) {
|
|
226
|
+
return decoded;
|
|
227
|
+
}
|
|
228
|
+
if (decoded.length > size) {
|
|
229
|
+
throw new Error(`Invalid P-256 coordinate: expected ${size} bytes, got ${decoded.length}`);
|
|
230
|
+
}
|
|
231
|
+
// Left-pad with zeros
|
|
232
|
+
const padded = new Uint8Array(size);
|
|
233
|
+
padded.set(decoded, size - decoded.length);
|
|
234
|
+
return padded;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Creates HKDF salt from public keys with fixed ordering by party role.
|
|
239
|
+
*
|
|
240
|
+
* The app's public key always comes first, followed by the wallet's public key.
|
|
241
|
+
* This ensures both parties produce the same salt.
|
|
242
|
+
*
|
|
243
|
+
* @param appKey - The app's public key in exported format
|
|
244
|
+
* @param walletKey - The wallet's public key in exported format
|
|
245
|
+
* @returns Concatenated bytes: app_x || app_y || wallet_x || wallet_y (128 bytes for P-256)
|
|
246
|
+
*/
|
|
247
|
+
function createSaltFromPublicKeys(appKey: ExportedPublicKey, walletKey: ExportedPublicKey): ArrayBuffer {
|
|
248
|
+
// Fixed ordering: app first, then wallet
|
|
249
|
+
// Each coordinate is fixed at 32 bytes for P-256
|
|
250
|
+
const appX = decodeCoordinateFixedSize(appKey.x, P256_COORDINATE_SIZE);
|
|
251
|
+
const appY = decodeCoordinateFixedSize(appKey.y, P256_COORDINATE_SIZE);
|
|
252
|
+
const walletX = decodeCoordinateFixedSize(walletKey.x, P256_COORDINATE_SIZE);
|
|
253
|
+
const walletY = decodeCoordinateFixedSize(walletKey.y, P256_COORDINATE_SIZE);
|
|
254
|
+
|
|
255
|
+
// Total: 4 * 32 = 128 bytes
|
|
256
|
+
const salt = new Uint8Array(4 * P256_COORDINATE_SIZE);
|
|
257
|
+
salt.set(appX, 0);
|
|
258
|
+
salt.set(appY, P256_COORDINATE_SIZE);
|
|
259
|
+
salt.set(walletX, 2 * P256_COORDINATE_SIZE);
|
|
260
|
+
salt.set(walletY, 3 * P256_COORDINATE_SIZE);
|
|
261
|
+
|
|
262
|
+
return salt.buffer as ArrayBuffer;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Derives session keys from ECDH key exchange using HKDF.
|
|
168
267
|
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
268
|
+
* This is the main key derivation function that produces:
|
|
269
|
+
* 1. An AES-256-GCM encryption key (first 256 bits)
|
|
270
|
+
* 2. An HMAC key for verificationHash computation (second 256 bits)
|
|
271
|
+
* 3. A verificationHash computed as HMAC(hmacKey, "aztec-wallet-verification-verificationHash")
|
|
171
272
|
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
273
|
+
* The keys are derived using a single HKDF call that produces 512 bits,
|
|
274
|
+
* then split into the two keys.
|
|
275
|
+
*
|
|
276
|
+
* @param ownKeyPair - The caller's ECDH key pair (private for ECDH, public for salt)
|
|
277
|
+
* @param peerPublicKey - The peer's ECDH public key (for ECDH and salt)
|
|
278
|
+
* @param isApp - true if caller is the app, false if caller is the wallet
|
|
279
|
+
* @returns Session keys containing encryption key and verificationHash
|
|
175
280
|
*
|
|
176
281
|
* @example
|
|
177
282
|
* ```typescript
|
|
178
|
-
* //
|
|
179
|
-
* const
|
|
180
|
-
*
|
|
181
|
-
*
|
|
283
|
+
* // App side
|
|
284
|
+
* const sessionA = await deriveSessionKeys(appKeyPair, walletPublicKey, true);
|
|
285
|
+
* // Wallet side
|
|
286
|
+
* const sessionB = await deriveSessionKeys(walletKeyPair, appPublicKey, false);
|
|
287
|
+
* // sessionA.verificationHash === sessionB.verificationHash
|
|
182
288
|
* ```
|
|
183
289
|
*/
|
|
184
|
-
export function
|
|
185
|
-
|
|
290
|
+
export async function deriveSessionKeys(
|
|
291
|
+
ownKeyPair: SecureKeyPair,
|
|
292
|
+
peerPublicKey: CryptoKey,
|
|
293
|
+
isApp: boolean,
|
|
294
|
+
): Promise<SessionKeys> {
|
|
295
|
+
// Step 1: ECDH to get raw shared secret
|
|
296
|
+
const sharedSecretBits = await crypto.subtle.deriveBits(
|
|
186
297
|
{
|
|
187
298
|
name: 'ECDH',
|
|
188
|
-
public:
|
|
299
|
+
public: peerPublicKey,
|
|
189
300
|
},
|
|
190
|
-
privateKey,
|
|
301
|
+
ownKeyPair.privateKey,
|
|
302
|
+
256,
|
|
303
|
+
);
|
|
304
|
+
|
|
305
|
+
// Step 2: Import shared secret as HKDF key material
|
|
306
|
+
const hkdfKey = await crypto.subtle.importKey('raw', sharedSecretBits, { name: 'HKDF' }, false, ['deriveBits']);
|
|
307
|
+
|
|
308
|
+
// Step 3: Export public keys and create salt (app first, wallet second)
|
|
309
|
+
const ownExportedKey = await exportPublicKey(ownKeyPair.publicKey);
|
|
310
|
+
const peerExportedKey = await exportPublicKey(peerPublicKey);
|
|
311
|
+
const appPublicKey = isApp ? ownExportedKey : peerExportedKey;
|
|
312
|
+
const walletPublicKey = isApp ? peerExportedKey : ownExportedKey;
|
|
313
|
+
const salt = createSaltFromPublicKeys(appPublicKey, walletPublicKey);
|
|
314
|
+
|
|
315
|
+
// Step 4: Derive 512 bits in a single HKDF call
|
|
316
|
+
const derivedBits = await crypto.subtle.deriveBits(
|
|
191
317
|
{
|
|
192
|
-
name: '
|
|
193
|
-
|
|
318
|
+
name: 'HKDF',
|
|
319
|
+
hash: 'SHA-256',
|
|
320
|
+
salt,
|
|
321
|
+
info: HKDF_INFO,
|
|
194
322
|
},
|
|
195
|
-
|
|
196
|
-
|
|
323
|
+
hkdfKey,
|
|
324
|
+
512, // 256 bits for GCM + 256 bits for HMAC
|
|
197
325
|
);
|
|
326
|
+
|
|
327
|
+
// Step 5: Split into GCM key (first 256 bits) and HMAC key (second 256 bits)
|
|
328
|
+
const gcmKeyBits = derivedBits.slice(0, 32);
|
|
329
|
+
const hmacKeyBits = derivedBits.slice(32, 64);
|
|
330
|
+
|
|
331
|
+
// Step 6: Import GCM key
|
|
332
|
+
const encryptionKey = await crypto.subtle.importKey('raw', gcmKeyBits, { name: 'AES-GCM', length: 256 }, false, [
|
|
333
|
+
'encrypt',
|
|
334
|
+
'decrypt',
|
|
335
|
+
]);
|
|
336
|
+
|
|
337
|
+
// Step 7: Import HMAC key
|
|
338
|
+
const hmacKey = await crypto.subtle.importKey('raw', hmacKeyBits, { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']);
|
|
339
|
+
|
|
340
|
+
// Step 8: Compute verificationHash as HMAC of fixed string
|
|
341
|
+
const verificationHashBytes = await crypto.subtle.sign('HMAC', hmacKey, FINGERPRINT_DATA);
|
|
342
|
+
|
|
343
|
+
// Convert to hex string
|
|
344
|
+
const verificationHash = arrayBufferToHex(verificationHashBytes);
|
|
345
|
+
|
|
346
|
+
return {
|
|
347
|
+
encryptionKey,
|
|
348
|
+
verificationHash,
|
|
349
|
+
};
|
|
198
350
|
}
|
|
199
351
|
|
|
200
352
|
/**
|
|
201
353
|
* Encrypts data using AES-256-GCM.
|
|
202
354
|
*
|
|
203
|
-
*
|
|
204
|
-
* generated for each encryption operation.
|
|
355
|
+
* A random 12-byte IV is generated for each encryption operation.
|
|
205
356
|
*
|
|
206
357
|
* AES-GCM provides both confidentiality and authenticity - any tampering
|
|
207
358
|
* with the ciphertext will cause decryption to fail.
|
|
208
359
|
*
|
|
209
|
-
* @param key - The AES-GCM key (from {@link
|
|
210
|
-
* @param data - The data to encrypt (
|
|
360
|
+
* @param key - The AES-GCM key (from {@link deriveSessionKeys})
|
|
361
|
+
* @param data - The string data to encrypt (caller is responsible for serialization)
|
|
211
362
|
* @returns The encrypted payload with IV and ciphertext
|
|
212
363
|
*
|
|
213
364
|
* @example
|
|
214
365
|
* ```typescript
|
|
215
|
-
* const encrypted = await encrypt(
|
|
366
|
+
* const encrypted = await encrypt(session.encryptionKey, JSON.stringify({ action: 'transfer', amount: 100 }));
|
|
216
367
|
* // encrypted.iv and encrypted.ciphertext are base64 strings
|
|
217
368
|
* ```
|
|
218
369
|
*/
|
|
219
|
-
export async function encrypt(key: CryptoKey, data:
|
|
370
|
+
export async function encrypt(key: CryptoKey, data: string): Promise<EncryptedPayload> {
|
|
220
371
|
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
221
|
-
const encoded = new TextEncoder().encode(
|
|
372
|
+
const encoded = new TextEncoder().encode(data);
|
|
222
373
|
|
|
223
374
|
const ciphertext = await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, encoded);
|
|
224
375
|
|
|
@@ -234,7 +385,7 @@ export async function encrypt(key: CryptoKey, data: unknown): Promise<EncryptedP
|
|
|
234
385
|
* The decrypted data is JSON parsed before returning.
|
|
235
386
|
*
|
|
236
387
|
* @typeParam T - The expected type of the decrypted data
|
|
237
|
-
* @param key - The AES-GCM key (from {@link
|
|
388
|
+
* @param key - The AES-GCM key (from {@link deriveSessionKeys})
|
|
238
389
|
* @param payload - The encrypted payload from {@link encrypt}
|
|
239
390
|
* @returns The decrypted and parsed data
|
|
240
391
|
*
|
|
@@ -242,7 +393,7 @@ export async function encrypt(key: CryptoKey, data: unknown): Promise<EncryptedP
|
|
|
242
393
|
*
|
|
243
394
|
* @example
|
|
244
395
|
* ```typescript
|
|
245
|
-
* const decrypted = await decrypt<{ action: string; amount: number }>(
|
|
396
|
+
* const decrypted = await decrypt<{ action: string; amount: number }>(session.encryptionKey, encrypted);
|
|
246
397
|
* console.log(decrypted.action); // 'transfer'
|
|
247
398
|
* ```
|
|
248
399
|
*/
|
|
@@ -281,3 +432,68 @@ function base64ToArrayBuffer(base64: string): ArrayBuffer {
|
|
|
281
432
|
}
|
|
282
433
|
return bytes.buffer;
|
|
283
434
|
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Converts base64url string to Uint8Array.
|
|
438
|
+
* @internal
|
|
439
|
+
*/
|
|
440
|
+
function base64UrlToBytes(base64url: string): Uint8Array {
|
|
441
|
+
// Convert base64url to base64
|
|
442
|
+
const base64 = base64url.replace(/-/g, '+').replace(/_/g, '/');
|
|
443
|
+
// Add padding if needed
|
|
444
|
+
const padded = base64 + '='.repeat((4 - (base64.length % 4)) % 4);
|
|
445
|
+
const binary = atob(padded);
|
|
446
|
+
const bytes = new Uint8Array(binary.length);
|
|
447
|
+
for (let i = 0; i < binary.length; i++) {
|
|
448
|
+
bytes[i] = binary.charCodeAt(i);
|
|
449
|
+
}
|
|
450
|
+
return bytes;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Converts ArrayBuffer to hex string.
|
|
455
|
+
* @internal
|
|
456
|
+
*/
|
|
457
|
+
function arrayBufferToHex(buffer: ArrayBuffer): string {
|
|
458
|
+
const bytes = new Uint8Array(buffer);
|
|
459
|
+
return Array.from(bytes)
|
|
460
|
+
.map(b => b.toString(16).padStart(2, '0'))
|
|
461
|
+
.join('');
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Default grid size for emoji verification display.
|
|
466
|
+
* 3x3 grid = 9 emojis = 72 bits of security.
|
|
467
|
+
*/
|
|
468
|
+
export const DEFAULT_EMOJI_GRID_SIZE = 9;
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* Converts a hex hash to an emoji sequence for visual verification.
|
|
472
|
+
*
|
|
473
|
+
* This is used for verification - both the dApp and wallet
|
|
474
|
+
* independently compute the same emoji sequence from the derived keys.
|
|
475
|
+
* Users can visually compare the sequences to detect interception.
|
|
476
|
+
*
|
|
477
|
+
* With a 256-emoji alphabet and 9 emojis (3x3 grid), this provides
|
|
478
|
+
* 72 bits of security (9 * 8 = 72 bits), making brute-force attacks
|
|
479
|
+
* computationally infeasible.
|
|
480
|
+
*
|
|
481
|
+
* @param hash - Hex string from verification hash (64 chars = 32 bytes)
|
|
482
|
+
* @param count - Number of emojis to generate (default: 9 for 3x3 grid)
|
|
483
|
+
* @returns A string of emojis representing the hash
|
|
484
|
+
*
|
|
485
|
+
* @example
|
|
486
|
+
* ```typescript
|
|
487
|
+
* const session = await deriveSessionKeys(...);
|
|
488
|
+
* const emoji = hashToEmoji(session.verificationHash); // e.g., "π΅π¦π―πΌππ²π¦πΈπ"
|
|
489
|
+
* // Display as 3x3 grid to user for verification
|
|
490
|
+
* ```
|
|
491
|
+
*/
|
|
492
|
+
export function hashToEmoji(hash: string, count: number = DEFAULT_EMOJI_GRID_SIZE): string {
|
|
493
|
+
const emojis: string[] = [];
|
|
494
|
+
for (let i = 0; i < hash.length && emojis.length < count; i += 2) {
|
|
495
|
+
const byteValue = parseInt(hash.slice(i, i + 2), 16);
|
|
496
|
+
emojis.push(EMOJI_ALPHABET[byteValue % EMOJI_ALPHABET_SIZE]);
|
|
497
|
+
}
|
|
498
|
+
return emojis.join('');
|
|
499
|
+
}
|