Skip to content

Commit 6f3febe

Browse files
committed
Add bytes encoding functionality with support for multiple formats
- Introduce `bytes.Encoding` for various encoding types: `raw`, `hex`, `0xhex`, `base64`, `base58`. - Add parsing, encoding, and decoding methods for `bytes.Encoding`. - Update documentation and changelog to reflect new encoding options via `--bytes-encoding` flag.
1 parent c196d15 commit 6f3febe

2 files changed

Lines changed: 127 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## v4.7.0
9+
10+
* Added support for `optional` fields in from-proto mode.
11+
* Added support for `repeated` native fields in from-proto mode.
12+
* Added bytes encoding support. use flags `--bytes-encoding` with values `raw`, `hex`, `0xhex`, `base64`, `base58`
13+
814
## v4.6.9
915

1016
* Bumped `substreams` to `v1.16.7-0` to add foundational stores support.

bytes/encoding.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package bytes
2+
3+
import (
4+
"encoding/base64"
5+
"encoding/hex"
6+
"fmt"
7+
"strings"
8+
9+
"github.com/btcsuite/btcutil/base58"
10+
)
11+
12+
// Encoding represents the different encoding types for protobuf bytes fields
13+
type Encoding int
14+
15+
const (
16+
// EncodingRaw keeps bytes as raw binary data (default)
17+
EncodingRaw Encoding = iota
18+
// EncodingHex encodes bytes as hexadecimal string
19+
EncodingHex
20+
// EncodingHexWith0x encodes bytes as hexadecimal string with 0x prefix
21+
EncodingHexWith0x
22+
// EncodingBase64 encodes bytes as base64 string
23+
EncodingBase64
24+
// EncodingBase58 encodes bytes as base58 string
25+
EncodingBase58
26+
)
27+
28+
// String returns the string representation of the encoding
29+
func (e Encoding) String() string {
30+
switch e {
31+
case EncodingRaw:
32+
return "raw"
33+
case EncodingHex:
34+
return "hex"
35+
case EncodingHexWith0x:
36+
return "0xhex"
37+
case EncodingBase64:
38+
return "base64"
39+
case EncodingBase58:
40+
return "base58"
41+
default:
42+
return "unknown"
43+
}
44+
}
45+
46+
// ParseEncoding parses a string into an Encoding type
47+
func ParseEncoding(s string) (Encoding, error) {
48+
switch strings.ToLower(s) {
49+
case "raw":
50+
return EncodingRaw, nil
51+
case "hex":
52+
return EncodingHex, nil
53+
case "0xhex":
54+
return EncodingHexWith0x, nil
55+
case "base64":
56+
return EncodingBase64, nil
57+
case "base58":
58+
return EncodingBase58, nil
59+
default:
60+
return EncodingRaw, fmt.Errorf("invalid encoding: %s", s)
61+
}
62+
}
63+
64+
// IsStringType returns true if the encoding converts bytes to string database type
65+
func (e Encoding) IsStringType() bool {
66+
return e != EncodingRaw
67+
}
68+
69+
// EncodeBytes encodes the given bytes using the specified encoding
70+
func (e Encoding) EncodeBytes(data []byte) (interface{}, error) {
71+
switch e {
72+
case EncodingRaw:
73+
return data, nil
74+
case EncodingHex:
75+
return hex.EncodeToString(data), nil
76+
case EncodingHexWith0x:
77+
return "0x" + hex.EncodeToString(data), nil
78+
case EncodingBase64:
79+
return base64.StdEncoding.EncodeToString(data), nil
80+
case EncodingBase58:
81+
return base58.Encode(data), nil
82+
default:
83+
return nil, fmt.Errorf("unsupported encoding: %s", e)
84+
}
85+
}
86+
87+
// DecodeBytes decodes the given string back to bytes using the specified encoding
88+
func (e Encoding) DecodeBytes(encoded interface{}) ([]byte, error) {
89+
switch e {
90+
case EncodingRaw:
91+
if data, ok := encoded.([]byte); ok {
92+
return data, nil
93+
}
94+
return nil, fmt.Errorf("expected []byte for raw encoding, got %T", encoded)
95+
case EncodingHex:
96+
if str, ok := encoded.(string); ok {
97+
return hex.DecodeString(str)
98+
}
99+
return nil, fmt.Errorf("expected string for hex encoding, got %T", encoded)
100+
case EncodingHexWith0x:
101+
if str, ok := encoded.(string); ok {
102+
if strings.HasPrefix(str, "0x") || strings.HasPrefix(str, "0X") {
103+
return hex.DecodeString(str[2:])
104+
}
105+
return hex.DecodeString(str)
106+
}
107+
return nil, fmt.Errorf("expected string for 0xhex encoding, got %T", encoded)
108+
case EncodingBase64:
109+
if str, ok := encoded.(string); ok {
110+
return base64.StdEncoding.DecodeString(str)
111+
}
112+
return nil, fmt.Errorf("expected string for base64 encoding, got %T", encoded)
113+
case EncodingBase58:
114+
if str, ok := encoded.(string); ok {
115+
return base58.Decode(str), nil
116+
}
117+
return nil, fmt.Errorf("expected string for base58 encoding, got %T", encoded)
118+
default:
119+
return nil, fmt.Errorf("unsupported encoding: %s", e)
120+
}
121+
}

0 commit comments

Comments
 (0)