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

fix(textual): Use HEX instead of base64 #12954

Merged
merged 6 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
2 changes: 1 addition & 1 deletion docs/architecture/adr-050-sign-mode-textual-annex1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions docs/architecture/adr-050-sign-mode-textual.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ We define "transaction envelope" as all data in a transaction that is not in the
```
Chain ID: <string>
Account number: <uint64>
*Public Key: <base64_string>
*Public Key: <hex_string>
Sequence: <uint64>
<TxBody> // See #8.
Fee: <coins> // See value renderers for coins encoding.
Expand All @@ -112,7 +112,7 @@ Tip: <string>
*<repeated Any>
*This transaction has <int> other signers: // Skipped if there is only one signer
*Signer (<int>/<int>):
*Public Key: <base64_string>
*Public Key: <hex_string>
*Sequence: <uint64>
*End of other signers
```
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tx/textual/valuerenderer/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package valuerenderer

import (
"context"
"encoding/base64"
"encoding/hex"
"io"

"google.golang.org/protobuf/reflect/protoreflect"
Expand All @@ -14,7 +14,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, hex.EncodeToString(v.Bytes()))
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
return err
}

Expand All @@ -24,7 +24,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
}
Expand Down
12 changes: 7 additions & 5 deletions tx/textual/valuerenderer/bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package valuerenderer_test

import (
"context"
"encoding/hex"
"encoding/base64"
"encoding/json"
"os"
"strings"
Expand All @@ -14,14 +14,16 @@ import (

func TestFormatBytes(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)

err = json.Unmarshal(raw, &testcases)
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)
Expand All @@ -30,16 +32,16 @@ func TestFormatBytes(t *testing.T) {
b := new(strings.Builder)
err = r.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())
}
}

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)
}