Skip to content

Commit 69ac84a

Browse files
quantumquantum
authored andcommitted
more work
1 parent 647da0c commit 69ac84a

File tree

3 files changed

+99
-27
lines changed

3 files changed

+99
-27
lines changed

packages/rs-sdk/src/platform/dpns_usernames.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl Sdk {
171171
use dpp::system_data_contracts::SystemDataContract;
172172
SystemDataContract::DPNS.id()
173173
};
174-
174+
175175
#[cfg(not(feature = "dpns-contract"))]
176176
let dpns_contract_id = {
177177
const DPNS_CONTRACT_ID: &str = "GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec";
@@ -183,13 +183,13 @@ impl Sdk {
183183
};
184184

185185
// First check if the contract is available in the context provider
186-
let context_provider = self.context_provider()
186+
let context_provider = self
187+
.context_provider()
187188
.ok_or_else(|| Error::DapiClientError("Context provider not set".to_string()))?;
188-
189-
let dpns_contract = match context_provider.get_data_contract(
190-
&dpns_contract_id,
191-
self.version(),
192-
)? {
189+
190+
let dpns_contract = match context_provider
191+
.get_data_contract(&dpns_contract_id, self.version())?
192+
{
193193
Some(contract) => contract,
194194
None => {
195195
// If not in context, fetch from platform
@@ -366,7 +366,7 @@ impl Sdk {
366366
use dpp::system_data_contracts::SystemDataContract;
367367
SystemDataContract::DPNS.id()
368368
};
369-
369+
370370
#[cfg(not(feature = "dpns-contract"))]
371371
let dpns_contract_id = {
372372
const DPNS_CONTRACT_ID: &str = "GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec";
@@ -378,13 +378,13 @@ impl Sdk {
378378
};
379379

380380
// First check if the contract is available in the context provider
381-
let context_provider = self.context_provider()
381+
let context_provider = self
382+
.context_provider()
382383
.ok_or_else(|| Error::DapiClientError("Context provider not set".to_string()))?;
383-
384-
let dpns_contract = match context_provider.get_data_contract(
385-
&dpns_contract_id,
386-
self.version(),
387-
)? {
384+
385+
let dpns_contract = match context_provider
386+
.get_data_contract(&dpns_contract_id, self.version())?
387+
{
388388
Some(contract) => contract,
389389
None => {
390390
// If not in context, fetch from platform
@@ -444,7 +444,7 @@ impl Sdk {
444444
use dpp::system_data_contracts::SystemDataContract;
445445
SystemDataContract::DPNS.id()
446446
};
447-
447+
448448
#[cfg(not(feature = "dpns-contract"))]
449449
let dpns_contract_id = {
450450
const DPNS_CONTRACT_ID: &str = "GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec";
@@ -456,13 +456,13 @@ impl Sdk {
456456
};
457457

458458
// First check if the contract is available in the context provider
459-
let context_provider = self.context_provider()
459+
let context_provider = self
460+
.context_provider()
460461
.ok_or_else(|| Error::DapiClientError("Context provider not set".to_string()))?;
461-
462-
let dpns_contract = match context_provider.get_data_contract(
463-
&dpns_contract_id,
464-
self.version(),
465-
)? {
462+
463+
let dpns_contract = match context_provider
464+
.get_data_contract(&dpns_contract_id, self.version())?
465+
{
466466
Some(contract) => contract,
467467
None => {
468468
// If not in context, fetch from platform
@@ -488,12 +488,12 @@ impl Sdk {
488488
// No dot found, use the whole name as the label
489489
name
490490
};
491-
491+
492492
// Validate the label before proceeding
493493
if label.is_empty() {
494494
return Ok(None);
495495
}
496-
496+
497497
let normalized_label = convert_to_homograph_safe_chars(label);
498498

499499
// Query for domain with this label

packages/wasm-sdk/index.html

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3529,16 +3529,43 @@ <h2>Results</h2>
35293529

35303530
console.log(`Using key ID ${keyId} for DPNS registration`);
35313531

3532+
// Show initial status
3533+
updateStatus('Submitting DPNS preorder document...', 'info');
3534+
3535+
// Add a small delay to let the status message show
3536+
await new Promise(resolve => setTimeout(resolve, 100));
3537+
35323538
result = await dpns_register_name(
35333539
sdk,
35343540
values.label,
35353541
identityId, // Use the identity ID from authentication
35363542
keyId, // Use the determined key ID
3537-
actualPrivateKey // Use the actual private key (without :keyId suffix)
3543+
actualPrivateKey, // Use the actual private key (without :keyId suffix)
3544+
// Callback for preorder success
3545+
(preorderInfo) => {
3546+
console.log('Preorder successful:', preorderInfo);
3547+
updateStatus('Preorder successful! Now submitting domain document...', 'info');
3548+
3549+
// Show preorder info in a temporary notification
3550+
const preorderMsg = `Preorder Document ID: ${preorderInfo.documentId}`;
3551+
const notification = document.createElement('div');
3552+
notification.className = 'preorder-notification';
3553+
notification.style.cssText = 'background: #4CAF50; color: white; padding: 10px; margin: 10px 0; border-radius: 4px;';
3554+
notification.textContent = preorderMsg;
3555+
3556+
const resultsSection = document.getElementById('results');
3557+
if (resultsSection) {
3558+
resultsSection.insertBefore(notification, resultsSection.firstChild);
3559+
// Remove notification after 5 seconds
3560+
setTimeout(() => notification.remove(), 5000);
3561+
}
3562+
}
35383563
);
35393564

3565+
// Since our callback is called at the end, let's update the message
3566+
// to reflect that both documents were submitted
35403567
displayResult(JSON.stringify(result, null, 2));
3541-
updateStatus('DPNS name registered successfully', 'success');
3568+
updateStatus('DPNS name registered successfully! Both preorder and domain documents submitted.', 'success');
35423569
} else if (transitionType === 'documentPurchase') {
35433570
// Handle document purchase
35443571
result = await sdk.documentPurchase(

packages/wasm-sdk/src/dpns.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ use crate::sdk::WasmSdk;
44
use serde::{Serialize, Deserialize};
55
use dash_sdk::platform::dpns_usernames::{convert_to_homograph_safe_chars, is_contested_username, is_valid_username, RegisterDpnsNameInput};
66
use dash_sdk::platform::{Fetch, Identity};
7-
use dash_sdk::dpp::document::DocumentV0Getters;
7+
use dash_sdk::dpp::document::{Document, DocumentV0Getters};
88
use dash_sdk::dpp::identity::accessors::IdentityGettersV0;
99
use dash_sdk::dpp::prelude::Identifier;
1010
use simple_signer::SingleKeySigner;
11+
use std::sync::Mutex;
1112

1213
#[derive(Serialize, Deserialize)]
1314
#[serde(rename_all = "camelCase")]
@@ -43,6 +44,7 @@ pub async fn dpns_register_name(
4344
identity_id: &str,
4445
public_key_id: u32,
4546
private_key_wif: &str,
47+
preorder_callback: Option<js_sys::Function>,
4648
) -> Result<JsValue, JsError> {
4749
// Parse identity ID
4850
let identity_id_parsed = Identifier::from_string(
@@ -66,12 +68,50 @@ pub async fn dpns_register_name(
6668
.ok_or_else(|| JsError::new(&format!("Public key with ID {} not found", public_key_id)))?
6769
.clone();
6870

69-
// Create registration input
71+
// Store the JS callback in a thread-local variable that we can access from the closure
72+
thread_local! {
73+
static PREORDER_CALLBACK: std::cell::RefCell<Option<js_sys::Function>> = std::cell::RefCell::new(None);
74+
}
75+
76+
// Set the callback if provided
77+
if let Some(ref js_callback) = preorder_callback {
78+
PREORDER_CALLBACK.with(|cb| {
79+
*cb.borrow_mut() = Some(js_callback.clone());
80+
});
81+
}
82+
83+
// Create a Rust callback that will call the JavaScript callback
84+
let callback_box = if preorder_callback.is_some() {
85+
Some(Box::new(move |doc: &Document| {
86+
PREORDER_CALLBACK.with(|cb| {
87+
if let Some(js_callback) = cb.borrow().as_ref() {
88+
let preorder_info = serde_json::json!({
89+
"documentId": doc.id().to_string(dash_sdk::dpp::platform_value::string_encoding::Encoding::Base58),
90+
"ownerId": doc.owner_id().to_string(dash_sdk::dpp::platform_value::string_encoding::Encoding::Base58),
91+
"revision": doc.revision().unwrap_or(0),
92+
"createdAt": doc.created_at(),
93+
"createdAtBlockHeight": doc.created_at_block_height(),
94+
"createdAtCoreBlockHeight": doc.created_at_core_block_height(),
95+
"message": "Preorder document submitted successfully",
96+
});
97+
98+
if let Ok(js_value) = serde_wasm_bindgen::to_value(&preorder_info) {
99+
let _ = js_callback.call1(&JsValue::NULL, &js_value);
100+
}
101+
}
102+
});
103+
}) as Box<dyn FnOnce(&Document) + Send>)
104+
} else {
105+
None
106+
};
107+
108+
// Create registration input with the callback
70109
let input = RegisterDpnsNameInput {
71110
label: label.to_string(),
72111
identity,
73112
identity_public_key,
74113
signer,
114+
preorder_callback: callback_box,
75115
};
76116

77117
// Register the name
@@ -80,6 +120,11 @@ pub async fn dpns_register_name(
80120
.await
81121
.map_err(|e| JsError::new(&format!("Failed to register DPNS name: {}", e)))?;
82122

123+
// Clear the thread-local callback
124+
PREORDER_CALLBACK.with(|cb| {
125+
*cb.borrow_mut() = None;
126+
});
127+
83128
// Convert result to JS-friendly format
84129
let js_result = RegisterDpnsNameResult {
85130
preorder_document_id: result.preorder_document.id().to_string(

0 commit comments

Comments
 (0)