Skip to content

Commit 2c64393

Browse files
committed
Cleanup
1 parent 7c5c6cd commit 2c64393

8 files changed

Lines changed: 74 additions & 70 deletions

File tree

keetanetwork-account/src/account.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ impl FromHex for IdentifierKey {
436436
type Error = AccountError;
437437

438438
fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
439-
let bytes = hex::decode(hex).map_err(|_| AccountError::InvalidConstruction)?;
439+
let bytes = hex::decode(hex)?;
440440
Self::try_from(bytes)
441441
}
442442
}
@@ -1319,7 +1319,7 @@ impl FromHex for GenericAccount {
13191319
type Error = AccountError;
13201320

13211321
fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
1322-
let data = hex::decode(hex).map_err(|_| AccountError::InvalidConstruction)?;
1322+
let data = hex::decode(hex)?;
13231323
if data.is_empty() {
13241324
return Err(AccountError::InvalidConstruction);
13251325
}
@@ -2849,7 +2849,7 @@ macro_rules! impl_from_hex {
28492849
type Error = AccountError;
28502850

28512851
fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
2852-
let data = hex::decode(hex).map_err(|_| AccountError::InvalidConstruction)?;
2852+
let data = hex::decode(hex)?;
28532853
if data.len() < 2 {
28542854
return Err(AccountError::InvalidConstruction);
28552855
}
@@ -3926,6 +3926,7 @@ mod tests {
39263926
ed25519_account.keypair.to_public_key_string()?
39273927
);
39283928
}
3929+
39293930
Ok(())
39303931
}
39313932

@@ -4023,6 +4024,7 @@ mod tests {
40234024
// Test network -> token generation (should succeed)
40244025
let token_from_network = network_account.generate_identifier(KeyPairType::TOKEN, None, 0);
40254026
assert!(token_from_network.is_ok());
4027+
40264028
Ok(())
40274029
}
40284030

@@ -4524,6 +4526,7 @@ mod tests {
45244526
assert_eq!(network_account.to_string(), public_network.to_string());
45254527
assert_eq!(network_account.is_identifier(), public_network.is_identifier());
45264528
assert!(!public_network.has_private_key());
4529+
45274530
Ok(())
45284531
}
45294532

@@ -4582,6 +4585,7 @@ mod tests {
45824585
GenericAccount::Multisig(acc) => assert_eq!(acc.to_keypair_type(), KeyPairType::MULTISIG),
45834586
}
45844587
}
4588+
45854589
Ok(())
45864590
}
45874591

keetanetwork-account/src/error.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ impl From<AccountError> for KeetaNetError {
8686
impl_variant_error_from!(AccountError, {
8787
FromHexError => InvalidConstruction,
8888
keetanetwork_crypto::operations::SignatureError => InvalidConstruction,
89+
core::array::TryFromSliceError => InvalidConstruction,
8990
});
9091

9192
#[cfg(any(feature = "der", feature = "rasn"))]
@@ -169,6 +170,13 @@ mod tests {
169170
]
170171
}
171172

173+
// Test From conversion for TryFromSliceError
174+
test_error_from_conversions! {
175+
test_try_from_slice_error_conversion, AccountError, [
176+
<[u8; 32]>::try_from([1u8, 2, 3].as_slice()).unwrap_err(),
177+
]
178+
}
179+
172180
#[test]
173181
fn test_error_code_format() {
174182
let error = AccountError::InvalidPrefix;

keetanetwork-asn1/src/rasn.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -390,11 +390,11 @@ mod tests {
390390
fn test_object_identifier_ext_from_str() -> Result<(), Asn1Error> {
391391
// Test valid OID strings
392392
let valid_oids = [
393-
("1.2.840.113549.1.1.1", vec![1, 2, 840, 113549, 1, 1, 1]), // RSA encryption
394-
("1.3.101.112", vec![1, 3, 101, 112]), // Ed25519
395-
("1.2.840.10045.3.1.7", vec![1, 2, 840, 10045, 3, 1, 7]), // secp256r1
396-
("1.3.132.0.10", vec![1, 3, 132, 0, 10]), // secp256k1
397-
("1.2", vec![1, 2]), // Simple OID
393+
(oids::RSA_ENCRYPTION, vec![1, 2, 840, 113549, 1, 1, 1]),
394+
(oids::ED25519, vec![1, 3, 101, 112]),
395+
(oids::SECP256R1, vec![1, 2, 840, 10045, 3, 1, 7]),
396+
(oids::SECP256K1, vec![1, 3, 132, 0, 10]),
397+
("1.2", vec![1, 2]), // Simple OID (intentionally hardcoded)
398398
];
399399

400400
for (oid_str, expected_arcs) in valid_oids {
@@ -425,7 +425,7 @@ mod tests {
425425

426426
#[test]
427427
fn test_object_identifier_ext_from_str_with_as_ref() -> Result<(), Asn1Error> {
428-
let oid_str = "1.3.101.112";
428+
let oid_str = oids::ED25519;
429429
let oid1 = ObjectIdentifier::from_str(oid_str)?;
430430
let oid_string = oid_str.to_string();
431431
let oid2 = ObjectIdentifier::from_str(&oid_string)?;

keetanetwork-asn1/src/utils.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,11 @@ mod tests {
403403
use crate::error::Asn1Error;
404404
use serde_json;
405405

406+
use crate::oids;
407+
406408
// Test data constants
407-
const TEST_OID_STR: &str = "1.2.840.113549.1.1.1";
408-
const TEST_OID_JSON: &str = r#""1.2.840.113549.1.1.1""#;
409+
const TEST_OID_STR: &str = oids::RSA_ENCRYPTION;
410+
const TEST_OID_JSON: &str = concat!('"', "1.2.840.113549.1.1.1", '"');
409411

410412
const TEST_OCTET_BYTES: &[u8] = &[0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef];
411413
const TEST_OCTET_JSON: &str = r#""0123456789abcdef""#;
@@ -414,11 +416,12 @@ mod tests {
414416
const TEST_BIT_JSON: &str = r#""fedcba98""#;
415417

416418
// Test OID database
419+
// Note: SHA256 hash OID is kept hardcoded as it's not defined in keetanetwork-asn1::oids
417420
const TEST_OID_DB: &[(&str, &str)] = &[
418-
("RSA", "1.2.840.113549.1.1.1"),
421+
("RSA", oids::RSA_ENCRYPTION),
419422
("SHA256", "2.16.840.1.101.3.4.2.1"),
420-
("Ed25519", "1.3.101.112"),
421-
("ECDSA_P256", "1.2.840.10045.3.1.7"),
423+
("Ed25519", oids::ED25519),
424+
("ECDSA_P256", oids::SECP256R1),
422425
];
423426

424427
fn get_test_oid_db() -> HashMap<&'static str, &'static str> {
@@ -596,7 +599,7 @@ mod tests {
596599

597600
// Test successful lookup
598601
let result = get_oid("RSA", &oid_db)?;
599-
assert_eq!(result.to_string(), "1.2.840.113549.1.1.1");
602+
assert_eq!(result.to_string(), oids::RSA_ENCRYPTION);
600603

601604
// Test unknown algorithm
602605
let result = get_oid("UnknownAlgorithm", &oid_db);
@@ -616,7 +619,7 @@ mod tests {
616619
let oid_db = get_test_oid_db();
617620

618621
// Test successful lookup
619-
let result = lookup_by_oid("1.2.840.113549.1.1.1", &oid_db)?;
622+
let result = lookup_by_oid(oids::RSA_ENCRYPTION, &oid_db)?;
620623
assert_eq!(result, "RSA");
621624

622625
// Test unknown OID (using a valid but non-existent OID)
@@ -632,9 +635,9 @@ mod tests {
632635

633636
// Test successful lookup
634637
#[cfg(feature = "der")]
635-
let oid = ObjectIdentifier::new("1.2.840.113549.1.1.1")?;
638+
let oid = ObjectIdentifier::new(oids::RSA_ENCRYPTION)?;
636639
#[cfg(all(feature = "rasn", not(feature = "der")))]
637-
let oid = crate::utils::parse_oid_string("1.2.840.113549.1.1.1")?;
640+
let oid = crate::utils::parse_oid_string(oids::RSA_ENCRYPTION)?;
638641

639642
let result = lookup_by_object_identifier(&oid, &oid_db)?;
640643
assert_eq!(result, "RSA");
@@ -654,8 +657,8 @@ mod tests {
654657
#[test]
655658
fn test_parse_oid_string() -> Result<(), Asn1Error> {
656659
// Test successful parsing
657-
let result = parse_oid_string("1.2.840.113549.1.1.1")?;
658-
assert_eq!(result.to_string(), "1.2.840.113549.1.1.1");
660+
let result = parse_oid_string(oids::RSA_ENCRYPTION)?;
661+
assert_eq!(result.to_string(), oids::RSA_ENCRYPTION);
659662

660663
// Test invalid OID format
661664
let result = parse_oid_string("invalid.oid");
@@ -667,7 +670,7 @@ mod tests {
667670
#[test]
668671
fn test_validate_oid_format() -> Result<(), Asn1Error> {
669672
// Test successful validation
670-
let result = validate_oid_format("1.2.840.113549.1.1.1")?;
673+
let result = validate_oid_format(oids::RSA_ENCRYPTION)?;
671674
assert_eq!(result, vec![1, 2, 840, 113549, 1, 1, 1]);
672675

673676
// Test empty string

keetanetwork-crypto/src/algorithms/aes_cbc.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,32 +128,30 @@ mod tests {
128128

129129
let result = aes_cbc.decrypt(key, short_ciphertext);
130130
assert!(result.is_err());
131-
assert!(matches!(result, Err(CryptoError::DecryptionFailed)));
131+
assert!(matches!(result.unwrap_err(), CryptoError::DecryptionFailed));
132132
}
133133

134134
#[test]
135-
fn test_aes_256_cbc_pkcs7_padding() -> Result<(), CryptoError> {
135+
fn test_aes_256_cbc_pkcs7_padding() {
136136
let aes_cbc = Aes256Cbc;
137137
let key = [0x42u8; 32];
138138

139139
// Test with data that's exactly block-aligned (16 bytes)
140140
let block_aligned = [0x55u8; 16];
141-
let ciphertext = aes_cbc.encrypt(key, None, block_aligned)?;
142-
let decrypted = aes_cbc.decrypt(key, &ciphertext)?;
141+
let ciphertext = aes_cbc.encrypt(key, None, block_aligned).unwrap();
142+
let decrypted = aes_cbc.decrypt(key, &ciphertext).unwrap();
143143
assert_eq!(decrypted, block_aligned);
144144

145145
// Test with data that needs padding (15 bytes)
146146
let needs_padding = [0x66u8; 15];
147-
let ciphertext = aes_cbc.encrypt(key, None, needs_padding)?;
148-
let decrypted = aes_cbc.decrypt(key, &ciphertext)?;
147+
let ciphertext = aes_cbc.encrypt(key, None, needs_padding).unwrap();
148+
let decrypted = aes_cbc.decrypt(key, &ciphertext).unwrap();
149149
assert_eq!(decrypted, needs_padding);
150150

151151
// Test with data that needs lots of padding (1 byte)
152152
let minimal_data = [0x77u8; 1];
153-
let ciphertext = aes_cbc.encrypt(key, None, minimal_data)?;
154-
let decrypted = aes_cbc.decrypt(key, &ciphertext)?;
153+
let ciphertext = aes_cbc.encrypt(key, None, minimal_data).unwrap();
154+
let decrypted = aes_cbc.decrypt(key, &ciphertext).unwrap();
155155
assert_eq!(decrypted, minimal_data);
156-
157-
Ok(())
158156
}
159157
}

keetanetwork-crypto/src/algorithms/aes_ctr.rs

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -196,44 +196,40 @@ mod tests {
196196
crate::test_utils::test_aes_symmetric!(Aes128CtrCipher, 16, "AES-128-CTR");
197197

198198
#[test]
199-
fn test_aes_128_ctr_with_iv() -> Result<(), CryptoError> {
199+
fn test_aes_128_ctr_with_iv() {
200200
let cipher = Aes128CtrCipher::new();
201201
let key = [0x42u8; 16];
202202
let iv = [0x12u8; 16];
203203
let plaintext = b"Test with specific IV";
204204

205205
// Test encryption with specific IV
206-
let ciphertext = cipher.encrypt_with_iv(key, iv, plaintext)?;
206+
let ciphertext = cipher.encrypt_with_iv(key, iv, plaintext).unwrap();
207207
assert_ne!(ciphertext.as_slice(), plaintext);
208208
assert_eq!(ciphertext.len(), plaintext.len()); // CTR preserves length
209209

210210
// Test decryption with same IV
211-
let decrypted = cipher.decrypt_with_iv(key, iv, &ciphertext)?;
211+
let decrypted = cipher.decrypt_with_iv(key, iv, &ciphertext).unwrap();
212212
assert_eq!(decrypted, plaintext);
213-
214-
Ok(())
215213
}
216214

217215
#[test]
218-
fn test_aes_128_ctr_deterministic_with_same_iv() -> Result<(), CryptoError> {
216+
fn test_aes_128_ctr_deterministic_with_same_iv() {
219217
let cipher = Aes128CtrCipher::new();
220218
let key = [0x42u8; 16];
221219
let iv = [0x12u8; 16];
222220
let plaintext = b"Same plaintext";
223221

224222
// Encrypt the same plaintext with the same IV twice
225223
// Should be identical (deterministic with same key + IV)
226-
let ciphertext1 = cipher.encrypt_with_iv(key, iv, plaintext)?;
227-
let ciphertext2 = cipher.encrypt_with_iv(key, iv, plaintext)?;
224+
let ciphertext1 = cipher.encrypt_with_iv(key, iv, plaintext).unwrap();
225+
let ciphertext2 = cipher.encrypt_with_iv(key, iv, plaintext).unwrap();
228226
assert_eq!(ciphertext1, ciphertext2);
229227

230228
// Both should decrypt correctly
231-
let decrypted1 = cipher.decrypt_with_iv(key, iv, &ciphertext1)?;
232-
let decrypted2 = cipher.decrypt_with_iv(key, iv, &ciphertext2)?;
229+
let decrypted1 = cipher.decrypt_with_iv(key, iv, &ciphertext1).unwrap();
230+
let decrypted2 = cipher.decrypt_with_iv(key, iv, &ciphertext2).unwrap();
233231
assert_eq!(decrypted1, plaintext);
234232
assert_eq!(decrypted2, plaintext);
235-
236-
Ok(())
237233
}
238234

239235
#[test]
@@ -248,41 +244,37 @@ mod tests {
248244
}
249245

250246
#[test]
251-
fn test_aes_128_ctr_default_constructor() -> Result<(), CryptoError> {
247+
fn test_aes_128_ctr_default_constructor() {
252248
// This fixes coverage issues and ensures the default is covered
253249
#[allow(clippy::default_constructed_unit_structs)]
254250
let cipher = Aes128CtrCipher::default();
255251
let key = [0x42u8; 16];
256252
let plaintext = b"Default constructor test";
257253

258-
let ciphertext = cipher.encrypt(key, None, plaintext)?;
259-
let decrypted = cipher.decrypt(key, &ciphertext)?;
254+
let ciphertext = cipher.encrypt(key, None, plaintext).unwrap();
255+
let decrypted = cipher.decrypt(key, &ciphertext).unwrap();
260256
assert_eq!(decrypted, plaintext);
261-
262-
Ok(())
263257
}
264258

265259
#[test]
266-
fn test_aes_128_ctr_stream_property() -> Result<(), CryptoError> {
260+
fn test_aes_128_ctr_stream_property() {
267261
let cipher = Aes128CtrCipher::new();
268262
let key = [0x42u8; 16];
269263
let iv = [0x12u8; 16];
270264

271265
let plaintext1 = b"short";
272266
let plaintext2 = b"a much longer plaintext message";
273267

274-
let ciphertext1 = cipher.encrypt_with_iv(key, iv, plaintext1)?;
275-
let ciphertext2 = cipher.encrypt_with_iv(key, iv, plaintext2)?;
268+
let ciphertext1 = cipher.encrypt_with_iv(key, iv, plaintext1).unwrap();
269+
let ciphertext2 = cipher.encrypt_with_iv(key, iv, plaintext2).unwrap();
276270
assert_eq!(ciphertext1.len(), plaintext1.len());
277271
assert_eq!(ciphertext2.len(), plaintext2.len());
278272

279273
// Verify decryption works correctly
280-
let decrypted1 = cipher.decrypt_with_iv(key, iv, &ciphertext1)?;
281-
let decrypted2 = cipher.decrypt_with_iv(key, iv, &ciphertext2)?;
274+
let decrypted1 = cipher.decrypt_with_iv(key, iv, &ciphertext1).unwrap();
275+
let decrypted2 = cipher.decrypt_with_iv(key, iv, &ciphertext2).unwrap();
282276
assert_eq!(decrypted1, plaintext1);
283277
assert_eq!(decrypted2, plaintext2);
284-
285-
Ok(())
286278
}
287279

288280
#[test]

keetanetwork-crypto/src/algorithms/ed25519.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,7 @@ impl TryFrom<&[u8]> for Ed25519PrivateKey {
124124
type Error = CryptoError;
125125

126126
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
127-
let bytes_array: [u8; ed25519_dalek::SECRET_KEY_LENGTH] = bytes
128-
.try_into()
129-
.map_err(|_| CryptoError::InvalidPrivateKey)?;
130-
127+
let bytes_array: [u8; ed25519_dalek::SECRET_KEY_LENGTH] = bytes.try_into()?;
131128
let signing_key = SigningKey::from_bytes(&bytes_array);
132129
Ok(Ed25519PrivateKey { inner: signing_key })
133130
}
@@ -262,10 +259,7 @@ impl TryFrom<&[u8]> for Ed25519PublicKey {
262259
type Error = CryptoError;
263260

264261
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
265-
let bytes_array: [u8; ed25519_dalek::PUBLIC_KEY_LENGTH] = bytes
266-
.try_into()
267-
.map_err(|_| CryptoError::InvalidPublicKey)?;
268-
262+
let bytes_array: [u8; ed25519_dalek::PUBLIC_KEY_LENGTH] = bytes.try_into()?;
269263
let verifying_key = VerifyingKey::from_bytes(&bytes_array).map_err(|_| CryptoError::InvalidPublicKey)?;
270264
Ok(Ed25519PublicKey { inner: verifying_key, bytes: bytes.to_vec() })
271265
}
@@ -416,10 +410,7 @@ impl TryFrom<&[u8]> for X25519PrivateKey {
416410
type Error = CryptoError;
417411

418412
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
419-
let bytes_array: [u8; 32] = bytes
420-
.try_into()
421-
.map_err(|_| CryptoError::InvalidPrivateKey)?;
422-
413+
let bytes_array: [u8; 32] = bytes.try_into()?;
423414
let bytes = bytes_array.into_secret();
424415
Ok(X25519PrivateKey { bytes })
425416
}
@@ -881,19 +872,19 @@ mod tests {
881872
let wrong_length_bytes = [0x01; 16]; // Wrong length
882873
let result = Ed25519PublicKey::try_from(wrong_length_bytes.as_slice());
883874
assert!(result.is_err());
884-
assert!(matches!(result, Err(CryptoError::InvalidPublicKey)));
875+
assert!(matches!(result, Err(CryptoError::InvalidKeySize)));
885876

886877
// Test with empty bytes
887878
let empty_bytes = [];
888879
let result = Ed25519PublicKey::try_from(empty_bytes.as_slice());
889880
assert!(result.is_err());
890-
assert!(matches!(result, Err(CryptoError::InvalidPublicKey)));
881+
assert!(matches!(result, Err(CryptoError::InvalidKeySize)));
891882

892883
// Test with too long bytes
893884
let too_long_bytes = [0x01; 64]; // Too long
894885
let result = Ed25519PublicKey::try_from(too_long_bytes.as_slice());
895886
assert!(result.is_err());
896-
assert!(matches!(result, Err(CryptoError::InvalidPublicKey)));
887+
assert!(matches!(result, Err(CryptoError::InvalidKeySize)));
897888
}
898889

899890
#[test]

0 commit comments

Comments
 (0)