1mod util;
8mod wasm_agent;
9
10use tap_agent::did::KeyType as TapKeyType;
11use wasm_bindgen::prelude::*;
12
13pub use wasm_agent::WasmTapAgent;
14
15#[cfg(feature = "wee_alloc")]
17#[global_allocator]
18static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
19
20#[wasm_bindgen(start)]
22pub fn start() -> Result<(), JsValue> {
23 console_error_panic_hook::set_once();
24 Ok(())
25}
26
27#[wasm_bindgen]
34pub fn generate_uuid_v4() -> String {
35 uuid::Uuid::new_v4().to_string()
36}
37
38#[wasm_bindgen(js_name = generatePrivateKey)]
40pub fn generate_private_key(key_type_str: String) -> Result<String, JsValue> {
41 #[cfg(any(
42 feature = "crypto-ed25519",
43 feature = "crypto-p256",
44 feature = "crypto-secp256k1"
45 ))]
46 {
47 use tap_agent::did::{DIDGenerationOptions, DIDKeyGenerator};
48
49 let key_type = match key_type_str.as_str() {
51 #[cfg(feature = "crypto-ed25519")]
52 "Ed25519" => TapKeyType::Ed25519,
53 #[cfg(feature = "crypto-p256")]
54 "P256" => TapKeyType::P256,
55 #[cfg(feature = "crypto-secp256k1")]
56 "Secp256k1" => TapKeyType::Secp256k1,
57 _ => {
58 return Err(JsValue::from_str(&format!(
59 "Invalid or disabled key type: {}",
60 key_type_str
61 )))
62 }
63 };
64
65 let generator = DIDKeyGenerator::new();
67 let options = DIDGenerationOptions { key_type };
68 let generated_key = generator
69 .generate_did(options)
70 .map_err(|e| JsValue::from_str(&format!("Failed to generate key: {}", e)))?;
71
72 Ok(hex::encode(&generated_key.private_key))
74 }
75
76 #[cfg(not(any(
77 feature = "crypto-ed25519",
78 feature = "crypto-p256",
79 feature = "crypto-secp256k1"
80 )))]
81 {
82 Err(JsValue::from_str("No cryptographic features enabled"))
83 }
84}
85
86#[wasm_bindgen(js_name = generateUUID)]
88pub fn generate_uuid() -> String {
89 generate_uuid_v4()
90}