-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
313b59d
commit c35425b
Showing
5 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
mod generated; | ||
pub use generated::*; | ||
|
||
mod str; | ||
|
||
mod scval_conversions; | ||
pub use scval_conversions::*; | ||
|
||
|
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,55 @@ | ||
#![cfg(feature = "std")] | ||
|
||
/// Custom string representations of the following types, also used for JSON | ||
/// formatting. | ||
/// | ||
/// ## Strkey Types | ||
/// - PublicKey | ||
/// - PublicKey | ||
/// - MuxedAccount | ||
/// - MuxedAccountMed25519 | ||
/// - SignerKey | ||
/// - SignerKeyEd25519SignedPayload | ||
/// - NodeId | ||
/// | ||
/// ## Asset Types | ||
/// - Asset | ||
/// - AlphaNum4 | ||
/// - AlphaNum12 | ||
/// | ||
/// ## ASCII Types | ||
/// - AssetCode | ||
/// - AssetCode4 | ||
/// - AssetCode12 | ||
use super::{Error, PublicKey, Uint256}; | ||
|
||
impl From<stellar_strkey::DecodeError> for Error { | ||
fn from(_: stellar_strkey::DecodeError) -> Self { | ||
// TODO: Add error type for strkeys. | ||
Error::Invalid | ||
} | ||
} | ||
|
||
impl core::fmt::Display for PublicKey { | ||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
match self { | ||
PublicKey::PublicKeyTypeEd25519(k) => { | ||
let k = stellar_strkey::ed25519::PublicKey::from_payload(&k.0) | ||
.map_err(|_| std::fmt::Error)?; | ||
let s = k.to_string(); | ||
f.write_str(&s)?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(feature = "alloc")] | ||
impl core::str::FromStr for PublicKey { | ||
type Err = Error; | ||
fn from_str(s: &str) -> core::result::Result<Self, Self::Err> { | ||
let stellar_strkey::ed25519::PublicKey(k) = | ||
stellar_strkey::ed25519::PublicKey::from_str(s)?; | ||
Ok(PublicKey::PublicKeyTypeEd25519(Uint256(k))) | ||
} | ||
} |
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,29 @@ | ||
#![cfg(all( | ||
any(feature = "curr", feature = "next"), | ||
not(all(feature = "curr", feature = "next")) | ||
))] | ||
#![cfg(feature = "std")] | ||
|
||
use std::str::FromStr; | ||
|
||
#[cfg(feature = "curr")] | ||
use stellar_xdr::curr as stellar_xdr; | ||
#[cfg(feature = "next")] | ||
use stellar_xdr::next as stellar_xdr; | ||
|
||
use stellar_xdr::{PublicKey, Uint256}; | ||
|
||
#[test] | ||
fn public_key_from_str() { | ||
let v = PublicKey::from_str("GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF"); | ||
assert_eq!(v, Ok(PublicKey::PublicKeyTypeEd25519(Uint256([0; 32])))); | ||
} | ||
|
||
#[test] | ||
fn public_key_to_string() { | ||
let s = PublicKey::PublicKeyTypeEd25519(Uint256([0; 32])).to_string(); | ||
assert_eq!( | ||
s, | ||
"GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF" | ||
); | ||
} |