Substrate 中 AccountId32 和 AccountId 之间的转换

Converting between AccountId32 and AccountId in Substrate

我有一个要求,我在一些字节中传递,我需要在 Substrate 2.0 中从它创建一个 accountId。

有没有办法在运行时 (FRAME) 中在 AccountId32<T as frame_system::Trait>::AccountId 之间进行转换?或者从字节创建 <T as frame_system::Trait>::AccountId 的实例?

谢谢

给定原始字节,您可以像这样尝试构造一个帐户 ID:

T::AccountId::decode(&mut &bytes[..]).unwrap_or_default();

您可能希望以不同于返回默认 AccountId 的方式处理错误情况。

如果您可以验证您的原始字节长度为 32 ([u8; 32]),此操作应该永远不会失败,因此您可以放置​​一个 .expect("32 bytes can always construct an AccountId32").