Added binary codec base58 as well as improving the help for --binary-codec flag (#548)

* Added binary codec `base58` as well as improving the help for `--binary-codec` flag
* Added tests for base58
* Fixed tests

Co-authored-by: Matthieu Vachon <matt@streamingfast.io>
This commit is contained in:
Matthieu Vachon 2022-01-27 23:10:01 -05:00 committed by GitHub
parent ccf901d5c7
commit 5803295174
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 12 additions and 2 deletions

1
go.mod
View File

@ -32,6 +32,7 @@ require (
github.com/mattn/go-sqlite3 v1.14.6 // indirect github.com/mattn/go-sqlite3 v1.14.6 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/ugorji/go/codec v1.2.6 // indirect github.com/ugorji/go/codec v1.2.6 // indirect
golang.org/x/sys v0.0.0-20210923061019-b8560ed6a9b7 // indirect golang.org/x/sys v0.0.0-20210923061019-b8560ed6a9b7 // indirect

2
go.sum
View File

@ -65,6 +65,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

View File

@ -4,11 +4,14 @@ import (
"encoding/base64" "encoding/base64"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"github.com/mr-tron/base58"
) )
const ( const (
CodecNone = "none" CodecNone = "none"
CodecHex = "hex" CodecHex = "hex"
CodecBase58 = "base58"
CodecBase64 = "base64" CodecBase64 = "base64"
) )
@ -19,7 +22,7 @@ var (
func SetBinaryCodec(codec string) error { func SetBinaryCodec(codec string) error {
switch codec { switch codec {
case CodecNone, CodecHex, CodecBase64: case CodecNone, CodecHex, CodecBase58, CodecBase64:
BinaryCodec = codec BinaryCodec = codec
default: default:
return fmt.Errorf("invalid binary codec: %v", codec) return fmt.Errorf("invalid binary codec: %v", codec)
@ -32,6 +35,8 @@ func encodeBinaryData(data []byte, codec string) string {
switch codec { switch codec {
case CodecHex: case CodecHex:
return hex.EncodeToString(data) return hex.EncodeToString(data)
case CodecBase58:
return base58.Encode(data)
case CodecBase64: case CodecBase64:
return base64.StdEncoding.EncodeToString(data) return base64.StdEncoding.EncodeToString(data)
default: default:

View File

@ -13,6 +13,7 @@ func TestSetBinaryCodec(t *testing.T) {
err error err error
}{ }{
{input: CodecNone, err: nil}, {input: CodecNone, err: nil},
{input: CodecBase58, err: nil},
{input: CodecBase64, err: nil}, {input: CodecBase64, err: nil},
{input: CodecHex, err: nil}, {input: CodecHex, err: nil},
{input: "foobar", err: errors.New("invalid binary codec: foobar")}, {input: "foobar", err: errors.New("invalid binary codec: foobar")},
@ -37,6 +38,7 @@ func Test_encodeBinaryData(t *testing.T) {
encoding string encoding string
}{ }{
{input: "hello world", expected: "hello world", encoding: CodecNone}, {input: "hello world", expected: "hello world", encoding: CodecNone},
{input: "hello world", expected: "StV1DL6CwTryKyV", encoding: CodecBase58},
{input: "hello world", expected: "aGVsbG8gd29ybGQ=", encoding: CodecBase64}, {input: "hello world", expected: "aGVsbG8gd29ybGQ=", encoding: CodecBase64},
{input: "hello world", expected: "68656c6c6f20776f726c64", encoding: CodecHex}, {input: "hello world", expected: "68656c6c6f20776f726c64", encoding: CodecHex},
} }

View File

@ -42,7 +42,7 @@ type Options struct {
ConnectionIdleTimeout int `long:"idle-timeout" description:"Set connection idle timeout in minutes" default:"180"` ConnectionIdleTimeout int `long:"idle-timeout" description:"Set connection idle timeout in minutes" default:"180"`
Cors bool `long:"cors" description:"Enable Cross-Origin Resource Sharing (CORS)"` Cors bool `long:"cors" description:"Enable Cross-Origin Resource Sharing (CORS)"`
CorsOrigin string `long:"cors-origin" description:"Allowed CORS origins" default:"*"` CorsOrigin string `long:"cors-origin" description:"Allowed CORS origins" default:"*"`
BinaryCodec string `long:"binary-codec" description:"Codec for binary data serialization"` BinaryCodec string `long:"binary-codec" description:"Codec for binary data serialization, one of 'none', 'hex', 'base58', 'base64'" default:"base64"`
} }
var Opts Options var Opts Options