[英]How do I compare the trait type with the string type in substrate module?
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";
我希望用户将地址输入
to:T::AccountIdAccountId
,它将与我设置的地址进行比较let disable_address = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY";
. . My question is how do I compare those two different types?
我的问题是如何比较这两种不同的类型?
if (receiver != disable_address)
, thanks. if (receiver != disable_address)
,谢谢。
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.您提供的是地址的 SS58 表示,这不是进行比较的好起点。
You should instead convert the SS58 into its byte/hex representation:您应该将 SS58 转换为其字节/十六进制表示形式:
Using: https://www.shawntabrizi.com/substrate-js-utilities/使用: 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:然后你想比较
AccountId
的编码版本,看看它是否与上面的十六进制的字节形式匹配:
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.
想象另一个使用不同帐户格式的链,例如以太坊 20 字节表示而不是 32 字节表示。 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在采购托盘的这个 PR 中有一个很好的例子: https://github.com/paritytech/polkadot/pull/1369/files#diff-e5e76e02c0d16e79c70b024cbe3c6ea56f3249382a0f987ba2R3c43
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.