bcgo

package module
Version: v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2021 License: Apache-2.0 Imports: 21 Imported by: 26

README

bcgo

This is a Go implementation of a blockchain using the BC data structures defined in Protobufs.

Build

go build

Documentation

Overview

Package containing utilities for BC in Go

Index

Constants

View Source
const (
	THRESHOLD_Z = 0
	THRESHOLD_I = 256 // (32 / 64) * 512 // Hash Space: 2 ^ 256 // Usage: General Purpose
	THRESHOLD_H = 272 // (34 / 64) * 512 // Hash Space: 2 ^ 240 // Usage: General Purpose
	THRESHOLD_G = 288 // (36 / 64) * 512 // Hash Space: 2 ^ 224 // Usage: General Purpose, Alias
	THRESHOLD_F = 304 // (38 / 64) * 512 // Hash Space: 2 ^ 208 // Usage: Hour Validation
	THRESHOLD_E = 320 // (40 / 64) * 512 // Hash Space: 2 ^ 192 // Usage: Day Validation
	THRESHOLD_D = 336 // (42 / 64) * 512 // Hash Space: 2 ^ 176 // Usage: Week Validation
	THRESHOLD_C = 352 // (44 / 64) * 512 // Hash Space: 2 ^ 160 // Usage: Year Validation
	THRESHOLD_B = 368 // (46 / 64) * 512 // Hash Space: 2 ^ 144 // Usage: Decade Validation
	THRESHOLD_A = 384 // (48 / 64) * 512 // Hash Space: 2 ^ 128 // Usage: Century Validation

	MAX_BLOCK_SIZE_BYTES   = uint64(2 * 1024 * 1024 * 1024) // 2Gb
	MAX_PAYLOAD_SIZE_BYTES = uint64(10 * 1024 * 1024)       // 10Mb
)
View Source
const (
	BC_HOST      = "bc.aletheiaware.com"
	BC_HOST_TEST = "test-bc.aletheiaware.com"

	BETA_FLAG = "BETA"
	LIVE_FLAG = "LIVE"
)
View Source
const (
	ERROR_CHAIN_INVALID   = "Chain invalid: %s"
	ERROR_CHAIN_TOO_SHORT = "Chain too short to replace current head: %d vs %d"
	ERROR_HASH_INCORRECT  = "Hash doesn't match block hash"
	ERROR_NAME_INCORRECT  = "Name doesn't match channel name: %s vs %s"
	ERROR_NAME_INVALID    = "Name invalid: %s"
)
View Source
const (
	ERROR_NO_ENTRIES_TO_MINE = "No entries to mine for channel: %s"
	ERROR_NONCE_WRAP_AROUND  = "Nonce wrapped around before reaching threshold"
)
View Source
const (
	ERROR_NO_SUCH_CHANNEL   = "No such channel: %s"
	ERROR_PAYLOAD_TOO_LARGE = "Payload too large: %s max: %s"
	ERROR_BLOCK_TOO_LARGE   = "Block too large: %s max: %s"
)
View Source
const (
	ERROR_CHANNEL_OUT_OF_DATE = "Channel out of date"
)

Variables

This section is empty.

Functions

func AddPeer

func AddPeer(directory, peer string) error

func Alias added in v1.2.0

func Alias() (string, error)

func BCHost added in v1.2.0

func BCHost() string

func BCWebsite added in v1.2.0

func BCWebsite() string

func BinarySizeToString

func BinarySizeToString(size uint64) string

func BooleanFlag added in v1.2.0

func BooleanFlag(name string) bool

func CacheDirectory added in v1.2.0

func CacheDirectory(directory string) (string, error)

func CertificateDirectory added in v1.2.0

func CertificateDirectory(directory string) (string, error)

func CreateRecords

func CreateRecords(creator Account, access []Identity, references []*Reference, reader io.Reader, callback func([]byte, *Record) error) (int, error)

Chunk the data from reader into individual records with their own secret key and access list

func DecimalSizeToString

func DecimalSizeToString(size uint64) string

func IsBeta

func IsBeta() bool

func IsLive

func IsLive() bool

func Iterate

func Iterate(channel string, hash []byte, block *Block, cache Cache, network Network, callback func([]byte, *Block) error) error

func IterateChronologically

func IterateChronologically(channel string, hash []byte, block *Block, cache Cache, network Network, callback func([]byte, *Block) error) error

func KeyDirectory added in v1.2.0

func KeyDirectory(directory string) (string, error)

func LastMinedTimestamp added in v1.2.0

func LastMinedTimestamp(node Node, channel Channel) (uint64, error)

func LoadConfig

func LoadConfig() error

func MineProto added in v1.2.0

func MineProto(node Node, channel Channel, threshold uint64, listener MiningListener, access []Identity, references []*Reference, message proto.Message) error

func MoneyToString

func MoneyToString(currency string, amount int64) string

func Ones

func Ones(data []byte) uint64

func Peers added in v1.2.0

func Peers(directory string) ([]string, error)

func PrintBlock

func PrintBlock(output io.Writer, prefix string, hash []byte, block *Block)

func PrintBlockEntry

func PrintBlockEntry(output io.Writer, prefix string, entry *BlockEntry)

func PrintRecord

func PrintRecord(output io.Writer, prefix string, hash []byte, record *Record)

func PrintReference

func PrintReference(output io.Writer, prefix string, reference *Reference)

func Read

func Read(channel string, hash []byte, block *Block, cache Cache, network Network, account Account, recordHash []byte, callback func(*BlockEntry, []byte, []byte) error) error

func ReadConfig

func ReadConfig(directory string) error

func ReadDelimitedProtobuf

func ReadDelimitedProtobuf(reader *bufio.Reader, destination proto.Message) error

func ReadKey

func ReadKey(channel string, hash []byte, block *Block, cache Cache, network Network, account Account, recordHash []byte, callback func([]byte) error) error

func RootDirectory added in v1.2.0

func RootDirectory() (string, error)

func RootDirectoryForUser added in v1.2.0

func RootDirectoryForUser(u *user.User) string

func SetupLogging

func SetupLogging(directory string) (*os.File, error)

func SplitRemoveEmpty

func SplitRemoveEmpty(s, sep string) []string

func Timestamp

func Timestamp() uint64

func TimestampToString

func TimestampToString(timestamp uint64) string

func ValidateName added in v1.2.0

func ValidateName(name string) error

ValidateName ensures all characters are in the set [a-zA-Z0-9.-_]

func WriteDelimitedProtobuf

func WriteDelimitedProtobuf(writer *bufio.Writer, source proto.Message) error

Types

type Account added in v1.2.0

type Account interface {
	Identity
	Decrypt(*BlockEntry, *Record_Access, func(*BlockEntry, []byte, []byte) error) error
	DecryptKey(*Record_Access, func([]byte) error) error
	Sign([]byte) ([]byte, cryptogo.SignatureAlgorithm, error)
}

type Block

type Block struct {
	// Timestamp (nanoseconds) when the block was created.
	Timestamp uint64 `protobuf:"fixed64,1,opt,name=timestamp,proto3" json:"timestamp,omitempty"`
	// Name of the channel.
	ChannelName string `protobuf:"bytes,2,opt,name=channel_name,json=channelName,proto3" json:"channel_name,omitempty"`
	// Length of chain in blocks (inclusive).
	Length uint64 `protobuf:"fixed64,3,opt,name=length,proto3" json:"length,omitempty"`
	// Hash of the previous block in the chain.
	Previous []byte `protobuf:"bytes,4,opt,name=previous,proto3" json:"previous,omitempty"`
	// Alias of the block miner's public key.
	Miner string `protobuf:"bytes,5,opt,name=miner,proto3" json:"miner,omitempty"`
	// The nonce mined to reach threshold.
	Nonce uint64 `protobuf:"fixed64,6,opt,name=nonce,proto3" json:"nonce,omitempty"`
	// The block's entries (list of hash/record pairs).
	Entry                []*BlockEntry `protobuf:"bytes,7,rep,name=entry,proto3" json:"entry,omitempty"`
	XXX_NoUnkeyedLiteral struct{}      `json:"-"`
	XXX_unrecognized     []byte        `json:"-"`
	XXX_sizecache        int32         `json:"-"`
}

func LoadBlock added in v1.2.0

func LoadBlock(channel string, cache Cache, network Network, hash []byte) (*Block, error)

func LoadBlockContainingRecord added in v1.2.0

func LoadBlockContainingRecord(channel string, cache Cache, network Network, hash []byte) (*Block, error)

func Mine added in v1.2.0

func Mine(node Node, channel Channel, threshold uint64, listener MiningListener) ([]byte, *Block, error)

func MineBlock added in v1.2.0

func MineBlock(node Node, channel Channel, threshold uint64, listener MiningListener, block *Block) ([]byte, *Block, error)

func MineEntries added in v1.2.0

func MineEntries(node Node, channel Channel, threshold uint64, listener MiningListener, entries []*BlockEntry) ([]byte, *Block, error)

func (*Block) Descriptor

func (*Block) Descriptor() ([]byte, []int)

func (*Block) GetChannelName

func (m *Block) GetChannelName() string

func (*Block) GetEntry

func (m *Block) GetEntry() []*BlockEntry

func (*Block) GetLength

func (m *Block) GetLength() uint64

func (*Block) GetMiner

func (m *Block) GetMiner() string

func (*Block) GetNonce

func (m *Block) GetNonce() uint64

func (*Block) GetPrevious

func (m *Block) GetPrevious() []byte

func (*Block) GetTimestamp

func (m *Block) GetTimestamp() uint64

func (*Block) ProtoMessage

func (*Block) ProtoMessage()

func (*Block) Reset

func (m *Block) Reset()

func (*Block) String

func (m *Block) String() string

func (*Block) XXX_DiscardUnknown

func (m *Block) XXX_DiscardUnknown()

func (*Block) XXX_Marshal

func (m *Block) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Block) XXX_Merge

func (m *Block) XXX_Merge(src proto.Message)

func (*Block) XXX_Size

func (m *Block) XXX_Size() int

func (*Block) XXX_Unmarshal

func (m *Block) XXX_Unmarshal(b []byte) error

type BlockEntry

type BlockEntry struct {
	// Hash of the record.
	RecordHash           []byte   `protobuf:"bytes,1,opt,name=record_hash,json=recordHash,proto3" json:"record_hash,omitempty"`
	Record               *Record  `protobuf:"bytes,2,opt,name=record,proto3" json:"record,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

func (*BlockEntry) Descriptor

func (*BlockEntry) Descriptor() ([]byte, []int)

func (*BlockEntry) GetRecord

func (m *BlockEntry) GetRecord() *Record

func (*BlockEntry) GetRecordHash

func (m *BlockEntry) GetRecordHash() []byte

func (*BlockEntry) ProtoMessage

func (*BlockEntry) ProtoMessage()

func (*BlockEntry) Reset

func (m *BlockEntry) Reset()

func (*BlockEntry) String

func (m *BlockEntry) String() string

func (*BlockEntry) XXX_DiscardUnknown

func (m *BlockEntry) XXX_DiscardUnknown()

func (*BlockEntry) XXX_Marshal

func (m *BlockEntry) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*BlockEntry) XXX_Merge

func (m *BlockEntry) XXX_Merge(src proto.Message)

func (*BlockEntry) XXX_Size

func (m *BlockEntry) XXX_Size() int

func (*BlockEntry) XXX_Unmarshal

func (m *BlockEntry) XXX_Unmarshal(b []byte) error

type Cache

type Cache interface {
	Head(channel string) (*Reference, error)
	Block(hash []byte) (*Block, error)
	BlockEntries(channel string, timestamp uint64) ([]*BlockEntry, error)
	BlockContainingRecord(channel string, hash []byte) (*Block, error)
	PutHead(channel string, reference *Reference) error
	PutBlock(hash []byte, block *Block) error
	PutBlockEntry(channel string, entry *BlockEntry) error
}

type Channel

type Channel interface {
	fmt.Stringer
	Name() string
	Head() []byte
	Timestamp() uint64
	AddTrigger(func())
	AddValidator(Validator)
	Update(Cache, Network, []byte, *Block) error
	Set(uint64, []byte)
	Load(Cache, Network) error
	Refresh(Cache, Network) error
	Pull(Cache, Network) error
	Push(Cache, Network) error
}

type Identity added in v1.2.0

type Identity interface {
	Alias() string
	PublicKey() ([]byte, cryptogo.PublicKeyFormat, error)
	Encrypt([]byte) ([]byte, cryptogo.EncryptionAlgorithm, error)
	EncryptKey([]byte) (*Record_Access, error)
	Verify([]byte, []byte, cryptogo.SignatureAlgorithm) error
}

type LoggingMiningListener added in v1.1.5

type LoggingMiningListener struct {
}

func (*LoggingMiningListener) OnMiningStarted added in v1.1.5

func (l *LoggingMiningListener) OnMiningStarted(channel Channel, size uint64)

func (*LoggingMiningListener) OnMiningThresholdReached added in v1.1.5

func (l *LoggingMiningListener) OnMiningThresholdReached(channel Channel, hash []byte, block *Block)

func (*LoggingMiningListener) OnNewMaxOnes added in v1.1.5

func (l *LoggingMiningListener) OnNewMaxOnes(channel Channel, nonce, ones uint64)

type MiningListener

type MiningListener interface {
	OnMiningStarted(channel Channel, size uint64)
	OnNewMaxOnes(channel Channel, nonce, ones uint64)
	OnMiningThresholdReached(channel Channel, hash []byte, block *Block)
}

type Network

type Network interface {
	// Requests the head hash of the given channel
	Head(channel string) (*Reference, error)
	// Requests the block from the given reference
	Block(reference *Reference) (*Block, error)
	// Broadcasts the channel update to the network
	Broadcast(channel Channel, cache Cache, hash []byte, block *Block) error
}

type Node

type Node interface {
	Account() Account
	Cache() Cache
	Network() Network
	AddChannel(Channel)
	Channel(string) (Channel, error)
	OpenChannel(string, func() Channel) Channel
	Channels() []Channel
	Write(uint64, Channel, []Identity, []*Reference, []byte) (*Reference, error)
}

type PrintingMiningListener

type PrintingMiningListener struct {
	Output io.Writer
}

func (*PrintingMiningListener) OnMiningStarted

func (p *PrintingMiningListener) OnMiningStarted(channel Channel, size uint64)

func (*PrintingMiningListener) OnMiningThresholdReached

func (p *PrintingMiningListener) OnMiningThresholdReached(channel Channel, hash []byte, block *Block)

func (*PrintingMiningListener) OnNewMaxOnes

func (p *PrintingMiningListener) OnNewMaxOnes(channel Channel, nonce, ones uint64)

type Record

type Record struct {
	// Timestamp (nanoseconds) when the record was created.
	Timestamp uint64 `protobuf:"fixed64,1,opt,name=timestamp,proto3" json:"timestamp,omitempty"`
	// Alias of the record creator's public key.
	Creator string `protobuf:"bytes,2,opt,name=creator,proto3" json:"creator,omitempty"`
	// The list of accesses granted.
	Access []*Record_Access `protobuf:"bytes,3,rep,name=access,proto3" json:"access,omitempty"`
	// Holds record content, optionally encrypted with a secret key.
	Payload []byte `protobuf:"bytes,4,opt,name=payload,proto3" json:"payload,omitempty"`
	// The algorithm used to compress the payload.
	CompressionAlgorithm cryptogo.CompressionAlgorithm `` /* 155-byte string literal not displayed */
	// The algorithm used to encrypt the payload.
	EncryptionAlgorithm cryptogo.EncryptionAlgorithm `` /* 151-byte string literal not displayed */
	// Signature of payload (signed by the record creator's private key).
	Signature []byte `protobuf:"bytes,7,opt,name=signature,proto3" json:"signature,omitempty"`
	// The algorithm used to sign the payload.
	SignatureAlgorithm cryptogo.SignatureAlgorithm `` /* 147-byte string literal not displayed */
	// References to previous records.
	Reference []*Reference `protobuf:"bytes,9,rep,name=reference,proto3" json:"reference,omitempty"`
	// Holds payload meta data.
	Meta                 map[string]string `` /* 150-byte string literal not displayed */
	XXX_NoUnkeyedLiteral struct{}          `json:"-"`
	XXX_unrecognized     []byte            `json:"-"`
	XXX_sizecache        int32             `json:"-"`
}

func CreateRecord

func CreateRecord(timestamp uint64, creator Account, access []Identity, references []*Reference, payload []byte) ([]byte, *Record, error)

func (*Record) Descriptor

func (*Record) Descriptor() ([]byte, []int)

func (*Record) GetAccess

func (m *Record) GetAccess() []*Record_Access

func (*Record) GetCompressionAlgorithm

func (m *Record) GetCompressionAlgorithm() cryptogo.CompressionAlgorithm

func (*Record) GetCreator

func (m *Record) GetCreator() string

func (*Record) GetEncryptionAlgorithm

func (m *Record) GetEncryptionAlgorithm() cryptogo.EncryptionAlgorithm

func (*Record) GetMeta

func (m *Record) GetMeta() map[string]string

func (*Record) GetPayload

func (m *Record) GetPayload() []byte

func (*Record) GetReference

func (m *Record) GetReference() []*Reference

func (*Record) GetSignature

func (m *Record) GetSignature() []byte

func (*Record) GetSignatureAlgorithm

func (m *Record) GetSignatureAlgorithm() cryptogo.SignatureAlgorithm

func (*Record) GetTimestamp

func (m *Record) GetTimestamp() uint64

func (*Record) ProtoMessage

func (*Record) ProtoMessage()

func (*Record) Reset

func (m *Record) Reset()

func (*Record) String

func (m *Record) String() string

func (*Record) XXX_DiscardUnknown

func (m *Record) XXX_DiscardUnknown()

func (*Record) XXX_Marshal

func (m *Record) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Record) XXX_Merge

func (m *Record) XXX_Merge(src proto.Message)

func (*Record) XXX_Size

func (m *Record) XXX_Size() int

func (*Record) XXX_Unmarshal

func (m *Record) XXX_Unmarshal(b []byte) error

type Record_Access

type Record_Access struct {
	// Alias of the public key granted access, empty if public.
	Alias string `protobuf:"bytes,1,opt,name=alias,proto3" json:"alias,omitempty"`
	// The secret access key used to encrypt the payload.
	SecretKey []byte `protobuf:"bytes,2,opt,name=secret_key,json=secretKey,proto3" json:"secret_key,omitempty"`
	// If the alias is set, the secret key will be encrypted by the alias' public key.
	// The algorithm used to encrypt the secret key.
	EncryptionAlgorithm  cryptogo.EncryptionAlgorithm `` /* 151-byte string literal not displayed */
	XXX_NoUnkeyedLiteral struct{}                     `json:"-"`
	XXX_unrecognized     []byte                       `json:"-"`
	XXX_sizecache        int32                        `json:"-"`
}

func (*Record_Access) Descriptor

func (*Record_Access) Descriptor() ([]byte, []int)

func (*Record_Access) GetAlias

func (m *Record_Access) GetAlias() string

func (*Record_Access) GetEncryptionAlgorithm

func (m *Record_Access) GetEncryptionAlgorithm() cryptogo.EncryptionAlgorithm

func (*Record_Access) GetSecretKey

func (m *Record_Access) GetSecretKey() []byte

func (*Record_Access) ProtoMessage

func (*Record_Access) ProtoMessage()

func (*Record_Access) Reset

func (m *Record_Access) Reset()

func (*Record_Access) String

func (m *Record_Access) String() string

func (*Record_Access) XXX_DiscardUnknown

func (m *Record_Access) XXX_DiscardUnknown()

func (*Record_Access) XXX_Marshal

func (m *Record_Access) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Record_Access) XXX_Merge

func (m *Record_Access) XXX_Merge(src proto.Message)

func (*Record_Access) XXX_Size

func (m *Record_Access) XXX_Size() int

func (*Record_Access) XXX_Unmarshal

func (m *Record_Access) XXX_Unmarshal(b []byte) error

type Reference

type Reference struct {
	// Timestamp (nanoseconds) when the referenced item was created.
	Timestamp uint64 `protobuf:"fixed64,1,opt,name=timestamp,proto3" json:"timestamp,omitempty"`
	// Name of the channel holding the referenced item.
	ChannelName string `protobuf:"bytes,2,opt,name=channel_name,json=channelName,proto3" json:"channel_name,omitempty"`
	// Hash of the block holding the referenced item.
	BlockHash []byte `protobuf:"bytes,3,opt,name=block_hash,json=blockHash,proto3" json:"block_hash,omitempty"`
	// Hash of the record holding the referenced item.
	RecordHash []byte `protobuf:"bytes,4,opt,name=record_hash,json=recordHash,proto3" json:"record_hash,omitempty"`
	// Index of block in chain holding the referenced item.
	Index                uint64   `protobuf:"fixed64,5,opt,name=index,proto3" json:"index,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

func LoadHead added in v1.2.0

func LoadHead(channel string, cache Cache, network Network) (*Reference, error)

func WriteRecord

func WriteRecord(channel string, cache Cache, record *Record) (*Reference, error)

func (*Reference) Descriptor

func (*Reference) Descriptor() ([]byte, []int)

func (*Reference) GetBlockHash

func (m *Reference) GetBlockHash() []byte

func (*Reference) GetChannelName

func (m *Reference) GetChannelName() string

func (*Reference) GetIndex

func (m *Reference) GetIndex() uint64

func (*Reference) GetRecordHash

func (m *Reference) GetRecordHash() []byte

func (*Reference) GetTimestamp

func (m *Reference) GetTimestamp() uint64

func (*Reference) ProtoMessage

func (*Reference) ProtoMessage()

func (*Reference) Reset

func (m *Reference) Reset()

func (*Reference) String

func (m *Reference) String() string

func (*Reference) XXX_DiscardUnknown

func (m *Reference) XXX_DiscardUnknown()

func (*Reference) XXX_Marshal

func (m *Reference) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Reference) XXX_Merge

func (m *Reference) XXX_Merge(src proto.Message)

func (*Reference) XXX_Size

func (m *Reference) XXX_Size() int

func (*Reference) XXX_Unmarshal

func (m *Reference) XXX_Unmarshal(b []byte) error

type StopIterationError

type StopIterationError struct {
}

func (StopIterationError) Error

func (e StopIterationError) Error() string

type Validator

type Validator interface {
	Validate(channel Channel, cache Cache, network Network, hash []byte, block *Block) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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