Radish alpha
h
rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5
Radicle Heartwood Protocol & Stack
Radicle
Git
crypto: Optionally provide JSON Schema
Merged lorenz opened 2 months ago

Providing the schema in radicle-crypto saves a lot of repetition in radicle, and can be done via an optional dependency.

9 files changed +21 -80 82ad52b1 faeee9f3
modified Cargo.lock
@@ -2959,6 +2959,7 @@ dependencies = [
 "qcheck",
 "qcheck-macros",
 "radicle-ssh",
+
 "schemars",
 "serde",
 "signature 2.2.0",
 "sqlite",
modified crates/radicle-crypto/Cargo.toml
@@ -25,6 +25,7 @@ multibase = { workspace = true }
qcheck = { workspace = true, optional = true }
git-ref-format-core = { workspace = true, optional = true }
radicle-ssh = { workspace = true, optional = true }
+
schemars = { workspace = true, optional = true, features = ["derive", "std"] }
serde = { workspace = true, features = ["derive", "std"] }
signature = { workspace = true }
sqlite = { workspace = true, features = ["bundled"], optional = true }
modified crates/radicle-crypto/src/lib.rs
@@ -158,6 +158,20 @@ impl TryFrom<String> for Signature {
/// The public/verification key.
#[derive(Hash, Serialize, Deserialize, PartialEq, Eq, Copy, Clone)]
#[serde(into = "String", try_from = "String")]
+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
+
#[cfg_attr(
+
    feature = "schemars",
+
    schemars(
+
        title = "Ed25519",
+
        description = "An Ed25519 public key in multibase encoding.",
+
        extend("examples" = [
+
            "z6MkrLMMsiPWUcNPHcRajuMi9mDfYckSoJyPwwnknocNYPm7",
+
            "z6MkvUJtYD9dHDJfpevWRT98mzDDpdAtmUjwyDSkyqksUr7C",
+
            "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi",
+
            "z6MkkfM3tPXNPrPevKr3uSiQtHPuwnNhu2yUVjgd2jXVsVz5",
+
        ]),
+
    ),
+
)]
pub struct PublicKey(pub ed25519::PublicKey);

#[cfg(feature = "cyphernet")]
modified crates/radicle-schemars/src/main.rs
@@ -83,10 +83,7 @@ fn print_schema() -> io::Result<()> {
            #[schemars(untagged)]
            #[allow(dead_code)]
            enum CommandResult {
-
                Nid(
-
                    #[schemars(with = "radicle::schemars_ext::crypto::PublicKey")]
-
                    radicle::node::NodeId,
-
                ),
+
                Nid(radicle::node::NodeId),
                Config(Box<radicle::node::Config>),
                ListenAddrs(ListenAddrs),
                ConnectResult(radicle::node::ConnectResult),
modified crates/radicle/Cargo.toml
@@ -20,6 +20,7 @@ qcheck = [
schemars = [
  "radicle-oid/schemars",
  "radicle-core/schemars",
+
  "radicle-crypto/schemars",
  "radicle-localtime/schemars",
  "dep:schemars"
]
modified crates/radicle/src/node.rs
@@ -553,10 +553,6 @@ impl std::fmt::Display for Link {
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct Session {
-
    #[cfg_attr(
-
        feature = "schemars",
-
        schemars(with = "crate::schemars_ext::crypto::PublicKey")
-
    )]
    pub nid: NodeId,
    pub link: Link,
    pub addr: Address,
@@ -576,10 +572,6 @@ impl Session {
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct Seed {
    /// The Node ID.
-
    #[cfg_attr(
-
        feature = "schemars",
-
        schemars(with = "crate::schemars_ext::crypto::PublicKey")
-
    )]
    pub nid: NodeId,
    /// Known addresses for this seed.
    pub addrs: Vec<KnownAddress>,
@@ -700,10 +692,6 @@ impl From<Vec<Seed>> for Seeds {
pub enum FetchResult {
    Success {
        updated: Vec<RefUpdate>,
-
        #[cfg_attr(
-
            feature = "schemars",
-
            schemars(with = "HashSet<crate::schemars_ext::crypto::PublicKey>")
-
        )]
        namespaces: HashSet<NodeId>,
        clone: bool,
    },
modified crates/radicle/src/node/command.rs
@@ -40,10 +40,6 @@ pub enum Command {
        rid: RepoId,

        /// The namespaces for which references should be announced.
-
        #[cfg_attr(
-
            feature = "schemars",
-
            schemars(with = "HashSet<crate::schemars_ext::crypto::PublicKey>")
-
        )]
        namespaces: HashSet<PublicKey>,
    },

@@ -69,13 +65,7 @@ pub enum Command {

    /// Disconnect from a node.
    #[serde(rename_all = "camelCase")]
-
    Disconnect {
-
        #[cfg_attr(
-
            feature = "schemars",
-
            schemars(with = "crate::schemars_ext::crypto::PublicKey")
-
        )]
-
        nid: NodeId,
-
    },
+
    Disconnect { nid: NodeId },

    /// Look up seeds for the given repository in the routing table.
    #[serde(rename_all = "camelCase")]
@@ -91,10 +81,6 @@ pub enum Command {
        rid: RepoId,

        /// The namespaces for which references should be announced.
-
        #[cfg_attr(
-
            feature = "schemars",
-
            schemars(with = "HashSet<crate::schemars_ext::crypto::PublicKey>")
-
        )]
        namespaces: HashSet<PublicKey>,
    },

@@ -102,22 +88,12 @@ pub enum Command {
    Sessions,

    /// Get a specific peer session.
-
    Session {
-
        #[cfg_attr(
-
            feature = "schemars",
-
            schemars(with = "crate::schemars_ext::crypto::PublicKey")
-
        )]
-
        nid: NodeId,
-
    },
+
    Session { nid: NodeId },

    /// Fetch the given repository from the network.
    #[serde(rename_all = "camelCase")]
    Fetch {
        rid: RepoId,
-
        #[cfg_attr(
-
            feature = "schemars",
-
            schemars(with = "crate::schemars_ext::crypto::PublicKey")
-
        )]
        nid: NodeId,
        timeout: time::Duration,
    },
@@ -136,23 +112,13 @@ pub enum Command {
    /// Follow the given node.
    #[serde(rename_all = "camelCase")]
    Follow {
-
        #[cfg_attr(
-
            feature = "schemars",
-
            schemars(with = "crate::schemars_ext::crypto::PublicKey")
-
        )]
        nid: NodeId,
        alias: Option<super::Alias>,
    },

    /// Unfollow the given node.
    #[serde(rename_all = "camelCase")]
-
    Unfollow {
-
        #[cfg_attr(
-
            feature = "schemars",
-
            schemars(with = "crate::schemars_ext::crypto::PublicKey")
-
        )]
-
        nid: NodeId,
-
    },
+
    Unfollow { nid: NodeId },

    /// Get the node's status.
    Status,
modified crates/radicle/src/schemars_ext.rs
@@ -4,29 +4,6 @@

use schemars::JsonSchema;

-
pub mod crypto {
-
    use super::*;
-
    /// See [`crate::node::NodeId`]
-
    /// See [`crate::storage::RemoteId`]
-
    /// See [`::crypto::PublicKey`]
-
    ///
-
    /// An Ed25519 public key in multibase encoding.
-
    ///
-
    /// `MULTIBASE(base58-btc, MULTICODEC(public-key-type, raw-public-key-bytes))`
-
    #[derive(JsonSchema)]
-
    #[schemars(
-
    title = "NodeId",
-
    description = "An Ed25519 public key in multibase encoding.",
-
    extend("examples" = [
-
        "z6MkrLMMsiPWUcNPHcRajuMi9mDfYckSoJyPwwnknocNYPm7",
-
        "z6MkvUJtYD9dHDJfpevWRT98mzDDpdAtmUjwyDSkyqksUr7C",
-
        "z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi",
-
        "z6MkkfM3tPXNPrPevKr3uSiQtHPuwnNhu2yUVjgd2jXVsVz5",
-
    ]),
-
)]
-
    pub struct PublicKey(String);
-
}
-

pub(crate) mod log {
    use super::*;

modified crates/radicle/src/storage/refs.rs
@@ -376,10 +376,6 @@ impl<V> Deref for SignedRefs<V> {
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct RefsAt {
    /// The remote namespace of the `rad/sigrefs`.
-
    #[cfg_attr(
-
        feature = "schemars",
-
        schemars(with = "crate::schemars_ext::crypto::PublicKey")
-
    )]
    pub remote: RemoteId,
    /// The commit SHA that `rad/sigrefs` points to.
    pub at: Oid,