Skip to content

Commit 0ca15dd

Browse files
committed
feat: kid
1 parent 1249484 commit 0ca15dd

File tree

10 files changed

+453
-65
lines changed

10 files changed

+453
-65
lines changed

rust/catalyst-types/Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@ anyhow = "1.0.95"
2020
blake2b_simd = "1.0.2"
2121
coset = "0.3.8"
2222
ed25519-dalek = "2.1.1"
23+
fluent-uri = "0.3.2"
2324
hex = "0.4.3"
2425
minicbor = { version = "0.25.1", features = ["std"] }
2526
num-traits = "0.2.19"
2627
orx-concurrent-vec = "3.1.0"
2728
pallas-crypto = { version = "0.30.1", git = "https://github.com/input-output-hk/catalyst-pallas.git", rev = "9b5183c8b90b90fe2cc319d986e933e9518957b3" }
2829
serde = { version = "1.0.217", features = ["derive"] }
29-
uuid = { version = "1.11.0", features = ["v4", "v7", "serde"] }
30+
thiserror = "2.0.9"
31+
base64-url = "3.0.0"
32+
uuid = { version = "1.11.0", features = ["v4", "v7", "serde"] }
33+
34+
[dev-dependencies]
35+
rand = "0.8.5"

rust/catalyst-types/README.md

Lines changed: 6 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,9 @@
1-
# CBOR Kit (`cbork`)
1+
# Catalyst Types
22

3-
This is a CLI tool for operating with CDDL and CBOR.
3+
This library is designed to streamline the organization and sharing of types across multiple crates.
4+
It provides a centralized location for reusable, enhanced types that are not specific to a particular domain, such as Cardano.
45

5-
It will grow over time to provide a number of features, and will be supported by individual `cbork-*` crates.
6+
## Purpose
67

7-
## Install
8-
9-
To install this tool run
10-
11-
```shell
12-
cargo install --git https://github.com/input-output-hk/catalyst-libs.git cbork
13-
```
14-
15-
## Features
16-
17-
### CDDL linter
18-
19-
[CDDL](#cddl-specifications) (Concise Data Definition Language)
20-
linting CLI tool.
21-
Enables users to check their CDDL code for errors, inconsistencies, and compliance with the CDDL specification.
22-
23-
#### Currently supports
24-
25-
* [CDDL][1]
26-
* [CDDL Errata][2]
27-
* [CDDL Extensions][3]
28-
29-
#### Planned support for
30-
31-
* [CDDL Modules][4]
32-
* [CDDL Module Standard Library][5]
33-
34-
## Planned Future Tools within the CLI
35-
36-
* [ ] A tool to generate a Rust module to decode/encode/validate Data efficiently from a [CDDL](#cddl-specifications) definition.
37-
* [ ] A tool to simply validate [CBOR][6] binary data against a [CDDL](#cddl-specifications) definition.
38-
39-
## Notes
40-
41-
There are semantic rules about well-formed [CDDL](#cddl-specifications) files that are not enforced by the grammar.
42-
The full parser does not support these rules currently, but is planned to be extended to validate
43-
those rules in future
44-
45-
The primary rule that is not currently enforced is that the very first definition in the file is the base type.
46-
We also cannot detect orphaned or missing definitions.
47-
48-
Both of these are planned for a future release of the tool.
49-
50-
There may be other checks needed to be performed on the parsed AST for validity.
51-
52-
## CDDL Specifications
53-
54-
* [RFC8610][1]
55-
* [RFC8610 Errata][2]
56-
* [RFC9165 CDDL Extensions][3]
57-
* [CDDL Modules][4]
58-
* [CDDL Modules Reference][5]
59-
60-
[1]: https://www.rfc-editor.org/rfc/rfc8610 "RFC-8610"
61-
[2]: https://www.ietf.org/archive/id/draft-ietf-cbor-update-8610-grammar-01.html "RFC-8610 Errata"
62-
[3]: https://www.rfc-editor.org/rfc/rfc9165 "RFC-9165 CDDL Extensions"
63-
[4]: https://cbor-wg.github.io/cddl-modules/draft-ietf-cbor-cddl-modules.html "CDDL Modules Specification"
64-
[5]: https://github.com/cabo/cddlc "CDDL Modules Standard Library"
65-
[6]: https://datatracker.ietf.org/doc/html/rfc8949 "RFC-8949 CBOR Data Interchange Format"
8+
* To enhance types that can be utilized across different libraries or projects.
9+
* To provide utility functions related to types and conversion between types.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//! COSE Signature Protected Header `kid` URI Authority.
2+
3+
use std::{
4+
fmt::{Display, Formatter},
5+
str::FromStr,
6+
};
7+
8+
/// URI Authority
9+
#[derive(Debug, Clone)]
10+
pub enum Authority {
11+
/// Cardano Blockchain
12+
Cardano,
13+
/// Midnight Blockchain
14+
Midnight,
15+
}
16+
17+
impl FromStr for Authority {
18+
type Err = anyhow::Error;
19+
20+
fn from_str(s: &str) -> Result<Self, Self::Err> {
21+
match s {
22+
"cardano" => Ok(Authority::Cardano),
23+
"midnight" => Ok(Authority::Midnight),
24+
_ => Err(anyhow::anyhow!("Unknown Authority: {s}")),
25+
}
26+
}
27+
}
28+
29+
impl Display for Authority {
30+
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
31+
let authority = match self {
32+
Self::Cardano => "cardano",
33+
Self::Midnight => "midnight",
34+
};
35+
write!(f, "{authority}")
36+
}
37+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//! Errors returned by this type
2+
3+
use thiserror::Error;
4+
5+
use super::{key_rotation::KeyRotationError, role_index::RoleIndexError};
6+
7+
/// Errors that can occur when parsing a `KidURI`
8+
#[derive(Error, Debug)]
9+
pub enum KidURIError {
10+
/// Invalid KID URI
11+
#[error("Invalid URI")]
12+
InvalidURI(#[from] fluent_uri::error::ParseError),
13+
/// Invalid Scheme, not a KID URI
14+
#[error("Invalid Scheme, not a KID URI")]
15+
InvalidScheme,
16+
/// Network not defined in URI
17+
#[error("No defined Network")]
18+
NoDefinedNetwork,
19+
/// Path of URI is invalid
20+
#[error("Invalid Path")]
21+
InvalidPath,
22+
/// Role 0 Key in path is invalid
23+
#[error("Invalid Role 0 Key")]
24+
InvalidRole0Key,
25+
/// Role 0 Key in path is not encoded correctly
26+
#[error("Invalid Role 0 Key Encoding")]
27+
InvalidRole0KeyEncoding(#[from] base64_url::base64::DecodeError),
28+
/// Role Index is invalid
29+
#[error("Invalid Role")]
30+
InvalidRole,
31+
/// Role Index is not encoded correctly
32+
#[error("Invalid Role Index")]
33+
InvalidRoleIndex(#[from] RoleIndexError),
34+
/// Role Key Rotation is invalid
35+
#[error("Invalid Rotation")]
36+
InvalidRotation,
37+
/// Role Key Rotation is not encoded correctly
38+
#[error("Invalid Rotation Value")]
39+
InvalidRotationValue(#[from] KeyRotationError),
40+
/// Encryption key Identifier Fragment is not valid
41+
#[error("Invalid Encryption Key Fragment")]
42+
InvalidEncryptionKeyFragment,
43+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//! COSE Signature Protected Header `kid` Role0 Key Version.
2+
3+
use std::{
4+
fmt::{Display, Formatter},
5+
num::ParseIntError,
6+
str::FromStr,
7+
};
8+
9+
use thiserror::Error;
10+
11+
/// Errors from parsing the `KeyRotation`
12+
#[derive(Error, Debug)]
13+
#[allow(clippy::module_name_repetitions)]
14+
pub enum KeyRotationError {
15+
/// Key Rotation could not be parsed from a string
16+
#[error("Invalid Role Key Rotation")]
17+
InvalidRole(#[from] ParseIntError),
18+
}
19+
20+
/// Rotation count of the Role Key.
21+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
22+
pub struct KeyRotation(u16);
23+
24+
impl From<u16> for KeyRotation {
25+
fn from(value: u16) -> Self {
26+
Self(value)
27+
}
28+
}
29+
30+
impl FromStr for KeyRotation {
31+
type Err = KeyRotationError;
32+
33+
fn from_str(s: &str) -> Result<Self, Self::Err> {
34+
Ok(Self(s.parse::<u16>()?))
35+
}
36+
}
37+
38+
impl Display for KeyRotation {
39+
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
40+
write!(f, "{}", self.0)
41+
}
42+
}

0 commit comments

Comments
 (0)