|
| 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