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
- Variables
- func FactomdRequest(method string, params, result interface{}) error
- func Request(endpoint, method string, params, result interface{}) error
- func WalletRequest(method string, params, result interface{}) error
- type Address
- func (a *Address) FromString(adrStr string) error
- func (a *Address) Get() error
- func (a Address) MarshalJSON() ([]byte, error)
- func (a *Address) PrivateKey() *PrivateKey
- func (a *Address) PublicKey() *[ed25519.PublicKeySize]byte
- func (a *Address) RCD() []byte
- func (a *Address) RCDHash() *RCDHash
- func (a Address) String() string
- func (a *Address) UnmarshalJSON(data []byte) error
- type Bytes
- type Bytes32
- type DBlock
- type EBlock
- type EBlockHeader
- type Entry
- type PrivateKey
- func (pk *PrivateKey) Bytes() *[ed25519.PrivateKeySize]byte
- func (pk *PrivateKey) FromString(fsAdrStr string) error
- func (pk PrivateKey) MarshalJSON() ([]byte, error)
- func (pk *PrivateKey) PublicKey() *[ed25519.PublicKeySize]byte
- func (pk PrivateKey) String() string
- func (pk *PrivateKey) UnmarshalJSON(data []byte) error
- type RCDHash
- type Time
Constants ¶
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 ¶
var DebugRPC bool
var GetHeights = factom.GetHeights
GetHeights returns a struct of Factom Blockchain Heights.
var RpcConfig = factom.RpcConfig
RpcConfig is a pointer to the RPC settings.
Functions ¶
func FactomdRequest ¶
func Request ¶
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 ¶
Types ¶
type Address ¶
type Address struct {
// contains filtered or unexported fields
}
Address represents a Factoid address.
func NewAddress ¶
NewAddress returns an Address with the given rcdHash.
func NewAddressFromString ¶
NewAddressFromString returns an Address
func (*Address) FromString ¶
func (Address) MarshalJSON ¶
MarshalJSON marshals a string with the human readable Factoid Address.
func (*Address) PrivateKey ¶
func (a *Address) PrivateKey() *PrivateKey
func (*Address) RCD ¶
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 ¶
RCDHash returns the RCDHash of the Address. If the rcdHash is nil, then it is computed and saved for future reuse.
func (*Address) UnmarshalJSON ¶
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 ¶
MarshalJSON marshals b into hex encoded data.
func (*Bytes) UnmarshalJSON ¶
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 EntryHash ¶
EntryHash returns the Entry hash of data. Entry's are hashed via: sha256(sha512(data) + data).
func NewBytes32 ¶
NewBytes32 allocates a new Bytes32 object with the first 32 bytes of data contained in s32.
func (Bytes32) MarshalJSON ¶
MarshalJSON marshals b into hex encoded data.
func (*Bytes32) UnmarshalJSON ¶
UnmarshalJSON unmarshals a string with exactly 32 bytes of hex encoded data.
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 ¶
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 ¶
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 ¶
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 ¶
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 (*EBlock) GetFirst ¶
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 ¶
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 ¶
IsPopulated returns true if eb has already been successfully populated by a call to Get. Returns false if eb.PrevKeyMR is nil.
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 ¶
ComputeHash returns the Entry's hash as computed by hashing the binary representation of the Entry.
func (*Entry) Get ¶
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 ¶
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 ¶
MarshalBinary marshals the entry to its binary representation. See UnmarshalBinary for encoding details.
func (*Entry) SetTimestampToNow ¶
func (e *Entry) SetTimestampToNow()
func (*Entry) UnmarshalBinary ¶
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 (*RCDHash) FromString ¶
func (RCDHash) MarshalJSON ¶
func (*RCDHash) UnmarshalJSON ¶
type 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 ¶
MarshalJSON marshals a string containing a Unix seconds since epoch timestamp.
func (*Time) UnmarshalJSON ¶
UnmarshalJSON unmarshals a string containing a Unix seconds since epoch timestamp.