-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #423 from Hou-Xiaoxuan/relay-CA
add `relay` module with CA by `RustyVault`
- Loading branch information
Showing
11 changed files
with
741 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ members = [ | |
"venus", | ||
"ceres", | ||
"libra", | ||
"relay", | ||
] | ||
exclude = ["craft"] | ||
resolver = "1" | ||
|
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,15 @@ | ||
[package] | ||
name = "replay" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
rusty_vault = "0.1.0" | ||
serde_json = "1.0.117" | ||
go-defer = "0.1.0" | ||
openssl = "0.10.63" | ||
hex = "0.4.3" | ||
lazy_static = "1.4.0" | ||
secp256k1 = { version = "0.27.0", features = ["serde", "bitcoin-hashes", "bitcoin-hashes-std", "rand"] } | ||
bs58 = "0.5.1" | ||
serde = { version = "1.0.117", features = ["derive"] } |
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,44 @@ | ||
use crate::vault::{read_secret, write_secret}; | ||
|
||
pub mod pki; | ||
pub mod vault; | ||
pub mod nostr; | ||
|
||
/// Initialize the Nostr ID if it's not found. | ||
/// - return: `(Nostr ID, secret_key)` | ||
/// - You can get `Public Key` by just `base58::decode(nostr)` | ||
pub fn init() -> (String, String) { | ||
let mut id = read_secret("id").unwrap(); | ||
if id.is_none() { | ||
println!("Nostr ID not found, generating new one..."); | ||
let (nostr, (secret_key, _)) = nostr::generate_nostr_id(); | ||
let data = serde_json::json!({ | ||
"nostr": nostr, | ||
"secret_key": secret_key.display_secret().to_string(), | ||
}) | ||
.as_object() | ||
.unwrap() | ||
.clone(); | ||
write_secret("id", Some(data)).unwrap_or_else(|e| { | ||
panic!("Failed to write Nostr ID: {:?}", e); | ||
}); | ||
id = read_secret("id").unwrap(); | ||
} | ||
let id_data = id.unwrap().data.unwrap(); | ||
( | ||
id_data["nostr"].as_str().unwrap().to_string(), | ||
id_data["secret_key"].as_str().unwrap().to_string(), | ||
) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_init() { | ||
let id = init(); | ||
println!("Nostr ID: {:?}", id.0); | ||
println!("Secret Key: {:?}", id.1); // private key | ||
} | ||
} |
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,3 @@ | ||
fn main() { | ||
println!("Hello, world!"); | ||
} |
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,34 @@ | ||
use secp256k1::{PublicKey, rand, Secp256k1, SecretKey}; | ||
|
||
pub fn generate_nostr_id() -> (String, (SecretKey, PublicKey)) { | ||
let secp = Secp256k1::new(); | ||
let secret_key = SecretKey::new(&mut rand::thread_rng()); | ||
let public_key = secret_key.public_key(&secp); | ||
let nostr = bs58::encode(public_key.serialize()).into_string(); | ||
|
||
(nostr, (secret_key, public_key)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use secp256k1::Message; | ||
use super::*; | ||
|
||
#[test] | ||
fn test_generate_nostr_id() { | ||
let (nostr, keypair) = generate_nostr_id(); | ||
println!("nostr: {:?}", nostr); | ||
println!("keypair: {:?}", keypair); | ||
let secret_key = keypair.0; | ||
let public_key = keypair.1; | ||
|
||
let nostr_decode = bs58::decode(&nostr).into_vec().unwrap(); | ||
assert_eq!(nostr_decode, public_key.serialize().to_vec()); | ||
assert_eq!(PublicKey::from_slice(&nostr_decode).unwrap(), public_key); | ||
// verify | ||
let secp = Secp256k1::new(); | ||
let message = Message::from_slice(&[0xab; 32]).expect("32 bytes"); | ||
let sig = secp.sign_ecdsa(&message, &secret_key); | ||
assert_eq!(secp.verify_ecdsa(&message, &sig, &public_key), Ok(())); | ||
} | ||
} |
Oops, something went wrong.
b3dd367
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
mega – ./
mega-git-main-gitmono.vercel.app
mega-gitmono.vercel.app
gitmega.dev
www.gitmega.dev