diff --git a/cid-fmt/main.go b/cid-fmt/main.go index 7dacb8a..988126a 100644 --- a/cid-fmt/main.go +++ b/cid-fmt/main.go @@ -1,7 +1,6 @@ package main import ( - "bytes" "fmt" "os" "strings" @@ -9,38 +8,14 @@ import ( c "github.com/ipfs/go-cid" mb "github.com/multiformats/go-multibase" - mh "github.com/multiformats/go-multihash" ) func usage() { fmt.Fprintf(os.Stderr, "usage: %s [-b multibase-code] [-v cid-version] ...\n\n", os.Args[0]) - fmt.Fprintf(os.Stderr, " is either 'prefix' or a printf style format string:\n%s", fmtRef) + fmt.Fprintf(os.Stderr, " is either 'prefix' or a printf style format string:\n%s", c.FormatRef) os.Exit(2) } -const fmtRef = ` - %% literal % - %b multibase name - %B multibase code - %v version string - %V version number - %c codec name - %C codec code - %h multihash name - %H multihash code - %L hash digest length - %m multihash encoded in base %b (with multibase prefix) - %M multihash encoded in base %b without multibase prefix - %d hash digest encoded in base %b (with multibase prefix) - %D hash digest encoded in base %b without multibase prefix - %s cid string encoded in base %b (1) - %s cid string encoded in base %b without multibase prefix - %P cid prefix: %v-%c-%h-%L - -(1) For CID version 0 the multibase must be base58btc and no prefix is -used. For Cid version 1 the multibase prefix is included. -` - func main() { if len(os.Args) < 2 { usage() @@ -55,11 +30,12 @@ outer: if len(args) < 2 { usage() } - if len(args[1]) != 1 { - fmt.Fprintf(os.Stderr, "Error: Invalid multibase code: %s\n", args[1]) + encoder, err := mb.EncoderByName(args[1]) + if err != nil { + fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error()) os.Exit(2) } - newBase = mb.Encoding(args[1][0]) + newBase = encoder.Encoding() args = args[2:] case "-v": if len(args) < 2 { @@ -93,15 +69,16 @@ outer: } } for _, cidStr := range args[1:] { - base, cid, err := decode(cidStr) + cid, err := c.Decode(cidStr) if err != nil { fmt.Fprintf(os.Stdout, "!INVALID_CID!\n") errorMsg("%s: %v", cidStr, err) // Don't abort on a bad cid continue } - if newBase != -1 { - base = newBase + base := newBase + if newBase == -1 { + base, _ = c.ExtractEncoding(cidStr) } if verConv != nil { cid, err = verConv(cid) @@ -112,11 +89,18 @@ outer: continue } } - str, err := fmtCid(fmtStr, base, cid) - if err != nil { + str, err := c.Format(fmtStr, base, cid) + switch err.(type) { + case c.FormatStringError: fmt.Fprintf(os.Stderr, "Error: %v\n", err) - // An error here means a bad format string, no point in continuing os.Exit(2) + default: + fmt.Fprintf(os.Stdout, "!ERROR!\n") + errorMsg("%s: %v", cidStr, err) + // Don't abort on cid specific errors + continue + case nil: + // no error } fmt.Fprintf(os.Stdout, "%s\n", str) } @@ -132,139 +116,6 @@ func errorMsg(fmtStr string, a ...interface{}) { exitCode = 1 } -func decode(v string) (mb.Encoding, *c.Cid, error) { - if len(v) < 2 { - return 0, nil, c.ErrCidTooShort - } - - if len(v) == 46 && v[:2] == "Qm" { - hash, err := mh.FromB58String(v) - if err != nil { - return 0, nil, err - } - - return mb.Base58BTC, c.NewCidV0(hash), nil - } - - base, data, err := mb.Decode(v) - if err != nil { - return 0, nil, err - } - - cid, err := c.Cast(data) - - return base, cid, err -} - -const ERR_STR = "!ERROR!" - -func fmtCid(fmtStr string, base mb.Encoding, cid *c.Cid) (string, error) { - p := cid.Prefix() - out := new(bytes.Buffer) - var err error - for i := 0; i < len(fmtStr); i++ { - if fmtStr[i] != '%' { - out.WriteByte(fmtStr[i]) - continue - } - i++ - if i >= len(fmtStr) { - return "", fmt.Errorf("premature end of format string") - } - switch fmtStr[i] { - case '%': - out.WriteByte('%') - case 'b': // base name - out.WriteString(baseToString(base)) - case 'B': // base code - out.WriteByte(byte(base)) - case 'v': // version string - fmt.Fprintf(out, "cidv%d", p.Version) - case 'V': // version num - fmt.Fprintf(out, "%d", p.Version) - case 'c': // codec name - out.WriteString(codecToString(p.Codec)) - case 'C': // codec code - fmt.Fprintf(out, "%d", p.Codec) - case 'h': // hash fun name - out.WriteString(hashToString(p.MhType)) - case 'H': // hash fun code - fmt.Fprintf(out, "%d", p.MhType) - case 'L': // hash length - fmt.Fprintf(out, "%d", p.MhLength) - case 'm', 'M': // multihash encoded in base %b - out.WriteString(encode(base, cid.Hash(), fmtStr[i] == 'M')) - case 'd', 'D': // hash digest encoded in base %b - dec, err := mh.Decode(cid.Hash()) - if err != nil { - out.WriteString(ERR_STR) - errorMsg("%v", err) - continue - } - out.WriteString(encode(base, dec.Digest, fmtStr[i] == 'D')) - case 's': // cid string encoded in base %b - str, err := cid.StringOfBase(base) - if err != nil { - out.WriteString(ERR_STR) - errorMsg("%v", err) - continue - } - out.WriteString(str) - case 'S': // cid string without base prefix - out.WriteString(encode(base, cid.Bytes(), true)) - case 'P': // prefix - fmt.Fprintf(out, "cidv%d-%s-%s-%d", - p.Version, - codecToString(p.Codec), - hashToString(p.MhType), - p.MhLength, - ) - default: - return "", fmt.Errorf("unrecognized specifier in format string: %c", fmtStr[i]) - } - - } - return out.String(), err -} - -func baseToString(base mb.Encoding) string { - // FIXME: Use lookup tables when they are added to go-multibase - switch base { - case mb.Base58BTC: - return "base58btc" - default: - return fmt.Sprintf("base?%c", base) - } -} - -func codecToString(num uint64) string { - name, ok := c.CodecToStr[num] - if !ok { - return fmt.Sprintf("codec?%d", num) - } - return name -} - -func hashToString(num uint64) string { - name, ok := mh.Codes[num] - if !ok { - return fmt.Sprintf("hash?%d", num) - } - return name -} - -func encode(base mb.Encoding, data []byte, strip bool) string { - str, err := mb.Encode(base, data) - if err != nil { - errorMsg("%v", err) - return ERR_STR - } - if strip { - return str[1:] - } - return str -} - func toCidV0(cid *c.Cid) (*c.Cid, error) { if cid.Type() != c.DagProtobuf { return nil, fmt.Errorf("can't convert non-protobuf nodes to cidv0") diff --git a/cid-fmt/main_test.go b/cid-fmt/main_test.go index 568b21a..ce66c0a 100644 --- a/cid-fmt/main_test.go +++ b/cid-fmt/main_test.go @@ -4,77 +4,13 @@ import ( "fmt" "testing" - mb "github.com/multiformats/go-multibase" + c "github.com/ipfs/go-cid" ) -func TestFmt(t *testing.T) { - cids := map[string]string{ - "cidv0": "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn", - "cidv1": "zdj7WfLr9DhLrb1hsoSi4fSdjjxuZmeqgEtBPWxMLtPbDNbFD", - } - tests := []struct { - cidId string - newBase mb.Encoding - fmtStr string - result string - }{ - {"cidv0", -1, "%P", "cidv0-protobuf-sha2-256-32"}, - {"cidv0", -1, "%b-%v-%c-%h-%L", "base58btc-cidv0-protobuf-sha2-256-32"}, - {"cidv0", -1, "%s", "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"}, - {"cidv0", -1, "%S", "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"}, - {"cidv0", -1, "ver#%V/#%C/#%H/%L", "ver#0/#112/#18/32"}, - {"cidv0", -1, "%m", "zQmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"}, - {"cidv0", -1, "%M", "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"}, - {"cidv0", -1, "%d", "z72gdmFAgRzYHkJzKiL8MgMMRW3BTSCGyDHroPxJbxMJn"}, - {"cidv0", -1, "%D", "72gdmFAgRzYHkJzKiL8MgMMRW3BTSCGyDHroPxJbxMJn"}, - {"cidv0", 'B', "%S", "CIQFTFEEHEDF6KLBT32BFAGLXEZL4UWFNWM4LFTLMXQBCERZ6CMLX3Y"}, - {"cidv0", 'B', "%B%S", "BCIQFTFEEHEDF6KLBT32BFAGLXEZL4UWFNWM4LFTLMXQBCERZ6CMLX3Y"}, - {"cidv1", -1, "%P", "cidv1-protobuf-sha2-256-32"}, - {"cidv1", -1, "%b-%v-%c-%h-%L", "base58btc-cidv1-protobuf-sha2-256-32"}, - {"cidv1", -1, "%s", "zdj7WfLr9DhLrb1hsoSi4fSdjjxuZmeqgEtBPWxMLtPbDNbFD"}, - {"cidv1", -1, "%S", "dj7WfLr9DhLrb1hsoSi4fSdjjxuZmeqgEtBPWxMLtPbDNbFD"}, - {"cidv1", -1, "ver#%V/#%C/#%H/%L", "ver#1/#112/#18/32"}, - {"cidv1", -1, "%m", "zQmYFbmndVP7QqAVWyKhpmMuQHMaD88pkK57RgYVimmoh5H"}, - {"cidv1", -1, "%M", "QmYFbmndVP7QqAVWyKhpmMuQHMaD88pkK57RgYVimmoh5H"}, - {"cidv1", -1, "%d", "zAux4gVVsLRMXtsZ9fd3tFEZN4jGYB6kP37fgoZNTc11H"}, - {"cidv1", -1, "%D", "Aux4gVVsLRMXtsZ9fd3tFEZN4jGYB6kP37fgoZNTc11H"}, - {"cidv1", 'B', "%s", "BAFYBEIETJGSRL3EQPQPCABV3G6IUBYTSIFVQ24XRRHD3JUETSKLTGQ7DJA"}, - {"cidv1", 'B', "%S", "AFYBEIETJGSRL3EQPQPCABV3G6IUBYTSIFVQ24XRRHD3JUETSKLTGQ7DJA"}, - {"cidv1", 'B', "%B%S", "BAFYBEIETJGSRL3EQPQPCABV3G6IUBYTSIFVQ24XRRHD3JUETSKLTGQ7DJA"}, - } - for _, tc := range tests { - name := fmt.Sprintf("%s/%s", tc.cidId, tc.fmtStr) - if tc.newBase != -1 { - name = fmt.Sprintf("%s/%c", name, tc.newBase) - } - cidStr := cids[tc.cidId] - t.Run(name, func(t *testing.T) { - testFmt(t, cidStr, tc.newBase, tc.fmtStr, tc.result) - }) - } -} - -func testFmt(t *testing.T, cidStr string, newBase mb.Encoding, fmtStr string, result string) { - base, cid, err := decode(cidStr) - if newBase != -1 { - base = newBase - } - if err != nil { - t.Fatal(err) - } - str, err := fmtCid(fmtStr, base, cid) - if err != nil { - t.Fatal(err) - } - if str != result { - t.Error(fmt.Sprintf("expected: %s; but got: %s", result, str)) - } -} - func TestCidConv(t *testing.T) { cidv0 := "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn" cidv1 := "zdj7WbTaiJT1fgatdet9Ei9iDB5hdCxkbVyhyh8YTUnXMiwYi" - _, cid, err := decode(cidv0) + cid, err := c.Decode(cidv0) if err != nil { t.Fatal(err) } @@ -98,7 +34,7 @@ func TestCidConv(t *testing.T) { func TestBadCidConv(t *testing.T) { // this cid is a raw leaf and should not be able to convert to cidv0 cidv1 := "zb2rhhzX7uSKrtQ2ZZXFAabKiKFYZrJqKY2KE1cJ8yre2GSWZ" - _, cid, err := decode(cidv1) + cid, err := c.Decode(cidv1) if err != nil { t.Fatal(err) } diff --git a/cid.go b/cid.go index 7859f75..337e45f 100644 --- a/cid.go +++ b/cid.go @@ -234,6 +234,28 @@ func Decode(v string) (*Cid, error) { return Cast(data) } +// Extract the encoding from a Cid. If Decode on the same string did +// not return an error neither will this function. +func ExtractEncoding(v string) (mbase.Encoding, error) { + if len(v) < 2 { + return -1, ErrCidTooShort + } + + if len(v) == 46 && v[:2] == "Qm" { + return mbase.Base58BTC, nil + } + + encoding := mbase.Encoding(v[0]) + + // check encoding is valid + _, err := mbase.NewEncoder(encoding) + if err != nil { + return -1, err + } + + return encoding, nil +} + func uvError(read int) error { switch { case read == 0: diff --git a/format.go b/format.go new file mode 100644 index 0000000..78bff4c --- /dev/null +++ b/format.go @@ -0,0 +1,151 @@ +package cid + +import ( + "bytes" + "fmt" + + mb "github.com/multiformats/go-multibase" + mh "github.com/multiformats/go-multihash" +) + +// FormatRef is a string documenting the format string for the Format function +const FormatRef = ` + %% literal % + %b multibase name + %B multibase code + %v version string + %V version number + %c codec name + %C codec code + %h multihash name + %H multihash code + %L hash digest length + %m multihash encoded in base %b (with multibase prefix) + %M multihash encoded in base %b without multibase prefix + %d hash digest encoded in base %b (with multibase prefix) + %D hash digest encoded in base %b without multibase prefix + %s cid string encoded in base %b (1) + %S cid string encoded in base %b without multibase prefix + %P cid prefix: %v-%c-%h-%L + +(1) For CID version 0 the multibase must be base58btc and no prefix is +used. For Cid version 1 the multibase prefix is included. +` + +// Format formats a cid according to the format specificer as +// documented in the FormatRef constant +func Format(fmtStr string, base mb.Encoding, cid *Cid) (string, error) { + p := cid.Prefix() + var out bytes.Buffer + var err error + encoder, err := mb.NewEncoder(base) + if err != nil { + return "", err + } + for i := 0; i < len(fmtStr); i++ { + if fmtStr[i] != '%' { + out.WriteByte(fmtStr[i]) + continue + } + i++ + if i >= len(fmtStr) { + return "", FormatStringError{"premature end of format string", ""} + } + switch fmtStr[i] { + case '%': + out.WriteByte('%') + case 'b': // base name + out.WriteString(baseToString(base)) + case 'B': // base code + out.WriteByte(byte(base)) + case 'v': // version string + fmt.Fprintf(&out, "cidv%d", p.Version) + case 'V': // version num + fmt.Fprintf(&out, "%d", p.Version) + case 'c': // codec name + out.WriteString(codecToString(p.Codec)) + case 'C': // codec code + fmt.Fprintf(&out, "%d", p.Codec) + case 'h': // hash fun name + out.WriteString(hashToString(p.MhType)) + case 'H': // hash fun code + fmt.Fprintf(&out, "%d", p.MhType) + case 'L': // hash length + fmt.Fprintf(&out, "%d", p.MhLength) + case 'm', 'M': // multihash encoded in base %b + out.WriteString(encode(encoder, cid.Hash(), fmtStr[i] == 'M')) + case 'd', 'D': // hash digest encoded in base %b + dec, err := mh.Decode(cid.Hash()) + if err != nil { + return "", err + } + out.WriteString(encode(encoder, dec.Digest, fmtStr[i] == 'D')) + case 's': // cid string encoded in base %b + str, err := cid.StringOfBase(base) + if err != nil { + return "", err + } + out.WriteString(str) + case 'S': // cid string without base prefix + out.WriteString(encode(encoder, cid.Bytes(), true)) + case 'P': // prefix + fmt.Fprintf(&out, "cidv%d-%s-%s-%d", + p.Version, + codecToString(p.Codec), + hashToString(p.MhType), + p.MhLength, + ) + default: + return "", FormatStringError{"unrecognized specifier in format string", fmtStr[i-1 : i+1]} + } + + } + return out.String(), err +} + +// FormatStringError is the error return from Format when the format +// string is ill formed +type FormatStringError struct { + Message string + Specifier string +} + +func (e FormatStringError) Error() string { + if e.Specifier == "" { + return e.Message + } else { + return fmt.Sprintf("%s: %s", e.Message, e.Specifier) + } +} + +func baseToString(base mb.Encoding) string { + baseStr, ok := mb.EncodingToStr[base] + if !ok { + return fmt.Sprintf("base?%c", base) + } + return baseStr +} + +func codecToString(num uint64) string { + name, ok := CodecToStr[num] + if !ok { + return fmt.Sprintf("codec?%d", num) + } + return name +} + +func hashToString(num uint64) string { + name, ok := mh.Codes[num] + if !ok { + return fmt.Sprintf("hash?%d", num) + } + return name +} + +func encode(base mb.Encoder, data []byte, strip bool) string { + str := base.Encode(data) + if strip { + return str[1:] + } + return str +} diff --git a/format_test.go b/format_test.go new file mode 100644 index 0000000..019d85c --- /dev/null +++ b/format_test.go @@ -0,0 +1,73 @@ +package cid + +import ( + "fmt" + "testing" + + mb "github.com/multiformats/go-multibase" +) + +func TestFmt(t *testing.T) { + cids := map[string]string{ + "cidv0": "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn", + "cidv1": "zdj7WfLr9DhLrb1hsoSi4fSdjjxuZmeqgEtBPWxMLtPbDNbFD", + } + tests := []struct { + cidId string + newBase mb.Encoding + fmtStr string + result string + }{ + {"cidv0", -1, "%P", "cidv0-protobuf-sha2-256-32"}, + {"cidv0", -1, "%b-%v-%c-%h-%L", "base58btc-cidv0-protobuf-sha2-256-32"}, + {"cidv0", -1, "%s", "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"}, + {"cidv0", -1, "%S", "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"}, + {"cidv0", -1, "ver#%V/#%C/#%H/%L", "ver#0/#112/#18/32"}, + {"cidv0", -1, "%m", "zQmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"}, + {"cidv0", -1, "%M", "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"}, + {"cidv0", -1, "%d", "z72gdmFAgRzYHkJzKiL8MgMMRW3BTSCGyDHroPxJbxMJn"}, + {"cidv0", -1, "%D", "72gdmFAgRzYHkJzKiL8MgMMRW3BTSCGyDHroPxJbxMJn"}, + {"cidv0", 'B', "%S", "CIQFTFEEHEDF6KLBT32BFAGLXEZL4UWFNWM4LFTLMXQBCERZ6CMLX3Y"}, + {"cidv0", 'B', "%B%S", "BCIQFTFEEHEDF6KLBT32BFAGLXEZL4UWFNWM4LFTLMXQBCERZ6CMLX3Y"}, + {"cidv1", -1, "%P", "cidv1-protobuf-sha2-256-32"}, + {"cidv1", -1, "%b-%v-%c-%h-%L", "base58btc-cidv1-protobuf-sha2-256-32"}, + {"cidv1", -1, "%s", "zdj7WfLr9DhLrb1hsoSi4fSdjjxuZmeqgEtBPWxMLtPbDNbFD"}, + {"cidv1", -1, "%S", "dj7WfLr9DhLrb1hsoSi4fSdjjxuZmeqgEtBPWxMLtPbDNbFD"}, + {"cidv1", -1, "ver#%V/#%C/#%H/%L", "ver#1/#112/#18/32"}, + {"cidv1", -1, "%m", "zQmYFbmndVP7QqAVWyKhpmMuQHMaD88pkK57RgYVimmoh5H"}, + {"cidv1", -1, "%M", "QmYFbmndVP7QqAVWyKhpmMuQHMaD88pkK57RgYVimmoh5H"}, + {"cidv1", -1, "%d", "zAux4gVVsLRMXtsZ9fd3tFEZN4jGYB6kP37fgoZNTc11H"}, + {"cidv1", -1, "%D", "Aux4gVVsLRMXtsZ9fd3tFEZN4jGYB6kP37fgoZNTc11H"}, + {"cidv1", 'B', "%s", "BAFYBEIETJGSRL3EQPQPCABV3G6IUBYTSIFVQ24XRRHD3JUETSKLTGQ7DJA"}, + {"cidv1", 'B', "%S", "AFYBEIETJGSRL3EQPQPCABV3G6IUBYTSIFVQ24XRRHD3JUETSKLTGQ7DJA"}, + {"cidv1", 'B', "%B%S", "BAFYBEIETJGSRL3EQPQPCABV3G6IUBYTSIFVQ24XRRHD3JUETSKLTGQ7DJA"}, + } + for _, tc := range tests { + name := fmt.Sprintf("%s/%s", tc.cidId, tc.fmtStr) + if tc.newBase != -1 { + name = fmt.Sprintf("%s/%c", name, tc.newBase) + } + cidStr := cids[tc.cidId] + t.Run(name, func(t *testing.T) { + testFmt(t, cidStr, tc.newBase, tc.fmtStr, tc.result) + }) + } +} + +func testFmt(t *testing.T, cidStr string, newBase mb.Encoding, fmtStr string, result string) { + cid, err := Decode(cidStr) + if err != nil { + t.Fatal(err) + } + base := newBase + if newBase == -1 { + base, _ = ExtractEncoding(cidStr) + } + str, err := Format(fmtStr, base, cid) + if err != nil { + t.Fatal(err) + } + if str != result { + t.Error(fmt.Sprintf("expected: %s; but got: %s", result, str)) + } +} diff --git a/package.json b/package.json index 4dcadb0..e6ea612 100644 --- a/package.json +++ b/package.json @@ -15,9 +15,9 @@ }, { "author": "whyrusleeping", - "hash": "QmexBtiTTEwwn42Yi6ouKt6VqzpA6wjJgiW1oh9VfaRrup", + "hash": "QmSbvata2WqNkqGtZNg8MR3SKwnB8iQ7vTPJgWqB8bC5kR", "name": "go-multibase", - "version": "0.2.6" + "version": "0.2.7" } ], "gxVersion": "0.8.0",