Skip to content

Commit

Permalink
feat: ADR-005: Support multisig chain link (#708)
Browse files Browse the repository at this point in the history
## Description

Closes: #633 
This PR implements ADR-005.



---

### 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...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/desmos-labs/desmos/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://docs.cosmos.network/v0.44/building-modules/intro.html)
- [x] included the necessary unit and integration [tests](https://github.com/desmos-labs/desmos/blob/master/CONTRIBUTING.md#testing)
- [x] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] 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](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) 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)
  • Loading branch information
dadamu committed Jan 14, 2022
1 parent 61ace0f commit 23cb923
Show file tree
Hide file tree
Showing 43 changed files with 3,957 additions and 346 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: feat
module: x/profiles
pull_request: 708
description: Added support for multisig chain links
backward_compatible: false
date: 2022-01-12T03:49:42.2638125Z
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,12 @@ proto-update-deps:
@mkdir -p $(IBC_TYPES)/core/client/v1
@curl -sSL $(IBC_URL)/core/client/v1/client.proto > $(IBC_TYPES)/core/client/v1/client.proto

@mkdir -p $(COSMOS_TYPES)/tx/signing/v1beta1
@curl -sSL $(COSMOS_URL)/tx/signing/v1beta1/signing.proto > $(COSMOS_TYPES)/tx/signing/v1beta1/signing.proto

@mkdir -p $(COSMOS_TYPES)/crypto/multisig/v1beta1
@curl -sSL $(COSMOS_URL)/crypto/multisig/v1beta1/multisig.proto > $(COSMOS_TYPES)/crypto/multisig/v1beta1/multisig.proto

## Importing of tendermint protobuf definitions currently requires the
## use of `sed` in order to build properly with cosmos-sdk's proto file layout
## (which is the standard Buf.build FILE_LAYOUT)
Expand Down
15 changes: 15 additions & 0 deletions app/desmos/cmd/chainlink/builder/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package builder

import (
"github.com/desmos-labs/desmos/v2/app/desmos/cmd/chainlink/types"
"github.com/desmos-labs/desmos/v2/x/profiles/client/utils"
)

// ChainLinkJSONBuilder allows to build a ChainLinkJSON instance
type ChainLinkJSONBuilder interface {
BuildChainLinkJSON(chain types.Chain) (utils.ChainLinkJSON, error)
}

// ChainLinkJSONBuilderProvider allows to provide the provider ChainLinkJSONBuilder implementation based on whether
// it should create the JSON chain link for single or
type ChainLinkJSONBuilderProvider func(isSingleAccount bool) ChainLinkJSONBuilder
99 changes: 99 additions & 0 deletions app/desmos/cmd/chainlink/builder/multi/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package multi

import (
"encoding/hex"
"fmt"
"io/ioutil"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"

"github.com/desmos-labs/desmos/v2/app"
"github.com/desmos-labs/desmos/v2/app/desmos/cmd/chainlink/getter"
"github.com/desmos-labs/desmos/v2/app/desmos/cmd/chainlink/types"
"github.com/desmos-labs/desmos/v2/x/profiles/client/utils"
profilestypes "github.com/desmos-labs/desmos/v2/x/profiles/types"
)

// AccountChainLinkJSONBuilder implements the ChainLinkJSONBuilder for multi signature accounts
type AccountChainLinkJSONBuilder struct {
getter getter.MultiSignatureAccountReferenceGetter
}

// NewAccountChainLinkJSONBuilder returns a new AccountChainLinkJSONBuilder instance
func NewAccountChainLinkJSONBuilder(getter getter.MultiSignatureAccountReferenceGetter) *AccountChainLinkJSONBuilder {
return &AccountChainLinkJSONBuilder{
getter: getter,
}
}

// BuildChainLinkJSON implements ChainLinkJSONBuilder
func (b *AccountChainLinkJSONBuilder) BuildChainLinkJSON(chain types.Chain) (utils.ChainLinkJSON, error) {
txFilePath, err := b.getter.GetMultiSignedTxFilePath()
if err != nil {
return utils.ChainLinkJSON{}, err
}

signedChainID, err := b.getter.GetSignedChainID()
if err != nil {
return utils.ChainLinkJSON{}, err
}

encodingConfig := app.MakeTestEncodingConfig()
txCfg := encodingConfig.TxConfig

// Read the transaction file
bytes, err := ioutil.ReadFile(txFilePath)
if err != nil {
return utils.ChainLinkJSON{}, err
}

// Parse the transaction
parsedTx, err := txCfg.TxJSONDecoder()(bytes)
if err != nil {
return utils.ChainLinkJSON{}, err
}

// Get the sign mode
signMode := signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON

// Wrap the transaction inside a builder to make it easier to get the signatures
txBuilder, err := txCfg.WrapTxBuilder(parsedTx)
if err != nil {
return utils.ChainLinkJSON{}, err
}

sigs, err := txBuilder.GetTx().GetSignaturesV2()
if err != nil {
return utils.ChainLinkJSON{}, err
}

// Make sure there is only one signature for the multisig account
if len(sigs) != 1 {
return utils.ChainLinkJSON{}, fmt.Errorf("invalid number of signatures")
}

// Re-create the bytes that have been signed in order to produce the signature
signingData := authsigning.SignerData{AccountNumber: 0, Sequence: 0, ChainID: signedChainID}
value, err := txCfg.SignModeHandler().GetSignBytes(signMode, signingData, parsedTx)
if err != nil {
return utils.ChainLinkJSON{}, err
}

addr, err := sdk.Bech32ifyAddressBytes(chain.Prefix, sigs[0].PubKey.Address().Bytes())
if err != nil {
return utils.ChainLinkJSON{}, err
}

sigData, err := profilestypes.SignatureDataFromCosmosSignatureData(sigs[0].Data)
if err != nil {
return utils.ChainLinkJSON{}, err
}

return utils.NewChainLinkJSON(
profilestypes.NewBech32Address(addr, chain.Prefix),
profilestypes.NewProof(sigs[0].PubKey, sigData, hex.EncodeToString(value)),
profilestypes.NewChainConfig(chain.Name),
), err
}
65 changes: 65 additions & 0 deletions app/desmos/cmd/chainlink/builder/single/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package single

import (
"encoding/hex"

"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"

"github.com/desmos-labs/desmos/v2/app/desmos/cmd/chainlink/getter"
"github.com/desmos-labs/desmos/v2/app/desmos/cmd/chainlink/types"
"github.com/desmos-labs/desmos/v2/x/profiles/client/utils"
profilestypes "github.com/desmos-labs/desmos/v2/x/profiles/types"
)

const (
KeyName = "desmos_chain_link_account"
)

// AccountChainLinkJSONBuilder implements the ChainLinkJSONBuilder for single signature accounts
type AccountChainLinkJSONBuilder struct {
getter getter.SingleSignatureAccountReferenceGetter
}

// NewAccountChainLinkJSONBuilder returns a new AccountChainLinkJSONBuilder instance
func NewAccountChainLinkJSONBuilder(getter getter.SingleSignatureAccountReferenceGetter) *AccountChainLinkJSONBuilder {
return &AccountChainLinkJSONBuilder{
getter: getter,
}
}

// BuildChainLinkJSON implements ChainLinkJSONBuilder
func (b *AccountChainLinkJSONBuilder) BuildChainLinkJSON(chain types.Chain) (utils.ChainLinkJSON, error) {
mnemonic, err := b.getter.GetMnemonic()
if err != nil {
return utils.ChainLinkJSON{}, err
}

// Create an in-memory keybase for signing
keyBase := keyring.NewInMemory()
_, err = keyBase.NewAccount(KeyName, mnemonic, "", chain.DerivationPath, hd.Secp256k1)
if err != nil {
return utils.ChainLinkJSON{}, err
}

// Generate the proof signing it with the key
key, _ := keyBase.Key(KeyName)
addr, _ := sdk.Bech32ifyAddressBytes(chain.Prefix, key.GetAddress())
value := []byte(addr)
sig, pubkey, err := keyBase.Sign(KeyName, value)
if err != nil {
return utils.ChainLinkJSON{}, err
}
sigData := &profilestypes.SingleSignatureData{
Mode: signing.SignMode_SIGN_MODE_DIRECT,
Signature: sig,
}

return utils.NewChainLinkJSON(
profilestypes.NewBech32Address(addr, chain.Prefix),
profilestypes.NewProof(pubkey, sigData, hex.EncodeToString(value)),
profilestypes.NewChainConfig(chain.Name),
), nil
}
71 changes: 23 additions & 48 deletions app/desmos/cmd/chainlink/create_json.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
package chainlink

import (
"encoding/hex"
"fmt"
"io/ioutil"

chainlinktypes "github.com/desmos-labs/desmos/v2/app/desmos/cmd/chainlink/types"
"github.com/desmos-labs/desmos/v2/app/desmos/cmd/chainlink/builder"
chainlinktypes "github.com/desmos-labs/desmos/v2/app/desmos/cmd/chainlink/getter"

"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"

"github.com/desmos-labs/desmos/v2/app"
profilescliutils "github.com/desmos-labs/desmos/v2/x/profiles/client/utils"
profilestypes "github.com/desmos-labs/desmos/v2/x/profiles/types"
)

// GetCreateChainLinkJSON returns the command allowing to generate the chain link JSON
// file that is required by the link-chain command
func GetCreateChainLinkJSON(getter chainlinktypes.ChainLinkReferenceGetter) *cobra.Command {
func GetCreateChainLinkJSON(
getter chainlinktypes.ChainLinkReferenceGetter,
provider builder.ChainLinkJSONBuilderProvider,
) *cobra.Command {
return &cobra.Command{
Use: "create-chain-link-json",
Short: "Start an interactive prompt to create a new chain link JSON object",
Expand All @@ -28,11 +26,17 @@ Once you have built the JSON object using this command, you can then run the fol
desmos tx profiles link-chain [/path/to/json/file.json]
--- Single signature accounts ---
Note that this command will ask you the mnemonic that should be used to generate the private key of the address you want to link.
The mnemonic is only used temporarily and never stored anywhere.`,
The mnemonic is only used temporarily and never stored anywhere.
--- Multi signature accounts ---
If you have are using a multi-signature account, you will be required to provide the path to a signed transaction file.
That transaction must be signed as normal, except for the specified "account-number" and "sequence" values which should be both set to 0.
Providing an invalid transaction (either with an account-number or sequence not set to 0, or not signed correctly) will result in a failing linkage later on.
`,
RunE: func(cmd *cobra.Command, args []string) error {
// Get the data
mnemonic, err := getter.GetMnemonic()
isSingleSignatureAccount, err := getter.IsSingleSignatureAccount()
if err != nil {
return err
}
Expand All @@ -42,59 +46,30 @@ The mnemonic is only used temporarily and never stored anywhere.`,
return err
}

filename, err := getter.GetFilename()
chainLinkJSON, err := provider(isSingleSignatureAccount).BuildChainLinkJSON(chain)
if err != nil {
return err
}

// Build che chain link JSON
chainLinkJSON, err := generateChainLinkJSON(mnemonic, chain)
// Marshal the chain link JSON
bz, err := app.MakeTestEncodingConfig().Marshaler.MarshalJSON(&chainLinkJSON)
if err != nil {
return err
}

cdc, _ := app.MakeCodecs()
bz, err := cdc.MarshalJSON(&chainLinkJSON)
// Write the chain link JSON to a file
filename, err := getter.GetFilename()
if err != nil {
return err
}

if filename != "" {
err = ioutil.WriteFile(filename, bz, 0600)
if err != nil {
return err
}
err = ioutil.WriteFile(filename, bz, 0600)
if err != nil {
return err
}

cmd.Println(fmt.Sprintf("Chain link JSON file stored at %s", filename))

return nil
},
}
}

// generateChainLinkJSON returns build a new ChainLinkJSON intance using the provided mnemonic and chain configuration
func generateChainLinkJSON(mnemonic string, chain chainlinktypes.Chain) (profilescliutils.ChainLinkJSON, error) {
// Create an in-memory keybase for signing
keyBase := keyring.NewInMemory()
keyName := "chainlink"
_, err := keyBase.NewAccount(keyName, mnemonic, "", chain.DerivationPath, hd.Secp256k1)
if err != nil {
return profilescliutils.ChainLinkJSON{}, err
}

// Generate the proof signing it with the key
key, _ := keyBase.Key(keyName)
addr, _ := sdk.Bech32ifyAddressBytes(chain.Prefix, key.GetAddress())
value := []byte(addr)
sig, pubkey, err := keyBase.Sign(keyName, value)
if err != nil {
return profilescliutils.ChainLinkJSON{}, err
}

return profilescliutils.NewChainLinkJSON(
profilestypes.NewBech32Address(addr, chain.Prefix),
profilestypes.NewProof(pubkey, hex.EncodeToString(sig), hex.EncodeToString(value)),
profilestypes.NewChainConfig(chain.Name),
), nil
}
Loading

0 comments on commit 23cb923

Please sign in to comment.