Skip to content

Commit

Permalink
add conversion other way
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch committed Sep 25, 2024
1 parent d4376b6 commit 6956f58
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/curr/account_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ impl From<PublicKey> for MuxedAccount {
}
}
}

impl MuxedAccount {
pub fn account_id(self) -> AccountId {
match self {
MuxedAccount::Ed25519(k) => AccountId(PublicKey::PublicKeyTypeEd25519(k)),
MuxedAccount::MuxedEd25519(m) => AccountId(PublicKey::PublicKeyTypeEd25519(m.ed25519)),
}
}
}
9 changes: 9 additions & 0 deletions src/next/account_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ impl From<PublicKey> for MuxedAccount {
}
}
}

impl MuxedAccount {
pub fn account_id(self) -> AccountId {
match self {
MuxedAccount::Ed25519(k) => AccountId(PublicKey::PublicKeyTypeEd25519(k)),
MuxedAccount::MuxedEd25519(m) => AccountId(PublicKey::PublicKeyTypeEd25519(m.ed25519)),
}
}
}
25 changes: 24 additions & 1 deletion tests/account_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
))]
#![cfg(feature = "std")]

use ::stellar_xdr::curr::Uint256;
use ::stellar_xdr::curr::{MuxedAccountMed25519, Uint256};
#[cfg(feature = "curr")]
use stellar_xdr::curr as stellar_xdr;
#[cfg(feature = "next")]
Expand All @@ -25,3 +25,26 @@ fn from_public_key_to_muxed_account() {
let muxed_account: MuxedAccount = public_key.into();
assert_eq!(muxed_account, MuxedAccount::Ed25519(Uint256([1u8; 32])));
}

#[test]
fn from_muxed_account_ed_to_account_id() {
let muxed_account: MuxedAccount = MuxedAccount::Ed25519(Uint256([1u8; 32]));
let account_id = muxed_account.account_id();
assert_eq!(
account_id,
AccountId(PublicKey::PublicKeyTypeEd25519(Uint256([1u8; 32])))
);
}

#[test]
fn from_muxed_account_med_to_account_id() {
let muxed_account: MuxedAccount = MuxedAccount::MuxedEd25519(MuxedAccountMed25519 {
id: 2,
ed25519: Uint256([1u8; 32]),
});
let account_id = muxed_account.account_id();
assert_eq!(
account_id,
AccountId(PublicKey::PublicKeyTypeEd25519(Uint256([1u8; 32])))
);
}

0 comments on commit 6956f58

Please sign in to comment.