Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Add JSON-RPC WalletConnect Support #386

Closed
wants to merge 1 commit into from
Closed

Conversation

jolube
Copy link
Contributor

@jolube jolube commented Jul 31, 2021

Closes: N/A

Description

This builds off of #328.

This PR adds a new cosmos namespace and implements the following methods in accordance with the WalletConnect JSON-RPC Cosmos API:

  • GetAccounts
  • SignDirect — corresponds to SignDoc
  • SignAmino — corresponds to the legacy StdSignDoc

Relevant docs: https://github.com/cosmos/cosmos-sdk/blob/master/docs/core/transactions.md#signing-transactions

Testing can be done by:

curl -X POST --data '{"jsonrpc":"2.0","method":"cosmos_getAccounts","params":[],"id":1}' -H "Content-Type: application/json" http://localhost:8545
curl -X POST --data '{"jsonrpc":"2.0","method":"cosmos_signDirect","params":[{"signerAddress":"eth1gqkt20afxpgaefp2cmy3w802th667j0cap6z98","signDoc":{"chainId":"ethermint-2","accountNumber":"1","authInfoBytes":"0a0a0a0012040a020801180112130a0d0a0575636f736d12043230303010c09a0c","bodyBytes":"0a90010a1c2f636f736d6f732e62616e6b2e763162657461312e4d736753656e6412700a2d636f736d6f7331706b707472653766646b6c366766727a6c65736a6a766878686c63337234676d6d6b38727336122d636f736d6f7331717970717870713971637273737a673270767871367273307a716733797963356c7a763778751a100a0575636f736d120731323334353637"}}],"id":1}' -H "Content-Type: application/json" http://localhost:8545

For contributor use:

  • Targeted PR against correct branch (see CONTRIBUTING.md)
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code follows the module structure standards.
  • Wrote unit and integration tests
  • Updated relevant documentation (docs/) or specification (x/<module>/spec/)
  • Added relevant godoc comments.
  • Added a relevant changelog entry to the Unreleased section in CHANGELOG.md
  • Re-reviewed Files changed in the Github PR explorer

For admin use:

  • Added appropriate labels to PR (ex. WIP, R4R, docs, etc)
  • Reviewers assigned
  • Squashed all commits, uses message "Merge pull request #XYZ: [title]" (coding standards)

@jolube jolube self-assigned this Jul 31, 2021
@github-actions github-actions bot added the C:JSON-RPC JSON-RPC client label Jul 31, 2021
Copy link
Contributor

@khoslaventures khoslaventures left a comment

Choose a reason for hiding this comment

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

Nice, want to write some tests in tests/rpc/cosmos_test.go?

Copy link
Contributor

@fedekunze fedekunze left a comment

Choose a reason for hiding this comment

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

Looks good! Thanks for the contribution. Left some minor comments. Can you add the namespace docs on docs/basics/json_rpc.md too?

@@ -0,0 +1,180 @@
package cosmos
Copy link
Contributor

Choose a reason for hiding this comment

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

nit, I think we should eventually restructure the packages to be rpc/namespaces/ since we are not only including ethereum namespaces

Copy link
Contributor

Choose a reason for hiding this comment

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

can be done in a separate PR though

Copy link
Contributor

Choose a reason for hiding this comment

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

}
for _, info := range list {

addr := sdk.AccAddress(info.GetAddress())
Copy link
Contributor

Choose a reason for hiding this comment

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

fix lint

Comment on lines +88 to +95
type SignDirectRequest struct {
SignerAddress sdk.AccAddress `json:"signerAddress"`
SignDoc SignDocDirect `json:"signDoc"`
}
type SignDirectResponse struct {
Signature string `json:"signature"`
SignDoc SignDocDirect `json:"signDoc"`
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd write comments on top of the structs as they are public

func (api *WalletConnectAPI) convertToTxType(signDoc SignDocDirect) (txtypes.SignDoc, error) {
accountNumber, err := strconv.ParseUint(signDoc.AccountNumber, 10, 64)
if err != nil {
api.logger.Error("failed to parse account number: %s, err: %s", signDoc.AccountNumber, err.Error())
Copy link
Contributor

Choose a reason for hiding this comment

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

wrap below error instead

Suggested change
api.logger.Error("failed to parse account number: %s, err: %s", signDoc.AccountNumber, err.Error())

}

// This method returns a signature for the provided document to be signed
// targetting the requested signer address corresponding to the keypair returned
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// targetting the requested signer address corresponding to the keypair returned
// targeting the requested signer address corresponding to the keypair returned

Comment on lines +129 to +144
type SignDocAmino struct {
AccountNumber string `json:"account_number"`
ChainID string `json:"chain_id"`
Sequence string `json:"sequence"`
Memo string `json:"memo"`
Msgs []sdk.Msg `json:"msgs"`
Fee legacytx.StdFee `json:"fee"`
}
type SignAminoRequest struct {
SignerAddress sdk.AccAddress `json:"signerAddress"`
SignDoc SignDocAmino `json:"signDoc"`
}
type SignAminoResponse struct {
Signature string `json:"signature"`
SignDoc SignDocAmino `json:"signDoc"`
}
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto comments

}

// This method returns a signature for the provided document to be signed
// targetting the requested signer address corresponding to the keypair returned
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// targetting the requested signer address corresponding to the keypair returned
// targeting the requested signer address corresponding to the keypair returned

SignDoc SignDocAmino `json:"signDoc"`
}

// This method returns a signature for the provided document to be signed
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// This method returns a signature for the provided document to be signed
// SignAmino returns a signature for the provided document to be signed

signDocAmino := req.SignDoc
accountNumber, err := strconv.ParseUint(signDocAmino.AccountNumber, 10, 64)
if err != nil {
api.logger.Error("failed to parse account number: %s, err: %s", signDocAmino.AccountNumber, err.Error())
Copy link
Contributor

Choose a reason for hiding this comment

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

wrap error

Suggested change
api.logger.Error("failed to parse account number: %s, err: %s", signDocAmino.AccountNumber, err.Error())

}
seq, err := strconv.ParseUint(signDocAmino.Sequence, 10, 64)
if err != nil {
api.logger.Error("failed to parse blockchain account sequence: %s, err: %s", signDocAmino.AccountNumber, err.Error())
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

@pedrouid
Copy link

pedrouid commented Aug 2, 2021

Excited to see this PR taking place 💪

I'm curious from your POV if the current API (params and result) for these JSON-RPC methods looks good.

These are mostly derived from cosmJS APIs but I would love feedback on what could be changed or improved

For example, I find the mixing of encoding on the params to be odd (utf8 strings and hex strings)

However if this seems to fit most APIs within the Cosmos ecosystem then I have no objections to sticking to the existing spec

@fedekunze
Copy link
Contributor

@pedrouid I think we can get rid of the signdoc field from the response types. Also, the SDK doesn't support bec32 for pubkeys anymore, they just print the string value of the protobuf Any field

@pedrouid
Copy link

pedrouid commented Aug 6, 2021

Do you think there would be cases where wallets would change the signed doc?

Say for example to adjust how much fees are used or which coins are used to pay those fees?

@jolube
Copy link
Contributor Author

jolube commented Aug 11, 2021

Will open a PR later with the conflicts fixed.

@jolube jolube closed this Aug 11, 2021
@fedekunze fedekunze deleted the jolube/cosmos-walletconnect branch August 11, 2021 11:11
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
C:JSON-RPC JSON-RPC client
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants