如何使用 RPC 调用从 Substrate 读取哈希值并获取相应的 AccountId?

How to read a hash value and get the corresponding AccountId from Substrate using RPC call?

我有一个底层节点,运行 存储项目为:value(Hash): Option<AccountId>。我的目标是提供散列值(比如 0x0000000000000000000000000000000000000000000000000000000000000001 并在 return 中获取相应的帐户 ID)。

当我通过 UI 执行此操作时,我得到以下信息:

我想通过 RPC 调用执行相同的任务。在阅读 this 博客 post 之后,我意识到我的情况是阅读 StorageMaps,因此我开始 运行 一些查询。如果我没记错的话,模块是Substratekitties,存储项是value。映射将是 valueAccountId.

我运行前两个电话:

util_crypto.xxhashAsHex("Substratekitties", 128)
"0xe4a154b5ba85d6072b187ee66e4fef05"
util_crypto.xxhashAsHex("Value", 128)
"0x6b2f21989c43cc4e06ac1ad3e2027000"

但是我在第三个调用encoding: encoding the file sha256 hash 的时候一头雾水。怎么做? 运行 util_crypto.blake2AsHex("0000000000000000000000000000000000000000000000000000000000000001", 256) "0x16821de47d8b3b0fa4ca43e5db1028d75207cbd1c379a4738144972b105355aa" 不会工作,也不会工作。

我的意思是不工作,我在执行此查询时得到 "null" 值。 这是存储结构:

use frame_support::{decl_module, decl_storage, dispatch::result::Result, ensure, StorageMap};
use frame_system::ensure_signed;
use sp_runtime::DispatchError;

// pub trait Trait: balances::Trait {}
pub trait Trait: pallet_balances::Trait {}

decl_storage! {
    trait Store for Module<T: Trait> as KittyStorage {
        // Value: map T::Hash => Option<T::AccountId>;
        // TODO: check whether this is the appropriate datatype(hash).
        Value: map hasher(blake2_256) T::Hash => Option<T::AccountId>;
        // Balances: map hasher(blake2_256) (T::AssetId, T::AccountId) => T::Balance;
    }
}

decl_module! {
    pub struct Module<T: Trait> for enum Call where origin: T::Origin {
        fn set_value(origin, value: T::Hash) -> Result<(), DispatchError> {
            let sender = ensure_signed(origin)?;
            ensure!(!<Value<T>>::contains_key(value), "key already exists");
            <Value<T>>::insert(value, sender);
            Ok(())
        }
    }
}

更新: 我的文字查询:

curl -H "Content-Type: application/json" -d '{"id":1, "jsonrpc":"2.0", "method": "state_getStorage", "params": ["0x3fd011a1ea758d2e1b46ed3cec43fc86b2f21989c43cc4e06ac1ad3e2027000d3585436436a2253c5163fa0cfe54a648fa533ef32ea10dbd966ac438af77b71"]}' http://localhost:9933/

十六进制查询:

util_crypto.xxhashAsHex("KittyStorage", 128)
"0xe3fd011a1ea758d2e1b46ed3cec43fc8"
util_crypto.xxhashAsHex("Value", 128)
"0x6b2f21989c43cc4e06ac1ad3e2027000"
util_crypto.blake2AsHex("0x0000000000000000000000000000000000000000000000000000000000001234")

"0xd3585436436a2253c5163fa0cfe54a648fa533ef32ea10dbd966ac438af77b71"

我认为这里的问题是您正在获取字符串“0000...0001”的哈希值而不是字节。尝试将 0x 添加到字符串的前面。

编辑:像这样尝试:

util_crypto.blake2AsHex("0x0000000000000000000000000000000000000000000000000000000000000001")

> "0x33e423980c9b37d048bd5fadbd4a2aeb95146922045405accc2f468d0ef96988"

编辑 2:这里的另一个问题是您使用了错误的密钥。

您的示例显示您正在使用存储密钥 "Substratekitties",但您的存储密钥是 KittyStorage,如 decl_storage! 宏中指定的那样。

所以你的第一个散列应该是:0xe3fd011a1ea758d2e1b46ed3cec43fc8