Skip to content

Commit

Permalink
adding base64 bytes-encoding to the print merged blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard-Voiculescu committed Aug 7, 2024
1 parent fb7012c commit cdb380b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cmd/tools/print/tools_print.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func NewToolsPrintCmd[B firecore.Block](chain *firecore.Chain[B]) *cobra.Command
toolsPrintCmd.AddCommand(toolsPrintMergedBlocksCmd)

toolsPrintCmd.PersistentFlags().StringP("output", "o", "text", "Output mode for block printing, either 'text', 'json' or 'jsonl'")
toolsPrintCmd.PersistentFlags().String("bytes-encoding", "hex", "Encoding for bytes fields, either 'hex' or 'base58'")
toolsPrintCmd.PersistentFlags().String("bytes-encoding", "hex", "Encoding for bytes fields, either 'hex', 'base58' or 'base64'")
toolsPrintCmd.PersistentFlags().StringSlice("proto-paths", []string{""}, "Paths to proto files to use for dynamic decoding of blocks")
toolsPrintCmd.PersistentFlags().Bool("transactions", false, "When in 'text' output mode, also print transactions summary")

Expand Down Expand Up @@ -281,9 +281,14 @@ func SetupJsonMarshaller(cmd *cobra.Command, chainFileDescriptor protoreflect.Fi

var options []fcjson.MarshallerOption
bytesEncoding := sflags.MustGetString(cmd, "bytes-encoding")

if bytesEncoding == "base58" {
options = append(options, fcjson.WithBytesEncoderFunc(fcjson.ToBase58))
}

if bytesEncoding == "base64" {
options = append(options, fcjson.WithBytesEncoderFunc(fcjson.ToBase64))
}

return fcjson.NewMarshaller(registry, options...), nil
}
5 changes: 5 additions & 0 deletions json/marshallers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package json

import (
"bytes"
"encoding/base64"
"encoding/hex"
"fmt"
"os"
Expand Down Expand Up @@ -189,6 +190,10 @@ func ToBase58(encoder *jsontext.Encoder, t []byte, options json.Options) error {
return encoder.WriteToken(jsontext.String(base58.Encode(t)))
}

func ToBase64(encoder *jsontext.Encoder, t []byte, options json.Options) error {
return encoder.WriteToken(jsontext.String(base64.StdEncoding.EncodeToString(t)))
}

func ToHex(encoder *jsontext.Encoder, t []byte, options json.Options) error {
return encoder.WriteToken(jsontext.String(hex.EncodeToString(t)))
}

0 comments on commit cdb380b

Please sign in to comment.