Skip to content

Commit

Permalink
Name to String and input validation
Browse files Browse the repository at this point in the history
  • Loading branch information
tupui committed Jul 24, 2024
1 parent 802da1d commit cc66627
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
15 changes: 12 additions & 3 deletions contracts/versioning/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub enum ContractErrors {
ProjectAlreadyExist = 2,
UnregisteredMaintainer = 3,
NoHashFound = 4,
InputValidationError = 5,
}

#[contracttype]
Expand All @@ -37,7 +38,7 @@ pub struct Config {

#[contracttype]
pub struct Project {
pub name: Bytes,
pub name: String,
pub config: Config,
pub maintainers: Vec<Address>,
}
Expand Down Expand Up @@ -66,7 +67,7 @@ impl Versioning {
pub fn register(
env: Env,
maintainer: Address,
name: Bytes,
name: String,
maintainers: Vec<Address>,
url: String,
hash: String,
Expand All @@ -77,7 +78,15 @@ impl Versioning {
maintainers,
};

let key: Bytes = env.crypto().keccak256(&name).into();
let str_len = name.len() as usize;
if str_len > 64 {
panic_with_error!(&env, &ContractErrors::InputValidationError);
}
let mut slice: [u8; 64] = [0; 64];
name.copy_into_slice(&mut slice[..str_len]);
let name_b = Bytes::from_slice(&env, &slice[0..str_len]);

let key: Bytes = env.crypto().keccak256(&name_b).into();

let key_ = ProjectKey::Key(key.clone());
if env
Expand Down
14 changes: 13 additions & 1 deletion contracts/versioning/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn test() {

contract.init(&contract_admin);

let name = Bytes::from_slice(&env, "soroban-versioning".as_bytes());
let name = String::from_str(&env, "soroban-versioning");
let url = String::from_str(&env, "github.com/file.toml");
let hash = String::from_str(&env, "2ef4f49fdd8fa9dc463f1f06a094c26b88710990");
let grogu = Address::generate(&env);
Expand Down Expand Up @@ -67,6 +67,18 @@ fn test() {

assert_eq!(error, ContractErrors::ProjectAlreadyExist.into());

// name too long
let name_long = String::from_str(
&env,
"soroban-versioningsoroban-versioningsoroban-versioningsoroban-versioning",
);
let error = contract
.try_register(&grogu, &name_long, &maintainers, &url, &hash)
.unwrap_err()
.unwrap();

assert_eq!(error, ContractErrors::InputValidationError.into());

// un-registered maintainer
let bob = Address::generate(&env);
let error = contract
Expand Down

0 comments on commit cc66627

Please sign in to comment.