status-go/vendor/github.com/wealdtech/go-multicodec
Andrea Maria Piana 4d15ae8a85 Upgrade to go 1.18 2022-05-11 12:39:54 +01:00
..
.gitignore
.travis.yml
LICENSE
README.md
codecs.go Upgrade go-ens 2021-07-20 10:57:38 +02:00
generate.go
generate.sh
multicodec.go Upgrade go-ens 2021-07-20 10:57:38 +02:00

README.md

go-multicodec

Tag License GoDoc Travis CI codecov.io

Go utility library to provide encoding and decoding of multicodec values.

Table of Contents

Install

go-multicodec is a standard Go module which can be installed with:

go get github.com/wealdtech/go-multicodec

Usage

Example

import (
    "bytes"
    "encoding/hex"
    "errors"

    multicodec "github.com/wealdtech/go-multicodec"
)

func main() {
    // Data in this case is an IPFS hash in multihash format with a dag-pb content type
    data, err := hex.DecodeString("70122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f")
    if err != nil {
        panic(err)
    }

    // Add the "ipfs-ns" namespace codec
    dataWithCodec, err := multicodec.AddCodec("ipfs-ns", data)
    if err != nil {
        panic(err)
    }

    if !multicodec.IsCodec("ipfs-ns", dataWithCodec) {
        panic(errors.New("data does not have correct codec prefix"))
    }

    // Remove the codec
    dataWithoutCodec, _, err := multicodec.RemoveCodec(dataWithCodec)
    if err != nil {
        panic(err)
    }

    if !bytes.Equal(data, dataWithoutCodec) {
        panic(errors.New("data mismatch"))
    }
}

Maintainers

Jim McDonald: @mcdee.

Contribute

Contributions welcome. Please check out the issues.

License

Apache-2.0 © 2019 Weald Technology Trading Ltd