1- use wasm_bindgen :: prelude :: * ;
1+ use base64_light :: { base64_decode , base64_encode_bytes } ;
22use js_sys:: Uint8Array ;
3- use serde:: { Deserialize } ;
4- use base64_light:: { base64_decode, base64_encode_bytes } ;
5- use tsify:: Tsify ;
63use lit_bls_wasm:: {
7- encrypt,
8- decrypt_with_signature_shares,
9- combine_signature_shares,
10- verify_signature,
4+ combine_signature_shares, decrypt_with_signature_shares, encrypt, verify_signature,
115} ;
6+ use serde:: Deserialize ;
7+ use tsify:: Tsify ;
8+ use wasm_bindgen:: prelude:: * ;
129
1310type JsResult < T > = Result < T , JsValue > ;
1411
@@ -17,102 +14,94 @@ type JsResult<T> = Result<T, JsValue>;
1714// -----------------------------------------------------------------------
1815#[ wasm_bindgen( js_name = "blsCombine" ) ]
1916pub fn bls_combine ( signature_shares : JsValue ) -> Result < String , String > {
20- let shares: Vec < String > = serde_wasm_bindgen
21- :: from_value ( signature_shares)
22- . map_err ( |e| format ! ( "Failed to parse shares: {}" , e) ) ?;
17+ let shares: Vec < String > = serde_wasm_bindgen:: from_value ( signature_shares)
18+ . map_err ( |e| format ! ( "Failed to parse shares: {}" , e) ) ?;
2319
24- let combined_signature = combine_signature_shares (
25- serde_wasm_bindgen:: to_value ( & shares) . unwrap ( )
26- ) . map_err ( |e| format ! ( "Failed to combine signature shares: {}" , e) ) ?;
20+ let combined_signature =
21+ combine_signature_shares ( serde_wasm_bindgen:: to_value ( & shares) . unwrap ( ) )
22+ . map_err ( |e| format ! ( "Failed to combine signature shares: {}" , e) ) ?;
2723
28- Ok ( combined_signature)
24+ Ok ( combined_signature)
2925}
3026
3127// -----------------------------------------------------------------------
3228// 2. blsVerify
3329// -----------------------------------------------------------------------
3430#[ wasm_bindgen( js_name = "blsVerify" ) ]
3531pub fn bls_verify (
36- public_key : Uint8Array , // buffer, but will be converted to hex string
37- message : Uint8Array , // buffer, but will be converted to hex string
38- signature : String // this is the result from bls_combine. It's a hex string
32+ public_key : Uint8Array , // buffer, but will be converted to hex string
33+ message : Uint8Array , // buffer, but will be converted to hex string
34+ signature : String , // this is the result from bls_combine. It's a hex string
3935) -> JsResult < ( ) > {
40- // check if signature is a valid hex string
41- if !signature. chars ( ) . all ( |c| c. is_ascii_hexdigit ( ) ) {
42- return Err ( JsValue :: from_str ( "Signature must be a hex string" ) ) ;
43- }
44- // convert public_key to hex string
45- let public_key_hex = hex:: encode ( public_key. to_vec ( ) ) ;
46-
47- // convert message to base64 string
48- let message_base64 = base64_encode_bytes ( & message. to_vec ( ) ) ;
49-
50- // Validate all inputs are hex
51- if !public_key_hex. chars ( ) . all ( |c| c. is_ascii_hexdigit ( ) ) {
52- return Err ( JsValue :: from_str ( "Public key must be a hex string" ) ) ;
53- }
54-
55- if !signature. chars ( ) . all ( |c| c. is_ascii_hexdigit ( ) ) {
56- return Err ( JsValue :: from_str ( "Signature must be a hex string" ) ) ;
57- }
58-
59- let signature_bytes = hex
60- :: decode ( & signature)
61- . map_err ( |e|
62- JsValue :: from_str ( & format ! ( "Failed to decode signature hex: {}" , e) )
63- ) ?;
64-
65- let signature_base64 = base64_encode_bytes ( & signature_bytes) ;
66-
67- verify_signature ( & public_key_hex, & message_base64, & signature_base64) . map_err (
68- |e| JsValue :: from_str ( & format ! ( "Verification failed: {}" , e) )
69- )
36+ // check if signature is a valid hex string
37+ if !signature. chars ( ) . all ( |c| c. is_ascii_hexdigit ( ) ) {
38+ return Err ( JsValue :: from_str ( "Signature must be a hex string" ) ) ;
39+ }
40+ // convert public_key to hex string
41+ let public_key_hex = hex:: encode ( public_key. to_vec ( ) ) ;
42+
43+ // convert message to base64 string
44+ let message_base64 = base64_encode_bytes ( & message. to_vec ( ) ) ;
45+
46+ // Validate all inputs are hex
47+ if !public_key_hex. chars ( ) . all ( |c| c. is_ascii_hexdigit ( ) ) {
48+ return Err ( JsValue :: from_str ( "Public key must be a hex string" ) ) ;
49+ }
50+
51+ if !signature. chars ( ) . all ( |c| c. is_ascii_hexdigit ( ) ) {
52+ return Err ( JsValue :: from_str ( "Signature must be a hex string" ) ) ;
53+ }
54+
55+ let signature_bytes = hex:: decode ( & signature)
56+ . map_err ( |e| JsValue :: from_str ( & format ! ( "Failed to decode signature hex: {}" , e) ) ) ?;
57+
58+ let signature_base64 = base64_encode_bytes ( & signature_bytes) ;
59+
60+ verify_signature ( & public_key_hex, & message_base64, & signature_base64)
61+ . map_err ( |e| JsValue :: from_str ( & format ! ( "Verification failed: {}" , e) ) )
7062}
7163
7264// -----------------------------------------------------------------------
7365// 3. blsEncrypt
7466// -----------------------------------------------------------------------
7567#[ wasm_bindgen( js_name = "blsEncrypt" ) ]
7668pub fn bls_encrypt (
77- encryption_key : Uint8Array ,
78- message : Uint8Array ,
79- identity : Uint8Array
69+ encryption_key : Uint8Array ,
70+ message : Uint8Array ,
71+ identity : Uint8Array ,
8072) -> JsResult < Uint8Array > {
81- let encryption_key_hex = hex:: encode ( encryption_key. to_vec ( ) ) ;
82- let message_base64 = base64_encode_bytes ( & message. to_vec ( ) ) ;
83- let identity_base64 = base64_encode_bytes ( & identity. to_vec ( ) ) ;
73+ let encryption_key_hex = hex:: encode ( encryption_key. to_vec ( ) ) ;
74+ let message_base64 = base64_encode_bytes ( & message. to_vec ( ) ) ;
75+ let identity_base64 = base64_encode_bytes ( & identity. to_vec ( ) ) ;
8476
85- let ciphertext = encrypt (
86- & encryption_key_hex,
87- & message_base64,
88- & identity_base64
89- ) . map_err ( |e| JsValue :: from_str ( & format ! ( "Encryption failed: {}" , e) ) ) ?;
77+ let ciphertext = encrypt ( & encryption_key_hex, & message_base64, & identity_base64)
78+ . map_err ( |e| JsValue :: from_str ( & format ! ( "Encryption failed: {}" , e) ) ) ?;
9079
91- let decoded_ciphertext = base64_decode ( & ciphertext) ;
80+ let decoded_ciphertext = base64_decode ( & ciphertext) ;
9281
93- Ok ( Uint8Array :: from ( decoded_ciphertext. as_slice ( ) ) )
82+ Ok ( Uint8Array :: from ( decoded_ciphertext. as_slice ( ) ) )
9483}
9584
9685// -----------------------------------------------------------------------
9786// 4. blsDecrypt
9887// -----------------------------------------------------------------------
9988#[ wasm_bindgen( js_name = "blsDecrypt" ) ]
10089pub fn bls_decrypt (
101- ciphertext : Uint8Array ,
102- signature_shares : JsValue // this is the result from bls_combine. It's a hex string
90+ ciphertext : Uint8Array ,
91+ signature_shares : JsValue , // this is the result from bls_combine. It's a hex string
10392) -> JsResult < Uint8Array > {
104- let ciphertext_base64 = base64_encode_bytes ( & ciphertext. to_vec ( ) ) ;
93+ let ciphertext_base64 = base64_encode_bytes ( & ciphertext. to_vec ( ) ) ;
10594
106- let shares: Vec < String > = serde_wasm_bindgen
107- :: from_value ( signature_shares)
108- . map_err ( |e| format ! ( "[blsDecrypt] Failed to parse shares: {}" , e) ) ?;
95+ let shares: Vec < String > = serde_wasm_bindgen:: from_value ( signature_shares)
96+ . map_err ( |e| format ! ( "[blsDecrypt] Failed to parse shares: {}" , e) ) ?;
10997
110- let plaintext = decrypt_with_signature_shares (
111- & ciphertext_base64,
112- serde_wasm_bindgen:: to_value ( & shares) . unwrap ( )
113- ) . map_err ( |e| JsValue :: from_str ( & format ! ( "Decryption failed: {}" , e) ) ) ?;
98+ let plaintext = decrypt_with_signature_shares (
99+ & ciphertext_base64,
100+ serde_wasm_bindgen:: to_value ( & shares) . unwrap ( ) ,
101+ )
102+ . map_err ( |e| JsValue :: from_str ( & format ! ( "Decryption failed: {}" , e) ) ) ?;
114103
115- let decoded_plaintext = base64_decode ( & plaintext) ;
104+ let decoded_plaintext = base64_decode ( & plaintext) ;
116105
117- Ok ( Uint8Array :: from ( decoded_plaintext. as_slice ( ) ) )
106+ Ok ( Uint8Array :: from ( decoded_plaintext. as_slice ( ) ) )
118107}
0 commit comments