Skip to content

Commit

Permalink
Create a CID encoder that optionally upgrads a CIDv0 to CIDv1
Browse files Browse the repository at this point in the history
  • Loading branch information
kevina committed Dec 12, 2018
1 parent e19f717 commit eb61bed
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions cidenc/encoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cidenc

import (
cid "github.com/ipfs/go-cid"
mbase "github.com/multiformats/go-multibase"
)

// Encoder is a basic Encoder that will encode Cid's using a specifed
// base and optionally upgrade a CIDv0 to CIDv1
type Encoder struct {
Base mbase.Encoder // The multibase to use
Upgrade bool // If true upgrade CIDv0 to CIDv1 when encoding
}

// Default return a new default encoder
func Default() Encoder {
return Encoder{Base: mbase.MustNewEncoder(mbase.Base58BTC)}
}

// Encode encoded the cid using the parameters of the Encoder
func (enc Encoder) Encode(c cid.Cid) string {
if enc.Upgrade && c.Version() == 0 {
c = cid.NewCidV1(c.Type(), c.Hash())
}
return c.Encode(enc.Base)
}

// Recode reencodes the cid string to match the parameters of the
// encoder
func (enc Encoder) Recode(v string) (string, error) {
skip, err := enc.noopRecode(v)
if skip || err != nil {
return v, err
}

c, err := cid.Decode(v)
if err != nil {
return v, err
}

return enc.Encode(c), nil
}

func (enc Encoder) noopRecode(v string) (bool, error) {
if len(v) < 2 {
return false, cid.ErrCidTooShort
}
ver := cidVer(v)
skip := ver == 0 && !enc.Upgrade || ver == 1 && v[0] == byte(enc.Base.Encoding())
return skip, nil
}

func cidVer(v string) int {
if len(v) == 46 && v[:2] == "Qm" {
return 0
} else {
return 1
}
}

0 comments on commit eb61bed

Please sign in to comment.