-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add new
Key
type; allowing public keys to be named keys
This will allow users to add public keys with `keys add` so that they can be referenced in commands that need public addresses and contract invoke's that take address arguments.
- Loading branch information
1 parent
e44b89f
commit 06714b0
Showing
11 changed files
with
214 additions
and
43 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
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
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,146 @@ | ||
use std::{fmt::Display, str::FromStr}; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::secret::{self, Secret}; | ||
use crate::xdr; | ||
|
||
#[derive(thiserror::Error, Debug)] | ||
pub enum Error { | ||
#[error("Failed to extract public key from secret")] | ||
SecretPublicKey, | ||
#[error(transparent)] | ||
Secret(#[from] secret::Error), | ||
#[error(transparent)] | ||
StrKey(#[from] stellar_strkey::DecodeError), | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] | ||
#[serde(untagged)] | ||
pub enum Key { | ||
Secret(Secret), | ||
PublicKey { public_key: PublicKey }, | ||
MuxedAccount { muxed_account: MuxedAccount }, | ||
} | ||
|
||
impl Key { | ||
pub fn public_key(&self, hd_path: Option<usize>) -> Result<xdr::MuxedAccount, Error> { | ||
let bytes = match self { | ||
Key::Secret(secret) => secret.public_key(hd_path)?.0, | ||
Key::PublicKey { | ||
public_key: PublicKey(key), | ||
} => key.0, | ||
Key::MuxedAccount { | ||
muxed_account: MuxedAccount(stellar_strkey::ed25519::MuxedAccount { ed25519, id }), | ||
} => { | ||
return Ok(xdr::MuxedAccount::MuxedEd25519(xdr::MuxedAccountMed25519 { | ||
ed25519: xdr::Uint256(*ed25519), | ||
id: *id, | ||
})) | ||
} | ||
}; | ||
Ok(xdr::MuxedAccount::Ed25519(xdr::Uint256(bytes))) | ||
} | ||
|
||
pub fn private_key( | ||
&self, | ||
hd_path: Option<usize>, | ||
) -> Result<stellar_strkey::ed25519::PrivateKey, Error> { | ||
match self { | ||
Key::Secret(secret) => Ok(secret.private_key(hd_path)?), | ||
_ => Err(Error::SecretPublicKey), | ||
} | ||
} | ||
} | ||
|
||
impl FromStr for Key { | ||
type Err = Error; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
if let Ok(secret) = s.parse() { | ||
return Ok(Key::Secret(secret)); | ||
} | ||
if let Ok(public_key) = s.parse() { | ||
return Ok(Key::PublicKey { public_key }); | ||
} | ||
if let Ok(muxed_account) = s.parse() { | ||
return Ok(Key::MuxedAccount { muxed_account }); | ||
} | ||
todo!("Error handling for invalid key format"); | ||
} | ||
} | ||
|
||
impl From<stellar_strkey::ed25519::PublicKey> for Key { | ||
fn from(value: stellar_strkey::ed25519::PublicKey) -> Self { | ||
Key::PublicKey { | ||
public_key: PublicKey(value), | ||
} | ||
} | ||
} | ||
|
||
impl From<&stellar_strkey::ed25519::PublicKey> for Key { | ||
fn from(stellar_strkey::ed25519::PublicKey(key): &stellar_strkey::ed25519::PublicKey) -> Self { | ||
stellar_strkey::ed25519::PublicKey(*key).into() | ||
} | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq, serde_with::SerializeDisplay, serde_with::DeserializeFromStr)] | ||
pub struct PublicKey(pub stellar_strkey::ed25519::PublicKey); | ||
|
||
impl FromStr for PublicKey { | ||
type Err = stellar_strkey::DecodeError; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
Ok(PublicKey(stellar_strkey::ed25519::PublicKey::from_str(s)?)) | ||
} | ||
} | ||
|
||
impl Display for PublicKey { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", self.0) | ||
} | ||
} | ||
|
||
impl From<&PublicKey> for stellar_strkey::ed25519::MuxedAccount { | ||
fn from(PublicKey(stellar_strkey::ed25519::PublicKey(key)): &PublicKey) -> Self { | ||
stellar_strkey::ed25519::MuxedAccount { | ||
id: 0, | ||
ed25519: *key, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq, serde_with::SerializeDisplay, serde_with::DeserializeFromStr)] | ||
pub struct MuxedAccount(pub stellar_strkey::ed25519::MuxedAccount); | ||
|
||
impl FromStr for MuxedAccount { | ||
type Err = stellar_strkey::DecodeError; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
Ok(MuxedAccount( | ||
stellar_strkey::ed25519::MuxedAccount::from_str(s)?, | ||
)) | ||
} | ||
} | ||
|
||
impl Display for MuxedAccount { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", self.0) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn public_key() { | ||
let key = Key::PublicKey { | ||
public_key: PublicKey(stellar_strkey::ed25519::PublicKey([0; 32])), | ||
}; | ||
let serialized = toml::to_string(&key).unwrap(); | ||
println!("{serialized}"); | ||
let deserialized: Key = toml::from_str(&serialized).unwrap(); | ||
assert_eq!(key, deserialized); | ||
} | ||
} |
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
Oops, something went wrong.