【问题标题】:No method named `generate_keypair` found for type `secp256k1::Secp256k1` in the current scope在当前范围内没有为类型 `secp256k1::Secp256k1` 找到名为 `generate_keypair` 的方法
【发布时间】:2018-07-08 19:16:38
【问题描述】:

我正在尝试在 Rust 中使用 secp256k1 库。我有一个简单的测试程序无法编译,因为它找不到generate_keypair

extern crate secp256k1;
extern crate rand;

use secp256k1::{Secp256k1, ContextFlag};
use rand::{thread_rng};

fn main() {
    let full = Secp256k1::with_caps(ContextFlag::Full);
    let (sk, pk) = full.generate_keypair(&mut thread_rng()).unwrap();
}

编译失败,报错:

error[E0599]: no method named `generate_keypair` found for type `secp256k1::Secp256k1` in the current scope
 --> src/main.rs:9:25
  |
9 |     let (sk, pk) = full.generate_keypair(&mut thread_rng()).unwrap();
  |                         ^^^^^^^^^^^^^^^^

据我所知,我使用的库类似于 how its used in the library's tests

我已将 rand 回滚到 0.3,将 secp256k1 回滚到 0.6,现在它可以工作了。我对为什么现在被打破的任何想法感兴趣。

【问题讨论】:

    标签: rust


    【解决方案1】:

    docs.rs 上的documentation for secp256k1 version 0.8.1 没有列出任何方法generate_keypair

    如果你look at the source,你会看到:

    /// Generates a random keypair. Convenience function for `key::SecretKey::new`
    /// and `key::PublicKey::from_secret_key`; call those functions directly for
    /// batch key generation. Requires a signing-capable context.
    #[inline]
    #[cfg(any(test, feature = "rand"))]
    pub fn generate_keypair<R: Rng>(&self, rng: &mut R)
    

    generate_keypair 功能仅在您启用可选的rand 依赖项时可用。这是在commit 29892960 中介绍的。不幸的是,这个 crate 的维护者并没有将版本标签发布到 git 存储库,所以很难判断这个变化发生在哪个版本。

    【讨论】:

    • 哇,是的,我的依赖文件更新为:secp256k1 = {version = "0.8", features = ["rand"]} 谢谢!
    猜你喜欢
    • 2019-11-16
    • 2017-03-22
    • 2023-01-15
    • 2017-03-16
    • 2021-05-13
    • 1970-01-01
    • 2021-02-28
    • 2021-02-19
    • 1970-01-01
    相关资源
    最近更新 更多