Rust k256 无法创建签名者

Rust k256 can't create signer

我有 k256 板条箱,我正在尝试对字符串进行签名。 这是我的代码:

let priv_key = SigningKey::from_bytes(self.private_key.as_bytes()).expect("Make priv key");
let tx_hash = transaction.get_hash();
let signer = Signer::new(&tx_hash);
let sig = signer.sign_digest_with_rng(&mut OsRng, tx_hash);

但是编译出现如下错误:

error[E0599]: the function or associated item `new` exists for trait object `dyn Signer<_>`, but its trait bounds were not satisfied
  --> src/wallet.rs:32:30
   |
32 |         let signer = Signer::new(&tx_hash);
   |                              ^^^ function or associated item cannot be called on `dyn Signer<_>` due to unsatisfied trait bounds
   |
  ::: /Users/oriont/.cargo/registry/src/github.com-1ecc6299db9ec823/signature-1.4.0/src/signer.rs:13:1
   |
13 | pub trait Signer<S: Signature> {
   | ------------------------------
   | |
   | doesn't satisfy `dyn Signer<_>: Default`
   | doesn't satisfy `dyn Signer<_>: HashMarker`
   | doesn't satisfy `dyn Signer<_>: Sized`
   | doesn't satisfy `dyn Signer<_>: sha2::Digest`
   | doesn't satisfy `dyn Signer<_>: sha2::digest::FixedOutput`
   | doesn't satisfy `dyn Signer<_>: sha2::digest::Update`
   |
   = note: the following trait bounds were not satisfied:
           `dyn Signer<_>: Sized`
           which is required by `dyn Signer<_>: sha2::Digest`
           `dyn Signer<_>: sha2::digest::FixedOutput`
           which is required by `dyn Signer<_>: sha2::Digest`
           `dyn Signer<_>: Default`
           which is required by `dyn Signer<_>: sha2::Digest`
           `dyn Signer<_>: sha2::digest::Update`
           which is required by `dyn Signer<_>: sha2::Digest`
           `dyn Signer<_>: HashMarker`
           which is required by `dyn Signer<_>: sha2::Digest`
           `&dyn Signer<_>: sha2::digest::FixedOutput`
           which is required by `&dyn Signer<_>: sha2::Digest`
           `&dyn Signer<_>: Default`
           which is required by `&dyn Signer<_>: sha2::Digest`
           `&dyn Signer<_>: sha2::digest::Update`
           which is required by `&dyn Signer<_>: sha2::Digest`
           `&dyn Signer<_>: HashMarker`
           which is required by `&dyn Signer<_>: sha2::Digest`
           `&mut dyn Signer<_>: sha2::digest::FixedOutput`
           which is required by `&mut dyn Signer<_>: sha2::Digest`
           `&mut dyn Signer<_>: Default`
           which is required by `&mut dyn Signer<_>: sha2::Digest`
           `&mut dyn Signer<_>: sha2::digest::Update`
           which is required by `&mut dyn Signer<_>: sha2::Digest`
           `&mut dyn Signer<_>: HashMarker`
           which is required by `&mut dyn Signer<_>: sha2::Digest`

For more information about this error, try `rustc --explain E0599`.

似乎 Signer 必须具有某种通用签名类型,但文档中的示例并未反映这一点。 https://docs.rs/k256/0.4.2/k256/ecdsa/index.html

我的 Cargo.toml 文件具有 ecdsaecdsa-core 特征:

[dependencies]
...
k256 = { version = "0.10.2", features = ["ecdsa", "ecdsa-core"] }

我该如何解决这个问题?

我解决了我的问题:我同时查看了 2 个不同版本的文档。 此页面显示最新版本:https://docs.rs/p256/latest/p256/ecdsa/index.html

我相应地更新了我的代码(并使用 hex 将字节转换为字符串`)

let priv_key_bytes = &hex::decode(&self.private_key).unwrap();
let priv_key = SigningKey::from_bytes(&priv_key_bytes).unwrap();
let tx_hash = transaction.get_hash();
let sig: Signature = priv_key.sign(tx_hash.as_bytes());
let sig_str = hex::encode(sig.to_vec());

现在可以了!