我正在从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 Node读取公钥,如下所示
// 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
来自基板锈蚀侧?