2021-12-29 20:03:50 +03:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2022-01-28 07:10:01 +03:00
|
|
|
|
|
|
|
"github.com/mr-tron/base58"
|
2021-12-29 20:03:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
CodecNone = "none"
|
|
|
|
CodecHex = "hex"
|
2022-01-28 07:10:01 +03:00
|
|
|
CodecBase58 = "base58"
|
2021-12-29 20:03:50 +03:00
|
|
|
CodecBase64 = "base64"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// BinaryEncodingFormat specifies the default serialization format of binary data
|
|
|
|
BinaryCodec = CodecBase64
|
|
|
|
)
|
|
|
|
|
|
|
|
func SetBinaryCodec(codec string) error {
|
|
|
|
switch codec {
|
2022-01-28 07:10:01 +03:00
|
|
|
case CodecNone, CodecHex, CodecBase58, CodecBase64:
|
2021-12-29 20:03:50 +03:00
|
|
|
BinaryCodec = codec
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid binary codec: %v", codec)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeBinaryData(data []byte, codec string) string {
|
|
|
|
switch codec {
|
|
|
|
case CodecHex:
|
|
|
|
return hex.EncodeToString(data)
|
2022-01-28 07:10:01 +03:00
|
|
|
case CodecBase58:
|
|
|
|
return base58.Encode(data)
|
2021-12-29 20:03:50 +03:00
|
|
|
case CodecBase64:
|
|
|
|
return base64.StdEncoding.EncodeToString(data)
|
|
|
|
default:
|
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
}
|