Skip to main content

tap_wasm/
lib.rs

1//! WebAssembly bindings for the Transaction Authorization Protocol
2//!
3//! This crate provides WebAssembly bindings for the TAP agent, allowing it to be used in
4//! browser and other JavaScript environments. It wraps the tap-agent crate's functionality
5//! with JavaScript-friendly interfaces.
6
7mod util;
8mod wasm_agent;
9
10use tap_agent::did::KeyType as TapKeyType;
11use wasm_bindgen::prelude::*;
12
13pub use wasm_agent::WasmTapAgent;
14
15// Use wee_alloc as the global allocator to reduce WASM binary size
16#[cfg(feature = "wee_alloc")]
17#[global_allocator]
18static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
19
20/// Set up panic hook for better error messages in browser
21#[wasm_bindgen(start)]
22pub fn start() -> Result<(), JsValue> {
23    console_error_panic_hook::set_once();
24    Ok(())
25}
26
27// MessageType enum removed - TypeScript handles message types natively
28// Key type enumeration removed - we use strings directly to avoid wasm_bindgen issues with conditional compilation
29
30// TapNode removed - tap-node functionality not needed in browser WASM
31
32/// Generates a UUID v4
33#[wasm_bindgen]
34pub fn generate_uuid_v4() -> String {
35    uuid::Uuid::new_v4().to_string()
36}
37
38/// Generates a new private key for the specified key type
39#[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        // Convert key type string to KeyType enum
50        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        // Generate a new key
66        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        // Return the private key as hex string
73        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/// Alias for generate_uuid_v4 to match the PRD specification
87#[wasm_bindgen(js_name = generateUUID)]
88pub fn generate_uuid() -> String {
89    generate_uuid_v4()
90}