-
Notifications
You must be signed in to change notification settings - Fork 9
feat(cat-voices): encode/decode cose documents #1408
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
e367cc3
feat: addd melos build_runner_repository to justfile
dt-iohk 0080f72
feat: update cose sign to support multiple different signatures and algs
dt-iohk 1dcd5c1
feat: add document manager that handles signed documents (COSE_SIGN)
dt-iohk b5d0eea
fix: kid should be encoded as Uint8List, not as string
dt-iohk e992717
style: typo
dt-iohk 8886bda
fix: collection equality
dt-iohk bfc83fc
chore: review feedback
dt-iohk 10ca574
feat: add content type
dt-iohk 27e1a0d
chore: rename document to binary document, export document manager
dt-iohk f2f1ba9
chore: copyWith fix
dt-iohk 878926d
fix: json content type
dt-iohk 4fac543
fix: put default alg in top-level headers if all signatures use the s…
dt-iohk 46a91e0
chore: refactor reference uuid to give it a more meaningful name
dt-iohk 5e25bcf
chore: get rid of equatable from SignedDocument interface
dt-iohk 81c548d
fix: do not put alg in top-level protected headers for COSE_SIGN
dt-iohk cd1e566
Merge branch 'mve3' into feat/encode_deocde_cose_documents
dt-iohk b3b6fc2
chore: update field name
dt-iohk 8292dbe
Merge branch 'mve3' into feat/encode_deocde_cose_documents
dt-iohk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...st_voices/packages/internal/catalyst_voices_shared/lib/src/document/document_manager.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import 'dart:typed_data'; | ||
|
|
||
| import 'package:catalyst_compression/catalyst_compression.dart'; | ||
| import 'package:catalyst_cose/catalyst_cose.dart'; | ||
| import 'package:catalyst_key_derivation/catalyst_key_derivation.dart'; | ||
| import 'package:cbor/cbor.dart'; | ||
| import 'package:equatable/equatable.dart'; | ||
|
|
||
| part 'document_manager_impl.dart'; | ||
|
|
||
| /// Parses the document from the bytes obtained from [BinaryDocument.toBytes]. | ||
| /// | ||
| /// Usually this would convert the [bytes] into a [String], | ||
| /// decode a [String] into a json and then parse the data class | ||
| /// from the json representation. | ||
| typedef DocumentParser<T extends BinaryDocument> = T Function(Uint8List bytes); | ||
|
|
||
| /// Manages the [SignedDocument]s. | ||
| abstract interface class DocumentManager { | ||
| /// The default constructor for the [DocumentManager], | ||
| /// provides the default implementation of the interface. | ||
| const factory DocumentManager() = _DocumentManagerImpl; | ||
|
|
||
| /// Parses the document from the [bytes] representation. | ||
| /// | ||
| /// The [parser] must be able to parse the document | ||
| /// from the bytes produced by [BinaryDocument.toBytes]. | ||
| /// | ||
| /// The implementation of this method must be able to understand the [bytes] | ||
| /// that are obtained from the [SignedDocument.toBytes] method. | ||
| Future<SignedDocument<T>> parseDocument<T extends BinaryDocument>( | ||
| Uint8List bytes, { | ||
| required DocumentParser<T> parser, | ||
| }); | ||
|
|
||
| /// Signs the [document] with a single [privateKey]. | ||
| /// | ||
| /// The [publicKey] will be added as metadata in the signed document | ||
| /// so that it's easier to identify who signed it. | ||
| Future<SignedDocument<T>> signDocument<T extends BinaryDocument>( | ||
| T document, { | ||
| required Uint8List publicKey, | ||
| required Uint8List privateKey, | ||
| }); | ||
| } | ||
|
|
||
| /// Represents an abstract document that is protected | ||
| /// with cryptographic signature. | ||
| /// | ||
| /// The [document] payload can be UTF-8 encoded bytes, a binary data | ||
| /// or anything else that can be represented in binary format. | ||
| abstract interface class SignedDocument<T extends BinaryDocument> { | ||
| /// The default constructor for the [SignedDocument]. | ||
| const SignedDocument(); | ||
|
|
||
| /// A getter that returns a parsed document. | ||
| T get document; | ||
|
|
||
| /// Verifies if the [document] has been signed by a private key | ||
| /// that belongs to the given [publicKey]. | ||
| Future<bool> verifySignature(Uint8List publicKey); | ||
|
|
||
| /// Converts the document into binary representation. | ||
| Uint8List toBytes(); | ||
| } | ||
|
|
||
| /// Represents an abstract document that can be represented in binary format. | ||
| // ignore: one_member_abstracts | ||
| abstract interface class BinaryDocument { | ||
| /// Converts the document into a binary representation. | ||
| /// | ||
| /// See [DocumentParser]. | ||
| Uint8List toBytes(); | ||
|
|
||
| /// Returns the document content type. | ||
| DocumentContentType get contentType; | ||
| } | ||
|
|
||
| /// Defines the content type of the [BinaryDocument]. | ||
| enum DocumentContentType { | ||
| /// The document's content type is JSON. | ||
| json, | ||
| } |
129 changes: 129 additions & 0 deletions
129
...ices/packages/internal/catalyst_voices_shared/lib/src/document/document_manager_impl.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| part of 'document_manager.dart'; | ||
|
|
||
| const _brotliEncoding = StringValue(CoseValues.brotliContentEncoding); | ||
|
|
||
| final class _DocumentManagerImpl implements DocumentManager { | ||
| const _DocumentManagerImpl(); | ||
|
|
||
| @override | ||
| Future<SignedDocument<T>> parseDocument<T extends BinaryDocument>( | ||
| Uint8List bytes, { | ||
| required DocumentParser<T> parser, | ||
| }) async { | ||
| final coseSign = CoseSign.fromCbor(cbor.decode(bytes)); | ||
| final payload = await _brotliDecompressPayload(coseSign); | ||
| final document = parser(payload); | ||
| return _CoseSignedDocument(coseSign, document); | ||
| } | ||
|
|
||
| @override | ||
| Future<SignedDocument<T>> signDocument<T extends BinaryDocument>( | ||
| T document, { | ||
| required Uint8List publicKey, | ||
| required Uint8List privateKey, | ||
| }) async { | ||
| final compressedPayload = await _brotliCompressPayload(document.toBytes()); | ||
|
|
||
| final coseSign = await CoseSign.sign( | ||
| protectedHeaders: CoseHeaders.protected( | ||
| contentEncoding: _brotliEncoding, | ||
| contentType: document.contentType.asCose, | ||
| ), | ||
| unprotectedHeaders: const CoseHeaders.unprotected(), | ||
| payload: compressedPayload, | ||
| signers: [_Bip32Ed25519XSigner(publicKey, privateKey)], | ||
| ); | ||
|
|
||
| return _CoseSignedDocument(coseSign, document); | ||
| } | ||
|
|
||
| Future<Uint8List> _brotliCompressPayload(Uint8List payload) async { | ||
| final compressor = CatalystCompression.instance.brotli; | ||
| final compressed = await compressor.compress(payload); | ||
| return Uint8List.fromList(compressed); | ||
| } | ||
|
|
||
| Future<Uint8List> _brotliDecompressPayload(CoseSign coseSign) async { | ||
| if (coseSign.protectedHeaders.contentEncoding == _brotliEncoding) { | ||
| final compressor = CatalystCompression.instance.brotli; | ||
| final decompressed = await compressor.decompress(coseSign.payload); | ||
| return Uint8List.fromList(decompressed); | ||
| } else { | ||
| return coseSign.payload; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| final class _CoseSignedDocument<T extends BinaryDocument> | ||
| extends SignedDocument<T> with EquatableMixin { | ||
| final CoseSign _coseSign; | ||
|
|
||
| @override | ||
| final T document; | ||
|
|
||
| const _CoseSignedDocument(this._coseSign, this.document); | ||
|
|
||
| @override | ||
| Future<bool> verifySignature(Uint8List publicKey) async { | ||
| return _coseSign.verify( | ||
| verifier: _Bip32Ed25519XVerifier(publicKey), | ||
| ); | ||
| } | ||
|
|
||
| @override | ||
| Uint8List toBytes() { | ||
| final bytes = cbor.encode(_coseSign.toCbor()); | ||
| return Uint8List.fromList(bytes); | ||
| } | ||
|
|
||
| @override | ||
| List<Object?> get props => [_coseSign, document]; | ||
| } | ||
|
|
||
| extension _CoseDocumentContentType on DocumentContentType { | ||
| /// Maps the [DocumentContentType] into COSE representation. | ||
| StringOrInt get asCose { | ||
| switch (this) { | ||
| case DocumentContentType.json: | ||
| return const IntValue(CoseValues.jsonContentType); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| final class _Bip32Ed25519XSigner implements CatalystCoseSigner { | ||
| final Uint8List publicKey; | ||
| final Uint8List privateKey; | ||
|
|
||
| const _Bip32Ed25519XSigner(this.publicKey, this.privateKey); | ||
|
|
||
| @override | ||
| StringOrInt? get alg => const IntValue(CoseValues.eddsaAlg); | ||
|
|
||
| @override | ||
| Future<Uint8List?> get kid async => publicKey; | ||
|
|
||
| @override | ||
| Future<Uint8List> sign(Uint8List data) async { | ||
| final pk = Bip32Ed25519XPrivateKeyFactory.instance.fromBytes(privateKey); | ||
| final signature = await pk.sign(data); | ||
| return Uint8List.fromList(signature.bytes); | ||
| } | ||
| } | ||
|
|
||
| final class _Bip32Ed25519XVerifier implements CatalystCoseVerifier { | ||
| final Uint8List publicKey; | ||
|
|
||
| const _Bip32Ed25519XVerifier(this.publicKey); | ||
|
|
||
| @override | ||
| Future<Uint8List?> get kid async => publicKey; | ||
|
|
||
| @override | ||
| Future<bool> verify(Uint8List data, Uint8List signature) async { | ||
| final pk = Bip32Ed25519XPublicKeyFactory.instance.fromBytes(publicKey); | ||
| return pk.verify( | ||
| data, | ||
| signature: Bip32Ed25519XSignatureFactory.instance.fromBytes(signature), | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.