cosmos-sdk/types/tx/direct_aux_test.go

91 lines
2.9 KiB
Go
Raw Normal View History

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
package tx_test
import (
"testing"
"github.com/stretchr/testify/require"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
feat: Add Tips middleware (#10208) <!-- 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 R4R Closes: #9912 This PR introduces 1 new middleware: - `TipsMiddleware`: transfer tip from tipper to feePayer when relevant. It also makes sure in the DIRECT_AUX sign mode handler that the fee payer cannot use that sign mode. Depends on: - [x] #10028 - [x] #10268 - [x] #10322 - [x] #10346 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> --- ### 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-16 09:55:12 -08:00
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
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
"github.com/cosmos/cosmos-sdk/testutil/testdata"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
)
func TestSignDocDirectAux(t *testing.T) {
bodyBz := []byte{42}
_, pk, addr := testdata.KeyTestPubAddr()
pkAny, err := codectypes.NewAnyWithValue(pk)
require.NoError(t, err)
testcases := []struct {
name string
sd tx.SignDocDirectAux
expErr bool
}{
{"empty bodyBz", tx.SignDocDirectAux{}, true},
{"empty pubkey", tx.SignDocDirectAux{BodyBytes: bodyBz}, true},
{"empty tipper", tx.SignDocDirectAux{BodyBytes: bodyBz, PublicKey: pkAny, Tip: &tx.Tip{Amount: testdata.NewTestFeeAmount()}}, true},
{"happy case w/o tip", tx.SignDocDirectAux{BodyBytes: bodyBz, PublicKey: pkAny}, false},
{"happy case w/ tip", tx.SignDocDirectAux{
BodyBytes: bodyBz,
PublicKey: pkAny,
Tip: &tx.Tip{Tipper: addr.String(), Amount: testdata.NewTestFeeAmount()},
}, false},
}
for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
err := tc.sd.ValidateBasic()
if tc.expErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}
func TestAuxSignerData(t *testing.T) {
bodyBz := []byte{42}
_, pk, addr := testdata.KeyTestPubAddr()
pkAny, err := codectypes.NewAnyWithValue(pk)
require.NoError(t, err)
sig := []byte{42}
sd := &tx.SignDocDirectAux{BodyBytes: bodyBz, PublicKey: pkAny}
testcases := []struct {
name string
sd tx.AuxSignerData
expErr bool
}{
{"empty address", tx.AuxSignerData{}, true},
{"empty sign mode", tx.AuxSignerData{Address: addr.String()}, true},
{"SIGN_MODE_DIRECT", tx.AuxSignerData{Address: addr.String(), Mode: signing.SignMode(signing.SignMode_SIGN_MODE_DIRECT)}, true},
{"no sig", tx.AuxSignerData{Address: addr.String(), Mode: signing.SignMode(signing.SignMode_SIGN_MODE_DIRECT_AUX)}, true},
{"happy case WITH DIRECT_AUX", tx.AuxSignerData{Address: addr.String(), Mode: signing.SignMode_SIGN_MODE_DIRECT_AUX, SignDoc: sd, Sig: sig}, false},
{"happy case WITH DIRECT_AUX", tx.AuxSignerData{Address: addr.String(), Mode: signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, SignDoc: sd, Sig: sig}, false},
}
for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
err := tc.sd.ValidateBasic()
if tc.expErr {
require.Error(t, err)
} else {
require.NoError(t, err)
feat: Add Tips middleware (#10208) <!-- 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 R4R Closes: #9912 This PR introduces 1 new middleware: - `TipsMiddleware`: transfer tip from tipper to feePayer when relevant. It also makes sure in the DIRECT_AUX sign mode handler that the fee payer cannot use that sign mode. Depends on: - [x] #10028 - [x] #10268 - [x] #10322 - [x] #10346 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> --- ### 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-16 09:55:12 -08:00
sigV2, err := tc.sd.GetSignatureV2()
require.NoError(t, err)
require.Equal(t, tc.sd.Mode, sigV2.Data.(*signing.SingleSignatureData).SignMode)
require.Equal(t, tc.sd.Sig, sigV2.Data.(*signing.SingleSignatureData).Signature)
require.Equal(t, tc.sd.SignDoc.Sequence, sigV2.Sequence)
require.True(t, tc.sd.SignDoc.PublicKey.GetCachedValue().(cryptotypes.PubKey).Equals(sigV2.PubKey))
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
}
})
}
}