Skip to content

Commit

Permalink
feat: multibase transcode command (#8403)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
andey-robins and gammazero authored Sep 21, 2021
1 parent 92854db commit c891109
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
1 change: 1 addition & 0 deletions core/commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func TestCommands(t *testing.T) {
"/multibase",
"/multibase/decode",
"/multibase/encode",
"/multibase/transcode",
"/multibase/list",
"/name",
"/name/publish",
Expand Down
59 changes: 56 additions & 3 deletions core/commands/multibase.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ var MbaseCmd = &cmds.Command{
Tagline: "Encode and decode files or stdin with multibase format",
},
Subcommands: map[string]*cmds.Command{
"encode": mbaseEncodeCmd,
"decode": mbaseDecodeCmd,
"list": basesCmd,
"encode": mbaseEncodeCmd,
"decode": mbaseDecodeCmd,
"transcode": mbaseTranscodeCmd,
"list": basesCmd,
},
Extra: CreateCmdExtras(SetDoesNotUseRepo(true)),
}
Expand Down Expand Up @@ -116,3 +117,55 @@ This command expects multibase inside of a file or via stdin:
return resp.Emit(reader)
},
}

var mbaseTranscodeCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Transcode multibase string between bases",
LongDescription: `
This command expects multibase inside of a file or via stdin.
By default it will use URL-safe base64url encoding,
but one can customize used base with -b:
> echo -n hello | ipfs multibase encode > file
> cat file
uaGVsbG8
> ipfs multibase transcode file -b base16 > transcoded_file
> cat transcoded_file
f68656c6c6f
`,
},
Arguments: []cmds.Argument{
cmds.FileArg("encoded_file", true, false, "encoded data to decode").EnableStdin(),
},
Options: []cmds.Option{
cmds.StringOption(mbaseOptionName, "multibase encoding").WithDefault("base64url"),
},
Run: func(req *cmds.Request, resp cmds.ResponseEmitter, env cmds.Environment) error {
if err := req.ParseBodyArgs(); err != nil {
return err
}
encoderName, _ := req.Options[mbaseOptionName].(string)
encoder, err := mbase.EncoderByName(encoderName)
if err != nil {
return err
}
files := req.Files.Entries()
file, err := cmdenv.GetFileArg(files)
if err != nil {
return fmt.Errorf("failed to access file: %w", err)
}
encoded_data, err := ioutil.ReadAll(file)
if err != nil {
return fmt.Errorf("failed to read file contents: %w", err)
}
_, data, err := mbase.Decode(string(encoded_data))
if err != nil {
return fmt.Errorf("failed to decode multibase: %w", err)
}
encoded := encoder.Encode(data)
reader := strings.NewReader(encoded)
return resp.Emit(reader)
},
}
13 changes: 13 additions & 0 deletions test/sharness/t0295-multibase.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ test_expect_success "multibase encode+decode roundtrip" '
test_cmp actual expected
'

test_expect_success "mutlibase transcode works (stdin)" '
echo -n f68656c6c6f > expected &&
echo -n uaGVsbG8 | ipfs multibase transcode -b base16 > actual &&
test_cmp actual expected
'

test_expect_success "multibase transcode works (file)" '
echo -n uaGVsbG8 > file &&
echo -n f68656c6c6f > expected &&
ipfs multibase transcode ./file -b base16> actual &&
test_cmp actual expected
'

test_expect_success "multibase error on unknown multibase prefix" '
echo "Error: failed to decode multibase: selected encoding not supported" > expected &&
echo -n ę-that-should-do-the-trick | ipfs multibase decode 2> actual ;
Expand Down

0 comments on commit c891109

Please sign in to comment.