store

package
v0.0.0-...-ddd0786 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2016 License: ISC Imports: 14 Imported by: 0

README

store

Package store is a data store for everything that bmclient needs. Refer to godoc for more docs.

Database structure:

- powQueue (bucket) (FIFO data structure)
-- 0x0000000000000001 (Queue entry #1)
--- Target (8 bytes) || Object Hash (64 bytes)

- pubkeyRequests (bucket)
-- BM-blahblahblah
--- number of requests (uint32) || last request time (binary time serialized 
      using time.MarshalBinary)

- misc (bucket)
-- dbMasterKey (encrypted)
-- salt
-- mailboxLatestID
-- powQueueLatestID
-- counters (bucket)
--- 0x00000000 (wire.ObjectTypeGetPubKey)
--- 0x00000002 (wire.ObjectTypeMsg)
--- 0x00000003 (wire.ObjectTypeBroadcast)

- mailboxes (bucket)
-- name (bucket)
--- data (bucket)
---- createdOn
--- 0x00000000000000010000000000000001 (Message of ID 1 and suffix 1)
---- Nonce (24 bytes) || Encrypted Contents

- broadcastAddresses (bucket)
-- BM-blahblahblah (no value)

Documentation

Overview

Package store is a persistent database for everything that bmclient needs for operation. This includes storing mailboxes and messages, proof of work queue, list of requested pubkeys.

Take note that this store does not encrypt everything. Only messages contents are encrypted. Encryption scheme used is SalsaX20 stream cipher with Poly1305 MAC, based on secretbox in NaCl.

WARNING: If both your database and password were compromised, changing your

password won't accomplish anything. This is because store encrypts
the master key using the password you specify when the database
is created. A malicious user that knew your password and had a
previous copy of the store knows the master key and can decrypt all
messages.

WARNING 2: Master key is kept in memory. The package is vulnerable to memory

reading malware.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is returned when a record matching the query or no record at
	// all is found in the database.
	ErrNotFound = errors.New("record not found")

	// ErrDecryptionFailed is returned when decryption of the master key fails.
	// This could be due to invalid passphrase or corrupt/tampered data.
	ErrDecryptionFailed = errors.New("invalid passphrase")

	// ErrDuplicateMailbox is returned by NewMailbox when a mailbox with the
	// given name already exists.
	ErrDuplicateMailbox = errors.New("duplicate mailbox")
)
View Source
var (

	// ErrDuplicateID is returned by InsertMessage when the a message with the
	// specified ID already exists in the folder.
	ErrDuplicateID = errors.New("duplicate ID")

	// ErrInvaidID is returned when an invalid id is provided.
	ErrInvalidID = errors.New("invalid ID")
)

Data keys of a mailbox.

Functions

func DisableLog

func DisableLog()

DisableLog disables all library log output. Logging output is disabled by default until either UseLogger or SetLogWriter are called.

func UseClientLogger

func UseClientLogger(logger btclog.Logger)

UseClientLogger uses a specified Logger to output client logging info. This should be used in preference to SetLogWriter if the caller is also using btclog.

func UseServerLogger

func UseServerLogger(logger btclog.Logger)

UseServerLogger uses a specified Logger to output client logging info. This should be used in preference to SetLogWriter if the caller is also using btclog.

Types

type BroadcastAddresses

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

BroadcastAddresses keeps track of the broadcasts that the user is listening to. It provides functionality for adding, removal and running a function for each address.

func (*BroadcastAddresses) Add

func (b *BroadcastAddresses) Add(address string) error

Add adds a new address to the store.

func (*BroadcastAddresses) ForEach

func (b *BroadcastAddresses) ForEach(f func(address *bmutil.Address) error) error

ForEach runs the specified function for each broadcast address, breaking early if an error occurs.

func (*BroadcastAddresses) Remove

func (b *BroadcastAddresses) Remove(address string) error

Remove removes an address from the store.

type Folder

type Folder interface {
	// Name returns the user-friendly name of the folder.
	Name() string

	// SetName changes the name of the folder.
	SetName(name string) error

	// InsertNewMessage inserts a new message with the specified suffix and
	// id into the folder and returns the ID. For normal folders, suffix
	// could be the encoding type. For special use folders like "Pending",
	// suffix could be used as a 'key', like a reason code (why the message is
	// marked as Pending).
	InsertNewMessage(msg []byte, suffix uint64) (uint64, error)

	// InsertMessage inserts a new message with the specified suffix and id
	// into the folder and returns the ID. If input id is 0 or >= NextID,
	// ErrInvalidID is returned.
	InsertMessage(id uint64, msg []byte, suffix uint64) error

	// GetMessage retrieves a message from the folder by its index. It returns the
	// suffix and the message. An error is returned if the message with the given
	// index doesn't exist in the database.
	GetMessage(id uint64) (uint64, []byte, error)

	// DeleteMessage deletes a message with the given index from the store. An error
	// is returned if the message doesn't exist in the store.
	DeleteMessage(id uint64) error

	// ForEachMessage runs the given function for messages that have IDs between
	// lowID and highID with the given suffix. If lowID is 0, it starts from the
	// first message. If highID is 0, it returns all messages with id >= lowID with
	// the given suffix. If suffix is zero, it returns all messages between lowID
	// and highID, irrespective of the suffix. Note that any combination of lowID,
	// highID and suffix can be zero for the desired results. Both lowID and highID
	// are inclusive.
	//
	// Suffix is useful for getting all messages of a particular type. For example,
	// retrieving all messages with encoding type 2.
	//
	// The function terminates early if an error occurs and iterates in the
	// increasing order of index. Make sure it doesn't take long to execute. DO NOT
	// execute any other database operations in it.
	ForEachMessage(lowID, highID, suffix uint64,
		fn func(id, suffix uint64, msg []byte) error) error

	// NextID returns the next index value that will be assigned in the mailbox..
	NextID() uint64

	// LastID returns the highest index value in the mailbox, followed by a
	// map containing the last indices for each suffix.
	LastID() (uint64, map[uint64]uint64)
}

type Loader

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

Type for transforming underlying database into Store. (used to abstract the details of the underlying bolt db).

func Open

func Open(file string) (*Loader, error)

Open creates a new Store from the given file.

func (*Loader) Close

func (l *Loader) Close() error

Close performs any necessary cleanups and then closes the database.

func (*Loader) Construct

func (l *Loader) Construct(pass []byte) (*Store, *PowQueue, *PKRequests, error)

Construct creates a new Store from the given file.

func (*Loader) IsEncrypted

func (l *Loader) IsEncrypted() bool

type PKRequests

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

PKRequests is a store that keeps track of getpubkey requests sent to the Bitmessage network. It stores the requested address along with the time the request was made. All addresses passed are expected to be valid Bitmessage addresses. A separate goroutine routinely queries the bmd RPC server for the public keys corresponding to the addresses in this store and removes them if bmd has received them. Thus, PKRequests serves as a watchlist.

func (*PKRequests) ForEach

func (r *PKRequests) ForEach(f func(address string, reqCount uint32,
	lastReqTime time.Time) error) error

ForEach runs the specified function for each item in the public key requests store, breaking early if an error occurs.

func (*PKRequests) LastRequestTime

func (r *PKRequests) LastRequestTime(addr string) (time.Time, error)

LastRequestTime returns the time that the last getpubkey request for the given address was sent out to the network. If the address doesn't exist, an ErrNotFound is returned.

func (*PKRequests) New

func (r *PKRequests) New(addr string) (uint32, error)

New adds a new address to the watchlist along with the current timestamp. If the address already exists, it increments the counter value specifying the number of times a getpubkey request was sent for the given address and returns it.

func (*PKRequests) Remove

func (r *PKRequests) Remove(addr string) error

Remove removes an address from the store.

type PowQueue

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

PowQueue is a FIFO queue for objects that need proof-of-work done on them. It implements Enqueue, Dequeue and Peek; the most basic queue operations.

func (*PowQueue) Dequeue

func (q *PowQueue) Dequeue() (uint64, uint32, []byte, error)

Dequeue removes the object message at beginning of the queue and returns its index, the user that requested it, and itself.

func (*PowQueue) Enqueue

func (q *PowQueue) Enqueue(target uint64, user uint32, obj []byte) (uint64, error)

Enqueue adds an object message with a target value for PoW to the end of the queue. It returns the index value of the stored element.

func (*PowQueue) PeekForPow

func (q *PowQueue) PeekForPow() (uint64, []byte, error)

PeekForPow returns the target and hash values for the object that would be removed when Dequeue is run next.

type Store

type Store struct {
	Users map[string]*UserData
	// contains filtered or unexported fields
}

Store persists all information about public key requests, pending POW, incoming/outgoing/pending messages to disk.

func (*Store) ChangePassphrase

func (s *Store) ChangePassphrase(pass []byte) error

ChangePassphrase changes the passphrase of the data store. It does not protect against a previous compromise of the data file. Refer to package docs for more details.

func (*Store) Close

func (s *Store) Close() error

Close performs any necessary cleanups and then closes the store.

func (*Store) GetCounter

func (s *Store) GetCounter(objType wire.ObjectType) (uint64, error)

GetCounter returns the stored counter value associated with the given object type.

func (*Store) GetUser

func (s *Store) GetUser(name string) (*UserData, error)

func (*Store) NewUser

func (s *Store) NewUser(name string) (*UserData, error)

func (*Store) SetCounter

func (s *Store) SetCounter(objType wire.ObjectType, counter uint64) error

SetCounter sets the counter value associated with the given object type.

type UserData

type UserData struct {
	BroadcastAddresses *BroadcastAddresses
	// contains filtered or unexported fields
}

func (*UserData) DeleteFolder

func (u *UserData) DeleteFolder(name string) error

Delete deletes the folder. Any operations after a Delete are invalid.

func (*UserData) FolderByName

func (u *UserData) FolderByName(name string) (Folder, error)

FolderByName retrieves the mailbox associated with the name. If the mailbox doesn't exist, an error is returned.

func (*UserData) Folders

func (u *UserData) Folders() []Folder

Folders returns a slice containing pointers to all folders for a given user.

func (*UserData) NewFolder

func (u *UserData) NewFolder(name string) (Folder, error)

NewFolder creates a new mailbox. Name must be unique.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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