-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(contract invoke): support contract aliases when parsing address
fixes: 1764
- Loading branch information
1 parent
9b8013a
commit d391741
Showing
5 changed files
with
123 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use std::str::FromStr; | ||
|
||
use crate::xdr; | ||
|
||
use super::{address, locator, ContractAddress}; | ||
|
||
/// `ScAddress` can be either a resolved `xdr::ScAddress` or an alias of a `Contract` or `MuxedAccount`. | ||
#[derive(Clone, Debug)] | ||
pub enum ScAddress { | ||
Address(xdr::ScAddress), | ||
Alias(String), | ||
} | ||
|
||
impl Default for ScAddress { | ||
fn default() -> Self { | ||
ScAddress::Alias(String::default()) | ||
} | ||
} | ||
|
||
#[derive(thiserror::Error, Debug)] | ||
pub enum Error { | ||
#[error(transparent)] | ||
Locator(#[from] locator::Error), | ||
#[error(transparent)] | ||
Address(#[from] address::Error), | ||
#[error("Account alias not Found{0}")] | ||
AccountAliasNotFound(String), | ||
} | ||
|
||
impl FromStr for ScAddress { | ||
type Err = Error; | ||
|
||
fn from_str(value: &str) -> Result<Self, Self::Err> { | ||
Ok(xdr::ScAddress::from_str(value) | ||
.map_or_else(|_| ScAddress::Alias(value.to_string()), ScAddress::Address)) | ||
} | ||
} | ||
|
||
impl ScAddress { | ||
pub fn resolve( | ||
self, | ||
locator: &locator::Args, | ||
network_passphrase: &str, | ||
) -> Result<xdr::ScAddress, Error> { | ||
let alias = match self { | ||
ScAddress::Address(addr) => return Ok(addr), | ||
ScAddress::Alias(alias) => alias, | ||
}; | ||
let contract = ContractAddress::resolve_alias(&alias, locator, network_passphrase); | ||
let muxed_account = super::Address::resolve_muxed_account_with_alias(&alias, locator, None); | ||
match (contract, muxed_account) { | ||
(Ok(contract), _) => Ok(xdr::ScAddress::Contract(xdr::Hash(contract.0))), | ||
(_, Ok(muxed_account)) => Ok(xdr::ScAddress::Account(muxed_account.account_id())), | ||
_ => Err(Error::AccountAliasNotFound(alias)), | ||
} | ||
} | ||
} |