Skip to content

Commit

Permalink
Merge pull request #423 from Hou-Xiaoxuan/relay-CA
Browse files Browse the repository at this point in the history
add `relay` module with CA by `RustyVault`
  • Loading branch information
genedna authored Jun 18, 2024
2 parents e9b57a4 + 0ad2cf8 commit b3dd367
Show file tree
Hide file tree
Showing 11 changed files with 741 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
"venus",
"ceres",
"libra",
"relay",
]
exclude = ["craft"]
resolver = "1"
Expand Down
2 changes: 1 addition & 1 deletion gateway/src/git_protocol/ssh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use ceres::protocol::{SmartProtocol, TransportProtocol};
use jupiter::context::Context;

type ClientMap = HashMap<(usize, ChannelId), Channel<Msg>>;

#[allow(dead_code)]
#[derive(Clone)]
pub struct SshServer {
pub client_pubkey: Arc<russh_keys::key::PublicKey>,
Expand Down
4 changes: 2 additions & 2 deletions gateway/src/model/query.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use serde::Deserialize;

#[allow(dead_code)]
#[derive(Debug, Deserialize)]
pub struct DirectoryQuery {
#[serde(default)] // Use default value if not provided in the query string
pub object_id: Option<String>,
#[serde(default = "default_path")]
pub repo_path: String,
}

#[allow(dead_code)]
#[derive(Debug, Deserialize)]
pub struct CodePreviewQuery {
#[serde(default)]
Expand Down
2 changes: 1 addition & 1 deletion libra/src/internal/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct RemoteConfig {
pub name: String,
pub url: String,
}

#[allow(dead_code)]
pub struct BranchConfig {
pub name: String,
pub merge: String,
Expand Down
2 changes: 1 addition & 1 deletion mercury/src/internal/pack/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ impl PackEncoder {

#[cfg(test)]
mod tests {
use std::{io::Cursor, path::PathBuf, usize};
use std::{io::Cursor, path::PathBuf};

use crate::internal::object::blob::Blob;
use crate::internal::pack::Pack;
Expand Down
15 changes: 15 additions & 0 deletions relay/Cargo.toml
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"] }
44 changes: 44 additions & 0 deletions relay/src/lib.rs
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
}
}
3 changes: 3 additions & 0 deletions relay/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
34 changes: 34 additions & 0 deletions relay/src/nostr.rs
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(()));
}
}
Loading

1 comment on commit b3dd367

@vercel
Copy link

@vercel vercel bot commented on b3dd367 Jun 18, 2024

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

Please sign in to comment.