Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: serialization for keypair #16

Closed
Dustin-Ray opened this issue Oct 17, 2023 · 1 comment
Closed

feature: serialization for keypair #16

Dustin-Ray opened this issue Oct 17, 2023 · 1 comment
Labels
good first issue Good for newcomers

Comments

@Dustin-Ray
Copy link
Owner

Dustin-Ray commented Oct 17, 2023

This is useful for the CLI/GUI to accept KeyPair objects to use in any asymmetric operations. Otherwise, we have to copy and paste public/private key values which is possible but cumbersome. Here's an example of where we ultimately want to end up after solving issue #21 using the capycrypt CLI to generate KeyPair objects:

capycrypt create_keypair --pw "testpassword" --owner "Dave Bowman" --curve "E448" --d 256 --output keypair.json

To achieve this, we only need to make a few changes:
In cargo.toml:
Add the following:

[dependencies]
serde = { version = "1.0", features = ["derive"] }

Then, in lib.rs where KeyPair is defined, we need to make the following modifcations:

use serde::Serialize;
use std::fmt;

#[derive(Serialize)]
struct KeyPair {
    owner: String,
    // ... other fields ...
}

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

impl KeyPair {
    // ... your new function and other methods ...
    
    pub fn save_to_file(&self, filename: &str) -> std::io::Result<()> {
        std::fs::write(filename, self.to_string())
    }
}

Finally, to write the KeyPair to a JSON, do the following:

let keypair = KeyPair {
    owner: "Frank Poole".to_string(),
    // ... initialize other fields ...
};

keypair.save_to_file("keypair.json").unwrap();

That's it for this issue!

@Dustin-Ray Dustin-Ray changed the title serialization feature: serialization Oct 18, 2023
@Dustin-Ray Dustin-Ray added good first issue Good for newcomers Medium Effort Nothing too crazy. Maybe some new concepts and design patterns. 1 - 2 weeks of casual effort. labels Oct 20, 2023
@Dustin-Ray
Copy link
Owner Author

@omibo everything is in place for you to follow the above steps to get everything working

@Dustin-Ray Dustin-Ray removed the Medium Effort Nothing too crazy. Maybe some new concepts and design patterns. 1 - 2 weeks of casual effort. label Apr 5, 2024
@Dustin-Ray Dustin-Ray changed the title feature: serialization feature: serialization for keypair Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant