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

feat: add support for EIP712 and eth address standard #2

Merged
merged 7 commits into from
Dec 14, 2022
Merged

Conversation

pythonberg1997
Copy link
Contributor

@pythonberg1997 pythonberg1997 commented Nov 7, 2022

Description

Add support for EIP712 and eth address standard

Rationale

In order that users could sign BFS transactions by a ethereum wallet like MetaMask directly, we adopt eth's secp256k1 and address rules as our keys algorithm. Besides, adding EIP712 support will make it more user friendly.

Example

N/A

Changes

Notable changes:

  • add cosmos Address implement, ETHAddress, which is compatible of BSC address
  • add EIP712 related support(sign, verify)
  • add secp256k1 algorithm support for key management

Reference

https://github.com/evmos/evmos/blob/main/app/ante/ante.go
https://github.com/evmos/evmos/blob/main/app/ante/handler_options.go
https://github.com/evmos/ethermint/blob/main/app/ante/eip712.go

@pythonberg1997 pythonberg1997 changed the title [WIP]Add support for metamask feat: Add support for metamask Nov 8, 2022
@pythonberg1997 pythonberg1997 changed the title feat: Add support for metamask feat: add support for EIP712 and eth address standard Nov 8, 2022
@unclezoro unclezoro changed the base branch from master to develop November 8, 2022 07:22
@unclezoro unclezoro marked this pull request as draft November 8, 2022 07:22
@unclezoro unclezoro added r4r and removed wip labels Nov 10, 2022
@unclezoro unclezoro marked this pull request as ready for review November 10, 2022 12:24
Comment on lines 260 to 264
// VerifySignature of ethsecp256k1 accepts 64 byte signature [R||S]
// WARNING! Under NO CIRCUMSTANCES try to use pubKey.VerifySignature there
if !secp256k1.VerifySignature(pubKey.Bytes(), sigHash, feePayerSig[:len(feePayerSig)-1]) {
return sdkerrors.Wrap(sdkerrors.ErrorInvalidSigner, "unable to verify signer signature of EIP712 typed data")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO, this is redundant. The effect is exactly the same with 236 - 250.

FeePayer: feePayer,
}

typedData, err := eip712.WrapTxToTypedData(ethermintCodec, extOpt.TypedDataChainID, msgs[0], txBytes, feeDelegation)
Copy link
Contributor

@owen-reorg owen-reorg Nov 14, 2022

Choose a reason for hiding this comment

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

msgs[0] here implied that we can only have 1 msg, but there is no associated check previously.
Maybe we should change

		if len(msgs) == 0 {
			return sdkerrors.Wrap(sdkerrors.ErrNoSignatures, "tx doesn't contain any msgs to verify signature")
		}

to

		if len(msgs) !=  1

Copy link
Contributor

Choose a reason for hiding this comment

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

Or is it possible to make the TypedData support multiple msgs in one tx?

@owen-reorg
Copy link
Contributor

Does the bfscli still work with the new signature mode? It might be necessary for internal quick test.

We may need an e2e test to demonstrate how to sign with Metamask.

Links might be useful:

app/ante/ante.go Outdated
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
)

// NewAnteHandler returns an ante handler responsible for attempting to route a
Copy link
Contributor

Choose a reason for hiding this comment

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

A bit confuse about this PR. If we use Cosmos CLI to structure and send transactions, it will choose the signature method according to signMode. Just like the below code in cosmos-sdk.

func makeAuxSignerData(clientCtx client.Context, f Factory, msgs ...sdk.Msg) (tx.AuxSignerData, error) {
        // ....
	err = b.SetSignMode(f.SignMode())
	if err != nil {
		return tx.AuxSignerData{}, err
	}
        // ...
	sig, _, err := clientCtx.Keyring.Sign(name, signBz)
	if err != nil {
		return tx.AuxSignerData{}, err
	}
	b.SetSignature(sig)

	return b.GetAuxSignerData()
}

enum SignMode {
  // ...
  // Since: cosmos-sdk 0.45.2
  SIGN_MODE_EIP_191 = 191;
}

Are we considering adding a SigMode to support EIP712? if not, we can't use the Cosmos CLI.

app/ante/reject_msgs.go Outdated Show resolved Hide resolved
app/app.go Outdated Show resolved Hide resolved
app/ante/single_msg.go Outdated Show resolved Hide resolved
app/ante/single_msg.go Outdated Show resolved Hide resolved
app/ante/ante.go Outdated Show resolved Hide resolved
app/ante/single_msg.go Outdated Show resolved Hide resolved
app/ante/utils_test.go Outdated Show resolved Hide resolved
@unclezoro unclezoro merged commit f0f0ae5 into develop Dec 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants