diff --git a/docs/architecture/adr-050-sign-mode-textual-annex1.md b/docs/architecture/adr-050-sign-mode-textual-annex1.md index 41d75dfff59c..8db3e9fb9fae 100644 --- a/docs/architecture/adr-050-sign-mode-textual-annex1.md +++ b/docs/architecture/adr-050-sign-mode-textual-annex1.md @@ -214,7 +214,7 @@ Rendered as either ISO8601 (`2021-01-01T12:00:00Z`). ### bytes -- Bytes are rendered in base64. +- Bytes are rendered in hexadecimal, all capital letters, without the `0x` prefix. ### address bytes diff --git a/docs/architecture/adr-050-sign-mode-textual.md b/docs/architecture/adr-050-sign-mode-textual.md index e58c02dc209b..aaa7e2bfd828 100644 --- a/docs/architecture/adr-050-sign-mode-textual.md +++ b/docs/architecture/adr-050-sign-mode-textual.md @@ -93,7 +93,7 @@ We define "transaction envelope" as all data in a transaction that is not in the ``` Chain ID: Account number: -*Public Key: +*Public Key: Sequence: // See #8. Fee: // See value renderers for coins encoding. @@ -112,7 +112,7 @@ Tip: * *This transaction has other signers: // Skipped if there is only one signer *Signer (/): -*Public Key: +*Public Key: *Sequence: *End of other signers ``` @@ -222,7 +222,7 @@ SIGN_MODE_TEXTUAL: ``` Chain ID: simapp-1 Account number: 10 -*Public Key: iQ...== // Base64 pubkey +*Public Key: iQ...== // Hex pubkey Sequence: 2 This transaction has 1 message: Message (1/1): bank v1beta1 send coins @@ -481,7 +481,7 @@ SIGN_MODE_TEXTUAL is purely additive, and doesn't break any backwards compatibil ### Negative -- Some fields are still encoded in non-human-readable ways, such as public keys in base64. +- Some fields are still encoded in non-human-readable ways, such as public keys in hexadecimal. - New ledger app needs to be released, still unclear ### Neutral diff --git a/tx/textual/valuerenderer/bytes.go b/tx/textual/valuerenderer/bytes.go index 79ee076ed0cd..f2206a00cf84 100644 --- a/tx/textual/valuerenderer/bytes.go +++ b/tx/textual/valuerenderer/bytes.go @@ -2,8 +2,9 @@ package valuerenderer import ( "context" - "encoding/base64" + "encoding/hex" "io" + "strings" "google.golang.org/protobuf/reflect/protoreflect" ) @@ -14,7 +15,7 @@ type bytesValueRenderer struct{} var _ ValueRenderer = bytesValueRenderer{} func (vr bytesValueRenderer) Format(ctx context.Context, v protoreflect.Value, w io.Writer) error { - _, err := io.WriteString(w, base64.StdEncoding.EncodeToString(v.Bytes())) + _, err := io.WriteString(w, strings.ToUpper(hex.EncodeToString(v.Bytes()))) return err } @@ -24,7 +25,7 @@ func (vr bytesValueRenderer) Parse(_ context.Context, r io.Reader) (protoreflect return protoreflect.ValueOfBytes([]byte{}), err } - data, err := base64.StdEncoding.DecodeString(string(formatted)) + data, err := hex.DecodeString(string(formatted)) if err != nil { return protoreflect.ValueOfBytes([]byte{}), err } diff --git a/tx/textual/valuerenderer/bytes_test.go b/tx/textual/valuerenderer/bytes_test.go index c5853d56214a..457f5ec93406 100644 --- a/tx/textual/valuerenderer/bytes_test.go +++ b/tx/textual/valuerenderer/bytes_test.go @@ -2,7 +2,7 @@ package valuerenderer_test import ( "context" - "encoding/hex" + "encoding/base64" "encoding/json" "os" "strings" @@ -12,8 +12,10 @@ import ( "google.golang.org/protobuf/reflect/protoreflect" ) -func TestFormatBytes(t *testing.T) { +func TestBytesJsonTestCases(t *testing.T) { var testcases []bytesTest + // Bytes.json contains bytes that are represented in base64 format, and + // their expected results in hex. raw, err := os.ReadFile("../internal/testdata/bytes.json") require.NoError(t, err) @@ -21,25 +23,31 @@ func TestFormatBytes(t *testing.T) { require.NoError(t, err) for _, tc := range testcases { - data, err := hex.DecodeString(tc.hex) + data, err := base64.StdEncoding.DecodeString(tc.base64) require.NoError(t, err) - r, err := valueRendererOf(data) + valrend, err := valueRendererOf(data) require.NoError(t, err) b := new(strings.Builder) - err = r.Format(context.Background(), protoreflect.ValueOfBytes(data), b) + err = valrend.Format(context.Background(), protoreflect.ValueOfBytes(data), b) require.NoError(t, err) - require.Equal(t, tc.expRes, b.String()) + require.Equal(t, tc.hex, b.String()) + + // Round trip + r := strings.NewReader(tc.hex) + val, err := valrend.Parse(context.Background(), r) + require.NoError(t, err) + require.Equal(t, tc.base64, base64.StdEncoding.EncodeToString(val.Bytes())) } } type bytesTest struct { hex string - expRes string + base64 string } func (t *bytesTest) UnmarshalJSON(b []byte) error { - a := []interface{}{&t.hex, &t.expRes} + a := []interface{}{&t.hex, &t.base64} return json.Unmarshal(b, &a) }