Skip to main content

poi_rs/
client.rs

1// Copyright 2020-2026 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4#[cfg(feature = "native-grpc")]
5use iota_grpc_client::Client as GrpcClient;
6
7use crate::{CommitteeResolution, CommitteeResolver, ProofBuilder, Source};
8
9/// Convenient entry point for proof construction and verification backed by one ledger source.
10#[derive(Clone, Debug)]
11pub struct PoiClient<S> {
12    source: S,
13}
14
15impl<S> PoiClient<S> {
16    /// Creates a client backed by `source`.
17    pub const fn new(source: S) -> Self {
18        Self { source }
19    }
20}
21
22impl<S> PoiClient<S>
23where
24    S: Source + Clone,
25{
26    /// Creates a fresh builder for one Proof of Inclusion.
27    pub fn proof(&self) -> ProofBuilder<S> {
28        ProofBuilder::new(self.source.clone())
29    }
30
31    /// Creates a verifier using the selected committee-resolution strategy.
32    ///
33    /// Retain the returned resolver when verifying multiple proofs so anchored
34    /// resolutions can reuse their authenticated committee cache.
35    pub fn verifier(&self, resolution: CommitteeResolution) -> CommitteeResolver<S> {
36        CommitteeResolver::new(self.source.clone(), resolution)
37    }
38}
39
40#[cfg(feature = "native-grpc")]
41impl PoiClient<GrpcClient> {
42    /// Creates a client connected to the public IOTA mainnet gRPC endpoint.
43    pub fn mainnet() -> iota_grpc_client::Result<Self> {
44        GrpcClient::new_mainnet().map(Self::new)
45    }
46
47    /// Creates a client connected to the public IOTA testnet gRPC endpoint.
48    pub fn testnet() -> iota_grpc_client::Result<Self> {
49        GrpcClient::new_testnet().map(Self::new)
50    }
51
52    /// Creates a client connected to the public IOTA devnet gRPC endpoint.
53    pub fn devnet() -> iota_grpc_client::Result<Self> {
54        GrpcClient::new_devnet().map(Self::new)
55    }
56
57    /// Creates a client backed by an existing SDK gRPC client.
58    pub const fn from_grpc_client(client: GrpcClient) -> Self {
59        Self::new(client)
60    }
61
62    /// Returns the underlying SDK gRPC client.
63    pub const fn grpc_client(&self) -> &GrpcClient {
64        &self.source
65    }
66}