multicodec

package module
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 27, 2023 License: Apache-2.0 Imports: 2 Imported by: 1

README

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/pulsedomains/go-multicodec

Usage

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

    multicodec "github.com/pulsedomains/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

Documentation

Overview

Package multicodec provides the ability to add and remove codec prefixes from data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddCodec

func AddCodec(name string, data []byte) ([]byte, error)

AddCodec adds a codec prefix to a byte array. It returns a new byte array with the relevant codec prefixed.

func GetCodec

func GetCodec(data []byte) (uint64, error)

GetCodec returns the ID of the codec prefix from a byte array

func ID

func ID(name string) (uint64, error)

ID obtains the ID of a codec from its name

func IsCodec

func IsCodec(name string, data []byte) bool

IsCodec returns true if the data is of the supplied codec prefix, otherwise false

func MustID

func MustID(name string) uint64

MustID obtains the ID of a codec from its name, panicking if not present.

func MustName

func MustName(id uint64) string

MustName obtains the name of a codec from its ID, panicking if not present.

func Name

func Name(id uint64) (string, error)

Name obtains the name of a codec from its ID

func RemoveCodec

func RemoveCodec(data []byte) ([]byte, uint64, error)

RemoveCodec removes a codec prefix from a byte array. It returns a slice of the input byte array without the codec prefix, along with the ID of the codec that has been removed.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL