@cryptotaxi247 / kubo / commits / c89110920

feat: multibase transcode command (#8403)

* Add a transcoder command to multibase In order to more easily facilitate the conversion between multibase formats, include a transcode command to avoid `multibase decode | multibase encode` * Example code needed go mod tidy Co-authored-by: gammazero <gammazero@users.noreply.github.com>

Andey Robins committed Sep 21, 2021 at 12:19 UTC c89110920e54437ebe45114edb4348812036d7bb
3 files changed +70 -3
core/commands/commands_test.go
+1
@@ -162,6 +162,7 @@ func TestCommands(t *testing.T) {
162 "/multibase",
163 "/multibase/decode",
164 "/multibase/encode",
165 + "/multibase/transcode",
166 "/multibase/list",
167 "/name",
168 "/name/publish",
core/commands/multibase.go
+56 -3
@@ -16,9 +16,10 @@ var MbaseCmd = &cmds.Command{
16 Tagline: "Encode and decode files or stdin with multibase format",
17 },
18 Subcommands: map[string]*cmds.Command{
19 - "encode": mbaseEncodeCmd,
20 - "decode": mbaseDecodeCmd,
21 - "list": basesCmd,
19 + "encode": mbaseEncodeCmd,
20 + "decode": mbaseDecodeCmd,
21 + "transcode": mbaseTranscodeCmd,
22 + "list": basesCmd,
23 },
24 Extra: CreateCmdExtras(SetDoesNotUseRepo(true)),
25 }
@@ -116,3 +117,55 @@ This command expects multibase inside of a file or via stdin:
117 return resp.Emit(reader)
118 },
119 }
120 +
121 +var mbaseTranscodeCmd = &cmds.Command{
122 + Helptext: cmds.HelpText{
123 + Tagline: "Transcode multibase string between bases",
124 + LongDescription: `
125 +This command expects multibase inside of a file or via stdin.
126 +
127 +By default it will use URL-safe base64url encoding,
128 +but one can customize used base with -b:
129 +
130 + > echo -n hello | ipfs multibase encode > file
131 + > cat file
132 + uaGVsbG8
133 +
134 + > ipfs multibase transcode file -b base16 > transcoded_file
135 + > cat transcoded_file
136 + f68656c6c6f
137 +`,
138 + },
139 + Arguments: []cmds.Argument{
140 + cmds.FileArg("encoded_file", true, false, "encoded data to decode").EnableStdin(),
141 + },
142 + Options: []cmds.Option{
143 + cmds.StringOption(mbaseOptionName, "multibase encoding").WithDefault("base64url"),
144 + },
145 + Run: func(req *cmds.Request, resp cmds.ResponseEmitter, env cmds.Environment) error {
146 + if err := req.ParseBodyArgs(); err != nil {
147 + return err
148 + }
149 + encoderName, _ := req.Options[mbaseOptionName].(string)
150 + encoder, err := mbase.EncoderByName(encoderName)
151 + if err != nil {
152 + return err
153 + }
154 + files := req.Files.Entries()
155 + file, err := cmdenv.GetFileArg(files)
156 + if err != nil {
157 + return fmt.Errorf("failed to access file: %w", err)
158 + }
159 + encoded_data, err := ioutil.ReadAll(file)
160 + if err != nil {
161 + return fmt.Errorf("failed to read file contents: %w", err)
162 + }
163 + _, data, err := mbase.Decode(string(encoded_data))
164 + if err != nil {
165 + return fmt.Errorf("failed to decode multibase: %w", err)
166 + }
167 + encoded := encoder.Encode(data)
168 + reader := strings.NewReader(encoded)
169 + return resp.Emit(reader)
170 + },
171 +}
test/sharness/t0295-multibase.sh
+13
@@ -74,6 +74,19 @@ test_expect_success "multibase encode+decode roundtrip" '
74 test_cmp actual expected
75 '
76
77 +test_expect_success "mutlibase transcode works (stdin)" '
78 + echo -n f68656c6c6f > expected &&
79 + echo -n uaGVsbG8 | ipfs multibase transcode -b base16 > actual &&
80 + test_cmp actual expected
81 +'
82 +
83 +test_expect_success "multibase transcode works (file)" '
84 + echo -n uaGVsbG8 > file &&
85 + echo -n f68656c6c6f > expected &&
86 + ipfs multibase transcode ./file -b base16> actual &&
87 + test_cmp actual expected
88 +'
89 +
90 test_expect_success "multibase error on unknown multibase prefix" '
91 echo "Error: failed to decode multibase: selected encoding not supported" > expected &&
92 echo -n ę-that-should-do-the-trick | ipfs multibase decode 2> actual ;