soroban_cli/config/
alias.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use std::{collections::HashMap, convert::Infallible, str::FromStr};

use serde::{Deserialize, Serialize};

use super::locator;

#[derive(Serialize, Deserialize, Default)]
pub struct Data {
    pub ids: HashMap<String, String>,
}

/// Address can be either a contract address, C.. or eventually an alias of a contract address.
#[derive(Clone, Debug)]
pub enum ContractAddress {
    ContractId(stellar_strkey::Contract),
    Alias(String),
}

impl Default for ContractAddress {
    fn default() -> Self {
        ContractAddress::Alias(String::default())
    }
}

impl FromStr for ContractAddress {
    type Err = Infallible;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        Ok(stellar_strkey::Contract::from_str(value).map_or_else(
            |_| ContractAddress::Alias(value.to_string()),
            ContractAddress::ContractId,
        ))
    }
}

impl ContractAddress {
    pub fn resolve_contract_id(
        &self,
        locator: &locator::Args,
        network_passphrase: &str,
    ) -> Result<stellar_strkey::Contract, locator::Error> {
        match self {
            ContractAddress::ContractId(muxed_account) => Ok(*muxed_account),
            ContractAddress::Alias(alias) => locator
                .get_contract_id(alias, network_passphrase)?
                .ok_or_else(|| locator::Error::ContractNotFound(alias.to_owned())),
        }
    }
}