gpt4 book ai didi

rust - 如何从 Substrate 端从 Polkadot 地址生成公钥

转载 作者:行者123 更新时间:2023-12-05 02:35:53 26 4
gpt4 key购买 nike

我正在从 PolkadotJS 生成一个公钥,如下所示

    const keyring = new Keyring({ type: "sr25519" });
const account = keyring.addFromUri("//Bob", { name: "Bob default" });

// encoded public key
let public_key = keyring.encodeAddress(account.publicKey, 42);
console.log(public_key);

我正在添加 public_key 的类型作为"public_key": "Vec<u8>",

我正在从 Substrate 节点读取公钥,如下所示

// pk_raw is a Vec<u8> array 
let pk = str::from_utf8(pk_raw.as_ref()).unwrap()
// the above returns `5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty`

我需要从这个值生成公钥。我尝试了以下内容

ed25519::Public::try_from(&*pk_raw).unwrap(); 
// above throws error since the data length is not equals to 32

fn try_from(data: &[u8]) -> Result<Self, Self::Error> {
if data.len() == 32 {
let mut inner = [0u8; 32];
inner.copy_from_slice(data);
Ok(Public(inner))
} else {
Err(())
}
}

有没有办法使用 5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty 生成公钥?来自 Substrate Rust Side?

最佳答案

你可以这样使用:

use sp_core::crypto::Ss58Codec;

ed25519::Public::from_ss58check("5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty").expect("Valid address")

关于rust - 如何从 Substrate 端从 Polkadot 地址生成公钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70429492/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com