Skip to content
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

feat(cat-voices): encode/decode cose documents #1408

Merged
merged 18 commits into from
Dec 24, 2024

Conversation

dtscalac
Copy link
Contributor

@dtscalac dtscalac commented Dec 17, 2024

Description

Adds utils around encoding/decoding/signing COSE_SIGN documents for proposals. The Document is not limited to a proposal, it can handle anything that can be represented as a binary list: json, images or any other format that can be encoded/decoded as bytes.

Limitations:

  • The DocumentManager assumes ED25519 signature algorithm and won't work with different ones unless we decide we want to support others.
  • The DocumentManager always compresses the document using brotli compression but it can receive both compressed or uncompressed document depending on the "content encoding" header from COSE_SIGN.
  • Not supporting yet document versions or template references, do we need it now?

Related Issue(s)

Closes #1400

Description of Changes

How to use the util:

  1. Define a document
final class _JsonDocument extends Equatable implements BinaryDocument {
  final String title;

  const _JsonDocument(this.title);

  factory _JsonDocument.fromBytes(Uint8List bytes) {
    final string = utf8.decode(bytes);
    final map = json.decode(string);
    return _JsonDocument.fromJson(map as Map<String, dynamic>);
  }

  factory _JsonDocument.fromJson(Map<String, dynamic> map) {
    return _JsonDocument(map['title'] as String);
  }

  Map<String, dynamic> toJson() {
    return {'title': title};
  }

  @override
  Uint8List toBytes() {
    final jsonString = json.encode(toJson());
    return utf8.encode(jsonString);
  }

  @override
  DocumentContentType get contentType => DocumentContentType.json;

  @override
  List<Object?> get props => [title];
}
  1. Sign it, encode/decode and verify it:
    test(
        'signDocument creates a signed document '
        'that can be converted from/to bytes', () async {
      const document = _JsonDocument('title');

      final signedDocument = await documentManager.signDocument(
        document,
        publicKey: _publicKey,
        privateKey: _privateKey,
      );

      expect(signedDocument.document, equals(document));

      final isVerified = await signedDocument.verifySignature(_publicKey);
      expect(isVerified, isTrue);

      final signedDocumentBytes = signedDocument.toBytes();
      final parsedDocument = await documentManager.parseDocument(
        signedDocumentBytes,
        parser: _JsonDocument.fromBytes,
      );

      expect(parsedDocument, equals(signedDocument));
    });

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

@dtscalac dtscalac added review me PR is ready for review dart Pull requests that update Dart code labels Dec 17, 2024
@dtscalac dtscalac added this to the M4: Voting & Delegation milestone Dec 17, 2024
@dtscalac dtscalac self-assigned this Dec 17, 2024
Copy link
Contributor

Test Report | ${\color{lightgreen}Pass: 367/367}$ | ${\color{red}Fail: 0/367}$ |

@dtscalac dtscalac changed the title feat(cat-voices): encode deocde cose documents feat(cat-voices): encode/decode cose documents Dec 17, 2024
Copy link
Contributor

Test Report | ${\color{lightgreen}Pass: 366/367}$ | ${\color{red}Fail: 1/367}$ |

Copy link
Contributor

Test Report | ${\color{lightgreen}Pass: 366/367}$ | ${\color{red}Fail: 1/367}$ |

Copy link
Contributor

Test Report | ${\color{lightgreen}Pass: 367/367}$ | ${\color{red}Fail: 0/367}$ |

@stevenj stevenj requested a review from saibatizoku December 17, 2024 14:57
@stevenj
Copy link
Collaborator

stevenj commented Dec 17, 2024

@dtscalac I added @saibatizoku to also do a review because hes doing the backend side of this.
Just to double check alignment

@stevenj
Copy link
Collaborator

stevenj commented Dec 18, 2024

@dtscalac further to the comment above about getting @saibatizoku to do a review. He has a PR in catalyst-libs for a decoder library. see: input-output-hk/catalyst-libs#101

Work with @saibatizoku by generating some signed docs using your code here so he can validate them against his decoder.
Ideally that should work, but if not we need to find out what side isn't working properly before merging. Easier to test it now together as both sides of the process are ready of review together.

Copy link
Contributor

Test Report | ${\color{lightgreen}Pass: 367/367}$ | ${\color{red}Fail: 0/367}$ |

Copy link
Contributor

Test Report | ${\color{lightgreen}Pass: 367/367}$ | ${\color{red}Fail: 0/367}$ |

Copy link
Contributor

Test Report | ${\color{lightgreen}Pass: 367/367}$ | ${\color{red}Fail: 0/367}$ |

Copy link
Contributor

Test Report | ${\color{lightgreen}Pass: 367/367}$ | ${\color{red}Fail: 0/367}$ |

Copy link
Contributor

Test Report | ${\color{lightgreen}Pass: 367/367}$ | ${\color{red}Fail: 0/367}$ |

Copy link
Contributor

Test Report | ${\color{lightgreen}Pass: 367/367}$ | ${\color{red}Fail: 0/367}$ |

Copy link
Contributor

Test Report | ${\color{lightgreen}Pass: 367/367}$ | ${\color{red}Fail: 0/367}$ |

Copy link
Contributor

Test Report | ${\color{lightgreen}Pass: 367/367}$ | ${\color{red}Fail: 0/367}$ |

# Conflicts:
#	catalyst_voices/packages/internal/catalyst_voices_shared/lib/src/catalyst_voices_shared.dart
Copy link
Contributor

Test Report | ${\color{lightgreen}Pass: 370/370}$ | ${\color{red}Fail: 0/370}$ |

Copy link
Contributor

@saibatizoku saibatizoku left a comment

Choose a reason for hiding this comment

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

Looks good to me.

@dtscalac dtscalac merged commit 14c01e4 into mve3 Dec 24, 2024
40 checks passed
@dtscalac dtscalac deleted the feat/encode_deocde_cose_documents branch December 24, 2024 08:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dart Pull requests that update Dart code review me PR is ready for review
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants