db

package
v0.0.0-...-32f009b Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2018 License: MIT Imports: 25 Imported by: 5

Documentation

Index

Constants

View Source
const (
	StatusSent           Status = 100
	StatusFailedToSend   Status = 200
	StatusDelivered      Status = 300
	StatusFailedToHandle Status = 400
	StatusPersisted      Status = 500
	DAppMessageVersion   uint   = 1
)
View Source
const (
	SignedPreKeyValidTimeFrame = time.Hour * 24 * 60
)

Variables

View Source
var ValidMessage = func(m Message) error {

	if m.ID == "" {
		return errors.New("invalid message id (empty string)")
	}

	if m.Version == 0 {
		return errors.New("invalid version - got 0")
	}

	if _, exist := statuses[m.Status]; !exist {
		return fmt.Errorf("invalid status: %d (is not registered)", m.Status)
	}

	if m.DApp == nil && m.AddUserToChat == nil && len(m.Message) == 0 {
		return errors.New("got invalid message - dapp and message are both nil")
	}

	if m.DApp != nil {

		if len(m.DApp.DAppPublicKey) != 32 {
			return fmt.Errorf("invalid dapp public key of length %d", len(m.DApp.DAppPublicKey))
		}

	}

	if m.CreatedAt <= 2147483647 {
		return errors.New("invalid created at - must be bigger than 2147483647")
	}

	if len(m.Sender) != 32 && m.DApp == nil {
		return fmt.Errorf("invalid sender of length %d", len(m.Sender))
	}

	return nil

}

validate a given message

Functions

func KMToDBPath

func KMToDBPath(dir string, km *km.KeyManager) (string, error)

get database path for key manager

func Migrate

func Migrate(prodDBFile string, migrations []Migration) error

migrate migrates a bold db database

Types

type AddUserToChat

type AddUserToChat struct {
	Users    []ed25519.PublicKey
	ChatName string
	ChatID   []byte
}

type BoltChatStorage

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

func NewChatStorage

func NewChatStorage(db storm.Node, listeners []func(event MessagePersistedEvent), km *km.KeyManager) *BoltChatStorage

func (*BoltChatStorage) AddListener

func (s *BoltChatStorage) AddListener(fn func(e MessagePersistedEvent))

func (*BoltChatStorage) AllChats

func (s *BoltChatStorage) AllChats() ([]Chat, error)

func (*BoltChatStorage) CreateChat

func (s *BoltChatStorage) CreateChat(partner ed25519.PublicKey) (int, error)

func (*BoltChatStorage) CreateGroupChat

func (s *BoltChatStorage) CreateGroupChat(partners []ed25519.PublicKey, name string) (int, error)

func (*BoltChatStorage) CreateGroupChatFromMsg

func (s *BoltChatStorage) CreateGroupChatFromMsg(msg Message) error

func (*BoltChatStorage) GetChat

func (s *BoltChatStorage) GetChat(chatID int) (*Chat, error)

func (*BoltChatStorage) GetChatByPartner

func (s *BoltChatStorage) GetChatByPartner(partner ed25519.PublicKey) (*Chat, error)

func (*BoltChatStorage) GetGroupChatByRemoteID

func (s *BoltChatStorage) GetGroupChatByRemoteID(id []byte) (*Chat, error)

func (*BoltChatStorage) ReadMessages

func (s *BoltChatStorage) ReadMessages(chatID int) error

func (*BoltChatStorage) UnreadMessages

func (s *BoltChatStorage) UnreadMessages(c Chat) error

type BoltDRKeyStorage

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

func NewBoltDRKeyStorage

func NewBoltDRKeyStorage(db storm.Node, km *km.KeyManager) *BoltDRKeyStorage

func (*BoltDRKeyStorage) All

func (s *BoltDRKeyStorage) All() map[dr.Key]map[uint]dr.Key

func (*BoltDRKeyStorage) Count

func (s *BoltDRKeyStorage) Count(k dr.Key) uint

func (*BoltDRKeyStorage) DeleteMk

func (s *BoltDRKeyStorage) DeleteMk(k dr.Key, msgNum uint)

@todo we need to change the dr package to use a better interface (error handling)

func (*BoltDRKeyStorage) DeletePk

func (s *BoltDRKeyStorage) DeletePk(k dr.Key)

func (*BoltDRKeyStorage) Get

func (s *BoltDRKeyStorage) Get(k dr.Key, msgNum uint) (mk dr.Key, ok bool)

func (*BoltDRKeyStorage) Put

func (s *BoltDRKeyStorage) Put(k dr.Key, msgNum uint, mk dr.Key)

type BoltOneTimePreKeyStorage

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

func NewBoltOneTimePreKeyStorage

func NewBoltOneTimePreKeyStorage(db storm.Node, km *km.KeyManager) *BoltOneTimePreKeyStorage

func (*BoltOneTimePreKeyStorage) Count

func (s *BoltOneTimePreKeyStorage) Count() (uint32, error)

func (*BoltOneTimePreKeyStorage) Cut

func (s *BoltOneTimePreKeyStorage) Cut(pubKey []byte) (*x3dh.PrivateKey, error)

func (*BoltOneTimePreKeyStorage) Put

func (s *BoltOneTimePreKeyStorage) Put(keyPairs []x3dh.KeyPair) error

type BoltSharedSecretStorage

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

func NewBoltSharedSecretStorage

func NewBoltSharedSecretStorage(db storm.Node, km *keyManager.KeyManager) *BoltSharedSecretStorage

func (*BoltSharedSecretStorage) Accept

func (b *BoltSharedSecretStorage) Accept(sharedSec *SharedSecret) error

func (*BoltSharedSecretStorage) Get

func (b *BoltSharedSecretStorage) Get(partner ed25519.PublicKey, id []byte) (*SharedSecret, error)

func (*BoltSharedSecretStorage) GetYoungest

func (b *BoltSharedSecretStorage) GetYoungest(partner ed25519.PublicKey) (*SharedSecret, error)

func (*BoltSharedSecretStorage) HasAny

func (b *BoltSharedSecretStorage) HasAny(partner ed25519.PublicKey) (bool, error)

func (*BoltSharedSecretStorage) Put

type BoltSignedPreKeyStorage

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

func NewBoltSignedPreKeyStorage

func NewBoltSignedPreKeyStorage(db storm.Node, km *keyManager.KeyManager) *BoltSignedPreKeyStorage

func (*BoltSignedPreKeyStorage) All

func (s *BoltSignedPreKeyStorage) All() ([]*x3dh.KeyPair, error)

func (*BoltSignedPreKeyStorage) Get

func (*BoltSignedPreKeyStorage) Put

func (s *BoltSignedPreKeyStorage) Put(signedPreKey x3dh.KeyPair) error

type BoltToStormMigration

type BoltToStormMigration struct {
	Km *keyManager.KeyManager
}

func (*BoltToStormMigration) Migrate

func (m *BoltToStormMigration) Migrate(db *storm.DB) error

func (*BoltToStormMigration) Version

func (m *BoltToStormMigration) Version() uint32

type BoltUserStorage

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

func NewBoltUserStorage

func NewBoltUserStorage(db *storm.DB) *BoltUserStorage

func (*BoltUserStorage) GetSignedPreKey

func (s *BoltUserStorage) GetSignedPreKey(idKey ed25519.PublicKey) (*preKey.PreKey, error)

func (*BoltUserStorage) PutSignedPreKey

func (s *BoltUserStorage) PutSignedPreKey(idKey ed25519.PublicKey, key preKey.PreKey) error

type Chat

type Chat struct {
	ID            int `storm:"id,increment"`
	GroupChatName string
	// partner will only be filled if this is a private chat
	Partner           ed25519.PublicKey `storm:"index,unique"`
	Partners          []ed25519.PublicKey
	UnreadMessages    bool
	GroupChatRemoteID []byte `storm:"unique"`
	// contains filtered or unexported fields
}

func (*Chat) AddChatPartners

func (c *Chat) AddChatPartners(partners []ed25519.PublicKey) error

func (*Chat) GetMessage

func (c *Chat) GetMessage(msgID int64) (*Message, error)

func (*Chat) IsGroupChat

func (c *Chat) IsGroupChat() bool

@todo maybe more checks

func (*Chat) Messages

func (c *Chat) Messages(start int64, amount uint) ([]*Message, error)

func (*Chat) PersistMessage

func (c *Chat) PersistMessage(msg Message) error

Persist Message struct

func (*Chat) SaveMessage

func (c *Chat) SaveMessage(rawMessage []byte) error

save a raw message

type ChatStorage

type ChatStorage interface {
	GetChatByPartner(pubKey ed25519.PublicKey) (*Chat, error)
	GetChat(chatID int) (*Chat, error)
	GetGroupChatByRemoteID(id []byte) (*Chat, error)
	// returned int is the chat ID
	CreateChat(partner ed25519.PublicKey) (int, error)
	CreateGroupChat(partners []ed25519.PublicKey, name string) (int, error)
	CreateGroupChatFromMsg(createMessage Message) error
	AddListener(func(e MessagePersistedEvent))
	AllChats() ([]Chat, error)
	// set state of chat to unread messages
	UnreadMessages(c Chat) error
	// set state of chat all messages read
	ReadMessages(chatID int) error
}

type DAppMessage

type DAppMessage struct {
	DAppPublicKey []byte
	Type          string
	Params        map[string]interface{}
	ShouldSend    bool
}

type DRKey

type DRKey struct {
	ID     int            `storm:"id,increment"`
	Key    dr.Key         `storm:"index"`
	MsgNum uint           `storm:"index"`
	MsgKey aes.CipherText `storm:"index"`
}

type DRKeyStorage

type DRKeyStorage interface {
	Get(k dr.Key, msgNum uint) (mk dr.Key, ok bool)
	Put(k dr.Key, msgNum uint, mk dr.Key)
	DeleteMk(k dr.Key, msgNum uint)
	DeletePk(k dr.Key)
	Count(k dr.Key) uint
	All() map[dr.Key]map[uint]dr.Key
}

type Message

type Message struct {
	DBID             int `storm:"id,increment"`
	ID               string
	Version          uint
	Status           Status
	Received         bool
	DApp             *DAppMessage
	Message          []byte `json:"-"`
	PersistedMessage aes.CipherText
	CreatedAt        int64
	Sender           []byte `storm:"index"`
	// the UniqueID is a unix nano timestamp
	// it's only unique in the relation with a chat id
	UniqueMsgID   int64 `storm:"index"`
	ChatID        int   `storm:"index"`
	AddUserToChat *AddUserToChat
	GroupChatID   []byte
}

type MessagePersistedEvent

type MessagePersistedEvent struct {
	Chat    Chat
	Message Message
}

type Migration

type Migration interface {
	Migrate(db *storm.DB) error
	Version() uint32
}

type OneTimePreKeyStorage

type OneTimePreKeyStorage interface {
	Cut(pubKey []byte) (*x3dh.PrivateKey, error)
	Count() (uint32, error)
	Put(keyPairs []x3dh.KeyPair) error
}

type SharedSecret

type SharedSecret struct {
	// this prop is filled in during saving the shared secret
	X3dhSS aes.CipherText

	Accepted              bool              `storm:"index"`
	CreatedAt             time.Time         `storm:"index"`
	DestroyAt             *time.Time        `storm:"index"`
	Partner               ed25519.PublicKey `storm:"index"`
	ID                    []byte            `storm:"index"`
	DBID                  int               `storm:"id,increment"`
	UsedOneTimePreKey     *x3dh.PublicKey
	UsedSignedPreKey      x3dh.PublicKey
	EphemeralKey          x3dh.PublicKey
	EphemeralKeySignature []byte
	// contains filtered or unexported fields
}

func (*SharedSecret) GetX3dhSecret

func (ss *SharedSecret) GetX3dhSecret() x3dh.SharedSecret

@todo I don't like this solution. However, we still need to figure out @todo how to ignore fields from storm

func (*SharedSecret) SetX3dhSecret

func (ss *SharedSecret) SetX3dhSecret(secret x3dh.SharedSecret)

type SharedSecretStorage

type SharedSecretStorage interface {
	HasAny(key ed25519.PublicKey) (bool, error)
	// must return an error if no shared secret found
	GetYoungest(key ed25519.PublicKey) (*SharedSecret, error)
	Put(ss SharedSecret) error
	// accept will mark the given shared secret as accepted
	// and will set a destroy date for all other shared secrets
	Accept(sharedSec *SharedSecret) error
	// get sender public key and shared secret id
	Get(key ed25519.PublicKey, sharedSecretID []byte) (*SharedSecret, error)
}

type SignedPreKey

type SignedPreKey struct {
	ValidTill           int64 `storm:"index"`
	EncryptedPrivateKey aes.CipherText

	PublicKey x3dh.PublicKey `storm:"index,id"`
	Version   uint
	// contains filtered or unexported fields
}

func (*SignedPreKey) PrivateKey

func (s *SignedPreKey) PrivateKey() x3dh.PrivateKey

type SignedPreKeyStorage

type SignedPreKeyStorage interface {
	// persist the signed pre key
	// @todo don't forget to give the option to register a listener in the put function
	// @todo publish signed pre key to backend
	Put(signedPreKey x3dh.KeyPair) error
	Get(publicKey x3dh.PublicKey) (*x3dh.PrivateKey, error)
	All() ([]*x3dh.KeyPair, error)
}

type Status

type Status uint

message status

type User

type User struct {
	IdKey        ed25519.PublicKey `storm:"index,id,unique"`
	SignedPreKey preKey.PreKey
	Version      uint
}

type UserStorage

type UserStorage interface {
	// don't forget to verify the signature when implementing this
	GetSignedPreKey(idKey ed25519.PublicKey) (*preKey.PreKey, error)
	PutSignedPreKey(idKey ed25519.PublicKey, key preKey.PreKey) error
}

user storage store meta data about users

Jump to

Keyboard shortcuts

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