-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add conversions from AccountId and PublicKey to MuxedAccount (#396)
* Add conversions from AccountId and PublicKey to MuxedAccount * add conversion other way * fix * fix
- Loading branch information
1 parent
b551684
commit 67be595
Showing
5 changed files
with
101 additions
and
0 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
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)), | ||
} | ||
} | ||
} |
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,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)), | ||
} | ||
} | ||
} |
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,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]))) | ||
); | ||
} |