Skip to content

Commit

Permalink
updated unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Zachary Frederick committed Jul 17, 2023
1 parent a225961 commit 6d8094c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
17 changes: 13 additions & 4 deletions pallets/creditcoin/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ fn extract_public_key_eth_sign<T: Config>(
sp_core::ecdsa::Public::from_raw(public_key),
) {
Some(s) => Ok(s),
None => Err(Error::MalformedExternalAddress),
None => Err(Error::EthSignExternalAddressGenerationFailed),
}
},
Err(_) => Err(Error::EthSignPublicKeyRecoveryFailed),
Err(_) => Err(Error::InvalidSignature),
}
}

Expand Down Expand Up @@ -224,10 +224,10 @@ pub fn extract_public_key_personal_sign<T: Config>(
sp_core::ecdsa::Public::from_raw(public_key),
) {
Some(s) => Ok(s),
None => Err(Error::MalformedExternalAddress),
None => Err(Error::PersonalSignExternalAddressGenerationFailed),
}
},
Err(_) => Err(Error::PersonalSignPublicKeyRecoveryFailed),
Err(_) => Err(Error::InvalidSignature),
}
}

Expand All @@ -245,3 +245,12 @@ fn test_extract_public_key_personal_sign() {

assert_eq!(message.as_slice(), expected_hash.as_slice());
}

pub fn blockchain_is_supported(blockchain: &Blockchain) -> bool {
match blockchain {
Blockchain::Luniverse | Blockchain::Ethereum | Blockchain::Rinkeby => true,
Blockchain::Bitcoin => false,
Blockchain::Other(_) => false,
_ => false,

Check failure on line 254 in pallets/creditcoin/src/helpers.rs

View workflow job for this annotation

GitHub Actions / Clippy

unreachable pattern

Check warning on line 254 in pallets/creditcoin/src/helpers.rs

View workflow job for this annotation

GitHub Actions / Extrinsics

unreachable pattern

Check warning on line 254 in pallets/creditcoin/src/helpers.rs

View workflow job for this annotation

GitHub Actions / build-creditcoin-node

unreachable pattern

Check warning on line 254 in pallets/creditcoin/src/helpers.rs

View workflow job for this annotation

GitHub Actions / test

unreachable pattern

Check warning on line 254 in pallets/creditcoin/src/helpers.rs

View workflow job for this annotation

GitHub Actions / benchmark

unreachable pattern

Check warning on line 254 in pallets/creditcoin/src/helpers.rs

View workflow job for this annotation

GitHub Actions / benchmark

unreachable pattern
}
}
21 changes: 13 additions & 8 deletions pallets/creditcoin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,9 @@ pub mod pallet {

/// ECDSA public key recovery failed for an ownership proof using PersonalSign
PersonalSignPublicKeyRecoveryFailed,

/// An unsupported blockchain was specified to register_address_v2
UnsupportedBlockchain,
}

#[pallet::genesis_config]
Expand Down Expand Up @@ -1378,10 +1381,19 @@ pub mod pallet {
ownership_proof: OwnershipProof,
) -> DispatchResult {
let who = ensure_signed(origin)?;

let encoded = who.encode();
let account = encoded.as_slice();

ensure!(
helpers::blockchain_is_supported(&blockchain),
Error::<T>::UnsupportedBlockchain
);

ensure!(
helpers::address_is_well_formed(&blockchain, &address),
Error::<T>::MalformedExternalAddress
);

match helpers::try_extract_address::<T>(ownership_proof, account, &blockchain, &address)
{
Ok(recreated_address) => {
Expand All @@ -1399,13 +1411,6 @@ pub mod pallet {
fail!(Error::<T>::AddressAlreadyRegistered);
}

// note: this error condition is unreachable!
// AddressFormatNotSupported or OwnershipNotSatisfied will error out first
ensure!(
helpers::address_is_well_formed(&blockchain, &address),
Error::<T>::MalformedExternalAddress
);

let entry = Address { blockchain, value: address, owner: who };
Self::deposit_event(Event::<T>::AddressRegistered(
address_id.clone(),
Expand Down
24 changes: 20 additions & 4 deletions pallets/creditcoin/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3026,7 +3026,7 @@ fn register_address_v2_should_fail_to_reregister_external_address_to_same_accoun
Origin::signed(who),
blockchain,
external_address,
proof.clone(),
proof,
),
crate::Error::<Test>::AddressAlreadyRegisteredByCaller
);
Expand Down Expand Up @@ -3126,7 +3126,7 @@ fn try_extract_address_should_error_when_signature_is_invalid() {
let blockchain = Blockchain::Rinkeby;
assert_noop!(
Creditcoin::register_address_v2(Origin::signed(who), blockchain, address, bad_proof),
crate::Error::<Test>::EthSignPublicKeyRecoveryFailed
crate::Error::<Test>::InvalidSignature
);
})
}
Expand All @@ -3153,7 +3153,7 @@ fn register_address_v2_should_fail_after_v1_registration_with_same_addres() {
Origin::signed(who),
blockchain,
external_address,
proof.clone(),
proof,
),
crate::Error::<Test>::AddressAlreadyRegisteredByCaller
);
Expand Down Expand Up @@ -3181,7 +3181,7 @@ fn register_address_v1_should_fail_after_v2_registration_with_same_addres() {
Origin::signed(who),
blockchain,
external_address,
ownership_proof.clone(),
ownership_proof,
),
crate::Error::<Test>::AddressAlreadyRegisteredByCaller
);
Expand Down Expand Up @@ -3220,3 +3220,19 @@ fn register_address_v2_should_work_personal_sign() {
);
});
}

#[test]
fn register_address_v2_should_error_with_unsupported_blockchain() {
ExtBuilder::default().build_and_execute(|| {
System::set_block_number(1);

let (who, address, ownership_proof, _) = generate_address_with_proof("owner");
let blockchain = Blockchain::Bitcoin;
let proof = OwnershipProof::EthSign(ownership_proof);

assert_noop!(
Creditcoin::register_address_v2(Origin::signed(who), blockchain, address, proof,),
crate::Error::<Test>::UnsupportedBlockchain
);
});
}

0 comments on commit 6d8094c

Please sign in to comment.