Skip to content

Commit

Permalink
add convert methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ktmlm committed Jun 18, 2023
1 parent cad664d commit 2a8c01e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ruc"
version = "5.0.0"
version = "5.0.3"
authors = ["rust-util-collections", "hui.fan@mail.ru"]
edition = "2021"
description = "Rust Util Collections"
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod sig;
pub mod trie;

pub use codec::base64::{
decode as base64_decode, decode_generic as base_decode_generic,
decode as base64_decode, decode_generic as base64_decode_generic,
encode as base64_encode,
};
pub use hasher::keccak::{
Expand Down
67 changes: 67 additions & 0 deletions src/crypto/sig/ed25519/readable/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::origin;
use crate::{crypto::codec::base64, *};
use std::fmt;

pub struct SignKey(String);
pub struct VerifyKey(String);
Expand All @@ -21,6 +22,34 @@ impl SignKey {
let sig = base64::encode(sk.sign(msg).to_bytes());
Ok(Sig(sig))
}

pub fn into_string(self) -> String {
self.0
}
}

impl fmt::Display for SignKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}

impl TryFrom<String> for SignKey {
type Error = Box<dyn RucError>;
fn try_from(s: String) -> Result<Self> {
let sk = base64::decode(&s).c(d!())?;
ed25519_zebra::SigningKey::try_from(sk.as_slice()).c(d!())?;
Ok(Self(s))
}
}

impl TryFrom<&str> for SignKey {
type Error = Box<dyn RucError>;
fn try_from(s: &str) -> Result<Self> {
let sk = base64::decode(s).c(d!())?;
ed25519_zebra::SigningKey::try_from(sk.as_slice()).c(d!())?;
Ok(Self(s.to_owned()))
}
}

impl VerifyKey {
Expand All @@ -30,6 +59,34 @@ impl VerifyKey {
ed25519_zebra::VerificationKey::try_from(vk.as_slice()).c(d!())?;
verify_by_raw_vk(&vk, sig, msg).c(d!())
}

pub fn into_string(self) -> String {
self.0
}
}

impl fmt::Display for VerifyKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}

impl TryFrom<String> for VerifyKey {
type Error = Box<dyn RucError>;
fn try_from(s: String) -> Result<Self> {
let vk = base64::decode(&s).c(d!())?;
ed25519_zebra::VerificationKey::try_from(vk.as_slice()).c(d!())?;
Ok(Self(s))
}
}

impl TryFrom<&str> for VerifyKey {
type Error = Box<dyn RucError>;
fn try_from(s: &str) -> Result<Self> {
let vk = base64::decode(s).c(d!())?;
ed25519_zebra::VerificationKey::try_from(vk.as_slice()).c(d!())?;
Ok(Self(s.to_owned()))
}
}

pub fn verify_by_raw_vk(
Expand All @@ -55,4 +112,14 @@ mod test {
let msg_fake = b"00000000000000000";
assert!(vk.verify(&sig, msg_fake).is_err());
}

#[test]
fn parse_key() {
assert!(SignKey::try_from(";akjflkjafj".to_owned()).is_err());
assert!(VerifyKey::try_from(";akjflkjafj".to_owned()).is_err());

let (sk, vk) = create_keypair();
assert!(SignKey::try_from(sk.to_string()).is_ok());
assert!(VerifyKey::try_from(vk.to_string()).is_ok());
}
}

0 comments on commit 2a8c01e

Please sign in to comment.