maple-sdk 4.0.2

Maple Rust SDK for secure AI APIs, encrypted sessions, and Nitro attestation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
mod common;

use maple_sdk::{KeyOptions, Result, SigningAlgorithm};
use uuid::Uuid;

#[tokio::test]
async fn test_user_profile_api() -> Result<()> {
    // Load environment variables from .env.local in SDK root
    let env_path = std::path::Path::new("../.env.local");
    if env_path.exists() {
        dotenvy::from_path(env_path).ok();
    }

    let base_url = std::env::var("VITE_OPEN_SECRET_API_URL")
        .unwrap_or_else(|_| "http://localhost:3000".to_string());
    let client_id = std::env::var("VITE_TEST_CLIENT_ID")
        .ok()
        .and_then(|id| Uuid::parse_str(&id).ok())
        .expect("VITE_TEST_CLIENT_ID must be set in .env.local");

    let test_email = std::env::var("VITE_TEST_EMAIL").expect("VITE_TEST_EMAIL must be set");
    let test_password =
        std::env::var("VITE_TEST_PASSWORD").expect("VITE_TEST_PASSWORD must be set");

    // Create client and login
    let client = common::new_test_client(base_url)?;
    client.perform_attestation_handshake().await?;

    // Try to login, register if it fails
    let _ = match client
        .login(test_email.clone(), test_password.clone(), client_id)
        .await
    {
        Ok(response) => response,
        Err(_) => {
            client
                .register(test_email.clone(), test_password, client_id, None)
                .await?
        }
    };

    // Test get_user endpoint
    let user_response = client.get_user().await?;
    assert_eq!(user_response.user.email, Some(test_email));
    println!("✓ User profile retrieved successfully");

    Ok(())
}

#[tokio::test]
async fn test_kv_storage_apis() -> Result<()> {
    // Load environment variables
    let env_path = std::path::Path::new("../.env.local");
    if env_path.exists() {
        dotenvy::from_path(env_path).ok();
    }

    let base_url = std::env::var("VITE_OPEN_SECRET_API_URL")
        .unwrap_or_else(|_| "http://localhost:3000".to_string());
    let client_id = std::env::var("VITE_TEST_CLIENT_ID")
        .ok()
        .and_then(|id| Uuid::parse_str(&id).ok())
        .expect("VITE_TEST_CLIENT_ID must be set");

    // Create client and login as guest for isolation
    let client = common::new_test_client(base_url)?;
    client.perform_attestation_handshake().await?;

    let guest_password = "test_kv_password";
    client
        .register_guest(guest_password.to_string(), client_id)
        .await?;

    // Test KV operations
    let test_key = "test_key_rust";
    let test_value = "test_value_from_rust_sdk";

    // PUT
    let put_result = client.kv_put(test_key, test_value.to_string()).await?;
    assert_eq!(put_result, test_value);
    println!("✓ KV PUT successful");

    // GET
    let get_result = client.kv_get(test_key).await?;
    assert_eq!(get_result, test_value);
    println!("✓ KV GET successful");

    // LIST
    let list_result = client.kv_list().await?;
    assert!(list_result.iter().any(|item| item.key == test_key));
    println!("✓ KV LIST successful");

    // DELETE
    client.kv_delete(test_key).await?;
    println!("✓ KV DELETE successful");

    // Verify deletion
    let list_after_delete = client.kv_list().await?;
    assert!(!list_after_delete.iter().any(|item| item.key == test_key));
    println!("✓ KV deletion verified");

    Ok(())
}

#[tokio::test]
async fn test_kv_delete_all() -> Result<()> {
    // Load environment variables
    let env_path = std::path::Path::new("../.env.local");
    if env_path.exists() {
        dotenvy::from_path(env_path).ok();
    }

    let base_url = std::env::var("VITE_OPEN_SECRET_API_URL")
        .unwrap_or_else(|_| "http://localhost:3000".to_string());
    let client_id = std::env::var("VITE_TEST_CLIENT_ID")
        .ok()
        .and_then(|id| Uuid::parse_str(&id).ok())
        .expect("VITE_TEST_CLIENT_ID must be set");

    // Create client and login as guest for isolation
    let client = common::new_test_client(base_url)?;
    client.perform_attestation_handshake().await?;

    client
        .register_guest("test_kv_delete_all".to_string(), client_id)
        .await?;

    // Add multiple keys
    client.kv_put("key1", "value1".to_string()).await?;
    client.kv_put("key2", "value2".to_string()).await?;
    client.kv_put("key3", "value3".to_string()).await?;

    // Verify they exist
    let list_result = client.kv_list().await?;
    assert!(list_result.len() >= 3);
    println!("✓ Keys added successfully");

    // Delete all
    client.kv_delete_all().await?;
    println!("✓ kv_delete_all called");

    // Verify empty
    let list_after_delete = client.kv_list().await?;
    assert!(list_after_delete.is_empty());
    println!("✓ All keys deleted successfully");

    Ok(())
}

#[tokio::test]
async fn test_kv_storage_with_special_characters() -> Result<()> {
    // Load environment variables
    let env_path = std::path::Path::new("../.env.local");
    if env_path.exists() {
        dotenvy::from_path(env_path).ok();
    }

    let base_url = std::env::var("VITE_OPEN_SECRET_API_URL")
        .unwrap_or_else(|_| "http://localhost:3000".to_string());
    let client_id = std::env::var("VITE_TEST_CLIENT_ID")
        .ok()
        .and_then(|id| Uuid::parse_str(&id).ok())
        .expect("VITE_TEST_CLIENT_ID must be set");

    // Create client and login
    let client = common::new_test_client(base_url)?;
    client.perform_attestation_handshake().await?;
    client
        .register_guest("test_kv_special".to_string(), client_id)
        .await?;

    // Test with various special characters that need URL encoding
    let test_cases = vec![
        ("key/with/slashes", "value1"),
        ("key?with=query&params", "value2"),
        ("key#with#hash", "value3"),
        ("key with spaces", "value4"),
        ("key%with%percents", "value5"),
        ("key@with!special$chars", "value6"),
        ("key[with]brackets{and}braces", "value7"),
        ("key:with:colons;semicolons", "value8"),
    ];

    for (key, value) in &test_cases {
        println!("Testing key: {:?}", key);

        // PUT with special characters
        let put_result = client.kv_put(key, value.to_string()).await?;
        assert_eq!(put_result, *value);

        // GET with special characters
        let get_result = client.kv_get(key).await?;
        assert_eq!(get_result, *value);

        // DELETE with special characters
        client.kv_delete(key).await?;

        println!("✓ KV operations successful for key: {:?}", key);
    }

    // Verify all were deleted
    let final_list = client.kv_list().await?;
    for (key, _) in &test_cases {
        assert!(
            !final_list.iter().any(|item| item.key == *key),
            "Key {:?} should have been deleted",
            key
        );
    }
    println!("✓ All special character keys handled correctly");

    Ok(())
}

#[tokio::test]
async fn test_private_key_generation() -> Result<()> {
    // Load environment variables
    let env_path = std::path::Path::new("../.env.local");
    if env_path.exists() {
        dotenvy::from_path(env_path).ok();
    }

    let base_url = std::env::var("VITE_OPEN_SECRET_API_URL")
        .unwrap_or_else(|_| "http://localhost:3000".to_string());
    let client_id = std::env::var("VITE_TEST_CLIENT_ID")
        .ok()
        .and_then(|id| Uuid::parse_str(&id).ok())
        .expect("VITE_TEST_CLIENT_ID must be set");

    // Create client and login as guest
    let client = common::new_test_client(base_url)?;
    client.perform_attestation_handshake().await?;
    client
        .register_guest("test_key_gen".to_string(), client_id)
        .await?;

    // Test private key generation without options
    let key_response = client.get_private_key(None).await?;
    assert!(!key_response.mnemonic.is_empty());
    let word_count = key_response.mnemonic.split_whitespace().count();
    assert!(word_count == 12 || word_count == 18 || word_count == 24);
    println!(
        "✓ Private key generated successfully ({} words)",
        word_count
    );

    // Test with BIP-85 derivation path for 24 words
    let options = KeyOptions {
        private_key_derivation_path: None,
        seed_phrase_derivation_path: Some("m/83696968'/39'/0'/24'/0'".to_string()),
    };
    let key_with_path = client.get_private_key(Some(options)).await?;
    assert!(!key_with_path.mnemonic.is_empty());
    assert_eq!(key_with_path.mnemonic.split_whitespace().count(), 24);
    println!("✓ Private key with BIP-85 derivation generated (24 words)");

    // Test private key bytes
    let key_bytes = client.get_private_key_bytes(None).await?;
    assert!(!key_bytes.private_key.is_empty());
    assert_eq!(key_bytes.private_key.len(), 64); // 32 bytes as hex = 64 chars
    println!("✓ Private key bytes retrieved successfully");

    Ok(())
}

#[tokio::test]
async fn test_message_signing() -> Result<()> {
    // Load environment variables
    let env_path = std::path::Path::new("../.env.local");
    if env_path.exists() {
        dotenvy::from_path(env_path).ok();
    }

    let base_url = std::env::var("VITE_OPEN_SECRET_API_URL")
        .unwrap_or_else(|_| "http://localhost:3000".to_string());
    let client_id = std::env::var("VITE_TEST_CLIENT_ID")
        .ok()
        .and_then(|id| Uuid::parse_str(&id).ok())
        .expect("VITE_TEST_CLIENT_ID must be set");

    // Create client and login
    let client = common::new_test_client(base_url)?;
    client.perform_attestation_handshake().await?;
    client
        .register_guest("test_signing".to_string(), client_id)
        .await?;

    let test_message = b"Hello from Rust SDK";

    // Test Schnorr signing
    let schnorr_sig = client
        .sign_message(test_message, SigningAlgorithm::Schnorr, None)
        .await?;
    assert!(!schnorr_sig.signature.is_empty());
    assert!(!schnorr_sig.message_hash.is_empty());
    println!("✓ Schnorr signature created");

    // Test ECDSA signing
    let ecdsa_sig = client
        .sign_message(test_message, SigningAlgorithm::Ecdsa, None)
        .await?;
    assert!(!ecdsa_sig.signature.is_empty());
    assert!(!ecdsa_sig.message_hash.is_empty());
    println!("✓ ECDSA signature created");

    // Test with derivation path
    let key_options = KeyOptions {
        private_key_derivation_path: Some("m/44'/0'/0'/0/0".to_string()),
        seed_phrase_derivation_path: None,
    };
    let sig_with_path = client
        .sign_message(test_message, SigningAlgorithm::Schnorr, Some(key_options))
        .await?;
    assert!(!sig_with_path.signature.is_empty());
    println!("✓ Signature with derivation path created");

    Ok(())
}

#[tokio::test]
async fn test_public_key_retrieval() -> Result<()> {
    // Load environment variables
    let env_path = std::path::Path::new("../.env.local");
    if env_path.exists() {
        dotenvy::from_path(env_path).ok();
    }

    let base_url = std::env::var("VITE_OPEN_SECRET_API_URL")
        .unwrap_or_else(|_| "http://localhost:3000".to_string());
    let client_id = std::env::var("VITE_TEST_CLIENT_ID")
        .ok()
        .and_then(|id| Uuid::parse_str(&id).ok())
        .expect("VITE_TEST_CLIENT_ID must be set");

    // Create client and login
    let client = common::new_test_client(base_url)?;
    client.perform_attestation_handshake().await?;
    client
        .register_guest("test_pubkey".to_string(), client_id)
        .await?;

    // Test Schnorr public key
    let schnorr_pubkey = client
        .get_public_key(SigningAlgorithm::Schnorr, None)
        .await?;
    assert!(!schnorr_pubkey.public_key.is_empty());
    assert!(matches!(
        schnorr_pubkey.algorithm,
        SigningAlgorithm::Schnorr
    ));
    println!("✓ Schnorr public key retrieved");

    // Test ECDSA public key
    let ecdsa_pubkey = client.get_public_key(SigningAlgorithm::Ecdsa, None).await?;
    assert!(!ecdsa_pubkey.public_key.is_empty());
    assert!(matches!(ecdsa_pubkey.algorithm, SigningAlgorithm::Ecdsa));
    println!("✓ ECDSA public key retrieved");

    // Test with derivation path
    let key_options = KeyOptions {
        private_key_derivation_path: Some("m/44'/0'/0'/0/1".to_string()),
        seed_phrase_derivation_path: None,
    };
    let pubkey_with_path = client
        .get_public_key(SigningAlgorithm::Schnorr, Some(key_options))
        .await?;
    assert!(!pubkey_with_path.public_key.is_empty());
    println!("✓ Public key with derivation path retrieved");

    Ok(())
}

#[tokio::test]
async fn test_encryption_decryption() -> Result<()> {
    // Load environment variables
    let env_path = std::path::Path::new("../.env.local");
    if env_path.exists() {
        dotenvy::from_path(env_path).ok();
    }

    let base_url = std::env::var("VITE_OPEN_SECRET_API_URL")
        .unwrap_or_else(|_| "http://localhost:3000".to_string());
    let client_id = std::env::var("VITE_TEST_CLIENT_ID")
        .ok()
        .and_then(|id| Uuid::parse_str(&id).ok())
        .expect("VITE_TEST_CLIENT_ID must be set");

    // Create client and login
    let client = common::new_test_client(base_url)?;
    client.perform_attestation_handshake().await?;
    client
        .register_guest("test_crypto".to_string(), client_id)
        .await?;

    let test_data = "Hello, World!";

    // Test basic encryption/decryption (matching TypeScript test exactly)
    let encrypted = client.encrypt_data(test_data.to_string(), None).await?;
    assert!(!encrypted.encrypted_data.is_empty());
    println!("✓ Data encrypted successfully");

    let decrypted = client
        .decrypt_data(encrypted.encrypted_data.clone(), None)
        .await?;
    assert_eq!(decrypted, test_data);
    println!("✓ Data decrypted successfully");

    // Test with BIP-32 derivation path (matching TypeScript test)
    let derivation_path = "m/44'/0'/0'/0/0".to_string();
    let key_options = KeyOptions {
        private_key_derivation_path: Some(derivation_path),
        seed_phrase_derivation_path: None,
    };
    let encrypted_with_path = client
        .encrypt_data(test_data.to_string(), Some(key_options.clone()))
        .await?;
    assert!(!encrypted_with_path.encrypted_data.is_empty());

    // Decrypt with same path
    let decrypted_with_path = client
        .decrypt_data(
            encrypted_with_path.encrypted_data.clone(),
            Some(key_options),
        )
        .await?;
    assert_eq!(decrypted_with_path, test_data);
    println!("✓ Encryption/decryption with BIP-32 derivation path successful");

    // Test decrypting with wrong derivation path (should fail) - matching TypeScript
    let wrong_options = KeyOptions {
        private_key_derivation_path: Some("m/44'/0'/0'/0/1".to_string()),
        seed_phrase_derivation_path: None,
    };
    let wrong_decrypt_result = client
        .decrypt_data(
            encrypted_with_path.encrypted_data.clone(),
            Some(wrong_options),
        )
        .await;
    assert!(
        wrong_decrypt_result.is_err(),
        "Should not decrypt with wrong derivation path"
    );
    println!("✓ Correctly failed to decrypt with wrong derivation path");

    // Test decrypting with no path when encrypted with path (should fail) - matching TypeScript
    let no_path_decrypt_result = client
        .decrypt_data(encrypted_with_path.encrypted_data.clone(), None)
        .await;
    assert!(
        no_path_decrypt_result.is_err(),
        "Should not decrypt with missing derivation path"
    );
    println!("✓ Correctly failed to decrypt with missing derivation path");

    // Test with invalid encrypted data (should fail) - matching TypeScript
    let invalid_decrypt_result = client.decrypt_data("invalid-data".to_string(), None).await;
    assert!(
        invalid_decrypt_result.is_err(),
        "Should not decrypt invalid data"
    );
    println!("✓ Correctly failed to decrypt invalid data");

    Ok(())
}

#[tokio::test]
async fn test_third_party_token() -> Result<()> {
    // Load environment variables
    let env_path = std::path::Path::new("../.env.local");
    if env_path.exists() {
        dotenvy::from_path(env_path).ok();
    }

    let base_url = std::env::var("VITE_OPEN_SECRET_API_URL")
        .unwrap_or_else(|_| "http://localhost:3000".to_string());
    let client_id = std::env::var("VITE_TEST_CLIENT_ID")
        .ok()
        .and_then(|id| Uuid::parse_str(&id).ok())
        .expect("VITE_TEST_CLIENT_ID must be set");

    let test_email = std::env::var("VITE_TEST_EMAIL").expect("VITE_TEST_EMAIL must be set");
    let test_password =
        std::env::var("VITE_TEST_PASSWORD").expect("VITE_TEST_PASSWORD must be set");

    // Create client and login with email user (third party tokens may require email user)
    let client = common::new_test_client(base_url)?;
    client.perform_attestation_handshake().await?;

    let _ = match client
        .login(test_email.clone(), test_password.clone(), client_id)
        .await
    {
        Ok(response) => response,
        Err(_) => {
            client
                .register(test_email, test_password, client_id, None)
                .await?
        }
    };

    // Test without audience
    let token_no_audience = client.generate_third_party_token(None).await?;
    assert!(!token_no_audience.token.is_empty());
    println!("✓ Third party token generated without audience");

    // Test with valid audience
    let valid_audience = "https://billing-dev.opensecret.cloud".to_string();
    let token_with_audience = client
        .generate_third_party_token(Some(valid_audience))
        .await?;
    assert!(!token_with_audience.token.is_empty());
    println!("✓ Third party token generated with audience");

    Ok(())
}