diff --git a/contracts/versioning/src/lib.rs b/contracts/versioning/src/lib.rs index 4cda040..1c12da5 100755 --- a/contracts/versioning/src/lib.rs +++ b/contracts/versioning/src/lib.rs @@ -16,6 +16,7 @@ pub enum ContractErrors { ProjectAlreadyExist = 2, UnregisteredMaintainer = 3, NoHashFound = 4, + InputValidationError = 5, } #[contracttype] @@ -37,7 +38,7 @@ pub struct Config { #[contracttype] pub struct Project { - pub name: Bytes, + pub name: String, pub config: Config, pub maintainers: Vec
, } @@ -66,7 +67,7 @@ impl Versioning { pub fn register( env: Env, maintainer: Address, - name: Bytes, + name: String, maintainers: Vec
, url: String, hash: String, @@ -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 diff --git a/contracts/versioning/src/test.rs b/contracts/versioning/src/test.rs index 9fa0626..c56ba45 100755 --- a/contracts/versioning/src/test.rs +++ b/contracts/versioning/src/test.rs @@ -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); @@ -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