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

docs(adr): ADR-007: Address generation of chain link proof #727

Merged
merged 15 commits into from
Sep 1, 2022

Conversation

dadamu
Copy link
Contributor

@dadamu dadamu commented Jan 24, 2022

Description

References: #724


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

@github-actions github-actions bot added the kind/adr An issue or PR relating to an architectural decision record label Jan 24, 2022
@dadamu dadamu marked this pull request as ready for review January 24, 2022 10:10
@dadamu dadamu requested review from leobragaz and RiccardoM January 24, 2022 10:11
Comment on lines 28 to 41
We propose to apply `Strategy` design pattern by a new interface to handle address generation from public key, which includes `GenerateAddress` function.
```go
type AddressGenerationStrategy interface{
GenerateAddress(pk PubKey) []byte
}
```

Subsequently, the `AddressData` instance like `Bech32Address` can include the any value of the strategy instance which implements `AddressGenerationStrategy` as a property.
```proto
message Bech32Address{
...
google.protobuf.Any strategy = 3 [(cosmos_proto.accepts_interface) = "AddressGenerationStrategy"];
}
```
Copy link
Contributor

@RiccardoM RiccardoM Apr 5, 2022

Choose a reason for hiding this comment

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

Honestly I'm not a big fan if this approach. We should use an interface (and the related serialization strategy - Any) only when there are different implementations that need different data to be created. This was true for AddressData:

  • Bech32AddressData requires a prefix and a value
  • HexAddressData requires only a value
  • Base58AddressData requires only a value

In this case, each address generation strategy does not require anything else aside from the public key, which is passed to the GenerateAddress method itself. For this reason, I think the best alternative is to simply add a new method to the AddressData interface instead:

// AddressData is an interface representing a generic external chain address
type AddressData interface {
    proto.Message

    // Validate checks the validity of the AddressData
    Validate() error

    // GetValue returns the address value
    GetValue() string

    // GetAddressGenerationStrategy returns the address generation strategy to be used
    GetAddressGenerationStrategy() AddressGenerationStrategy

    // VerifyAddress verifies that the given address is associated with this address data
    VerifyAddress(address []byte) (bool, error)
}

As you can see, I've also changed VerifyPubKey to VerifyAddress. Now the flow should become:

  1. generate the proper address using the associated strategy;
  2. verify that the address matches the address data value.
address, err := GenerateAddress(b.GetAddressGenerationStrategy(), key)
if err != nil {
    return err
}

valid, err := addressData.VerifyAddress(bz)
if err != nil {
    return err
}

The GenerateAddress is going to be a public function that simply takes an AddressGenerationStrategy, a public key and generates the address properly:

func GenerateAddress(strategy AddressGenerationStrategy, pubKey cryptotypes.PubKey) (address []byte, err error) {
    switch strategy { 
        case StrategyRipemd160:
            return generateRipemd160Address(pubKey)
        // ...
    }

This implementation will also require to change all the AddressData instances constructors to allow passing an (optional) address generation strategy

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll wait for this to be included in the ADR and then review it. Notify me when ready @dadamu.

## Abstract

Currently, Desmos chain link supports `Bech32`, `Base58` and `Hex` address formats. Besides, each of them only couples one address generation algorithms.
Since Desmos will support more addresses from other chains, which might apply the supported address format but with the unsupported algorithm, we SHOULD split the address algorithm from `AddressData` instances.
Copy link
Contributor

Choose a reason for hiding this comment

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

which might apply the supported address format but with the unsupported algorithm

I was wondering: do we have such examples?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

elrond address is a bech32 format, but it encode public key into bech32 directly instead of ripemd160(sha256(pubKey)) as cosmos ecosystem.

@RiccardoM
Copy link
Contributor

@dadamu @bragaz I've completely reviewed this ADR as I think there might be an easier way to go to support different algorithm strategies and encodings.


// Address contains the data of an external address
message Address {
// Hex-encoded value of the address
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of requesting the developers to put the hex-encoded address bytes here, we can also allow them to put the already-encoded address using the specified encoding algorithm. Then, inside the Validate check we simply:

  1. generate the addresss bytes from the public key;
  2. encode the address using the provided encoding method;
  3. check if the resulting encoded address is equals to the provided value

option (cosmos_proto.implements_interface) = "AddressEncoding";

// Prefix to be used
string prefix = 1;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we should provide the prefix field since Polkadot base58 address format has prefix.
References:
https://wiki.polkadot.network/docs/learn-accounts#address-format

Copy link
Contributor

Choose a reason for hiding this comment

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

I've nothing against this, since it brings more support for other chains.
Thoughts? @RiccardoM

Copy link
Contributor

Choose a reason for hiding this comment

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

Nothing against, just made it optional though

@dadamu dadamu requested review from leobragaz and RiccardoM August 10, 2022 08:02
Signed-off-by: Riccardo Montagnin <[email protected]>
@RiccardoM RiccardoM requested a review from manu0466 September 1, 2022 09:23
Signed-off-by: Riccardo Montagnin <[email protected]>
@RiccardoM RiccardoM added the automerge Automatically merge PR once all prerequisites pass label Sep 1, 2022
@mergify mergify bot merged commit d968175 into master Sep 1, 2022
@mergify mergify bot deleted the paul/adr-007-chain-link-proof-address-generation branch September 1, 2022 14:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Automatically merge PR once all prerequisites pass kind/adr An issue or PR relating to an architectural decision record
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants