Skip to content

Commit

Permalink
feat: add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Nov 6, 2024
1 parent 3c22797 commit 403d524
Show file tree
Hide file tree
Showing 27 changed files with 1,201 additions and 383 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
/.vscode
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export RUSTFLAGS=-Dwarnings -Dclippy::all -Dclippy::pedantic

CARGO_HACK_ARGS=--feature-powerset --exclude-features default --group-features base64,serde,arbitrary,hex
CARGO_HACK_ARGS=--feature-powerset --exclude-features default --group-features base64,serde,arbitrary,hex,sha2

CARGO_DOC_ARGS?=--open

Expand Down
218 changes: 111 additions & 107 deletions src/curr/account_conversions.rs
Original file line number Diff line number Diff line change
@@ -1,118 +1,145 @@
use super::{
AccountId, Hash, MuxedAccount, MuxedAccountMed25519, PublicKey, ScAddress, ScVal, Uint256,
AccountId, Error, Hash, MuxedAccount, MuxedAccountMed25519, PublicKey, ScAddress, ScVal,
Uint256,
};

impl From<stellar_strkey::ed25519::PublicKey> for PublicKey {
fn from(k: stellar_strkey::ed25519::PublicKey) -> Self {
PublicKey::PublicKeyTypeEd25519(k.0.into())
#[cfg(feature = "alloc")]
mod strkey {
use super::*;
impl From<stellar_strkey::ed25519::PublicKey> for PublicKey {
fn from(k: stellar_strkey::ed25519::PublicKey) -> Self {
PublicKey::PublicKeyTypeEd25519(k.0.into())
}
}
}
impl From<&stellar_strkey::ed25519::PublicKey> for PublicKey {
fn from(k: &stellar_strkey::ed25519::PublicKey) -> Self {
PublicKey::PublicKeyTypeEd25519(k.0.into())
impl From<&stellar_strkey::ed25519::PublicKey> for PublicKey {
fn from(k: &stellar_strkey::ed25519::PublicKey) -> Self {
PublicKey::PublicKeyTypeEd25519(k.0.into())
}
}
}

impl From<stellar_strkey::ed25519::PublicKey> for AccountId {
fn from(k: stellar_strkey::ed25519::PublicKey) -> Self {
AccountId(k.into())
impl From<stellar_strkey::ed25519::PublicKey> for AccountId {
fn from(k: stellar_strkey::ed25519::PublicKey) -> Self {
AccountId(k.into())
}
}
}
impl From<&stellar_strkey::ed25519::PublicKey> for AccountId {
fn from(k: &stellar_strkey::ed25519::PublicKey) -> Self {
AccountId(k.into())
impl From<&stellar_strkey::ed25519::PublicKey> for AccountId {
fn from(k: &stellar_strkey::ed25519::PublicKey) -> Self {
AccountId(k.into())
}
}
}

impl From<stellar_strkey::ed25519::PublicKey> for ScAddress {
fn from(t: stellar_strkey::ed25519::PublicKey) -> Self {
ScAddress::Account(t.into())
impl From<stellar_strkey::ed25519::PublicKey> for ScAddress {
fn from(t: stellar_strkey::ed25519::PublicKey) -> Self {
ScAddress::Account(t.into())
}
}
}
impl From<&stellar_strkey::ed25519::PublicKey> for ScAddress {
fn from(t: &stellar_strkey::ed25519::PublicKey) -> Self {
ScAddress::Account(t.into())
impl From<&stellar_strkey::ed25519::PublicKey> for ScAddress {
fn from(t: &stellar_strkey::ed25519::PublicKey) -> Self {
ScAddress::Account(t.into())
}
}
}

impl From<stellar_strkey::ed25519::PublicKey> for ScVal {
fn from(k: stellar_strkey::ed25519::PublicKey) -> Self {
ScVal::Address(k.into())
impl From<stellar_strkey::ed25519::PublicKey> for ScVal {
fn from(k: stellar_strkey::ed25519::PublicKey) -> Self {
ScVal::Address(k.into())
}
}
}
impl From<&stellar_strkey::ed25519::PublicKey> for ScVal {
fn from(k: &stellar_strkey::ed25519::PublicKey) -> Self {
ScVal::Address(k.into())
impl From<&stellar_strkey::ed25519::PublicKey> for ScVal {
fn from(k: &stellar_strkey::ed25519::PublicKey) -> Self {
ScVal::Address(k.into())
}
}
}

impl From<stellar_strkey::ed25519::MuxedAccount> for PublicKey {
fn from(k: stellar_strkey::ed25519::MuxedAccount) -> Self {
PublicKey::PublicKeyTypeEd25519(k.ed25519.into())
impl From<stellar_strkey::ed25519::MuxedAccount> for PublicKey {
fn from(k: stellar_strkey::ed25519::MuxedAccount) -> Self {
PublicKey::PublicKeyTypeEd25519(k.ed25519.into())
}
}
}
impl From<&stellar_strkey::ed25519::MuxedAccount> for PublicKey {
fn from(
stellar_strkey::ed25519::MuxedAccount{ ed25519, .. }: &stellar_strkey::ed25519::MuxedAccount,
) -> Self {
PublicKey::PublicKeyTypeEd25519(ed25519.into())
impl From<&stellar_strkey::ed25519::MuxedAccount> for PublicKey {
fn from(
stellar_strkey::ed25519::MuxedAccount{ ed25519, .. }: &stellar_strkey::ed25519::MuxedAccount,
) -> Self {
PublicKey::PublicKeyTypeEd25519(ed25519.into())
}
}
}

impl From<stellar_strkey::ed25519::MuxedAccount> for AccountId {
fn from(k: stellar_strkey::ed25519::MuxedAccount) -> Self {
AccountId(k.into())
impl From<stellar_strkey::ed25519::MuxedAccount> for AccountId {
fn from(k: stellar_strkey::ed25519::MuxedAccount) -> Self {
AccountId(k.into())
}
}
}
impl From<&stellar_strkey::ed25519::MuxedAccount> for AccountId {
fn from(k: &stellar_strkey::ed25519::MuxedAccount) -> Self {
AccountId(k.into())
impl From<&stellar_strkey::ed25519::MuxedAccount> for AccountId {
fn from(k: &stellar_strkey::ed25519::MuxedAccount) -> Self {
AccountId(k.into())
}
}
}

impl From<stellar_strkey::ed25519::MuxedAccount> for ScAddress {
fn from(t: stellar_strkey::ed25519::MuxedAccount) -> Self {
ScAddress::Account(t.into())
impl From<stellar_strkey::ed25519::MuxedAccount> for ScAddress {
fn from(t: stellar_strkey::ed25519::MuxedAccount) -> Self {
ScAddress::Account(t.into())
}
}
}
impl From<&stellar_strkey::ed25519::MuxedAccount> for ScAddress {
fn from(t: &stellar_strkey::ed25519::MuxedAccount) -> Self {
ScAddress::Account(t.into())
impl From<&stellar_strkey::ed25519::MuxedAccount> for ScAddress {
fn from(t: &stellar_strkey::ed25519::MuxedAccount) -> Self {
ScAddress::Account(t.into())
}
}
}

impl From<stellar_strkey::ed25519::MuxedAccount> for ScVal {
fn from(k: stellar_strkey::ed25519::MuxedAccount) -> Self {
ScVal::Address(k.into())
impl From<stellar_strkey::ed25519::MuxedAccount> for ScVal {
fn from(k: stellar_strkey::ed25519::MuxedAccount) -> Self {
ScVal::Address(k.into())
}
}
}
impl From<&stellar_strkey::ed25519::MuxedAccount> for ScVal {
fn from(k: &stellar_strkey::ed25519::MuxedAccount) -> Self {
ScVal::Address(k.into())
impl From<&stellar_strkey::ed25519::MuxedAccount> for ScVal {
fn from(k: &stellar_strkey::ed25519::MuxedAccount) -> Self {
ScVal::Address(k.into())
}
}
}

impl TryFrom<&stellar_strkey::Strkey> for ScAddress {
type Error = super::Error;
fn try_from(strkey: &stellar_strkey::Strkey) -> Result<Self, Self::Error> {
match strkey {
stellar_strkey::Strkey::PublicKeyEd25519(k) => Ok(ScAddress::Account(k.into())),
stellar_strkey::Strkey::MuxedAccountEd25519(m) => Ok(ScAddress::Account(m.into())),
stellar_strkey::Strkey::Contract(k) => Ok(ScAddress::Contract(k.into())),
_ => Err(super::Error::Invalid),
impl TryFrom<&stellar_strkey::Strkey> for ScAddress {
type Error = super::Error;
fn try_from(strkey: &stellar_strkey::Strkey) -> Result<Self, Self::Error> {
match strkey {
stellar_strkey::Strkey::PublicKeyEd25519(k) => Ok(ScAddress::Account(k.into())),
stellar_strkey::Strkey::MuxedAccountEd25519(m) => Ok(ScAddress::Account(m.into())),
stellar_strkey::Strkey::Contract(k) => Ok(ScAddress::Contract(k.into())),
_ => Err(super::Error::Invalid),
}
}
}
}

impl From<PublicKey> for stellar_strkey::ed25519::PublicKey {
fn from(k: PublicKey) -> Self {
(&k).into()
impl From<PublicKey> for stellar_strkey::ed25519::PublicKey {
fn from(k: PublicKey) -> Self {
(&k).into()
}
}
}
impl From<&PublicKey> for stellar_strkey::ed25519::PublicKey {
fn from(k: &PublicKey) -> Self {
match k {
PublicKey::PublicKeyTypeEd25519(k) => stellar_strkey::ed25519::PublicKey(k.into()),
impl From<&PublicKey> for stellar_strkey::ed25519::PublicKey {
fn from(k: &PublicKey) -> Self {
match k {
PublicKey::PublicKeyTypeEd25519(k) => stellar_strkey::ed25519::PublicKey(k.into()),
}
}
}
impl From<ScAddress> for stellar_strkey::Strkey {
fn from(sc_address: ScAddress) -> Self {
match sc_address {
ScAddress::Account(account_id) => {
stellar_strkey::Strkey::PublicKeyEd25519(account_id.into())
}
ScAddress::Contract(contract) => stellar_strkey::Strkey::Contract(contract.into()),
}
}
}
impl From<&ScAddress> for stellar_strkey::Strkey {
fn from(sc_address: &ScAddress) -> Self {
match sc_address {
ScAddress::Account(AccountId(PublicKey::PublicKeyTypeEd25519(Uint256(k)))) => {
stellar_strkey::Strkey::PublicKeyEd25519(stellar_strkey::ed25519::PublicKey(*k))
}
ScAddress::Contract(Hash(h)) => {
stellar_strkey::Strkey::Contract(stellar_strkey::Contract(*h))
}
}
}
}
}
Expand Down Expand Up @@ -253,29 +280,6 @@ impl From<&MuxedAccount> for MuxedAccount {
}
}

impl From<ScAddress> for stellar_strkey::Strkey {
fn from(sc_address: ScAddress) -> Self {
match sc_address {
ScAddress::Account(account_id) => {
stellar_strkey::Strkey::PublicKeyEd25519(account_id.into())
}
ScAddress::Contract(contract) => stellar_strkey::Strkey::Contract(contract.into()),
}
}
}
impl From<&ScAddress> for stellar_strkey::Strkey {
fn from(sc_address: &ScAddress) -> Self {
match sc_address {
ScAddress::Account(AccountId(PublicKey::PublicKeyTypeEd25519(Uint256(k)))) => {
stellar_strkey::Strkey::PublicKeyEd25519(stellar_strkey::ed25519::PublicKey(*k))
}
ScAddress::Contract(Hash(h)) => {
stellar_strkey::Strkey::Contract(stellar_strkey::Contract(*h))
}
}
}
}

impl From<&ScAddress> for ScAddress {
fn from(sc_address: &ScAddress) -> Self {
match sc_address {
Expand Down
17 changes: 4 additions & 13 deletions src/curr/asset_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,14 @@ use super::{
AlphaNum12, AlphaNum4, Asset, AssetCode12, AssetCode4, ChangeTrustAsset, ContractIdPreimage,
};
#[cfg(feature = "sha2")]
use super::{Hash, HashIdPreimage, HashIdPreimageContractId, HashBytes};

pub trait IntoContractId {
type Error;
fn into_contract_id(
self,
network_passphrase: &str,
) -> Result<stellar_strkey::Contract, Self::Error>;
}
use super::{Hash, HashIdPreimage, HashIdPreimageContractId};

#[cfg(feature = "sha2")]
impl IntoContractId for Asset {
type Error = super::Error;
fn into_contract_id(
impl Asset {
pub fn into_contract_id(
self,
network_passphrase: &str,
) -> Result<stellar_strkey::Contract, Self::Error> {
) -> Result<stellar_strkey::Contract, super::Error> {
let network_id = Hash::hash_bytes(network_passphrase);
HashIdPreimage::ContractId(HashIdPreimageContractId {
network_id,
Expand Down
3 changes: 2 additions & 1 deletion src/curr/bytes_conversions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{BytesM, ScVal};

impl From<BytesM> for ScVal {
fn from(value: BytesM) -> Self {
ScVal::Bytes(value.into())
}
}
}
Loading

0 comments on commit 403d524

Please sign in to comment.