Skip to content

Commit

Permalink
fix(rust/signed_doc): cleanup and fix to DocumentRef serde
Browse files Browse the repository at this point in the history
  • Loading branch information
saibatizoku committed Dec 17, 2024
1 parent a69a90e commit 8e07f3e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 27 deletions.
8 changes: 2 additions & 6 deletions rust/signed_doc/examples/cat-signed-doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,14 @@ enum Cli {
Inspect {
/// Path to the fully formed (should has at least one signature) COSE document
cose_sign: PathBuf,
/// Path to the json schema (Draft 7) to validate document against it
doc_schema: PathBuf,
},
}

impl Cli {
/// Execute Cli command
fn exec(self) -> anyhow::Result<()> {
match self {
Self::Inspect {
cose_sign,
doc_schema: _,
} => {
Self::Inspect { cose_sign } => {
//
let mut cose_file = File::open(cose_sign)?;
let mut cose_file_bytes = Vec::new();
Expand All @@ -48,6 +43,7 @@ impl Cli {

fn main() {
println!("Catalyst Signed Document");
println!("------------------------");
if let Err(err) = Cli::parse().exec() {
println!("{err}");
}
Expand Down
5 changes: 3 additions & 2 deletions rust/signed_doc/examples/mk_signed_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn decode_cbor_uuid(val: &coset::cbor::Value) -> anyhow::Result<uuid::Uuid> {
fn encode_cbor_document_ref(doc_ref: &DocumentRef) -> coset::cbor::Value {
match doc_ref {
DocumentRef::Latest { id } => encode_cbor_uuid(id),
DocumentRef::WithVer { id, ver } => {
DocumentRef::WithVer(id, ver) => {
coset::cbor::Value::Array(vec![encode_cbor_uuid(id), encode_cbor_uuid(ver)])
},
}
Expand All @@ -101,7 +101,7 @@ fn decode_cbor_document_ref(val: &coset::cbor::Value) -> anyhow::Result<Document
anyhow::ensure!(array.len() == 2, "Invalid CBOR encoded document `ref` type");
let id = decode_cbor_uuid(&array[0])?;
let ver = decode_cbor_uuid(&array[1])?;
Ok(DocumentRef::WithVer { id, ver })
Ok(DocumentRef::WithVer(id, ver))
}
}

Expand Down Expand Up @@ -133,6 +133,7 @@ impl Cli {
let schema = load_schema_from_file(&schema)?;
let cose = load_cose_from_file(&doc)?;
validate_cose(&cose, &pk, &schema)?;
println!("Document is valid.");
},
}
println!("Done");
Expand Down
14 changes: 5 additions & 9 deletions rust/signed_doc/meta.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,11 @@
}
},
{
"type": "object",
"properties": {
"id": {
"$ref": "#/definitions/uuidv7"
},
"ver": {
"$ref": "#/definitions/uuidv7"
}
}
"type": "array",
"items": {
"$ref": "#/definitions/uuidv7"
},
"minItems": 2
}
]
},
Expand Down
16 changes: 6 additions & 10 deletions rust/signed_doc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ pub struct CatalystSignedDocument {
impl Display for CatalystSignedDocument {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
writeln!(f, "{}", self.inner.metadata)?;
writeln!(f, "JSON Payload {:#}", self.inner.payload)?;
writeln!(f, "JSON Payload {:#}\n", self.inner.payload)?;
writeln!(f, "Signatures [")?;
for signature in &self.inner.signatures {
writeln!(f, " {:#}", hex::encode(signature.signature.as_slice()))?;
writeln!(f, " 0x{:#}", hex::encode(signature.signature.as_slice()))?;
}
writeln!(f, "]")?;
writeln!(f, "]\n")?;
writeln!(f, "Content Errors [")?;
for error in &self.inner.content_errors {
writeln!(f, " {error:#}")?;
Expand Down Expand Up @@ -116,12 +116,8 @@ pub enum DocumentRef {
id: uuid::Uuid,
},
/// Reference to the specific document version
WithVer {
/// Document ID UUID
id: uuid::Uuid,
/// Document Version UUID
ver: uuid::Uuid,
},
/// Document ID UUID, Document Ver UUID
WithVer(uuid::Uuid, uuid::Uuid),
}

// Do this instead of `new` if we are converting a single parameter into a struct/type we
Expand Down Expand Up @@ -249,7 +245,7 @@ fn decode_cbor_document_ref(val: &coset::cbor::Value) -> anyhow::Result<Document
anyhow::ensure!(array.len() == 2, "Invalid CBOR encoded document `ref` type");
let id = decode_cbor_uuid(&array[0])?;
let ver = decode_cbor_uuid(&array[1])?;
Ok(DocumentRef::WithVer { id, ver })
Ok(DocumentRef::WithVer(id, ver))
}
}

Expand Down

0 comments on commit 8e07f3e

Please sign in to comment.