rust - 如何从 secp256k1 中提取未压缩的公钥?
问题描述
编辑: 问题经过大量编辑以缩小问题的焦点
我正在尝试从结构中提取未压缩的公钥secp256k1::key::PublicKey
。
根据我使用的打印格式,我可以获得压缩或未压缩的密钥。
正如这个问题所观察到的,未压缩的密钥是十六进制(或字节)反转的(您可以看到压缩版本的反转是未压缩版本的开始)。
我不知道如何从结构中提取 64 字节(反转)键来操作它。如果我尝试.serialize()
在公钥上使用该方法,它会返回压缩密钥的 33 字节切片,而不是公钥。
有谁知道如何从结构中提取和操作 64 字节密钥?
下面的代码片段:
//Calculate BIP32 Extended Public Key and Public Key
let ex_pub_key = ExtendedPubKey::from_private(&secp, &ex_priv_key);
println!("Compressed Public Key: {}", ex_pub_key.public_key.key);
//returns the 33-byte:
//03a593ae00ed6b402c801d2a2edca9f5c057f2be7327e3d81c5747df058e169cd5
println!("Uncompressed Public Key: {:?}", ex_pub_key.public_key.key);
//returns the 64-byte struct:
//PublicKey(d59c168e05df47571cd8e32773bef257c0f5a9dc2e2a1d802c406bed00ae93a57d95dc0fa554e33bda9c7cd87e4b8e8b788b759c0bb1160bb803240468d0e559)
//Serializing the key returns the compressed version of the key
let serialized_key = public_key.serialize();
println!("Serialized Key: {:?}", serialized_key);
//returns the 33-byte slice:
//[3, 165, 147, 174, 0, 237, 107, 64, 44, 128, 29, 42, 46, 220, 169, 245, 192, 87, 242, 190, 115, 39, 227, 216, 28, 87, 71, 223, 5, 142, 22, 156, 213]
解决方案
经过大量研究,我发现有一种简单的方法可以通过使用公钥serialize_uncompressed()
上的方法来提取未压缩的公钥,公钥是同一个secp256k1
crate 的一部分。
这提供了一个 65 字节的输出,其中第一个字节应该被删除以创建正确的 64 字节公钥。
推荐阅读
- ionic-framework - 视差图像不适用于 ionic 4 的 ion-slider
- rust - 如何禁用“不必要的路径消歧器”警告?
- html - JQueryUI 对话框模式功能不起作用
- tabulator - 更改制表符中只读单元格的样式
- mysql - mySQL - 需要帮助创建以不同方式引用多个表的查询
- .net - 如何在远程服务器上构建 .net 应用程序?
- rust - 如何声明一个可以将稀疏向量的引用添加到一起的通用函数?
- php - 如何在父数组中对相同的值进行分组?
- sql - 如何维护每个公共列值的事务完整性以插入两个表?
- android - App Bundle - 动态功能模块:在动态功能模块错误中找不到基础项目