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

Improve didcore VerificationMethod in-memory representation #1163

Open
Patrik-Stas opened this issue Mar 22, 2024 · 0 comments
Open

Improve didcore VerificationMethod in-memory representation #1163

Patrik-Stas opened this issue Mar 22, 2024 · 0 comments
Assignees
Labels
good first issue Good for newcomers

Comments

@Patrik-Stas
Copy link
Contributor

The current implementation of VerificationMethod looks as follow:

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, TypedBuilder)]
#[serde(rename_all = "camelCase")]
pub struct VerificationMethod {
    id: DidUrl,
    controller: Did,
    #[serde(rename = "type")]
    verification_method_type: VerificationMethodType,
    #[serde(flatten)]
    public_key: PublicKeyField,
}

and

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(untagged)]
#[serde(deny_unknown_fields)]
pub enum PublicKeyField {
    #[serde(rename_all = "camelCase")]
    Multibase { public_key_multibase: String },
    #[serde(rename_all = "camelCase")]
    Jwk { public_key_jwk: JsonWebKey },
    #[serde(rename_all = "camelCase")]
    Base58 { public_key_base58: String },
    #[serde(rename_all = "camelCase")]
    Base64 { public_key_base64: String },
    #[serde(rename_all = "camelCase")]
    Hex { public_key_hex: String },
    #[serde(rename_all = "camelCase")]
    Pem { public_key_pem: String },
    #[serde(rename_all = "camelCase")]
    Pgp { public_key_pgp: String },
}

The current internal representation VerificationMethod maps 1:1 with how Verification Method object is described. This makes it de/serialization simple, given that it default serde de/serialization is sufficient. However, it's not data model one would naturally come up if this was designed if de/serialization wasn't concern, for following reasons:

  • it's doesn't follow Law of Demeter - encoded representation is concern of de/serialization, in-memory encoding serves here no purpose
  • it's not efficient - whenever we want to read raw key value from a Verification Method, we need to re-decode they key value
  • deserialization of VerificationMethod does not provide guarantee as to whether the key is correctly encoded - it only guarantees the value is a String. Decoding is indirectly verifier upon upon an attempt to read raw key value from public_key

That being said, we likely want to keep attached information about "how should the raw key be encoded" upon serialization. Upon deserialization, we can set the value based on key encoding in the input.

Addressing this will be mainly matter of changes to VerificationMethod struct:

  • getting rid of PublicKeyField struct in favor of storing raw bytes
  • introducing flag signalling encoding to be used upon serialization
  • customizing process of serde de/serialization
@Patrik-Stas Patrik-Stas added the good first issue Good for newcomers label Mar 22, 2024
@Patrik-Stas Patrik-Stas changed the title Enhance didcore VerificationMethod in-memory representation Improve didcore VerificationMethod in-memory representation Mar 22, 2024
@xprazak2 xprazak2 self-assigned this Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants