You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Rust Tonic连接Polar上LND节点遇CaUsedAsEndEntity错误求助

解决连接Polar LND节点的TLS证书错误

错误原因

你遇到的InvalidCertificate(Other(CaUsedAsEndEntity))错误,是因为LND的tls.cert是自签名证书,它同时具备CA证书和终端实体证书的属性。你之前的代码将其当作CA证书传入ca_certificate方法,但TLS验证逻辑会检查服务器证书的类型——服务器用CA证书作为终端实体时,会触发这个无效证书错误。

解决方案

修改TLS配置,将LND的tls.cert添加到客户端的信任根证书存储中,而不是当作CA证书使用:

#[tokio::main]
async fn main() {
    // build our application with a single route
    let app = Router::new().route("/", get(|| async { "Hello, World!" }));

    // 加载LND的自签名证书
    let cert = rustls::Certificate(include_bytes!("<route-to>/lnd/alice/tls.cert").to_vec());
    
    // 创建根证书存储并添加该证书
    let mut root_store = rustls::RootCertStore::empty();
    root_store.add(&cert).unwrap();

    // 构建正确的TLS配置
    let tls_conf = tonic::transport::ClientTlsConfig::new()
        .domain_name("localhost")
        .root_certificates(root_store);

    // 创建连接通道
    let channel = tonic::transport::Channel::from_static("https://127.0.0.1:10001")
        .tls_config(tls_conf)
        .unwrap()
        .connect()
        .await
        .unwrap();

    // run it with hyper on localhost:3000
    axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
        .serve(app.into_make_service())
        .await.unwrap();
}

关键说明

  • LND的tls.cert是自签名的,客户端需要明确信任这个证书才能通过TLS验证。
  • root_certificates方法会将证书添加到客户端的信任根,这样服务器的自签名证书会被视为合法的终端实体证书。

内容的提问来源于stack exchange,提问作者lescuer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 13:22:25