Skip to content

Commit

Permalink
Code to check for existing domain record
Browse files Browse the repository at this point in the history
  • Loading branch information
tupui committed Jul 29, 2024
1 parent f9138ab commit 8175877
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
33 changes: 29 additions & 4 deletions contracts/versioning/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -16,7 +23,7 @@ pub enum ContractErrors {
ProjectAlreadyExist = 2,
UnregisteredMaintainer = 3,
NoHashFound = 4,
InputValidationError = 5,
InvalidDomainError = 5,
}

#[contracttype]
Expand Down Expand Up @@ -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]);

Expand All @@ -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);
Expand Down Expand Up @@ -174,6 +188,7 @@ fn auth_maintainers(env: &Env, maintainer: &Address, maintainers: &Vec<Address>)
}
}

/// 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;
Expand Down Expand Up @@ -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;
14 changes: 4 additions & 10 deletions contracts/versioning/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
#![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();
env.mock_all_auths();

// 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;
Expand All @@ -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(),
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 8175877

Please sign in to comment.