use miden_client::account::AccountId;
use miden_client::address::{Address, AddressId};
use miden_client::vm::typed::{TypedError, TypedProcInfo};
mod account_id;
mod asset;
pub use account_id::AccountIdCodec;
pub use asset::AssetCodec;
pub(crate) const ACCOUNT_ID_WIT_NAME: &str = "account-id";
pub(crate) fn parse_account_id_token(token: &str) -> Result<AccountId, TypedError> {
if token.starts_with("0x") {
return AccountId::from_hex(token)
.map_err(|err| invalid_scalar(ACCOUNT_ID_WIT_NAME, token, &err));
}
let (_, address) =
Address::decode(token).map_err(|err| invalid_scalar(ACCOUNT_ID_WIT_NAME, token, &err))?;
match address.id() {
AddressId::AccountId(id) => Ok(id),
_ => Err(invalid_scalar(
ACCOUNT_ID_WIT_NAME,
token,
"the address doesn't name an account ID",
)),
}
}
pub(crate) fn invalid_scalar(
wit_name: &str,
token: &str,
reason: &(impl ToString + ?Sized),
) -> TypedError {
TypedError::InvalidScalar {
wit_name: wit_name.to_string(),
token: token.to_string(),
reason: reason.to_string(),
}
}
pub fn with_cli_codecs(typed: TypedProcInfo) -> TypedProcInfo {
typed
.with_scalar_codec(Box::new(AccountIdCodec))
.with_scalar_codec(Box::new(AssetCodec))
}