2020-07-06 10:03:45 -07:00
|
|
|
package tx
|
|
|
|
|
|
|
|
import (
|
2020-07-24 15:24:57 -07:00
|
|
|
"fmt"
|
|
|
|
|
2020-08-03 12:47:25 -07:00
|
|
|
signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing"
|
|
|
|
|
2020-07-29 15:33:42 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
|
2020-07-06 10:03:45 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/signing"
|
|
|
|
)
|
|
|
|
|
2020-08-03 12:47:25 -07:00
|
|
|
type config struct {
|
2020-07-06 10:03:45 -07:00
|
|
|
handler signing.SignModeHandler
|
|
|
|
decoder sdk.TxDecoder
|
|
|
|
encoder sdk.TxEncoder
|
|
|
|
jsonDecoder sdk.TxDecoder
|
|
|
|
jsonEncoder sdk.TxEncoder
|
2020-10-27 06:53:54 -07:00
|
|
|
protoCodec codec.ProtoCodecMarshaler
|
2020-07-06 10:03:45 -07:00
|
|
|
}
|
|
|
|
|
2020-09-21 09:48:28 -07:00
|
|
|
// NewTxConfig returns a new protobuf TxConfig using the provided ProtoCodec and sign modes. The
|
2020-08-03 12:47:25 -07:00
|
|
|
// first enabled sign mode will become the default sign mode.
|
2020-10-27 06:53:54 -07:00
|
|
|
func NewTxConfig(protoCodec codec.ProtoCodecMarshaler, enabledSignModes []signingtypes.SignMode) client.TxConfig {
|
2020-08-03 12:47:25 -07:00
|
|
|
return &config{
|
|
|
|
handler: makeSignModeHandler(enabledSignModes),
|
2020-09-21 09:48:28 -07:00
|
|
|
decoder: DefaultTxDecoder(protoCodec),
|
2020-07-25 01:10:04 -07:00
|
|
|
encoder: DefaultTxEncoder(),
|
2020-09-21 09:48:28 -07:00
|
|
|
jsonDecoder: DefaultJSONTxDecoder(protoCodec),
|
2020-10-15 06:07:59 -07:00
|
|
|
jsonEncoder: DefaultJSONTxEncoder(protoCodec),
|
2020-07-29 15:33:42 -07:00
|
|
|
protoCodec: protoCodec,
|
2020-07-06 10:03:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 12:47:25 -07:00
|
|
|
func (g config) NewTxBuilder() client.TxBuilder {
|
feat: Add AuxTxBuilder (#10455)
<!--
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: #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)
2021-11-11 03:25:13 -08:00
|
|
|
return newBuilder(g.protoCodec)
|
2020-07-06 10:03:45 -07:00
|
|
|
}
|
|
|
|
|
2020-07-24 15:24:57 -07:00
|
|
|
// WrapTxBuilder returns a builder from provided transaction
|
2020-08-03 12:47:25 -07:00
|
|
|
func (g config) WrapTxBuilder(newTx sdk.Tx) (client.TxBuilder, error) {
|
2020-08-24 07:41:08 -07:00
|
|
|
newBuilder, ok := newTx.(*wrapper)
|
2020-07-24 15:24:57 -07:00
|
|
|
if !ok {
|
2020-08-24 07:41:08 -07:00
|
|
|
return nil, fmt.Errorf("expected %T, got %T", &wrapper{}, newTx)
|
2020-07-24 15:24:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return newBuilder, nil
|
|
|
|
}
|
|
|
|
|
2020-08-03 12:47:25 -07:00
|
|
|
func (g config) SignModeHandler() signing.SignModeHandler {
|
2020-07-06 10:03:45 -07:00
|
|
|
return g.handler
|
|
|
|
}
|
|
|
|
|
2020-08-03 12:47:25 -07:00
|
|
|
func (g config) TxEncoder() sdk.TxEncoder {
|
2020-07-06 10:03:45 -07:00
|
|
|
return g.encoder
|
|
|
|
}
|
|
|
|
|
2020-08-03 12:47:25 -07:00
|
|
|
func (g config) TxDecoder() sdk.TxDecoder {
|
2020-07-06 10:03:45 -07:00
|
|
|
return g.decoder
|
|
|
|
}
|
|
|
|
|
2020-08-03 12:47:25 -07:00
|
|
|
func (g config) TxJSONEncoder() sdk.TxEncoder {
|
2020-07-06 10:03:45 -07:00
|
|
|
return g.jsonEncoder
|
|
|
|
}
|
|
|
|
|
2020-08-03 12:47:25 -07:00
|
|
|
func (g config) TxJSONDecoder() sdk.TxDecoder {
|
2020-07-06 10:03:45 -07:00
|
|
|
return g.jsonDecoder
|
|
|
|
}
|