delta_domain_sdk/domain_agreement.rs
1use crate::{
2 base_layer::BaseLayer,
3 config::Config,
4};
5use base_sdk::{
6 core::Planck,
7 crypto::ed25519,
8 sdl::VerificationKey,
9};
10use snafu::{
11 ResultExt,
12 Snafu,
13};
14
15/// Status of the domain agreement on this shard.
16#[derive(Snafu, Debug)]
17pub enum Error {
18 /// There is a domain agreement but for a different operator
19 #[snafu(display("Active domain agreement is for key {current_operator} != the configured one! Transactions will be rejected by the base layer."))]
20 WrongOperator {
21 /// Operator of the existing domain agreement
22 current_operator: ed25519::PubKey,
23 },
24 /// There is no existing domain agreement
25 #[snafu(display("No existing domain agreement found on this shard! Transactions will be rejected by the base layer."))]
26 NotFound,
27 /// RPC request to the base layer failed
28 #[snafu(display("RPC request failed: {source}"))]
29 Rpc {
30 /// Generic, underlying error of the RPC client
31 source: Box<dyn std::error::Error + Send + Sync>,
32 },
33}
34
35/// Check that there is an active Domain Agreement for the given config.
36///
37/// This function can be used as a safe-guard before starting a domain or
38/// submitting transactions. The base layer only accepts transactions from
39/// domains with a valid domain agreement.
40///
41/// If you already have a running domain, you can call
42/// [DomainClient::check_valid_domain_agreement](crate::DomainClient::check_valid_domain_agreement)
43/// directly. See [Config] for how to load configuration.
44pub async fn check_valid_domain_agreement(config: Config) -> Result<(), Error> {
45 let shard = config.shard;
46 BaseLayer::from_config(config)
47 .await
48 .boxed()
49 .context(RpcSnafu)?
50 .check_valid_domain_agreement(shard)
51 .await
52}
53
54/// Submit a Domain Agreement for the given config.
55///
56/// Having an active domain agreement is a prerequisite to submit transactions
57/// to the base layer. You can check whether a domain agreement is already
58/// active with [check_valid_domain_agreement].
59///
60/// Note that after submitting the domain agreement, it still has to be applied
61/// on the base layer, before the domain is allowed to send transactions.
62///
63/// ## Parameters
64///
65/// - `config` - configuration data of the domain. See [Config] for how to load
66/// configuration.
67/// - `new_shard_fee` - amount of native token that will be charged to the
68/// domain operator keys's base layer vault for creation of the new shard
69/// - `local_laws_vkey` - verification key of the enforced local laws of the
70/// domain
71///
72/// If you already have a running domain, you can call
73/// [DomainClient::submit_domain_agreement](crate::DomainClient::submit_domain_agreement)
74/// directly.
75pub async fn submit_domain_agreement(
76 config: Config,
77 new_shard_fee: Planck,
78 local_laws_vkey: Option<VerificationKey>,
79) -> Result<(), Error> {
80 let shard = config.shard;
81 BaseLayer::from_config(config)
82 .await
83 .boxed()
84 .context(RpcSnafu)?
85 .submit_domain_agreement(shard, new_shard_fee, local_laws_vkey)
86 .await
87 .boxed()
88 .context(RpcSnafu)
89}