@@ -61,6 +61,7 @@ fn usernames_from_documents(documents_result: Documents) -> Array {
6161 usernames_array
6262}
6363
64+ // TS definition for DpnsUsernamesQuery used by getDpnsUsernames*
6465#[ wasm_bindgen( typescript_custom_section) ]
6566const DPNS_USERNAMES_QUERY_TS : & ' static str = r#"
6667/**
@@ -121,6 +122,7 @@ pub struct DpnsUsernamesProofResponseWasm {
121122 #[ wasm_bindgen( getter_with_clone) ]
122123 pub proof : ProofInfoWasm ,
123124}
125+
124126#[ wasm_bindgen( js_name = "DpnsUsernameProofResponse" ) ]
125127#[ derive( Clone ) ]
126128pub struct DpnsUsernameProofResponseWasm {
@@ -202,14 +204,17 @@ impl WasmSdk {
202204 pub fn dpns_convert_to_homograph_safe ( input : & str ) -> String {
203205 convert_to_homograph_safe_chars ( input)
204206 }
207+
205208 #[ wasm_bindgen( js_name = "dpnsIsValidUsername" ) ]
206209 pub fn dpns_is_valid_username ( label : & str ) -> bool {
207210 is_valid_username ( label)
208211 }
212+
209213 #[ wasm_bindgen( js_name = "dpnsIsContestedUsername" ) ]
210214 pub fn dpns_is_contested_username ( label : & str ) -> bool {
211215 is_contested_username ( label)
212216 }
217+
213218 #[ wasm_bindgen( js_name = "dpnsRegisterName" ) ]
214219 pub async fn dpns_register_name (
215220 & self ,
@@ -222,27 +227,33 @@ impl WasmSdk {
222227 #[ wasm_bindgen( js_name = "preorderCallback" ) ] preorder_callback : Option < js_sys:: Function > ,
223228 ) -> Result < JsValue , WasmSdkError > {
224229 let identity_id_parsed = identifier_from_js ( & identity_id, "identity ID" ) ?;
230+
225231 let identity = Identity :: fetch ( self . as_ref ( ) , identity_id_parsed)
226232 . await ?
227233 . ok_or_else ( || WasmSdkError :: not_found ( "Identity not found" ) ) ?;
234+
228235 let signer = SingleKeySigner :: new ( private_key_wif) . map_err ( |e| {
229236 WasmSdkError :: invalid_argument ( format ! ( "Invalid private key WIF: {}" , e) )
230237 } ) ?;
238+
231239 let identity_public_key = identity
232240 . get_public_key_by_id ( public_key_id)
233241 . ok_or_else ( || {
234242 WasmSdkError :: not_found ( format ! ( "Public key with ID {} not found" , public_key_id) )
235243 } ) ?
236244 . clone ( ) ;
245+
237246 thread_local ! {
238247 static PREORDER_CALLBACK : std:: cell:: RefCell < Option < js_sys:: Function >>
239248 = const { std:: cell:: RefCell :: new( None ) } ;
240249 }
250+
241251 if let Some ( ref js_callback) = preorder_callback {
242252 PREORDER_CALLBACK . with ( |cb| {
243253 * cb. borrow_mut ( ) = Some ( js_callback. clone ( ) ) ;
244254 } ) ;
245255 }
256+
246257 let callback_box = if preorder_callback. is_some ( ) {
247258 Some ( Box :: new ( move |doc : & Document | {
248259 PREORDER_CALLBACK . with ( |cb| {
@@ -265,46 +276,56 @@ impl WasmSdk {
265276 } else {
266277 None
267278 } ;
279+
268280 let input = RegisterDpnsNameInput {
269281 label : label. to_string ( ) ,
270282 identity,
271283 identity_public_key,
272284 signer,
273285 preorder_callback : callback_box,
274286 } ;
287+
275288 let result = self . as_ref ( ) . register_dpns_name ( input) . await ?;
289+
276290 PREORDER_CALLBACK . with ( |cb| {
277291 * cb. borrow_mut ( ) = None ;
278292 } ) ;
293+
279294 let js_result = RegisterDpnsNameResult {
280295 preorder_document_id : result. preorder_document . id ( ) . to_string ( Encoding :: Base58 ) ,
281296 domain_document_id : result. domain_document . id ( ) . to_string ( Encoding :: Base58 ) ,
282297 full_domain_name : result. full_domain_name ,
283298 } ;
299+
284300 let serializer = serde_wasm_bindgen:: Serializer :: json_compatible ( ) ;
285301 js_result
286302 . serialize ( & serializer)
287303 . map_err ( |e| WasmSdkError :: serialization ( format ! ( "Failed to serialize result: {}" , e) ) )
288304 }
305+
289306 #[ wasm_bindgen( js_name = "dpnsIsNameAvailable" ) ]
290307 pub async fn dpns_is_name_available ( & self , label : & str ) -> Result < bool , WasmSdkError > {
291308 self . as_ref ( )
292309 . is_dpns_name_available ( label)
293310 . await
294311 . map_err ( WasmSdkError :: from)
295312 }
313+
296314 #[ wasm_bindgen( js_name = "dpnsResolveName" ) ]
297315 pub async fn dpns_resolve_name ( & self , name : & str ) -> Result < JsValue , WasmSdkError > {
298316 let result = self . as_ref ( ) . resolve_dpns_name ( name) . await ?;
317+
299318 match result {
300319 Some ( identity_id) => Ok ( JsValue :: from_str ( & identity_id. to_string ( Encoding :: Base58 ) ) ) ,
301320 None => Ok ( JsValue :: NULL ) ,
302321 }
303322 }
323+
304324 #[ wasm_bindgen( js_name = "getDpnsUsernameByName" ) ]
305325 pub async fn get_dpns_username_by_name ( & self , username : & str ) -> Result < JsValue , WasmSdkError > {
306326 const DPNS_CONTRACT_ID : & str = "GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec" ;
307327 const DPNS_DOCUMENT_TYPE : & str = "domain" ;
328+
308329 let parts: Vec < & str > = username. split ( '.' ) . collect ( ) ;
309330 if parts. len ( ) != 2 {
310331 return Err ( WasmSdkError :: invalid_argument (
@@ -313,33 +334,40 @@ impl WasmSdk {
313334 }
314335 let label = parts[ 0 ] ;
315336 let domain = parts[ 1 ] ;
337+
316338 let contract_id =
317339 Identifier :: from_string ( DPNS_CONTRACT_ID , Encoding :: Base58 ) . map_err ( |e| {
318340 WasmSdkError :: invalid_argument ( format ! ( "Invalid DPNS contract ID: {}" , e) )
319341 } ) ?;
342+
320343 let mut query = DocumentQuery :: new_with_data_contract_id (
321344 self . as_ref ( ) ,
322345 contract_id,
323346 DPNS_DOCUMENT_TYPE ,
324347 )
325348 . await ?;
349+
326350 query = query. with_where ( WhereClause {
327351 field : "normalizedLabel" . to_string ( ) ,
328352 operator : WhereOperator :: Equal ,
329353 value : Value :: Text ( label. to_lowercase ( ) ) ,
330354 } ) ;
355+
331356 query = query. with_where ( WhereClause {
332357 field : "normalizedParentDomainName" . to_string ( ) ,
333358 operator : WhereOperator :: Equal ,
334359 value : Value :: Text ( domain. to_lowercase ( ) ) ,
335360 } ) ;
361+
336362 let documents = Document :: fetch_many ( self . as_ref ( ) , query) . await ?;
363+
337364 if let Some ( ( _, Some ( document) ) ) = documents. into_iter ( ) . next ( ) {
338365 let result = DpnsUsernameInfo {
339366 username : username. to_string ( ) ,
340367 identity_id : document. owner_id ( ) . to_string ( Encoding :: Base58 ) ,
341368 document_id : document. id ( ) . to_string ( Encoding :: Base58 ) ,
342369 } ;
370+
343371 serde_wasm_bindgen:: to_value ( & result) . map_err ( |e| {
344372 WasmSdkError :: serialization ( format ! ( "Failed to serialize response: {}" , e) )
345373 } )
@@ -350,13 +378,15 @@ impl WasmSdk {
350378 ) ) )
351379 }
352380 }
381+
353382 #[ wasm_bindgen( js_name = "getDpnsUsernameByNameWithProofInfo" ) ]
354383 pub async fn get_dpns_username_by_name_with_proof_info (
355384 & self ,
356385 username : & str ,
357386 ) -> Result < JsValue , WasmSdkError > {
358387 const DPNS_CONTRACT_ID : & str = "GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec" ;
359388 const DPNS_DOCUMENT_TYPE : & str = "domain" ;
389+
360390 let parts: Vec < & str > = username. split ( '.' ) . collect ( ) ;
361391 if parts. len ( ) != 2 {
362392 return Err ( WasmSdkError :: invalid_argument (
@@ -365,39 +395,48 @@ impl WasmSdk {
365395 }
366396 let label = parts[ 0 ] ;
367397 let domain = parts[ 1 ] ;
398+
368399 let contract_id =
369400 Identifier :: from_string ( DPNS_CONTRACT_ID , Encoding :: Base58 ) . map_err ( |e| {
370401 WasmSdkError :: invalid_argument ( format ! ( "Invalid DPNS contract ID: {}" , e) )
371402 } ) ?;
403+
372404 let mut query = DocumentQuery :: new_with_data_contract_id (
373405 self . as_ref ( ) ,
374406 contract_id,
375407 DPNS_DOCUMENT_TYPE ,
376408 )
377409 . await ?;
410+
378411 query = query. with_where ( WhereClause {
379412 field : "normalizedLabel" . to_string ( ) ,
380413 operator : WhereOperator :: Equal ,
381414 value : Value :: Text ( label. to_lowercase ( ) ) ,
382415 } ) ;
416+
383417 query = query. with_where ( WhereClause {
384418 field : "normalizedParentDomainName" . to_string ( ) ,
385419 operator : WhereOperator :: Equal ,
386420 value : Value :: Text ( domain. to_lowercase ( ) ) ,
387421 } ) ;
422+
388423 let ( documents, metadata, proof) =
389424 Document :: fetch_many_with_metadata_and_proof ( self . as_ref ( ) , query, None ) . await ?;
425+
390426 if let Some ( ( _, Some ( document) ) ) = documents. into_iter ( ) . next ( ) {
391427 let result = DpnsUsernameInfo {
392428 username : username. to_string ( ) ,
393429 identity_id : document. owner_id ( ) . to_string ( Encoding :: Base58 ) ,
394430 document_id : document. id ( ) . to_string ( Encoding :: Base58 ) ,
395431 } ;
432+
396433 let data = serde_wasm_bindgen:: to_value ( & result) . map_err ( |e| {
397434 WasmSdkError :: serialization ( format ! ( "Failed to serialize username info: {}" , e) )
398435 } ) ?;
436+
399437 let response =
400438 ProofMetadataResponseWasm :: from_parts ( data, metadata. into ( ) , proof. into ( ) ) ;
439+
401440 Ok ( JsValue :: from ( response) )
402441 } else {
403442 Err ( WasmSdkError :: not_found ( format ! (
@@ -406,6 +445,7 @@ impl WasmSdk {
406445 ) ) )
407446 }
408447 }
448+
409449 #[ wasm_bindgen( js_name = "getDpnsUsernames" ) ]
410450 pub async fn get_dpns_usernames (
411451 & self ,
@@ -417,6 +457,7 @@ impl WasmSdk {
417457 . await ?;
418458 Ok ( usernames. into ( ) )
419459 }
460+
420461 #[ wasm_bindgen( js_name = "getDpnsUsername" ) ]
421462 pub async fn get_dpns_username (
422463 & self ,
@@ -425,9 +466,11 @@ impl WasmSdk {
425466 identity_id : JsValue ,
426467 ) -> Result < JsValue , WasmSdkError > {
427468 let identity_id_parsed = identifier_from_js ( & identity_id, "identity ID" ) ?;
469+
428470 let array = self
429471 . fetch_dpns_usernames ( identity_id_parsed, Some ( 1 ) )
430472 . await ?;
473+
431474 if array. length ( ) > 0 {
432475 Ok ( array. get ( 0 ) )
433476 } else {
@@ -443,6 +486,7 @@ impl WasmSdk {
443486 self . fetch_dpns_usernames_with_proof ( params. identity_id , params. limit )
444487 . await
445488 }
489+
446490 #[ wasm_bindgen( js_name = "getDpnsUsernameWithProofInfo" ) ]
447491 pub async fn get_dpns_username_with_proof_info (
448492 & self ,
@@ -451,18 +495,21 @@ impl WasmSdk {
451495 identity_id : JsValue ,
452496 ) -> Result < DpnsUsernameProofResponseWasm , WasmSdkError > {
453497 let identity_id_parsed = identifier_from_js ( & identity_id, "identity ID" ) ?;
498+
454499 let DpnsUsernamesProofResponseWasm {
455500 usernames,
456501 metadata,
457502 proof,
458503 } = self
459504 . fetch_dpns_usernames_with_proof ( identity_id_parsed, Some ( 1 ) )
460505 . await ?;
506+
461507 let username = if usernames. length ( ) > 0 {
462508 usernames. get ( 0 )
463509 } else {
464510 JsValue :: NULL
465511 } ;
512+
466513 Ok ( DpnsUsernameProofResponseWasm {
467514 username,
468515 metadata,
0 commit comments