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

api: massage protojson's output to use hex rather than base64 #1393

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
48 changes: 48 additions & 0 deletions api/helpers.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package api

import (
"bytes"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"math"
"math/big"
"reflect"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -71,9 +74,54 @@ func protoTxAsJSON(tx []byte) []byte {
if err != nil {
panic(err)
}
// protojson follows protobuf's json mapping behavior,
// which requires bytes to be encoded as base64:
// https://protobuf.dev/programming-guides/proto3/#json
//
// We want hex rather than base64 for consistency with our REST API
// protojson does not expose any option to configure its behavior,
// and as the Go types are code generated, we cannot use our HexBytes type.
//
// Do a bit of a hack: walk the protobuf value with reflection,
// find all []byte values, and search-and-replace their base64 encoding with hex
// in the protojson bytes. This can be slow if we have many byte slices,
// but in general the protobuf message will be small so there will be few.
bytesValues := collectBytesValues(nil, reflect.ValueOf(&ptx))
for _, bv := range bytesValues {
asBase64 := base64.StdEncoding.AppendEncode(nil, bv)
asHex := hex.AppendEncode(nil, bv)
asJSON = bytes.Replace(asJSON, asBase64, asHex, 1)
}
return asJSON
}

var typBytes = reflect.TypeFor[[]byte]()

func collectBytesValues(result [][]byte, val reflect.Value) [][]byte {
typ := val.Type()
if typ == typBytes {
return append(result, val.Bytes())
}
switch typ.Kind() {
case reflect.Pointer, reflect.Interface:
if !val.IsNil() {
result = collectBytesValues(result, val.Elem())
}
case reflect.Struct:
for i := 0; i < val.NumField(); i++ {
if !typ.Field(i).IsExported() {
continue
}
result = collectBytesValues(result, val.Field(i))
}
case reflect.Slice, reflect.Array:
for i := 0; i < val.Len(); i++ {
result = collectBytesValues(result, val.Index(i))
}
}
return result
}

// isTransactionType checks if the given transaction is of the given type.
// t is expected to be a pointer to a protobuf transaction message.
func isTransactionType[T any](signedTxBytes []byte) (bool, error) {
Expand Down
16 changes: 14 additions & 2 deletions api/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func TestConvertKeysToCamel(t *testing.T) {
}

func TestProtoTxAsJSON(t *testing.T) {
wantJSON := strings.TrimSpace(`
inputJSON := strings.TrimSpace(`
{
"setProcess": {
"txtype": "SET_PROCESS_CENSUS",
Expand All @@ -167,9 +167,21 @@ func TestProtoTxAsJSON(t *testing.T) {
"censusSize": "1000"
}
}
`)
wantJSON := strings.TrimSpace(`
{
"setProcess": {
"txtype": "SET_PROCESS_CENSUS",
"nonce": 1,
"processId": "b31dff61814dab90d6c3a9b65d6c90830480e4b75ad32e747942020000000000",
"censusRoot": "cd453d05c4cb0429ee5ee1aefed016f553b8026b4ced5b0c352905bfa53c7e81",
"censusURI": "ipfs://bafybeicyfukarcryrvy5oe37ligmxwf55sbfiojori4t25wencma4ymxfa",
"censusSize": "1000"
}
}
`)
var ptx models.Tx
err := protojson.Unmarshal([]byte(wantJSON), &ptx)
err := protojson.Unmarshal([]byte(inputJSON), &ptx)
qt.Assert(t, err, qt.IsNil)

asProto, err := proto.Marshal(&ptx)
Expand Down
Loading