forked from everx-labs/ever-bls-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add ability to generate several keys * Several keys are generated independently
- Loading branch information
1 parent
4f08941
commit aa4ff1b
Showing
4 changed files
with
103 additions
and
17 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
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 +1,2 @@ | ||
pub mod bls; | ||
pub mod serde_bls; |
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,45 @@ | ||
use serde::Serialize; | ||
use std::path::Path; | ||
|
||
use crate::bls::BLS_PUBLIC_KEY_LEN; | ||
use crate::bls::BLS_SECRET_KEY_LEN; | ||
|
||
pub struct BLSKeyPair { | ||
pub public: [u8; BLS_PUBLIC_KEY_LEN], | ||
pub secret: [u8; BLS_SECRET_KEY_LEN], | ||
} | ||
|
||
#[derive(Serialize)] | ||
struct WrappedBLSKeyPair { | ||
public: String, | ||
secret: String, | ||
} | ||
|
||
impl WrappedBLSKeyPair { | ||
pub fn new(key_pair: &BLSKeyPair) -> Self { | ||
WrappedBLSKeyPair { | ||
public: hex::encode(key_pair.public), | ||
secret: hex::encode(key_pair.secret), | ||
} | ||
} | ||
} | ||
|
||
impl BLSKeyPair { | ||
pub fn from(data: ([u8; BLS_PUBLIC_KEY_LEN], [u8; BLS_SECRET_KEY_LEN])) -> Self { | ||
Self { | ||
public: data.0, | ||
secret: data.1, | ||
} | ||
} | ||
|
||
pub fn to_string(&self) -> anyhow::Result<String> { | ||
let wrapped = WrappedBLSKeyPair::new(self); | ||
serde_json::to_string_pretty(&wrapped) | ||
.map_err(|e| anyhow::format_err!("Failed to serialize BLSKeyPair: {e}")) | ||
} | ||
|
||
pub fn save_to_file(&self, path: impl AsRef<Path>) -> anyhow::Result<()> { | ||
std::fs::write(path, self.to_string()?) | ||
.map_err(|e| anyhow::format_err!("Failed to save BLSKetPait: {e}")) | ||
} | ||
} |