简体   繁体   English

如何导入substrate_primitives 以使用sr25519?

[英]How to import substrate_primitives in order to use sr25519?

I have the following dependencies in my Cargo.toml file:我的Cargo.toml文件中有以下依赖Cargo.toml

[package]
name = "api-client-tutorial"
version = "0.1.0"
authors = ["Supercomputing Systems AG <info@scs.ch>"]
edition = "2018"

[dependencies]
substrate-api-client = { git = "https://github.com/scs/substrate-api-client.git" }
codec = { package = "parity-scale-codec", features = ["derive"], version = "1.0.0", default-features = false }

[dependencies.primitives]
git = "https://github.com/paritytech/substrate"
rev = "3bf9540e72df5ecb3955845764dfee7dcdbb26b5"
package = "substrate-primitives"

[dependencies.keyring]
git = "https://github.com/paritytech/substrate"
rev = "3bf9540e72df5ecb3955845764dfee7dcdbb26b5"
package = "substrate-keyring"

I am unsure of the difference between dependencies section and dependencies.primitives section, but the package substrate-primitives is included in the primitives section.我不确定dependencies部分和dependencies.primitives部分之间的区别,但是包substrate-primitives包含在primitives 部分中。

I have seen that substrate_primitives has the module sr25519 I need to use, but when I try to import it in my code:已经看到substrate_primitives有我需要使用的模块sr25519 ,但是当我尝试在我的代码中导入它时:

use substrate_api_client::{Api, node_metadata};
use substrate_primitives::sr25519;
fn main() {
    // instantiate an Api that connects to the given address
    let url = "127.0.0.1:9944";
    // if no signer is set in the whole program, we need to give to Api a specific type instead of an associated type
    // as during compilation the type needs to be defined.
    let api = Api::<sr25519::Pair>::new(format!("ws://{}", url));

    let meta = api.get_metadata();
    println!("Metadata:\n {}", node_metadata::pretty_format(&meta).unwrap());
}

I get the following error:我收到以下错误:

unresolved import `substrate_primitives`

use of undeclared type or module `substrate_primitives`rustc(E0432)
main.rs(2, 5): use of undeclared type or module `substrate_primitives`

How do I import sr25519 so that I can use the following line in my code?如何导入 sr25519 以便我可以在我的代码中使用以下行?

let api = Api::<sr25519::Pair>::new(format!("ws://{}", url));

The difference between tables under [dependencies] and the [dependencies.primitives] table is that the table for the primitives dependency is not inlined. [dependencies]下的表和[dependencies.primitives]表之间的区别在于primitives依赖项的表没有内联。 You could also just inline the primitives dependency and put it under [dependencies] like that:您也可以内联primitives依赖项并将其放在[dependencies]下,如下所示:

primitives = { git = "https://github.com/paritytech/substrate", rev = "3bf9540e72df5ecb3955845764dfee7dcdbb26b5", package = "substrate-primitives" }

The toml documentation can give you more details on tables and inline tables. toml 文档可以为您提供有关表和内联表的更多详细信息。

Regarding your problem.关于你的问题。 You cannot import the crate like that, because it is renamed to primitives .你不能像那样导入板条箱,因为它被重命名为primitives The package field specifies the real name of the dependency and the table name defines the new name which is used to import it inside your project. package字段指定依赖项的真实名称,表名定义用于将其导入到项目中的新名称。 For details have a look at the cargo documentation .有关详细信息,请查看货物文件

Your import should therefore look like this: use primitives::sr25519;因此,您的导入应如下所示: use primitives::sr25519;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM