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(textual): Add Tx envelope Value Renderer #13600

Merged
merged 75 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
0b0f659
wip
amaury1093 Oct 20, 2022
ea7691b
wip
amaury1093 Oct 20, 2022
d857356
Add signer data to Textual
amaury1093 Oct 20, 2022
6c3c77a
Use any
amaury1093 Oct 20, 2022
94c0501
wip
amaury1093 Oct 24, 2022
49cff84
WIP
amaury1093 Oct 24, 2022
10891fd
add tx vr
amaury1093 Oct 25, 2022
9700665
Add examples
amaury1093 Oct 25, 2022
709dfb5
revert
amaury1093 Oct 25, 2022
460bce9
Add hash
amaury1093 Oct 25, 2022
eeaeaa9
Merge branch 'main' of ssh://github.com/cosmos/cosmos-sdk into am/135…
amaury1093 Oct 25, 2022
ae1fb1f
codegen
amaury1093 Oct 25, 2022
1513109
updates
amaury1093 Oct 25, 2022
18777bf
simplify test
amaury1093 Oct 25, 2022
b29e359
go mod tidy
amaury1093 Oct 25, 2022
b3a5670
Useless import
amaury1093 Oct 25, 2022
4d28d9c
Revert
amaury1093 Oct 25, 2022
f97e3ea
typo
amaury1093 Oct 25, 2022
8e363d5
remove textual fields
amaury1093 Oct 27, 2022
0bad609
create proto types
amaury1093 Oct 27, 2022
6d252e4
Fix tests
amaury1093 Oct 27, 2022
4e24b71
add comment
amaury1093 Oct 27, 2022
d1338ed
regenerate to internal
amaury1093 Nov 2, 2022
65e60ed
wip
amaury1093 Nov 7, 2022
75e0bc3
Merge branch 'main' of ssh://github.com/cosmos/cosmos-sdk into am/135…
amaury1093 Nov 14, 2022
025a9eb
go mod tidy
amaury1093 Nov 14, 2022
93d4b5d
Use MessageValueRenderer
amaury1093 Nov 14, 2022
9c87a2c
Merge branch 'main' of ssh://github.com/cosmos/cosmos-sdk into am/135…
amaury1093 Nov 28, 2022
f2b7a58
fix conflicts
amaury1093 Nov 28, 2022
0efbc25
Add part2
amaury1093 Nov 28, 2022
77a6ae1
Start doing parse
amaury1093 Nov 28, 2022
4f810fa
Merge branch 'main' of ssh://github.com/cosmos/cosmos-sdk into am/135…
amaury1093 Nov 28, 2022
00c313a
wip
amaury1093 Nov 29, 2022
7c3a6e6
Merge branch 'main' of ssh://github.com/cosmos/cosmos-sdk into am/135…
amaury1093 Nov 29, 2022
96e5137
Show transactions
amaury1093 Nov 29, 2022
bbee363
Add metadata
amaury1093 Nov 29, 2022
98c26e9
Add all fields
amaury1093 Nov 29, 2022
721e0c6
Merge branch 'main' of ssh://github.com/cosmos/cosmos-sdk into am/135…
amaury1093 Nov 30, 2022
bac3383
Add complex test case
amaury1093 Dec 1, 2022
db5dbfb
Link to JSON
amaury1093 Dec 1, 2022
45401a8
changelog
amaury1093 Dec 1, 2022
463a5e4
Merge branch 'main' of ssh://github.com/cosmos/cosmos-sdk into am/135…
amaury1093 Dec 1, 2022
7042623
Update
amaury1093 Dec 1, 2022
6fd8d79
update links
amaury1093 Dec 1, 2022
d0bb4f8
Merge branch 'main' of ssh://github.com/cosmos/cosmos-sdk into am/135…
amaury1093 Dec 2, 2022
201a1ff
go mod tidy
amaury1093 Dec 2, 2022
32a0608
Fix enum
amaury1093 Dec 2, 2022
01cfb3c
Make JSON clearer
amaury1093 Dec 2, 2022
2169ea1
Update links
amaury1093 Dec 2, 2022
b3e0fca
Refactor proto files
amaury1093 Dec 2, 2022
cc59b56
Clean up
amaury1093 Dec 2, 2022
5d96b7d
Merge branch 'main' of ssh://github.com/cosmos/cosmos-sdk into am/135…
amaury1093 Dec 2, 2022
727b93c
WIP parsing
amaury1093 Dec 2, 2022
79c6698
newlines
amaury1093 Dec 6, 2022
0106bc1
Pluralize
amaury1093 Dec 6, 2022
d05479d
Use enveloppe in approach 2a
amaury1093 Dec 6, 2022
cb0b6cc
Coin amounts are Ints
amaury1093 Dec 6, 2022
a1bc7dd
Add tests
amaury1093 Dec 6, 2022
37c9c52
buf format
amaury1093 Dec 6, 2022
82c0dd7
Add address
amaury1093 Dec 6, 2022
ee78038
Fix all tests
amaury1093 Dec 6, 2022
7c9dabc
Add address to spec
amaury1093 Dec 6, 2022
d05951f
Add changelog
amaury1093 Dec 6, 2022
d2d6d19
Update links to examples
amaury1093 Dec 6, 2022
f80932c
Merge branch 'main' into am/13579-textual-tx
amaury1093 Dec 6, 2022
f14e704
envelope
amaury1093 Dec 7, 2022
094abcd
2 test files
amaury1093 Dec 7, 2022
5647c04
Update exampls
amaury1093 Dec 7, 2022
f540695
Merge branch 'main' into am/13579-textual-tx
amaury1093 Dec 7, 2022
979e3ba
Merge branch 'main' into am/13579-textual-tx
amaury1093 Dec 12, 2022
c827732
Merge branch 'main' of ssh://github.com/cosmos/cosmos-sdk into am/135…
amaury1093 Jan 9, 2023
caf25d9
Add references to when to change Envelope
amaury1093 Jan 11, 2023
1a94bd7
Merge branch 'main' into am/13579-textual-tx
amaury1093 Jan 11, 2023
e0a1626
Fix tests
amaury1093 Jan 11, 2023
e3e55b5
Merge branch 'am/13579-textual-tx' of ssh://github.com/cosmos/cosmos-…
amaury1093 Jan 11, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
cosmossdk.io/depinject v1.0.0-alpha.2
cosmossdk.io/errors v1.0.0-beta.7
cosmossdk.io/math v1.0.0-beta.3
cosmossdk.io/tx v0.0.0-00010101000000-000000000000
github.com/99designs/keyring v1.2.1
github.com/armon/go-metrics v0.4.1
github.com/bgentry/speakeasy v0.1.0
Expand Down Expand Up @@ -160,6 +161,8 @@ require (
)

replace (
// temporary until we tag a new go module
cosmossdk.io/tx => ./tx
github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0
// dgrijalva/jwt-go is deprecated and doesn't receive security updates.
// TODO: remove it: https://github.com/cosmos/cosmos-sdk/issues/13134
Expand Down
32 changes: 32 additions & 0 deletions tx/signing/sign_mode_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package signing

// SignerData is the specific information needed to sign a transaction that generally
// isn't included in the transaction body itself
type SignerData struct {
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
// The address of the signer.
//
// In case of multisigs, this should be the multisig's address.
Address string

// ChainID is the chain that this transaction is targeted
ChainID string

// AccountNumber is the account number of the signer.
//
// In case of multisigs, this should be the multisig account number.
AccountNumber uint64

// Sequence is the account sequence number of the signer that is used
// for replay protection. This field is only useful for Legacy Amino signing,
// since in SIGN_MODE_DIRECT the account sequence is already in the signer
// info.
//
// In case of multisigs, this should be the multisig sequence.
Sequence uint64

// PubKey is the public key of the signer.
//
// In case of multisigs, this should be the pubkey of the member of the
// multisig that is signing the current sign doc.
PubKey cryptotypes.PubKey
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
}
3 changes: 2 additions & 1 deletion tx/textual/valuerenderer/bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"testing"

"cosmossdk.io/tx/signing"
"cosmossdk.io/tx/textual/valuerenderer"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/reflect/protoreflect"
Expand All @@ -21,7 +22,7 @@ func TestBytesJsonTestCases(t *testing.T) {
err = json.Unmarshal(raw, &testcases)
require.NoError(t, err)

textual := valuerenderer.NewTextual(mockCoinMetadataQuerier)
textual := valuerenderer.NewTextual(mockCoinMetadataQuerier, signing.SignerData{})

for _, tc := range testcases {
data, err := base64.StdEncoding.DecodeString(tc.base64)
Expand Down
7 changes: 4 additions & 3 deletions tx/textual/valuerenderer/coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1"
basev1beta1 "cosmossdk.io/api/cosmos/base/v1beta1"
"cosmossdk.io/tx/signing"
"cosmossdk.io/tx/textual/valuerenderer"
)

Expand All @@ -33,7 +34,7 @@ func mockCoinMetadataQuerier(ctx context.Context, denom string) (*bankv1beta1.Me

func TestMetadataQuerier(t *testing.T) {
// Errors on nil metadata querier
textual := valuerenderer.NewTextual(nil)
textual := valuerenderer.NewTextual(nil, signing.SignerData{})
vr, err := textual.GetValueRenderer(fieldDescriptorFromName("COIN"))
require.NoError(t, err)
_, err = vr.Format(context.Background(), protoreflect.ValueOf((&basev1beta1.Coin{}).ProtoReflect()))
Expand All @@ -43,7 +44,7 @@ func TestMetadataQuerier(t *testing.T) {
expErr := fmt.Errorf("mock error")
textual = valuerenderer.NewTextual(func(_ context.Context, _ string) (*bankv1beta1.Metadata, error) {
return nil, expErr
})
}, signing.SignerData{})
vr, err = textual.GetValueRenderer(fieldDescriptorFromName("COIN"))
require.NoError(t, err)
_, err = vr.Format(context.Background(), protoreflect.ValueOf((&basev1beta1.Coin{}).ProtoReflect()))
Expand All @@ -59,7 +60,7 @@ func TestCoinJsonTestcases(t *testing.T) {
err = json.Unmarshal(raw, &testcases)
require.NoError(t, err)

textual := valuerenderer.NewTextual(mockCoinMetadataQuerier)
textual := valuerenderer.NewTextual(mockCoinMetadataQuerier, signing.SignerData{})
vr, err := textual.GetValueRenderer(fieldDescriptorFromName("COIN"))
require.NoError(t, err)

Expand Down
3 changes: 2 additions & 1 deletion tx/textual/valuerenderer/coins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1"
basev1beta1 "cosmossdk.io/api/cosmos/base/v1beta1"
"cosmossdk.io/tx/signing"
"cosmossdk.io/tx/textual/valuerenderer"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/reflect/protoreflect"
Expand All @@ -20,7 +21,7 @@ func TestCoinsJsonTestcases(t *testing.T) {
err = json.Unmarshal(raw, &testcases)
require.NoError(t, err)

textual := valuerenderer.NewTextual(mockCoinMetadataQuerier)
textual := valuerenderer.NewTextual(mockCoinMetadataQuerier, signing.SignerData{})
vr, err := textual.GetValueRenderer(fieldDescriptorFromName("COINS"))
require.NoError(t, err)

Expand Down
3 changes: 2 additions & 1 deletion tx/textual/valuerenderer/dec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"testing"

"cosmossdk.io/tx/signing"
"cosmossdk.io/tx/textual/valuerenderer"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/reflect/protoreflect"
Expand All @@ -18,7 +19,7 @@ func TestDecJsonTestcases(t *testing.T) {
err = json.Unmarshal(raw, &testcases)
require.NoError(t, err)

textual := valuerenderer.NewTextual(nil)
textual := valuerenderer.NewTextual(nil, signing.SignerData{})

for _, tc := range testcases {
tc := tc
Expand Down
3 changes: 2 additions & 1 deletion tx/textual/valuerenderer/int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"

"cosmossdk.io/math"
"cosmossdk.io/tx/signing"
"cosmossdk.io/tx/textual/valuerenderer"
)

Expand All @@ -22,7 +23,7 @@ func TestIntJsonTestcases(t *testing.T) {
err = json.Unmarshal(raw, &testcases)
require.NoError(t, err)

textual := valuerenderer.NewTextual(nil)
textual := valuerenderer.NewTextual(nil, signing.SignerData{})

for _, tc := range testcases {
// Parse test case strings as protobuf uint64
Expand Down
60 changes: 60 additions & 0 deletions tx/textual/valuerenderer/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package valuerenderer

import (
"context"
"fmt"

"google.golang.org/protobuf/reflect/protoreflect"

txv1beta1 "cosmossdk.io/api/cosmos/tx/v1beta1"
"cosmossdk.io/tx/signing"
)

type txValueRenderer struct {
signerData signing.SignerData
t *Textual
}

// NewTimestampValueRenderer returns a ValueRenderer for the protobuf Tx type,
// as called the transaction envelope. It follows the specification defined
// in ADR-050.
func NewTxValueRenderer(t *Textual, signerData signing.SignerData) ValueRenderer {
return txValueRenderer{
t: t,
signerData: signerData,
}
}

// Format implements the ValueRenderer interface.
func (vr txValueRenderer) Format(ctx context.Context, v protoreflect.Value) ([]Screen, error) {
// Reify the reflected message as a proto Timestamp
msg := v.Message().Interface()
protoTx, ok := msg.(*txv1beta1.Tx)
if !ok {
return nil, fmt.Errorf("expected Tx, got %T", msg)
}

screens := make([]Screen, 3)
screens[0].Text = fmt.Sprintf("Chain ID: %s", vr.signerData.ChainID)
screens[1].Text = fmt.Sprintf("Account number: %d", vr.signerData.AccountNumber)
screens[2].Text = fmt.Sprintf("Public key: %s", &vr.signerData.PubKey)

// Get sdk.Msgs screens, from Tx.Body.Messages (field number 1).
msgVr, err := vr.t.GetValueRenderer(protoTx.Body.ProtoReflect().Descriptor().Fields().ByNumber(1))
if err != nil {
return nil, err
}
msgScreens, err := msgVr.Format(ctx, protoreflect.ValueOf(protoTx.Body.Messages))
if err != nil {
return nil, err
}

screens = append(screens, msgScreens...)

return screens, nil
}

// Parse implements the ValueRenderer interface.
func (vr txValueRenderer) Parse(_ context.Context, screens []Screen) (protoreflect.Value, error) {
panic("TODO")
}
9 changes: 7 additions & 2 deletions tx/textual/valuerenderer/valuerenderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1"
basev1beta1 "cosmossdk.io/api/cosmos/base/v1beta1"
txv1beta1 "cosmossdk.io/api/cosmos/tx/v1beta1"
"cosmossdk.io/tx/signing"
cosmos_proto "github.com/cosmos/cosmos-proto"
)

Expand All @@ -35,12 +37,14 @@ type Textual struct {
// - Protobuf timestamp
// - Protobuf duration
messages map[protoreflect.FullName]ValueRenderer
// signerData defines information on the signer
signerData signing.SignerData
}

// NewTextual returns a new Textual which provides
// value renderers.
func NewTextual(q CoinMetadataQueryFn) Textual {
t := Textual{coinMetadataQuerier: q}
func NewTextual(q CoinMetadataQueryFn, signerData signing.SignerData) Textual {
t := Textual{coinMetadataQuerier: q, signerData: signerData}
t.init()
return t
}
Expand Down Expand Up @@ -102,6 +106,7 @@ func (r *Textual) init() {
r.messages[(&basev1beta1.Coin{}).ProtoReflect().Descriptor().FullName()] = NewCoinsValueRenderer(r.coinMetadataQuerier)
r.messages[(&durationpb.Duration{}).ProtoReflect().Descriptor().FullName()] = NewDurationValueRenderer()
r.messages[(&timestamppb.Timestamp{}).ProtoReflect().Descriptor().FullName()] = NewTimestampValueRenderer()
r.messages[(&txv1beta1.Tx{}).ProtoReflect().Descriptor().FullName()] = NewTxValueRenderer(r, r.signerData)
}
}

Expand Down
3 changes: 2 additions & 1 deletion tx/textual/valuerenderer/valuerenderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/reflect/protoreflect"

"cosmossdk.io/tx/signing"
"cosmossdk.io/tx/textual/internal/testpb"
"cosmossdk.io/tx/textual/valuerenderer"
)
Expand All @@ -32,7 +33,7 @@ func TestDispatcher(t *testing.T) {
for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
textual := valuerenderer.NewTextual(nil)
textual := valuerenderer.NewTextual(nil, signing.SignerData{})
rend, err := textual.GetValueRenderer(fieldDescriptorFromName(tc.name))

if tc.expErr {
Expand Down
33 changes: 2 additions & 31 deletions x/auth/signing/sign_mode_handler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package signing

import (
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
txsigning "cosmossdk.io/tx/signing"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
)
Expand All @@ -21,33 +21,4 @@ type SignModeHandler interface {
GetSignBytes(mode signing.SignMode, data SignerData, tx sdk.Tx) ([]byte, error)
}

// SignerData is the specific information needed to sign a transaction that generally
// isn't included in the transaction body itself
type SignerData struct {
// The address of the signer.
//
// In case of multisigs, this should be the multisig's address.
Address string

// ChainID is the chain that this transaction is targeted
ChainID string

// AccountNumber is the account number of the signer.
//
// In case of multisigs, this should be the multisig account number.
AccountNumber uint64

// Sequence is the account sequence number of the signer that is used
// for replay protection. This field is only useful for Legacy Amino signing,
// since in SIGN_MODE_DIRECT the account sequence is already in the signer
// info.
//
// In case of multisigs, this should be the multisig sequence.
Sequence uint64

// PubKey is the public key of the signer.
//
// In case of multisigs, this should be the pubkey of the member of the
// multisig that is signing the current sign doc.
PubKey cryptotypes.PubKey
}
type SignerData txsigning.SignerData