Skip to content

identity::Keypair ergonomics improvements #3649

@divagant-martian

Description

@divagant-martian

Motivation

After #3350 a couple of things that were easily achievable with a match statement are convoluted/potentially less performant.

Simple examples:

Accessing the kind of key by value when supporting more than one kind requires clone

Getting the value of the key while supporting more than one key type

match key {
    Secp256k1(pk) => {         
        do_smth_with_secp256k1_value(pk)
    }                                     
    Ed25519(pk) => {           
        do_smth_with_ed25519_value(pk)  
    }
}

Now this requires a clone since into_secp256k1 consumes the value

if let Some(pk) = key.clone().into_secp256k1() {
        do_smth_with_secp256k1_value(pk)
} else if let Some(pk) = key.into_ed25519() {
        do_smth_with_ed25519_value(pk)  
}

Accessing the kind of key by reference

match &key {
    Secp256k1(pk /* A reference */) => {         
        do_smth_with_secp256k1_ref(pk)
    }                                     
    Ed25519(pk /* A reference */ ) => {           
        do_smth_with_ed25519_ref(pk)  
    }
}

Now this requires a clone since into_secp256k1 consumes the value

if let Some(pk) = key.clone().into_secp256k1() {
        do_smth_with_secp256k1_ref(&pk)
} else if let Some(pk) = key.into_ed25519() {
        do_smth_with_ed25519_ref(&pk)  
}

Supporting a new key kind

Before: Enable the feature and compiler would tell of a missing match branch.
Now: Enable feature and dig in the code where the into_keykind() are, to add a new one

Proposed sol

Assuming the code base is going ahead with this deprecation,

for 1: Implement TryFrom/TryInto for the necessary types. Alternative, add the matching try_into_secp256k1 and others. In these, the error type should give back the original key to be used.

for 2: Implement AsRef for the necessary types. Alternative, add the matching as_secp245k1() and others

for 3. Can't see a good solution right now

Are you planning to do it yourself in a pull request?

Maybe.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions