I want the user to input the address to:T::AccountIdAccountId
, and it will compare to the address that I set let disable_address = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY";
. My question is how do I compare those two different types? if (receiver != disable_address)
, thanks.
My code
use frame_support::{decl_storage, decl_module, dispatch::DispatchResult};
use frame_system::ensure_signed;
decl_storage! {
trait Store for Module<T: Trait> as VerifiableCreds {
storeValue: u32;
}
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
#[weight = 0]
fn verify(origin, to:T::AccountId) -> DispatchResult
{
let sender = ensure_signed(origin)?;
let receiver = to;
let disable_address = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY";
if (receiver != disable_address)
{
//do something
}
Ok(())
}
}
}
What you have provided is an SS58 representation of an address, which is not a good starting point for doing comparison.
You should instead convert the SS58 into its byte/hex representation:
Using: https://www.shawntabrizi.com/substrate-js-utilities/
5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY
> 0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d
Then you want to compare the encoded version of the AccountId
and see if it matches the byte form of the hex above:
let account_bytes: Vec<u8> = to.encode();
let match_bytes: Vec<u8> = hex_literal::hex!["d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"].into();
if account_bytes == match_bytes { ... }
Something like that... but in general, I would never recommend writing code this way. To start, you make an assumption about your Runtime that your accounts are of a certain format. Imagine another chain which uses a different account format, like the Ethereum 20 byte representation rather than a 32 byte representation. Any hardcoded logic like this would fail to work.
Instead, you should provide a configuration trait:
type DisableAddress: Get<Self::AccountId>;
Then you should do the match like so:
if to == T::DisableAddress::get() { ... }
Then you would do the same byte conversion logic I showed above within the runtime.
There is a good example of exactly this in this PR for the Purchase Pallet: https://github.com/paritytech/polkadot/pull/1369/files#diff-e5e76e02c0d16e79c70b024cbe3c6ea56f3249382a0f987ba203c34fcb40ed66R954