// keyspace_management.rhai // Advanced keyspace and keypair management test print("=== Testing Keyspace Management ==="); // Clear any existing session clear_session(); // Test creating multiple keyspaces print("Creating multiple keyspaces..."); let space1_created = create_key_space("personal", "personal_password"); let space2_created = create_key_space("business", "business_password"); let space3_created = create_key_space("testing", "testing_password"); if !space1_created || !space2_created || !space3_created { throw "Failed to create one or more keyspaces"; } print("✓ Multiple keyspaces created successfully"); // Test listing keyspaces print("Testing keyspace listing..."); let spaces = list_keyspaces(); if spaces.len() < 3 { throw "Should have at least 3 keyspaces"; } print("✓ Keyspaces listed: " + spaces.len() + " found"); // Test working with personal keyspace print("Working with personal keyspace..."); select_keyspace("personal"); // Create multiple keypairs in personal space create_keypair("main_key"); create_keypair("backup_key"); create_keypair("signing_key"); let personal_keypairs = list_keypairs(); if personal_keypairs.len() != 3 { throw "Personal keyspace should have 3 keypairs"; } print("✓ Personal keyspace has " + personal_keypairs.len() + " keypairs"); // Test working with business keyspace print("Working with business keyspace..."); select_keyspace("business"); // Create keypairs in business space create_keypair("company_key"); create_keypair("contract_key"); let business_keypairs = list_keypairs(); if business_keypairs.len() != 2 { throw "Business keyspace should have 2 keypairs"; } print("✓ Business keyspace has " + business_keypairs.len() + " keypairs"); // Test switching between keypairs print("Testing keypair switching..."); select_keypair("company_key"); let company_pubkey = keypair_pub_key(); select_keypair("contract_key"); let contract_pubkey = keypair_pub_key(); if company_pubkey == contract_pubkey { throw "Different keypairs should have different public keys"; } print("✓ Keypair switching works correctly"); // Test signing with different keypairs print("Testing signatures with different keypairs..."); let message = "Business contract data"; select_keypair("company_key"); let company_signature = sign(message); select_keypair("contract_key"); let contract_signature = sign(message); if company_signature == contract_signature { throw "Different keypairs should produce different signatures"; } print("✓ Different keypairs produce different signatures"); // Test cross-verification (should fail) select_keypair("company_key"); let company_valid = verify(message, contract_signature); if company_valid { throw "Company key should not verify contract key signature"; } print("✓ Cross-verification correctly fails"); // Test correct verification let correct_valid = verify(message, company_signature); if !correct_valid { throw "Company key should verify its own signature"; } print("✓ Self-verification works correctly"); // Test session isolation print("Testing session isolation..."); select_keyspace("testing"); let testing_keypairs = list_keypairs(); if testing_keypairs.len() != 0 { throw "Testing keyspace should be empty"; } print("✓ Keyspaces are properly isolated"); // Test error handling print("Testing error handling..."); let invalid_select = select_keyspace("non_existent"); if invalid_select { throw "Should not be able to select non-existent keyspace"; } let invalid_keypair = select_keypair("non_existent"); if invalid_keypair { throw "Should not be able to select non-existent keypair"; } print("✓ Error handling works correctly"); print("=== All keyspace management tests passed! ===");