factom

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2019 License: MIT Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package factom provides data types corresponding to some of the Factom blockchain's data structures, as well as methods on those types for querying the data from factomd's JSON RPC API. This is not intended to be a general purpose package for querying factomd's JSON RPC API. Only the data required for use in fatd is queried and retained.

All of the Factom data structure types in this package have the Get and IsPopulated methods.

Get makes the appropriate factomd JSON RPC API queries to populate the data in the variable on which it is called.

IsPopulated returns whether the data in the variable has been populated by a successful call to Get.

Certain data already stored in the struct are used as the arguments for the factomd JSON RPC API queries. Which data needs to be populated prior to calling Get varies by the data type and is documented with the respective functions.

Get returns any critical errors, like networking or marshaling errors, but not JSON RPC errors, such as factomd not being able to find the given KeyMR. To check if the variable has been successfully populated, call IsPopulated().

The provided Factom types are designed to reduce moving around large chucks of memory when passing them around by copy. All raw data is held in either slices or pointers to arrays in the case of fixed length data that needs to be compared, like chain IDs, key MRs, hashes, etc. You should normally pass the Factom data structure types around by copy, but keep in mind that the underlying byte data is shared.

The Bytes and Bytes32 types are used when JSON marshaling and unmarshaling to and from hex encoded data is required.

Index

Constants

View Source
const (
	// RCDType is the magic number identifying the currenctly accepted RCD.
	RCDType byte = 0x01
	// RCDSize is the size of the RCD.
	RCDSize = ed25519.PublicKeySize + 1
	// SignatureSize is the size of the ed25519 signatures.
	SignatureSize = ed25519.SignatureSize
)

Variables

View Source
var DebugRPC bool
View Source
var GetHeights = factom.GetHeights

GetHeights returns a struct of Factom Blockchain Heights.

View Source
var RpcConfig = factom.RpcConfig

RpcConfig is a pointer to the RPC settings.

Functions

func FactomdRequest

func FactomdRequest(method string, params, result interface{}) error

func Request

func Request(endpoint, method string, params, result interface{}) error

request makes a JSON RPC request with the given method and params, and then parses the response with the given result type. request only returns networking and unmarshaling errors. JSON RPC Errors are not returned. It is up to the caller to determine whether their result is properly populated after request returns. Since data will need to be marshaled into result, the result type should be passed as a pointer.

func WalletRequest

func WalletRequest(method string, params, result interface{}) error

Types

type Address

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

Address represents a Factoid address.

func NewAddress

func NewAddress(rcdHash *RCDHash) Address

NewAddress returns an Address with the given rcdHash.

func NewAddressFromString

func NewAddressFromString(adrStr string) (Address, error)

NewAddressFromString returns an Address

func (*Address) FromString

func (a *Address) FromString(adrStr string) error

func (*Address) Get

func (a *Address) Get() error

func (Address) MarshalJSON

func (a Address) MarshalJSON() ([]byte, error)

MarshalJSON marshals a string with the human readable Factoid Address.

func (*Address) PrivateKey

func (a *Address) PrivateKey() *PrivateKey

func (*Address) PublicKey

func (a *Address) PublicKey() *[ed25519.PublicKeySize]byte

func (*Address) RCD

func (a *Address) RCD() []byte

RCD returns the RCD of the Address. If the rcd is nil, then it is computed and saved for future reuse. If the PrivateKey is nil, then the PrivateKey is allocated with all zeroes.

func (*Address) RCDHash

func (a *Address) RCDHash() *RCDHash

RCDHash returns the RCDHash of the Address. If the rcdHash is nil, then it is computed and saved for future reuse.

func (Address) String

func (a Address) String() string

String returns the human readable Factoid Address.

func (*Address) UnmarshalJSON

func (a *Address) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals a string with a human readable Factoid Address.

type Bytes

type Bytes []byte

Bytes implements json.Marshaler and json.Unmarshaler to encode and decode strings with hex encoded data, such as an Entry's external IDs or content.

func (Bytes) MarshalJSON

func (b Bytes) MarshalJSON() ([]byte, error)

MarshalJSON marshals b into hex encoded data.

func (*Bytes) Set

func (b *Bytes) Set(data string) error

func (Bytes) String

func (b Bytes) String() string

String returns the hex encoded data of b.

func (*Bytes) UnmarshalJSON

func (b *Bytes) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals a string of hex encoded data.

type Bytes32

type Bytes32 [32]byte

Bytes32 implements json.Marshaler and json.Unmarshaler to encode and decode strings with exactly 32 bytes of hex encoded data, such as chain IDs and key MRs.

func ChainID

func ChainID(nameIDs []Bytes) Bytes32

ChainID returns the chain ID for a set of NameIDs.

func EntryHash

func EntryHash(data []byte) Bytes32

EntryHash returns the Entry hash of data. Entry's are hashed via: sha256(sha512(data) + data).

func NewBytes32

func NewBytes32(s32 []byte) *Bytes32

NewBytes32 allocates a new Bytes32 object with the first 32 bytes of data contained in s32.

func ZeroBytes32

func ZeroBytes32() Bytes32

ZeroBytes32 returns an all zero Byte32.

func (Bytes32) MarshalJSON

func (b Bytes32) MarshalJSON() ([]byte, error)

MarshalJSON marshals b into hex encoded data.

func (*Bytes32) Scan

func (b *Bytes32) Scan(v interface{}) error

func (Bytes32) String

func (b Bytes32) String() string

String returns the hex encoded data of b.

func (*Bytes32) UnmarshalJSON

func (b *Bytes32) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals a string with exactly 32 bytes of hex encoded data.

func (Bytes32) Value

func (b Bytes32) Value() (driver.Value, error)

type DBlock

type DBlock struct {
	Height uint64 `json:"height"`

	// DBlock.Get populates EBlocks with their ChainID and KeyMR.
	EBlocks []EBlock `json:"dbentries,omitempty"`
}

DBlock represents a Factom Directory Block.

func (*DBlock) Get

func (db *DBlock) Get() error

Get queries factomd for the Directory Block at db.Height.

Get returns any networking or marshaling errors, but not JSON RPC errors. To check if the DBlock has been successfully populated, call IsPopulated().

func (DBlock) IsPopulated

func (db DBlock) IsPopulated() bool

IsPopulated returns true if db has already been successfully populated by a call to Get. IsPopulated returns false if db.EBlocks is nil.

type EBlock

type EBlock struct {
	// DBlock.Get populates the ChainID, KeyMR, and Height.
	ChainID *Bytes32 `json:"chainid,omitempty"`
	KeyMR   *Bytes32 `json:"keymr,omitempty"`

	// EBlock.Get populates the EBlockHeader.PrevKeyMR and the Entries with
	// their Hash and Timestamp.
	EBlockHeader `json:"header,omitempty"`
	Entries      []Entry `json:"entrylist,omitempty"`
}

EBlock represents an Factom Entry Block.

func (*EBlock) Get

func (eb *EBlock) Get() error

Get queries factomd for the Entry Block corresponding to eb.KeyMR, if not nil, and otherwise the Entry Block chain head for eb.ChainID. Either eb.KeyMR or eb.ChainID must be allocated or else Get will fail to populate the EBlock.

Get returns any networking or marshaling errors, but not JSON RPC errors. To check if the EBlock has been successfully populated, call IsPopulated().

func (EBlock) GetAllPrev

func (eb EBlock) GetAllPrev() ([]EBlock, error)

GetAllPrev returns a slice of all preceding EBlocks in eb's chain, in order from earliest to latest, up to and including eb. So the last element of the returned slice is always equal to eb. If eb is the first entry block in its chain, then it is the only element in the slice.

If you are only interested in obtaining the first entry block in eb's chain, and not all of the intermediary ones, then use GetFirst to reduce memory usage.

Like Get, GetAllPrev returns any networking or marshaling errors, but not JSON RPC errors. However, failing to populate any EBlock in the chain will result in returning a nil slice, thus it is unneccessary to call IsPopulated on any of the EBlocks in the returned slice.

func (*EBlock) GetChainHead

func (eb *EBlock) GetChainHead() error

func (*EBlock) GetFirst

func (eb *EBlock) GetFirst() error

GetFirst finds the first Entry Block in eb's chain, and populates eb as such.

GetFirst differs from GetAllPrev in that it does not allocate any additional EBlocks. GetFirst avoids allocating any new EBlocks by reusing eb to traverse up to the first entry block.

Like Get, GetFirst returns any networking or marshaling errors, but not JSON RPC errors. To check if the EBlock has been successfully populated, call IsPopulated().

func (EBlock) IsFirst

func (eb EBlock) IsFirst() bool

IsFirst returns true if this is the first EBlock in its chain, indicated by the PrevKeyMR being all zeroes. IsFirst returns false if eb is not populated or if the PrevKeyMR is not all zeroes.

func (EBlock) IsPopulated

func (eb EBlock) IsPopulated() bool

IsPopulated returns true if eb has already been successfully populated by a call to Get. Returns false if eb.PrevKeyMR is nil.

func (EBlock) Prev

func (eb EBlock) Prev() EBlock

Prev returns the previous EBlock in eb's chain, without populating it with a call to Get. In other words, the KeyMR will be equal to eb.PrevKeyMR and the ChainID will be equal to eb.ChainID. If eb is the first Entry Block in the chain, then eb is returned.

type EBlockHeader

type EBlockHeader struct {
	PrevKeyMR *Bytes32 `json:"prevkeymr,omitempty"`
	Height    uint64   `json:"dbheight"`
}

EBlockHeader is required for unmashaling the nested structure of the Entry Block response from the factomd JSON RPC API.

type Entry

type Entry struct {
	// EBlock.Get populates the Hash, Timestamp, ChainID, and Height.
	Hash      *Bytes32 `json:"entryhash,omitempty"`
	Timestamp *Time    `json:"timestamp,omitempty"`
	ChainID   *Bytes32 `json:"chainid,omitempty"`
	Height    uint64   `json:"-"`

	// Entry.Get populates the Content and ExtIDs.
	ExtIDs  []Bytes `json:"extids"`
	Content Bytes   `json:"content"`
}

Entry represents a Factom Entry.

func (Entry) ComputeHash

func (e Entry) ComputeHash() Bytes32

ComputeHash returns the Entry's hash as computed by hashing the binary representation of the Entry.

func (*Entry) Create

func (e *Entry) Create(ecpub string) (*Bytes32, error)

func (*Entry) Get

func (e *Entry) Get() error

Get queries factomd for the entry corresponding to e.Hash.

Get returns any networking or marshaling errors, but not JSON RPC errors. To check if the Entry has been successfully populated, call IsPopulated().

func (Entry) IsPopulated

func (e Entry) IsPopulated() bool

IsPopulated returns true if e has already been successfully populated by a call to Get. IsPopulated returns false if both e.ExtIDs and e.Content are nil.

func (Entry) MarshalBinary

func (e Entry) MarshalBinary() []byte

MarshalBinary marshals the entry to its binary representation. See UnmarshalBinary for encoding details.

func (*Entry) SetTimestampToNow

func (e *Entry) SetTimestampToNow()

func (*Entry) UnmarshalBinary

func (e *Entry) UnmarshalBinary(data []byte) error

UnmarshalBinary unmarshals raw entry data. It does not populate the Entry.Hash. Entries are encoded as follows and use big endian uint16:

[Version byte (0x00)] + [ChainID (Bytes32)] + [Total ExtID encoded length (uint16)] + [ExtID 0 length (uint16)] + [ExtID 0 (Bytes)] + ... + [ExtID X length (uint16)] + [ExtID X (Bytes)] + [Content (Bytes)]

https://github.com/FactomProject/FactomDocs/blob/master/factomDataStructureDetails.md#entry

type PrivateKey

type PrivateKey [ed25519.PrivateKeySize]byte

func NewPrivateKey

func NewPrivateKey(s64 []byte) *PrivateKey

func (*PrivateKey) Bytes

func (pk *PrivateKey) Bytes() *[ed25519.PrivateKeySize]byte

func (*PrivateKey) FromString

func (pk *PrivateKey) FromString(fsAdrStr string) error

func (PrivateKey) MarshalJSON

func (pk PrivateKey) MarshalJSON() ([]byte, error)

func (*PrivateKey) PublicKey

func (pk *PrivateKey) PublicKey() *[ed25519.PublicKeySize]byte

func (PrivateKey) String

func (pk PrivateKey) String() string

func (*PrivateKey) UnmarshalJSON

func (pk *PrivateKey) UnmarshalJSON(data []byte) error

type RCDHash

type RCDHash [32]byte

func NewRCDHash

func NewRCDHash(s32 []byte) *RCDHash

func (*RCDHash) FromString

func (rcdHash *RCDHash) FromString(faAdrStr string) error

func (RCDHash) MarshalJSON

func (rcdHash RCDHash) MarshalJSON() ([]byte, error)

func (*RCDHash) Scan

func (b *RCDHash) Scan(v interface{}) error

func (RCDHash) String

func (rcdHash RCDHash) String() string

func (*RCDHash) UnmarshalJSON

func (rcdHash *RCDHash) UnmarshalJSON(data []byte) error

func (RCDHash) Value

func (b RCDHash) Value() (driver.Value, error)

type Time

type Time struct {
	time.Time
}

Time embeds time.Time and implements the json.Unmarshaler interface for correctly parsing the timestamps returned by the factomd JSON RPC API.

func (*Time) MarshalJSON

func (t *Time) MarshalJSON() ([]byte, error)

MarshalJSON marshals a string containing a Unix seconds since epoch timestamp.

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals a string containing a Unix seconds since epoch timestamp.

Jump to

Keyboard shortcuts

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