Skip to content

Commit

Permalink
Add conversions from AccountId and PublicKey to MuxedAccount (#396)
Browse files Browse the repository at this point in the history
* Add conversions from AccountId and PublicKey to MuxedAccount

* add conversion other way

* fix

* fix
  • Loading branch information
leighmcculloch authored Sep 25, 2024
1 parent b551684 commit 67be595
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/curr/account_conversions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use super::{AccountId, MuxedAccount, PublicKey};

impl From<AccountId> for MuxedAccount {
fn from(account_id: AccountId) -> Self {
account_id.0.into()
}
}

impl From<PublicKey> for MuxedAccount {
fn from(public_key: PublicKey) -> Self {
match public_key {
PublicKey::PublicKeyTypeEd25519(k) => MuxedAccount::Ed25519(k),
}
}
}

impl MuxedAccount {
#[must_use]
pub fn account_id(self) -> AccountId {
match self {
MuxedAccount::Ed25519(k) => AccountId(PublicKey::PublicKeyTypeEd25519(k)),
MuxedAccount::MuxedEd25519(m) => AccountId(PublicKey::PublicKeyTypeEd25519(m.ed25519)),
}
}
}
1 change: 1 addition & 0 deletions src/curr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod str;

mod scval_conversions;
pub use scval_conversions::*;
mod account_conversions;
mod transaction_conversions;

mod scval_validations;
Expand Down
25 changes: 25 additions & 0 deletions src/next/account_conversions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use super::{AccountId, MuxedAccount, PublicKey};

impl From<AccountId> for MuxedAccount {
fn from(account_id: AccountId) -> Self {
account_id.0.into()
}
}

impl From<PublicKey> for MuxedAccount {
fn from(public_key: PublicKey) -> Self {
match public_key {
PublicKey::PublicKeyTypeEd25519(k) => MuxedAccount::Ed25519(k),
}
}
}

impl MuxedAccount {
#[must_use]
pub fn account_id(self) -> AccountId {
match self {
MuxedAccount::Ed25519(k) => AccountId(PublicKey::PublicKeyTypeEd25519(k)),
MuxedAccount::MuxedEd25519(m) => AccountId(PublicKey::PublicKeyTypeEd25519(m.ed25519)),
}
}
}
1 change: 1 addition & 0 deletions src/next/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod str;

mod scval_conversions;
pub use scval_conversions::*;
mod account_conversions;
mod transaction_conversions;

mod scval_validations;
Expand Down
49 changes: 49 additions & 0 deletions tests/account_conversions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#![cfg(all(
any(feature = "curr", feature = "next"),
not(all(feature = "curr", feature = "next"))
))]
#![cfg(feature = "std")]

#[cfg(feature = "curr")]
use stellar_xdr::curr as stellar_xdr;
#[cfg(feature = "next")]
use stellar_xdr::next as stellar_xdr;

use stellar_xdr::{AccountId, MuxedAccount, MuxedAccountMed25519, PublicKey, Uint256};

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

#[test]
fn from_public_key_to_muxed_account() {
let public_key = PublicKey::PublicKeyTypeEd25519(Uint256([1u8; 32]));
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 67be595

Please sign in to comment.