-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5079 from zgfzgf/feat-shed-commp
add commp-to-cid base64 decode
- Loading branch information
Showing
1 changed file
with
32 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,55 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/hex" | ||
"fmt" | ||
|
||
commcid "github.com/filecoin-project/go-fil-commcid" | ||
"github.com/urfave/cli/v2" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
var commpToCidCmd = &cli.Command{ | ||
Name: "commp-to-cid", | ||
Usage: "Convert commP to Cid", | ||
Description: "Convert a raw commP to a piece-Cid", | ||
ArgsUsage: "[data]", | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "encoding", | ||
Value: "base64", | ||
Usage: "specify input encoding to parse", | ||
}, | ||
}, | ||
Action: func(cctx *cli.Context) error { | ||
if !cctx.Args().Present() { | ||
return fmt.Errorf("must specify commP to convert") | ||
} | ||
|
||
dec, err := hex.DecodeString(cctx.Args().First()) | ||
if err != nil { | ||
return fmt.Errorf("failed to decode input as hex string: %w", err) | ||
var dec []byte | ||
switch cctx.String("encoding") { | ||
case "base64": | ||
data, err := base64.StdEncoding.DecodeString(cctx.Args().First()) | ||
if err != nil { | ||
return xerrors.Errorf("decoding base64 value: %w", err) | ||
} | ||
dec = data | ||
case "hex": | ||
data, err := hex.DecodeString(cctx.Args().First()) | ||
if err != nil { | ||
return xerrors.Errorf("decoding hex value: %w", err) | ||
} | ||
dec = data | ||
default: | ||
return xerrors.Errorf("unrecognized encoding: %s", cctx.String("encoding")) | ||
} | ||
|
||
fmt.Println(commcid.PieceCommitmentV1ToCID(dec)) | ||
cid, err := commcid.PieceCommitmentV1ToCID(dec) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(cid) | ||
return nil | ||
}, | ||
} |