merklize

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2023 License: Apache-2.0, MIT Imports: 17 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrorFieldIsEmpty is returned when field path to resolve is empty
	ErrorFieldIsEmpty = errors.New("fieldPath is empty")
	// ErrorContextTypeIsEmpty is returned when context type tp resolve is empty
	ErrorContextTypeIsEmpty = errors.New("ctxType is empty")
	// ErrorUnsupportedType is returned when type is not supported
	ErrorUnsupportedType = errors.New("unsupported type")
)
View Source
var ErrIncorrectType = errors.New("incorrect type")

Functions

func AddEntriesToMerkleTree

func AddEntriesToMerkleTree(ctx context.Context, mt mtAppender,
	entries []RDFEntry) error

func HashValue added in v1.1.3

func HashValue(datatype string, value any) (*big.Int, error)

HashValue hashes value according to datatype.

func HashValueWithHasher added in v1.1.3

func HashValueWithHasher(h Hasher, datatype string, value any) (*big.Int, error)

HashValueWithHasher hashes value according to datatype with a provided Hasher.

func NewDocumentLoader added in v1.2.1

func NewDocumentLoader(ipfsCli *shell.Shell, ipfsGW string) ld.DocumentLoader

NewDocumentLoader creates a new document loader with a cache for http. ipfs cache is not implemented yet.

func SetDocumentLoader added in v1.3.1

func SetDocumentLoader(docLoader ld.DocumentLoader)

SetDocumentLoader changes default DocumentLoader

func SetHasher

func SetHasher(h Hasher)

SetHasher changes default hasher

func TypeFromContext added in v1.1.3

func TypeFromContext(ctxBytes []byte, path string) (string, error)

TypeFromContext returns type of field from context by path.

func TypeIDFromContext added in v1.1.6

func TypeIDFromContext(ctxBytes []byte, typeName string) (string, error)

TypeIDFromContext returns @id attribute for type from JSON-LD context

Types

type Hasher

type Hasher interface {
	Hash(inpBI []*big.Int) (*big.Int, error)
	HashBytes(msg []byte) (*big.Int, error)
	Prime() *big.Int
}

Hasher is an interface to hash data

type MerkleTree

type MerkleTree interface {
	Add(context.Context, *big.Int, *big.Int) error
	GenerateProof(context.Context, *big.Int) (*merkletree.Proof, error)
	Root() *merkletree.Hash
}

MerkleTree is merkle tree structure

func MerkleTreeSQLAdapter

func MerkleTreeSQLAdapter(mt *merkletree.MerkleTree) MerkleTree

MerkleTreeSQLAdapter is merkle tree sql adapter

type MerklizeOption

type MerklizeOption func(m *Merklizer)

MerklizeOption is options for merklizer

func WithDocumentLoader added in v1.2.1

func WithDocumentLoader(documentLoader ld.DocumentLoader) MerklizeOption

WithDocumentLoader sets DocumentLoader

func WithHasher

func WithHasher(h Hasher) MerklizeOption

WithHasher sets Hasher option

func WithIPFSClient added in v1.2.1

func WithIPFSClient(ipfsCli *shell.Shell) MerklizeOption

WithIPFSClient sets IPFS client option required to resolve ipfs:// contexts. It works only if documentLoader is not set using WithDocumentLoader option. Otherwise, it will be ignored.

func WithIPFSGateway added in v1.2.1

func WithIPFSGateway(ipfsGW string) MerklizeOption

WithIPFSGateway sets IPFS gateway URL option required to resolve ipfs:// contexts.

If WithIPFSClient option is set, gateway would be ignored and ipfs requests would be sent directly to the client.

If WithDocumentLoader option is set, gateway would be ignored and documents would be loaded using the document loader.

func WithMerkleTree

func WithMerkleTree(mt MerkleTree) MerklizeOption

WithMerkleTree sets MerkleTree option

func WithSafeMode added in v1.1.0

func WithSafeMode(safeMode bool) MerklizeOption

WithSafeMode enables the Safe mode when extending a JSON-LD document. The default setting for this mode is "true". If the function encounters an unknown field with an incorrect IRI predicate, it will return an error. However, if the Safe mode is set to "false", the function will simply skip the incorrect field and continue the merklization process without it.

type Merklizer

type Merklizer struct {
	// contains filtered or unexported fields
}

Merklizer is a struct to work with json-ld doc merklization

func MerklizeJSONLD

func MerklizeJSONLD(ctx context.Context, in io.Reader,
	opts ...MerklizeOption) (*Merklizer, error)

MerklizeJSONLD takes a JSON-LD document, parses it and returns a Merklizer

func (*Merklizer) Hasher added in v1.1.3

func (m *Merklizer) Hasher() Hasher

func (*Merklizer) JSONLDType added in v1.1.3

func (m *Merklizer) JSONLDType(path Path) (string, error)

JSONLDType returns the JSON-LD type of the given path. If there is no literal by this path, it returns an error.

func (*Merklizer) MkValue

func (m *Merklizer) MkValue(val any) (Value, error)

func (*Merklizer) Proof

func (m *Merklizer) Proof(ctx context.Context,
	path Path) (*merkletree.Proof, Value, error)

Proof generate and return Proof and Value by the given Path. If the path is not found, it returns nil as value interface.

func (*Merklizer) RawValue

func (m *Merklizer) RawValue(path Path) (any, error)

func (*Merklizer) ResolveDocPath

func (m *Merklizer) ResolveDocPath(path string) (Path, error)

func (*Merklizer) Root

func (m *Merklizer) Root() *merkletree.Hash

type Options

type Options struct {
	Hasher         Hasher
	DocumentLoader ld.DocumentLoader
}

Options type allows to change hashing algorithm and create Path and RDFEntry instances with hasher different from default one.

func (Options) NewPath

func (o Options) NewPath(parts ...interface{}) (Path, error)

func (Options) NewPathFromDocument added in v1.3.1

func (o Options) NewPathFromDocument(docBytes []byte,
	path string) (Path, error)

func (Options) NewRDFEntry

func (o Options) NewRDFEntry(key Path, value interface{}) (RDFEntry, error)

func (Options) PathFromContext

func (o Options) PathFromContext(ctxBytes []byte, path string) (Path, error)

func (Options) TypeFromContext added in v1.3.1

func (o Options) TypeFromContext(ctxBytes []byte, path string) (string, error)

TypeFromContext returns type of field from context by path.

func (Options) TypeIDFromContext added in v1.3.1

func (o Options) TypeIDFromContext(ctxBytes []byte,
	typeName string) (string, error)

TypeIDFromContext returns @id attribute for type from JSON-LD context

type Path

type Path struct {
	// contains filtered or unexported fields
}

func NewFieldPathFromContext

func NewFieldPathFromContext(ctxBytes []byte, ctxType, fieldPath string) (Path, error)

NewFieldPathFromContext resolves field path without type path prefix

func NewPath

func NewPath(parts ...interface{}) (Path, error)

func NewPathFromContext

func NewPathFromContext(ctxBytes []byte, path string) (Path, error)

NewPathFromContext parses context and do its best to generate full Path from shortcut line field1.field2.field3...

func NewPathFromDocument

func NewPathFromDocument(docBytes []byte, path string) (Path, error)

func (*Path) Append

func (p *Path) Append(parts ...interface{}) error

func (*Path) MtEntry

func (p *Path) MtEntry() (*big.Int, error)

func (*Path) Parts

func (p *Path) Parts() []interface{}

func (*Path) Prepend

func (p *Path) Prepend(parts ...interface{}) error

Prepend path's parts from the beginning

type PoseidonHasher

type PoseidonHasher struct{}

PoseidonHasher is an applier of poseidon hash algorithm

func (PoseidonHasher) Hash

func (p PoseidonHasher) Hash(inpBI []*big.Int) (*big.Int, error)

Hash returns poseidon hash on big int params

func (PoseidonHasher) HashBytes

func (p PoseidonHasher) HashBytes(msg []byte) (*big.Int, error)

HashBytes returns poseidon hash on bytes

func (PoseidonHasher) Prime

func (p PoseidonHasher) Prime() *big.Int

Prime returns Q constant

type RDFEntry

type RDFEntry struct {
	// contains filtered or unexported fields
}

func EntriesFromRDF

func EntriesFromRDF(ds *ld.RDFDataset) ([]RDFEntry, error)

EntriesFromRDF creates entries from RDF dataset suitable to add to merkle tree

func EntriesFromRDFWithHasher

func EntriesFromRDFWithHasher(ds *ld.RDFDataset,
	hasher Hasher) ([]RDFEntry, error)

EntriesFromRDFWithHasher creates entries from RDF dataset suitable to add to with a provided Hasher merkle tree

func NewRDFEntry

func NewRDFEntry(key Path, value any) (RDFEntry, error)

func (RDFEntry) KeyMtEntry

func (e RDFEntry) KeyMtEntry() (*big.Int, error)

func (RDFEntry) KeyValueMtEntries

func (e RDFEntry) KeyValueMtEntries() (
	keyMtEntry *big.Int, valueMtEntry *big.Int, err error)

func (RDFEntry) ValueMtEntry

func (e RDFEntry) ValueMtEntry() (*big.Int, error)

type Value

type Value interface {
	MtEntry() (*big.Int, error)

	IsTime() bool
	AsTime() (time.Time, error)

	IsString() bool
	AsString() (string, error)

	IsInt64() bool
	AsInt64() (int64, error)

	IsBool() bool
	AsBool() (bool, error)
}

func NewValue

func NewValue(hasher Hasher, val any) (Value, error)

NewValue creates new Value

Jump to

Keyboard shortcuts

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