Skip to content

chore: provide newtypes for verification key and signature#1111

Merged
Fraser999 merged 1 commit intomainfrom
fraser/crypto-newtypes
May 27, 2024
Merged

chore: provide newtypes for verification key and signature#1111
Fraser999 merged 1 commit intomainfrom
fraser/crypto-newtypes

Conversation

@Fraser999
Copy link
Contributor

Summary

The ed25519-consensus types not already wrapped in newtypes are wrapped in this PR.

Background

This is partly for consistency (we recently provided a newtype for the signing key), partly to avoid having third party types in our core crate's public API, and partly to support memoizing the address of the verification key.

Changes

  • Provided newtypes in the crypto module of astria-core for VerificationKey, Signature and Error.
  • Replaced all usages of the naked ed25519-consensus types with the newtypes.
  • Replaced Address::from_verification_key with VerificationKey::address.

Testing

Provided unit tests for the VerificationKey to ensure the PartialEq, Eq, PartialOrd, Ord and Hash impls are correct given that they're hand-rolled in order to exclude the memoized address field.

Related Issues

Closes #1079.

@Fraser999 Fraser999 requested a review from a team as a code owner May 24, 2024 17:06
@Fraser999 Fraser999 requested a review from noot May 24, 2024 17:06
@github-actions github-actions bot added conductor pertaining to the astria-conductor crate sequencer pertaining to the astria-sequencer crate composer pertaining to composer labels May 24, 2024
fn get_address_pretty(signing_key: &SigningKey) -> String {
let address = Address::from_verification_key(signing_key.verification_key());
let address = *signing_key.verification_key().address();
hex::encode(address.to_vec())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We're hex-encoding here and in a couple of functions above - maybe that should be changed to base64?

Copy link
Contributor

Choose a reason for hiding this comment

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

we need to change addresses to bech32 :/ addresses are annoying cause sometimes cometbft encodes them as hex (like when logging) and other times base64 (like in the json-rpc responses)

/// An Ed25519 verification key.
#[derive(Clone)]
pub struct VerificationKey {
key: Ed25519VerificationKey,
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe impl From<Ed25519VerificationKey> as well?

Copy link
Contributor

Choose a reason for hiding this comment

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

That would make it part of the public API of this crate, but the goal is to avoid that.

@Fraser999 Fraser999 added this pull request to the merge queue May 27, 2024
Merged via the queue into main with commit 6b91329 May 27, 2024
@Fraser999 Fraser999 deleted the fraser/crypto-newtypes branch May 27, 2024 10:28
github-merge-queue bot pushed a commit that referenced this pull request Sep 4, 2024
## Summary
Added a lazily-initialized field `address_bytes` to `VerificationKey`.

## Background
Testing showed that `address_bytes` was called multiple times on a given
`VerificationKey` (up to 11 times in some cases). Each time the key's
bytes were being hashed, so this change ensures that hashing only
happens once for a given verification key instance.

Note that this was implemented previously in #1111 and was then reverted
in #1124. However, when reverted, the manual impls of `PartialEq`, `Eq`,
`PartialOrd`, `Ord` and `Hash` were left as-is, as were the unit tests
for these. Hence this PR doesn't need to make any changes to these trait
impls or tests.

## Changes
- Added `address_bytes: OnceLock<[u8; ADDRESS_LEN]>` to
`VerificiationKey`.

## Testing
No new tests required.  

## Related Issues
Closes #1351.
jbowen93 pushed a commit that referenced this pull request Sep 6, 2024
## Summary
Added a lazily-initialized field `address_bytes` to `VerificationKey`.

## Background
Testing showed that `address_bytes` was called multiple times on a given
`VerificationKey` (up to 11 times in some cases). Each time the key's
bytes were being hashed, so this change ensures that hashing only
happens once for a given verification key instance.

Note that this was implemented previously in #1111 and was then reverted
in #1124. However, when reverted, the manual impls of `PartialEq`, `Eq`,
`PartialOrd`, `Ord` and `Hash` were left as-is, as were the unit tests
for these. Hence this PR doesn't need to make any changes to these trait
impls or tests.

## Changes
- Added `address_bytes: OnceLock<[u8; ADDRESS_LEN]>` to
`VerificiationKey`.

## Testing
No new tests required.  

## Related Issues
Closes #1351.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

composer pertaining to composer conductor pertaining to the astria-conductor crate sequencer pertaining to the astria-sequencer crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide newtype wrappers for ed25519-consensus types

3 participants