core

package module
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2021 License: AGPL-3.0 Imports: 11 Imported by: 17

README

go-iden3-core

Go Reference Go Report Card Test Lint

Low level API to create and manipulate iden3 Claims.

WARNING

All code here is experimental and WIP

Testing

go test ./...

License

go-iden3-core is part of the iden3 project copyright 2018 0kims association and published with GPL-3 license, please check the LICENSE file for more details.

Documentation

Overview

Package core contains a low level API to create and manipulate claims.

Claims are created using the constructor function core.NewClaim. It accepts core.SchemaHash and a number of core.Option functions.

Index

Examples

Constants

View Source
const (
	SlotNameIndexA = SlotName("IndexA")
	SlotNameIndexB = SlotName("IndexB")
	SlotNameValueA = SlotName("ValueA")
	SlotNameValueB = SlotName("ValueB")
)

Variables

View Source
var ErrDataOverflow = errors.New("data does not fits SNARK size")

ErrDataOverflow means that given *big.Int value does not fit in Field Q e.g. greater than Q constant:

Q constant: 21888242871839275222246405745257275088548364400416034343698204186575808495617
View Source
var ErrIncorrectIDPosition = errors.New("incorrect ID position")

ErrIncorrectIDPosition means that passed position is not one of predefined: IDPositionIndex or IDPositionValue

View Source
var ErrNoID = errors.New("ID is not set")

ErrNoID returns when ID not found in the Claim.

View Source
var (
	// TypeBJP0 specifies the BJ-P0
	// - first 2 bytes: `00000000 00000000`
	// - curve of k_op: babyjubjub
	// - hash function: `Poseidon` with 4+4 elements
	TypeBJP0 = [2]byte{0x00, 0x00}
)

Functions

func CalculateChecksum

func CalculateChecksum(typ [2]byte, genesis [27]byte) [2]byte

CalculateChecksum returns the checksum for a given type and genesis_root, where checksum:

hash( [type | root_genesis ] )

func CheckChecksum

func CheckChecksum(id ID) bool

CheckChecksum returns a bool indicating if the ID.Checksum is consistent with the rest of the ID data

func DecomposeID

func DecomposeID(id ID) ([2]byte, [27]byte, [2]byte, error)

DecomposeID returns type, genesis and checksum from an ID

func IdenState

func IdenState(clr *merkletree.Hash,
	rer *merkletree.Hash,
	ror *merkletree.Hash) *merkletree.Hash

IdenState calculates the Identity State from the Claims Tree Root, Revocation Tree Root and Roots Tree Root.

Types

type Claim

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

func NewClaim

func NewClaim(schemaHash SchemaHash, options ...Option) (*Claim, error)

NewClaim creates new Claim with specified SchemaHash and any number of options. Using options you can specify any field in claim.

Example
var schemaHash SchemaHash
expDate := time.Date(2021, 1, 10, 20, 30, 0, 0, time.UTC)
claim, err := NewClaim(schemaHash,
	WithExpirationDate(expDate),
	WithVersion(42))
if err != nil {
	panic(err)
}
expDateRes, ok := claim.GetExpirationDate()
fmt.Println(ok)
fmt.Println(expDateRes.In(time.UTC).Format(time.RFC3339))

fmt.Println(claim.GetVersion())

entry := claim.TreeEntry()
indexHash, valueHash, err := entry.HiHv()
if err != nil {
	panic(err)
}
fmt.Println(hex.EncodeToString(indexHash[:]))
fmt.Println(hex.EncodeToString(valueHash[:]))
Output:
true
2021-01-10T20:30:00Z
42
a07b32a81b631544f9199f4bf429ad2026baec31ba5e5e707a49cc2c9d243f18
8e6bca4b559d758eca7b6125faea23ed0765cdcb6f85b3fe9477ca4293a6fd05

func (*Claim) Clone

func (c *Claim) Clone() *Claim

Clone returns full deep copy of claim

func (*Claim) GetExpirationDate

func (c *Claim) GetExpirationDate() (time.Time, bool)

GetExpirationDate returns expiration date and flag. Flag is true if expiration date is present, false if null.

func (*Claim) GetFlagUpdatable

func (c *Claim) GetFlagUpdatable() bool

GetFlagUpdatable returns claim's flag `updatable`

func (*Claim) GetID

func (c *Claim) GetID() (ID, error)

GetID returns ID from claim's index of value. Returns error ErrNoID if ID is not set.

func (*Claim) GetRevocationNonce

func (c *Claim) GetRevocationNonce() uint64

GetRevocationNonce returns revocation nonce

func (*Claim) GetSchemaHash

func (c *Claim) GetSchemaHash() SchemaHash

GetSchemaHash return copy of claim's schema hash.

func (*Claim) GetVersion

func (c *Claim) GetVersion() uint32

GetVersion returns claim's version

func (*Claim) ResetExpirationDate

func (c *Claim) ResetExpirationDate()

ResetExpirationDate removes expiration date from claim

func (*Claim) ResetID

func (c *Claim) ResetID()

ResetID deletes ID from index and from value.

func (*Claim) SetExpirationDate

func (c *Claim) SetExpirationDate(dt time.Time)

SetExpirationDate sets expiration date to dt

func (*Claim) SetFlagUpdatable

func (c *Claim) SetFlagUpdatable(val bool)

SetFlagUpdatable sets claim's flag `updatable`

func (*Claim) SetIndexData

func (c *Claim) SetIndexData(slotA, slotB DataSlot) error

SetIndexData sets data to index slots A & B. Returns ErrSlotOverflow if slotA or slotB value are too big.

func (*Claim) SetIndexDataBytes

func (c *Claim) SetIndexDataBytes(slotA, slotB []byte) error

SetIndexDataBytes sets data to index slots A & B. Returns ErrSlotOverflow if slotA or slotB value are too big.

func (*Claim) SetIndexDataInts

func (c *Claim) SetIndexDataInts(slotA, slotB *big.Int) error

SetIndexDataInts sets data to index slots A & B. Returns ErrSlotOverflow if slotA or slotB value are too big.

func (*Claim) SetIndexID

func (c *Claim) SetIndexID(id ID)

SetIndexID sets id to index. Removes id from value if any.

func (*Claim) SetRevocationNonce

func (c *Claim) SetRevocationNonce(nonce uint64)

SetRevocationNonce sets claim's revocation nonce

func (*Claim) SetSchemaHash

func (c *Claim) SetSchemaHash(schema SchemaHash)

SetSchemaHash updates claim's schema hash.

func (*Claim) SetValueData

func (c *Claim) SetValueData(slotA, slotB DataSlot) error

SetValueData sets data to value slots A & B. Returns ErrSlotOverflow if slotA or slotB value are too big.

func (*Claim) SetValueDataBytes

func (c *Claim) SetValueDataBytes(slotA, slotB []byte) error

SetValueDataBytes sets data to value slots A & B. Returns ErrSlotOverflow if slotA or slotB value are too big.

func (*Claim) SetValueDataInts

func (c *Claim) SetValueDataInts(slotA, slotB *big.Int) error

SetValueDataInts sets data to value slots A & B. Returns ErrSlotOverflow if slotA or slotB value are too big.

func (*Claim) SetValueID

func (c *Claim) SetValueID(id ID)

SetValueID sets id to value. Removes id from index if any.

func (*Claim) SetVersion

func (c *Claim) SetVersion(ver uint32)

SetVersion sets claim's version

func (*Claim) TreeEntry

func (c *Claim) TreeEntry() merkletree.Entry

TreeEntry creates new merkletree.Entry from the claim. Following changes to claim does not change returned merkletree.Entry.

type DataSlot

type DataSlot [32]byte

DataSlot length is 32 bytes. But not all 32-byte values are valid. The value should be not greater than Q constant 21888242871839275222246405745257275088548364400416034343698204186575808495617

func NewDataSlotFromInt

func NewDataSlotFromInt(i *big.Int) (DataSlot, error)

NewDataSlotFromInt creates new DataSlot from *big.Int. Returns error ErrDataOverflow if value is too large to fill the Field Q.

func (*DataSlot) SetInt

func (ds *DataSlot) SetInt(value *big.Int) error

SetInt sets data slot to serialized value of *big.Int. And checks that the value is valid (fills in Field Q). Returns ErrDataOverflow if the value is too large

func (DataSlot) ToInt

func (ds DataSlot) ToInt() *big.Int

ToInt returns *big.Int representation of DataSlot.

type ErrSlotOverflow

type ErrSlotOverflow struct {
	Field SlotName
}

ErrSlotOverflow means some DataSlot overflows Q Field. And wraps the name of overflowed slot.

func (ErrSlotOverflow) Error

func (e ErrSlotOverflow) Error() string

type ID

type ID [31]byte

ID is a byte array with [ type | root_genesis | checksum ] [2 bytes | 27 bytes | 2 bytes ] where the root_genesis are the first 28 bytes from the hash root_genesis

func CalculateGenesisID

func CalculateGenesisID(clr *merkletree.Hash) (*ID, error)

CalculateGenesisID calculate genesis id based on provided claims tree root

func IDFromBytes

func IDFromBytes(b []byte) (ID, error)

IDFromBytes returns the ID from a given byte array

func IDFromString

func IDFromString(s string) (ID, error)

IDFromString returns the ID from a given string

func IdGenesisFromIdenState

func IdGenesisFromIdenState(hash *merkletree.Hash) *ID

IdGenesisFromIdenState calculates the genesis Id from an Identity State.

func NewID

func NewID(typ [2]byte, genesis [27]byte) ID

NewID creates a new ID from a type and genesis

func (*ID) BigInt

func (id *ID) BigInt() *big.Int

func (*ID) Bytes

func (id *ID) Bytes() []byte

Bytes returns the bytes from the ID

func (*ID) Equal

func (id *ID) Equal(id2 *ID) bool

func (*ID) Equals

func (id *ID) Equals(id2 *ID) bool

func (ID) MarshalText

func (id ID) MarshalText() ([]byte, error)

func (*ID) String

func (id *ID) String() string

String returns a base58 from the ID

func (*ID) UnmarshalText

func (id *ID) UnmarshalText(b []byte) error

type IDPosition

type IDPosition uint8
const (
	// IDPositionIndex means ID value is in index slots
	IDPositionIndex IDPosition = iota + 1 // value 0 is position undefined
	// IDPositionValue means ID value is in value slots
	IDPositionValue
)

type Option

type Option func(*Claim) error

Option provides the ability to set different Claim's fields on construction

func WithExpirationDate

func WithExpirationDate(dt time.Time) Option

WithExpirationDate sets claim's expiration date to `dt`.

func WithFlagUpdatable

func WithFlagUpdatable(val bool) Option

WithFlagUpdatable sets claim's flag `updatable`

func WithID

func WithID(id ID, pos IDPosition) Option

WithID sets ID to claim's index or value depending on `pos`.

func WithIndexData

func WithIndexData(slotA, slotB DataSlot) Option

WithIndexData sets data to index slots A & B. Returns ErrSlotOverflow if slotA or slotB value are too big.

func WithIndexDataBytes

func WithIndexDataBytes(slotA, slotB []byte) Option

WithIndexDataBytes sets data to index slots A & B. Returns ErrSlotOverflow if slotA or slotB value are too big.

func WithIndexDataInts

func WithIndexDataInts(slotA, slotB *big.Int) Option

WithIndexDataInts sets data to index slots A & B. Returns ErrSlotOverflow if slotA or slotB value are too big.

func WithIndexID

func WithIndexID(id ID) Option

WithIndexID sets ID to claim's index

func WithRevocationNonce

func WithRevocationNonce(nonce uint64) Option

WithRevocationNonce sets claim's revocation nonce.

func WithValueData

func WithValueData(slotA, slotB DataSlot) Option

WithValueData sets data to value slots A & B. Returns ErrSlotOverflow if slotA or slotB value are too big.

func WithValueDataBytes

func WithValueDataBytes(slotA, slotB []byte) Option

WithValueDataBytes sets data to value slots A & B. Returns ErrSlotOverflow if slotA or slotB value are too big.

func WithValueDataInts

func WithValueDataInts(slotA, slotB *big.Int) Option

WithValueDataInts sets data to value slots A & B. Returns ErrSlotOverflow if slotA or slotB value are too big.

func WithValueID

func WithValueID(id ID) Option

WithValueID sets ID to claim's value

func WithVersion

func WithVersion(ver uint32) Option

WithVersion sets claim's version

type SchemaHash

type SchemaHash [schemaHashLn]byte

SchemaHash is a 16-bytes hash of file's content, that describes claim structure.

func (SchemaHash) MarshalText

func (sc SchemaHash) MarshalText() ([]byte, error)

MarshalText returns HEX representation of SchemaHash.

Returning error is always nil.

type SlotName

type SlotName string

type Subject

type Subject byte

Subject for the time being describes the location of ID (in index or value slots or nowhere at all).

Values SubjectInvalid, SubjectObjectIndex, SubjectObjectValue presents for backward compatibility and for now means nothing.

const (
	SubjectSelf           Subject = iota // 000
	SubjectInvalid                       // 001
	SubjectOtherIdenIndex                // 010
	SubjectOtherIdenValue                // 011
	SubjectObjectIndex                   // 100
	SubjectObjectValue                   // 101
)

Jump to

Keyboard shortcuts

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