Skip to content

feat(rust/signed-doc): CBOR encoder using minicbor #351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 18 commits into from

Conversation

no30bit
Copy link
Contributor

@no30bit no30bit commented May 28, 2025

Description

Migrating signed-doc encoding from coset to minicbor.

Related Issue(s)

Closes #332

Description of Changes

  • Refactored the document builder to be more general.
  • Implemented used COSE logic.
  • Refactored ContentType, ContentEncoding and DocumentRef to minicbor.

Breaking Changes

  • serde support removed for signed_doc.
  • ciborium and coset support removed.
  • Builder type is refactored a lot.

Related Pull Requests

#338

Please confirm the following checks

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream module

@no30bit no30bit changed the base branch from main to feat/new-cat-signed-doc May 28, 2025 02:54
@no30bit no30bit requested a review from Mr-Leshiy May 28, 2025 02:54
@no30bit no30bit self-assigned this May 28, 2025
@no30bit no30bit added enhancement New feature or request do not review wip labels May 28, 2025
@no30bit no30bit added this to Catalyst May 28, 2025
@no30bit no30bit moved this from New to 🏗 In progress in Catalyst May 28, 2025
@no30bit no30bit added the do not merge yet PR is not ready to merge yet label May 28, 2025
@github-project-automation github-project-automation bot moved this from 🏗 In progress to 🔖 Ready in Catalyst May 28, 2025
@no30bit no30bit moved this from 🔖 Ready to 🏗 In progress in Catalyst Jun 2, 2025

anyhow = "1.0.95"
serde = { version = "1.0.217", features = ["derive"] }
serde_json = "1.0.134"
coset = "0.3.8"
minicbor = { version = "0.25.1", features = ["half"] }
coset = { version = "0.3.8", features = ["std"] }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove all use of coset.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we have the decoding side still. Shouldn't it come form coset until that's merged and only then fully migrated to minicbor (having been tested)?

@@ -66,7 +66,7 @@ impl Cli {
// Possibly encode if Metadata has an encoding set.
let payload = serde_json::to_vec(&json_doc)?;
// Start with no signatures.
let signed_doc = Builder::new()
let signed_doc = CoseSignBuilder::new()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just am not seeing the need for a builder.
We should just be creating a CatalystSignedDocument type,
Adding whatever fields to it we like.
Adding a payload.
And then encoding it (it stores its encoded data internally).
Once encoded, we can call a function on the object to attach a signature, given a catalyst-id and a private key.

I think its a huge over-complication to build a generic COSE builder when we only need a way to serialize catalyst signed documents.

If we can encode the payload and metadata, this builder is literally just encoding:

[
  headers,
  payload,
  [ array of signatures ] 
]

which is just a single byte defining an array of 3 entries. Which does seem overkill for a general COSE builder in our use case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing that @Mr-Leshiy pointed out to me is that an unrestricted builder could be handy in tests.

In other words, a generic builder allows for making a valid COSE that isn't a valid Catalyst Signed Doc.
And being generic and abstracted it could simplify the process of adding slight deviations to the doc structure, and checking these to be rejected by CatalystSignedDocument::decode.

For example, as you mentioned, we want to keep field ordering deterministic. This can be checked trivially if there's some abstraction.

Copy link
Contributor Author

@no30bit no30bit Jun 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't have to be a builder, it could as well be an extension trait, which would allow to encode a type as a protected header. Or at least something that abstracts enough, so that devs don't have to look up the RFC.

For example, this seems to me like a misuse of an encoder. And this would have to be replaced (so could as well be abstracted for both the signature protected_headers and document protected_headers.

The links point to the new PR, where abstractions over minicbor encoder aren't used.

}

encoder.into_writer()
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given we have a fixed set of headers, and we will only be using those, seems overcomplex to maintain a general COSE header builder which already requires the keys and values to be encoded. its just complication for no gain.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree on this. This function exists, because @Mr-Leshiy and I used to want the generic builder, and since these were all immediately encoded and stored in a map, I had to make a concatenating function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the generic builder is deprecated, I'm sure this can be done way simpler and better.

@github-project-automation github-project-automation bot moved this from 🏗 In progress to 🔖 Ready in Catalyst Jun 4, 2025
@Mr-Leshiy
Copy link
Contributor

Done under this PR #353

@Mr-Leshiy Mr-Leshiy closed this Jun 16, 2025
@github-project-automation github-project-automation bot moved this from 🔖 Ready to 🔬 Ready For QA in Catalyst Jun 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do not merge yet PR is not ready to merge yet do not review enhancement New feature or request wip
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Implement true minicbor encoder for catalyst signed documents.
3 participants