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 ¶
- Constants
- Variables
- func CalculateChecksum(typ [2]byte, genesis [27]byte) [2]byte
- func CheckChecksum(id ID) bool
- func DecomposeID(id ID) ([2]byte, [27]byte, [2]byte, error)
- func ElemBytesToInts(elements []ElemBytes) []*big.Int
- func IdenState(clr, rer, ror *big.Int) (*big.Int, error)
- type Blockchain
- type Claim
- func (c *Claim) Clone() *Claim
- func (c *Claim) GetExpirationDate() (time.Time, bool)
- func (c *Claim) GetFlagUpdatable() bool
- func (c *Claim) GetID() (ID, error)
- func (c *Claim) GetIDPosition() (IDPosition, error)
- func (c *Claim) GetRevocationNonce() uint64
- func (c *Claim) GetSchemaHash() SchemaHash
- func (c *Claim) GetVersion() uint32
- func (c *Claim) HIndex() (*big.Int, error)
- func (c *Claim) HValue() (*big.Int, error)
- func (c *Claim) HiHv() (*big.Int, *big.Int, error)
- func (c Claim) MarshalBinary() ([]byte, error)
- func (c Claim) MarshalJSON() ([]byte, error)
- func (c *Claim) RawSlots() (index [4]ElemBytes, value [4]ElemBytes)
- func (c *Claim) RawSlotsAsInts() []*big.Int
- func (c *Claim) ResetExpirationDate()
- func (c *Claim) ResetID()
- func (c *Claim) SetExpirationDate(dt time.Time)
- func (c *Claim) SetFlagUpdatable(val bool)
- func (c *Claim) SetIndexData(slotA, slotB ElemBytes) error
- func (c *Claim) SetIndexDataBytes(slotA, slotB []byte) error
- func (c *Claim) SetIndexDataInts(slotA, slotB *big.Int) error
- func (c *Claim) SetIndexID(id ID)
- func (c *Claim) SetRevocationNonce(nonce uint64)
- func (c *Claim) SetSchemaHash(schema SchemaHash)
- func (c *Claim) SetValueData(slotA, slotB ElemBytes) error
- func (c *Claim) SetValueDataBytes(slotA, slotB []byte) error
- func (c *Claim) SetValueDataInts(slotA, slotB *big.Int) error
- func (c *Claim) SetValueID(id ID)
- func (c *Claim) SetVersion(ver uint32)
- func (c *Claim) UnmarshalBinary(data []byte) error
- func (c *Claim) UnmarshalJSON(in []byte) error
- type DID
- type DIDOption
- type ElemBytes
- type ErrSlotOverflow
- type ID
- type IDPosition
- type NetworkID
- type Option
- func WithExpirationDate(dt time.Time) Option
- func WithFlagUpdatable(val bool) Option
- func WithID(id ID, pos IDPosition) Option
- func WithIndexData(slotA, slotB ElemBytes) Option
- func WithIndexDataBytes(slotA, slotB []byte) Option
- func WithIndexDataInts(slotA, slotB *big.Int) Option
- func WithIndexID(id ID) Option
- func WithRevocationNonce(nonce uint64) Option
- func WithValueData(slotA, slotB ElemBytes) Option
- func WithValueDataBytes(slotA, slotB []byte) Option
- func WithValueDataInts(slotA, slotB *big.Int) Option
- func WithValueID(id ID) Option
- func WithVersion(ver uint32) Option
- type SchemaHash
- type SlotName
Examples ¶
Constants ¶
const ( SlotNameIndexA = SlotName("IndexA") SlotNameIndexB = SlotName("IndexB") SlotNameValueA = SlotName("ValueA") SlotNameValueB = SlotName("ValueB") )
const DIDMethod = "iden3"
DIDMethod DID method-name
const DIDSchema = "did"
DIDSchema DID Schema
Variables ¶
var ( // TypeDefault specifies the regular identity // - first 2 bytes: `00000000 00000000` TypeDefault = [2]byte{0x00, 0x00} // TypeReadOnly specifies the readonly identity, this type of identity MUST not be published on chain // - first 2 bytes: `00000000 00000001` TypeReadOnly = [2]byte{0b00000000, 0b00000001} )
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
var ( // ErrDoesNotMatchRegexp is returned when did string parsed ErrDoesNotMatchRegexp = errors.New("did does not match regex") )
var ErrIncorrectIDPosition = errors.New("incorrect ID position")
ErrIncorrectIDPosition means that passed position is not one of predefined: IDPositionIndex or IDPositionValue
var ErrInvalidSubjectPosition = errors.New("invalid subject position")
ErrInvalidSubjectPosition returns when subject position flags sets in invalid value.
var ErrNoID = errors.New("ID is not set")
ErrNoID returns when ID not found in the Claim.
Functions ¶
func CalculateChecksum ¶
CalculateChecksum returns the checksum for a given type and genesis_root, where checksum:
hash( [type | root_genesis ] )
func CheckChecksum ¶
CheckChecksum returns a bool indicating if the ID.Checksum is consistent with the rest of the ID data
func DecomposeID ¶
DecomposeID returns type, genesis and checksum from an ID
func ElemBytesToInts ¶ added in v0.0.13
ElemBytesToInts converts slice of ElemBytes to slice of *big.Int
Types ¶
type Blockchain ¶ added in v0.0.13
type Blockchain string
Blockchain id of the network "eth", "polygon", etc.
const ( ETHEREUM Blockchain = "eth" // ETHEREUM ethereum network POLYGON Blockchain = "polygon" // POLYGON polygon network )
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()) indexEntry, valueEntry := claim.RawSlots() indexHash, err := poseidon.Hash(ElemBytesToInts(indexEntry[:])) if err != nil { panic(err) } valueHash, err := poseidon.Hash(ElemBytesToInts(valueEntry[:])) if err != nil { panic(err) } indexSlot, err := NewElemBytesFromInt(indexHash) if err != nil { panic(err) } valueSlot, err := NewElemBytesFromInt(valueHash) if err != nil { panic(err) } fmt.Println(hex.EncodeToString(indexSlot[:])) fmt.Println(hex.EncodeToString(valueSlot[:]))
Output: true 2021-01-10T20:30:00Z 42 a07b32a81b631544f9199f4bf429ad2026baec31ba5e5e707a49cc2c9d243f18 8e6bca4b559d758eca7b6125faea23ed0765cdcb6f85b3fe9477ca4293a6fd05
func (*Claim) GetExpirationDate ¶
GetExpirationDate returns expiration date and flag. Flag is true if expiration date is present, false if null.
func (*Claim) GetFlagUpdatable ¶
GetFlagUpdatable returns claim's flag `updatable`
func (*Claim) GetID ¶
GetID returns ID from claim's index of value. Returns error ErrNoID if ID is not set.
func (*Claim) GetIDPosition ¶ added in v0.0.17
func (c *Claim) GetIDPosition() (IDPosition, error)
GetIDPosition returns the position at which the ID is stored.
func (*Claim) GetRevocationNonce ¶
GetRevocationNonce returns revocation nonce
func (*Claim) GetSchemaHash ¶
func (c *Claim) GetSchemaHash() SchemaHash
GetSchemaHash return copy of claim's schema hash.
func (Claim) MarshalBinary ¶ added in v0.0.14
func (Claim) MarshalJSON ¶ added in v0.0.14
func (*Claim) RawSlotsAsInts ¶ added in v0.0.14
RawSlotsAsInts returns slots as []*big.Int
func (*Claim) ResetExpirationDate ¶
func (c *Claim) ResetExpirationDate()
ResetExpirationDate removes expiration date from claim
func (*Claim) SetExpirationDate ¶
SetExpirationDate sets expiration date to dt
func (*Claim) SetFlagUpdatable ¶
SetFlagUpdatable sets claim's flag `updatable`
func (*Claim) SetIndexData ¶
SetIndexData sets data to index slots A & B. Returns ErrSlotOverflow if slotA or slotB value are too big.
func (*Claim) SetIndexDataBytes ¶
SetIndexDataBytes sets data to index slots A & B. Returns ErrSlotOverflow if slotA or slotB value are too big.
func (*Claim) SetIndexDataInts ¶
SetIndexDataInts sets data to index slots A & B. Returns ErrSlotOverflow if slotA or slotB value are too big.
func (*Claim) SetIndexID ¶
SetIndexID sets id to index. Removes id from value if any.
func (*Claim) SetRevocationNonce ¶
SetRevocationNonce sets claim's revocation nonce
func (*Claim) SetSchemaHash ¶
func (c *Claim) SetSchemaHash(schema SchemaHash)
SetSchemaHash updates claim's schema hash.
func (*Claim) SetValueData ¶
SetValueData sets data to value slots A & B. Returns ErrSlotOverflow if slotA or slotB value are too big.
func (*Claim) SetValueDataBytes ¶
SetValueDataBytes sets data to value slots A & B. Returns ErrSlotOverflow if slotA or slotB value are too big.
func (*Claim) SetValueDataInts ¶
SetValueDataInts sets data to value slots A & B. Returns ErrSlotOverflow if slotA or slotB value are too big.
func (*Claim) SetValueID ¶
SetValueID sets id to value. Removes id from index if any.
func (*Claim) UnmarshalBinary ¶ added in v0.0.14
func (*Claim) UnmarshalJSON ¶ added in v0.0.14
type DID ¶ added in v0.0.13
type DID struct { ID ID // ID did specific id Blockchain Blockchain // Blockchain network identifier eth / polygon,... NetworkID NetworkID // NetworkID specific network identifier eth {main, ropsten, rinkeby, kovan} }
DID Decentralized Identifiers (DIDs) https://w3c.github.io/did-core/#did-syntax
type DIDOption ¶ added in v0.0.13
func WithNetwork ¶ added in v0.0.13
func WithNetwork(blockchain Blockchain, network NetworkID) DIDOption
WithNetwork sets Blockchain and NetworkID (eth:main)
type ElemBytes ¶ added in v0.0.13
type ElemBytes [32]byte
ElemBytes length is 32 bytes. But not all 32-byte values are valid. The value should be not greater than Q constant 21888242871839275222246405745257275088548364400416034343698204186575808495617
func NewElemBytesFromInt ¶ added in v0.0.13
NewElemBytesFromInt creates new ElemBytes from *big.Int. Returns error ErrDataOverflow if value is too large to fill the Field Q.
type ErrSlotOverflow ¶
type ErrSlotOverflow struct {
Field SlotName
}
ErrSlotOverflow means some ElemBytes 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 IDFromBytes ¶
IDFromBytes returns the ID from a given byte array
func IDFromString ¶
IDFromString returns the ID from a given string
func IdGenesisFromIdenState ¶
IdGenesisFromIdenState calculates the genesis ID from an Identity State.
func (ID) MarshalText ¶
func (*ID) UnmarshalText ¶
type IDPosition ¶
type IDPosition uint8
const ( // IDPositionNone means ID value not located in claim. IDPositionNone IDPosition = iota // IDPositionIndex means ID value is in index slots. IDPositionIndex // IDPositionValue means ID value is in value slots. IDPositionValue )
type Option ¶
Option provides the ability to set different Claim's fields on construction
func WithExpirationDate ¶
WithExpirationDate sets claim's expiration date to `dt`.
func WithFlagUpdatable ¶
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 ¶
WithIndexData sets data to index slots A & B. Returns ErrSlotOverflow if slotA or slotB value are too big.
func WithIndexDataBytes ¶
WithIndexDataBytes sets data to index slots A & B. Returns ErrSlotOverflow if slotA or slotB value are too big.
func WithIndexDataInts ¶
WithIndexDataInts sets data to index slots A & B. Returns ErrSlotOverflow if slotA or slotB value are too big.
func WithRevocationNonce ¶
WithRevocationNonce sets claim's revocation nonce.
func WithValueData ¶
WithValueData sets data to value slots A & B. Returns ErrSlotOverflow if slotA or slotB value are too big.
func WithValueDataBytes ¶
WithValueDataBytes sets data to value slots A & B. Returns ErrSlotOverflow if slotA or slotB value are too big.
func WithValueDataInts ¶
WithValueDataInts sets data to value slots A & B. Returns ErrSlotOverflow if slotA or slotB value are too big.
type SchemaHash ¶
type SchemaHash [schemaHashLn]byte
SchemaHash is a 16-bytes hash of file's content, that describes claim structure.
func NewSchemaHashFromHex ¶ added in v0.0.14
func NewSchemaHashFromHex(s string) (SchemaHash, error)
NewSchemaHashFromHex creates new SchemaHash from hex string
func NewSchemaHashFromInt ¶ added in v0.0.16
func NewSchemaHashFromInt(i *big.Int) SchemaHash
NewSchemaHashFromInt creates new SchemaHash from big.Int
func (SchemaHash) BigInt ¶ added in v0.0.16
func (sc SchemaHash) BigInt() *big.Int
BigInt returns a bigInt presentation of SchemaHash
func (SchemaHash) MarshalText ¶
func (sc SchemaHash) MarshalText() ([]byte, error)
MarshalText returns HEX representation of SchemaHash.
Returning error is always nil.