Skip to content

Commit

Permalink
feat: Add AuxTxBuilder (cosmos#10455)
Browse files Browse the repository at this point in the history
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->

## Description

Closes: cosmos#10443 

For creating an intermediary/auxiliary tx (e.g. by the tipper in tipped transactions), using the existing `client.TxBuilder` is awkward. We propose a new client-side builder for this purpose.

API Usage (e.g. how the tipper would programmtically use this):
```go
// Note: there's no need to use clientCtx.TxConfig anymore!

bldr := clienttx.NewAuxTxBuilder()
err := bldr.SetMsgs(msgs...)
bldr.SetAddress("cosmos1...")
bldr.SetMemo(...)
bldr.SetTip(...)
bldr.SetPubKey(...)
err := bldr.SetSignMode(...) // DIRECT_AUX or AMINO, or else error
// ... other setters are available

// Get the bytes to sign.
signBz, err := bldr.GetSignBytes()

// Sign the bz using your favorite method.
sig, err := privKey.sign(signBz)

// Set the signature
bldr.SetSig(sig)

// Get the final auxSignerData to be sent to the fee payer
auxSignerData, err:= bldr.GetAuxSignerData()
```

auxSignerData is a protobuf message, whose JSON reprensentation looks like:

```json
{
  "address": "cosmos1...",
  "mode": "SIGN_MODE_{DIRECT_AUX,LEGACY_AMINO_JSON}",
  "sign_doc": {
    "body_bytes": "{base64 bytes}",
    "public_key": {
      "@type": "cosmos.sepc256k1.PubKey",
      "key": "{base64 bytes}"
    },
    "chain_id": "...",
    "account_number": 24,
    "sequence": 42,
    "tip": {
      "amount": [{ "denom": "uregen", "amount": 1000 }],
      "tipper": "cosmos1..."
    }
  },
  "sig": "{base64 bytes}"
}
```

Then the fee payer would use the TxBuilder to construct the final TX, with a new helper method: `AddAuxSignerData`:

```go
// get auxSignerData from AuxTxBuilder
auxSignerData := ...

txBuilder := txConfig.NewTxBuilder()
err := txBuilder.AddAuxSignerData(auxSignerData)

// Set fee payer data
txBuilder.SetFee()
txBuilder.SetGasLimit()
txBuilder.SetFeePayer()

sigs, err := txBuilder.GetSignaturesV2()
auxSig := sigs[0] // the aux signer's signature

// Set all signer infos (1st round of calling SetSignatures)
txBuilder.SetSignatures(
  auxSig,                   // The aux SignatureV2
  signing.SignatureV2{...}, // The feePayer's SignatureV2
)

// Sign
signBz, err = encCfg.TxConfig.SignModeHandler().GetSignBytes(...)
feepayerSig, err := feepayerPriv.Sign(signBz)

// Set all signatures (2nd round of calling SetSignatures)
txBuilder.SetSignatures(
  auxSig,                   // The aux SignatureV2
  signing.SignatureV2{feepayerSig, ...}, // The feePayer's SignatureV2
)
```

---

### 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](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
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] 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
amaury1093 authored and blewater committed Dec 8, 2021
1 parent 8ca6560 commit 5d1e68a
Show file tree
Hide file tree
Showing 17 changed files with 1,287 additions and 74 deletions.
212 changes: 212 additions & 0 deletions client/tx/aux_builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package tx

import (
"github.com/gogo/protobuf/proto"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
)

// AuxTxBuilder is a client-side builder for creating an AuxSignerData.
type AuxTxBuilder struct {
// msgs is used to store the sdk.Msgs that are added to the
// TxBuilder. It's also added inside body.Messages, because:
// - b.msgs is used for constructing the AMINO sign bz,
// - b.body is used for constructing the DIRECT_AUX sign bz.
msgs []sdk.Msg
body *tx.TxBody
auxSignerData *tx.AuxSignerData
}

// NewAuxTxBuilder creates a new client-side builder for constructing an
// AuxSignerData.
func NewAuxTxBuilder() AuxTxBuilder {
return AuxTxBuilder{}
}

// SetAddress sets the aux signer's bech32 address.
func (b *AuxTxBuilder) SetAddress(addr string) {
b.checkEmptyFields()

b.auxSignerData.Address = addr
}

// SetMemo sets a memo in the tx.
func (b *AuxTxBuilder) SetMemo(memo string) {
b.checkEmptyFields()

b.body.Memo = memo
}

// SetTimeoutHeight sets a timeout height in the tx.
func (b *AuxTxBuilder) SetTimeoutHeight(height uint64) {
b.checkEmptyFields()

b.body.TimeoutHeight = height
}

// SetMsgs sets an array of Msgs in the tx.
func (b *AuxTxBuilder) SetMsgs(msgs ...sdk.Msg) error {
anys := make([]*codectypes.Any, len(msgs))
for i, msg := range msgs {
var err error
anys[i], err = codectypes.NewAnyWithValue(msg)
if err != nil {
return err
}
}

b.checkEmptyFields()

b.msgs = msgs
b.body.Messages = anys

return nil
}

// SetAccountNumber sets the aux signer's account number in the AuxSignerData.
func (b *AuxTxBuilder) SetAccountNumber(accNum uint64) {
b.checkEmptyFields()

b.auxSignerData.SignDoc.AccountNumber = accNum
}

// SetChainID sets the chain id in the AuxSignerData.
func (b *AuxTxBuilder) SetChainID(chainID string) {
b.checkEmptyFields()

b.auxSignerData.SignDoc.ChainId = chainID
}

// SetSequence sets the aux signer's sequence in the AuxSignerData.
func (b *AuxTxBuilder) SetSequence(accSeq uint64) {
b.checkEmptyFields()

b.auxSignerData.SignDoc.Sequence = accSeq
}

// SetPubKey sets the aux signer's pubkey in the AuxSignerData.
func (b *AuxTxBuilder) SetPubKey(pk cryptotypes.PubKey) error {
any, err := codectypes.NewAnyWithValue(pk)
if err != nil {
return err
}

b.checkEmptyFields()

b.auxSignerData.SignDoc.PublicKey = any

return nil
}

// SetSignMode sets the aux signer's sign mode. Allowed sign modes are
// DIRECT_AUX and LEGACY_AMINO_JSON.
func (b *AuxTxBuilder) SetSignMode(mode signing.SignMode) error {
switch mode {
case signing.SignMode_SIGN_MODE_DIRECT_AUX, signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON:
default:
return sdkerrors.ErrInvalidRequest.Wrapf("AuxTxBuilder can only sign with %s or %s", signing.SignMode_SIGN_MODE_DIRECT_AUX, signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON)
}

b.auxSignerData.Mode = mode

return nil
}

// SetTip sets an optional tip.
func (b *AuxTxBuilder) SetTip(tip *tx.Tip) {
b.checkEmptyFields()

b.auxSignerData.SignDoc.Tip = tip
}

// SetSignature sets the aux signer's signature.
func (b *AuxTxBuilder) SetSignature(sig []byte) {
b.checkEmptyFields()

b.auxSignerData.Sig = sig
}

// GetSignBytes returns the builder's sign bytes.
func (b *AuxTxBuilder) GetSignBytes() ([]byte, error) {
auxTx := b.auxSignerData
if auxTx == nil {
return nil, sdkerrors.ErrLogic.Wrap("aux tx is nil, call setters on AuxTxBuilder first")
}

body := b.body
if body == nil {
return nil, sdkerrors.ErrLogic.Wrap("tx body is nil, call setters on AuxTxBuilder first")
}

sd := auxTx.SignDoc
if sd == nil {
return nil, sdkerrors.ErrLogic.Wrap("sign doc is nil, call setters on AuxTxBuilder first")
}

bodyBz, err := proto.Marshal(body)
if err != nil {
return nil, err
}

sd.BodyBytes = bodyBz

if err := b.auxSignerData.SignDoc.ValidateBasic(); err != nil {
return nil, err
}

var signBz []byte
switch b.auxSignerData.Mode {
case signing.SignMode_SIGN_MODE_DIRECT_AUX:
{
signBz, err = proto.Marshal(b.auxSignerData.SignDoc)
if err != nil {
return nil, err
}
}
case signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON:
{
signBz = legacytx.StdSignBytes(
b.auxSignerData.SignDoc.ChainId, b.auxSignerData.SignDoc.AccountNumber,
b.auxSignerData.SignDoc.Sequence, b.body.TimeoutHeight,
// Aux signer never signs over fee.
// For LEGACY_AMINO_JSON, we use the convention to sign
// over empty fees.
// ref: https://github.com/cosmos/cosmos-sdk/pull/10348
legacytx.StdFee{},
b.msgs, b.body.Memo, b.auxSignerData.SignDoc.Tip,
)
}
default:
return nil, sdkerrors.ErrInvalidRequest.Wrapf("got unknown sign mode %s", b.auxSignerData.Mode)
}

return signBz, nil
}

// GetAuxSignerData returns the builder's AuxSignerData.
func (b *AuxTxBuilder) GetAuxSignerData() (tx.AuxSignerData, error) {
if err := b.auxSignerData.ValidateBasic(); err != nil {
return tx.AuxSignerData{}, err
}

return *b.auxSignerData, nil
}

func (b *AuxTxBuilder) checkEmptyFields() {
if b.body == nil {
b.body = &tx.TxBody{}
}

if b.auxSignerData == nil {
b.auxSignerData = &tx.AuxSignerData{}
if b.auxSignerData.SignDoc == nil {
b.auxSignerData.SignDoc = &tx.SignDocDirectAux{}
}
}
}
Loading

0 comments on commit 5d1e68a

Please sign in to comment.