-1

I am trying to generate a keypair using secp256k1 library.

In the official documentation I found this code:

use rand::OsRng;
use secp256k1::{Secp256k1, Message};

let secp = Secp256k1::new();
let mut rng = OsRng::new().expect("OsRng");
let (secret_key, public_key) = secp.generate_keypair(&mut rng);

And I put this in my Cargo.toml:

[dependencies]
rand = "0.6.1"

[dependencies.secp256k1]
features = ["rand"]
version = "0.12.0"

However, I get this compile error:

| secp.generate_keypair(&mut rng);
|      ^^^^^^^^^^^^^^^^ the trait `secp256k1::rand::Rng` is not implemented for 
                        `std::result::Result<rand::rngs::OsRng, rand::Error>`

I am very new to Rust and I am trying to understand it but I find it extremely difficult. Please explain what is my mistake. Thanks!

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
Octav
  • 5
  • 2
  • 1
    I have copied your code and get a different error: _"no method named `generate_keypair` found for type `secp256k1::Secp256k1` in the current scope"_. Please provide a [mcve], including crate versions, so that the same error can be obtained. – Peter Hall Dec 10 '18 at 09:40

1 Answers1

2

This appears to be a crate version mismatch. The latest version of the rand crate is 0.6.1, but secp256k1 0.12.0 depends on a much older version - 0.4.3.

A quick fix is to use an older version of rand:

[dependencies]
rand = "0.4.3"

And consider asking the authors of secp256k1 to update their dependencies.

The way that I found this was to search in the Cargo.lock file, which contains all of the actual versions of dependencies used by your application.

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
  • *search in the Cargo.lock file* — the duplicate describes a more automated way to find this error. – Shepmaster Dec 10 '18 at 13:45
  • *to update their dependencies* — I'd ask them to increase the version range, specifically. Presumably their code can support versions 0.4 through 0.6 of rand. – Shepmaster Dec 10 '18 at 13:49