From 81758779d5da57841cee2ac3ea2ac59759a27054 Mon Sep 17 00:00:00 2001 From: Tupui <23188539+tupui@users.noreply.github.com> Date: Tue, 30 Jul 2024 01:26:57 +0200 Subject: [PATCH] Code to check for existing domain record --- contracts/versioning/src/lib.rs | 33 ++++++++++++++++++++++++++++---- contracts/versioning/src/test.rs | 14 ++++---------- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/contracts/versioning/src/lib.rs b/contracts/versioning/src/lib.rs index 0787436..1828175 100755 --- a/contracts/versioning/src/lib.rs +++ b/contracts/versioning/src/lib.rs @@ -1,5 +1,12 @@ #![no_std] +mod domain_contract { + soroban_sdk::contractimport!( + file = "../domain_3ebbeec072f4996958d4318656186732773ab5f0c159dcf039be202b4ecb8af8.wasm" + ); +} + +use soroban_sdk::testutils::arbitrary::std::println; use soroban_sdk::{ contract, contractimpl, contracttype, panic_with_error, symbol_short, vec, Address, Bytes, BytesN, Env, IntoVal, String, Symbol, Val, Vec, @@ -16,7 +23,7 @@ pub enum ContractErrors { ProjectAlreadyExist = 2, UnregisteredMaintainer = 3, NoHashFound = 4, - InputValidationError = 5, + InvalidDomainError = 5, } #[contracttype] @@ -79,10 +86,10 @@ impl Versioning { maintainers, }; let str_len = name.len() as usize; - if str_len > 64 { - panic_with_error!(&env, &ContractErrors::InputValidationError); + if str_len > 15 { + panic_with_error!(&env, &ContractErrors::InvalidDomainError); } - let mut slice: [u8; 64] = [0; 64]; + let mut slice: [u8; 15] = [0; 15]; name.copy_into_slice(&mut slice[..str_len]); let name_b = Bytes::from_slice(&env, &slice[0..str_len]); @@ -99,6 +106,13 @@ impl Versioning { } else { auth_maintainers(&env, &maintainer, &project.maintainers); + let node = domain_node(&env, &key); + let record_keys = domain_contract::RecordKeys::Record(node); + + let domain_client = domain_contract::Client::new(&env, &domain_contract_id); + let record = domain_client.record(&record_keys); + println!("{record:#?}"); + domain_register(&env, &name_b, &maintainer, domain_contract_id); env.storage().persistent().set(&key_, &project); @@ -174,6 +188,7 @@ fn auth_maintainers(env: &Env, maintainer: &Address, maintainers: &Vec
) } } +/// Register a Soroban Domain: https://sorobandomains.org fn domain_register(env: &Env, name: &Bytes, maintainer: &Address, domain_contract_id: Address) { let tld = Bytes::from_slice(env, &[120, 108, 109]); // xlm let min_duration: u64 = 31536000; @@ -201,4 +216,14 @@ fn domain_register(env: &Env, name: &Bytes, maintainer: &Address, domain_contrac ); } +fn domain_node(env: &Env, domain: &Bytes) -> BytesN<32> { + let tld = Bytes::from_slice(env, &[120, 108, 109]); // xlm + let parent_hash: Bytes = env.crypto().keccak256(&tld).into(); + let mut node_builder: Bytes = Bytes::new(env); + node_builder.append(&parent_hash); + node_builder.append(domain); + + env.crypto().keccak256(&node_builder).into() +} + mod test; diff --git a/contracts/versioning/src/test.rs b/contracts/versioning/src/test.rs index 6cd0833..8bfb0aa 100755 --- a/contracts/versioning/src/test.rs +++ b/contracts/versioning/src/test.rs @@ -1,17 +1,11 @@ #![cfg(test)] -use super::{ContractErrors, Versioning, VersioningClient}; +use super::{domain_contract, ContractErrors, Versioning, VersioningClient}; use soroban_sdk::testutils::Address as _; use soroban_sdk::{ symbol_short, testutils::Events, token, vec, Address, Bytes, Env, IntoVal, String, Vec, }; -mod domain_contract { - soroban_sdk::contractimport!( - file = "../domain_3ebbeec072f4996958d4318656186732773ab5f0c159dcf039be202b4ecb8af8.wasm" - ); -} - #[test] fn test() { let env = Env::default(); @@ -19,7 +13,7 @@ fn test() { // setup for Soroban Domain let domain_contract_id = env.register_contract_wasm(None, domain_contract::WASM); - let domain_contract = domain_contract::Client::new(&env, &domain_contract_id); + let domain_client = domain_contract::Client::new(&env, &domain_contract_id); let adm: Address = Address::generate(&env); let node_rate: u128 = 100; @@ -38,7 +32,7 @@ fn test() { let col_asset_client = token::TokenClient::new(&env, &token_address); let col_asset_stellar = token::StellarAssetClient::new(&env, &token_address); - domain_contract.init( + domain_client.init( &adm, &node_rate, &col_asset_client.address.clone(), @@ -141,7 +135,7 @@ fn test() { .unwrap_err() .unwrap(); - assert_eq!(error, ContractErrors::InputValidationError.into()); + assert_eq!(error, ContractErrors::InvalidDomainError.into()); // un-registered maintainer let bob = Address::generate(&env);