2019-05-23 08:47:20 +00:00
|
|
|
package multidevice
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2019-07-17 22:25:42 +00:00
|
|
|
"database/sql"
|
2019-07-03 19:13:11 +00:00
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
2019-05-23 08:47:20 +00:00
|
|
|
)
|
|
|
|
|
2019-06-26 09:32:59 +00:00
|
|
|
type InstallationMetadata struct {
|
|
|
|
// The name of the device
|
|
|
|
Name string `json:"name"`
|
|
|
|
// The type of device
|
|
|
|
DeviceType string `json:"deviceType"`
|
|
|
|
// The FCMToken for mobile devices
|
|
|
|
FCMToken string `json:"fcmToken"`
|
|
|
|
}
|
|
|
|
|
2019-05-23 08:47:20 +00:00
|
|
|
type Installation struct {
|
2019-06-26 09:32:59 +00:00
|
|
|
// Identity is the string identity of the owner
|
|
|
|
Identity string `json:"identity"`
|
2019-06-03 14:29:14 +00:00
|
|
|
// The installation-id of the device
|
2019-06-26 09:32:59 +00:00
|
|
|
ID string `json:"id"`
|
2019-06-03 14:29:14 +00:00
|
|
|
// The last known protocol version of the device
|
2019-06-26 09:32:59 +00:00
|
|
|
Version uint32 `json:"version"`
|
|
|
|
// Enabled is whether the installation is enabled
|
|
|
|
Enabled bool `json:"enabled"`
|
|
|
|
// Timestamp is the last time we saw this device
|
|
|
|
Timestamp int64 `json:"timestamp"`
|
|
|
|
// InstallationMetadata
|
|
|
|
InstallationMetadata *InstallationMetadata `json:"metadata"`
|
2019-05-23 08:47:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
MaxInstallations int
|
|
|
|
ProtocolVersion uint32
|
|
|
|
InstallationID string
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
type Multidevice struct {
|
|
|
|
persistence *sqlitePersistence
|
|
|
|
config *Config
|
2019-05-23 08:47:20 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
func New(db *sql.DB, config *Config) *Multidevice {
|
|
|
|
return &Multidevice{
|
|
|
|
config: config,
|
|
|
|
persistence: newSQLitePersistence(db),
|
|
|
|
}
|
2019-05-23 08:47:20 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
func (s *Multidevice) InstallationID() string {
|
2019-07-03 19:13:11 +00:00
|
|
|
return s.config.InstallationID
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
func (s *Multidevice) GetActiveInstallations(identity *ecdsa.PublicKey) ([]*Installation, error) {
|
2019-05-23 08:47:20 +00:00
|
|
|
identityC := crypto.CompressPubkey(identity)
|
|
|
|
return s.persistence.GetActiveInstallations(s.config.MaxInstallations, identityC)
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
func (s *Multidevice) GetOurActiveInstallations(identity *ecdsa.PublicKey) ([]*Installation, error) {
|
2019-05-23 08:47:20 +00:00
|
|
|
identityC := crypto.CompressPubkey(identity)
|
|
|
|
installations, err := s.persistence.GetActiveInstallations(s.config.MaxInstallations-1, identityC)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-03 14:29:14 +00:00
|
|
|
|
2019-05-23 08:47:20 +00:00
|
|
|
installations = append(installations, &Installation{
|
|
|
|
ID: s.config.InstallationID,
|
|
|
|
Version: s.config.ProtocolVersion,
|
|
|
|
})
|
|
|
|
|
|
|
|
return installations, nil
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
func (s *Multidevice) GetOurInstallations(identity *ecdsa.PublicKey) ([]*Installation, error) {
|
2019-06-26 09:32:59 +00:00
|
|
|
var found bool
|
|
|
|
identityC := crypto.CompressPubkey(identity)
|
|
|
|
installations, err := s.persistence.GetInstallations(identityC)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, installation := range installations {
|
|
|
|
if installation.ID == s.config.InstallationID {
|
|
|
|
found = true
|
|
|
|
installation.Enabled = true
|
|
|
|
installation.Version = s.config.ProtocolVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
installations = append(installations, &Installation{
|
|
|
|
ID: s.config.InstallationID,
|
|
|
|
Enabled: true,
|
|
|
|
Version: s.config.ProtocolVersion,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return installations, nil
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
func (s *Multidevice) AddInstallations(identity []byte, timestamp int64, installations []*Installation, defaultEnabled bool) ([]*Installation, error) {
|
2019-07-03 19:13:11 +00:00
|
|
|
return s.persistence.AddInstallations(identity, timestamp, installations, defaultEnabled)
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
func (s *Multidevice) SetInstallationMetadata(identity *ecdsa.PublicKey, installationID string, metadata *InstallationMetadata) error {
|
2019-06-26 09:32:59 +00:00
|
|
|
identityC := crypto.CompressPubkey(identity)
|
|
|
|
return s.persistence.SetInstallationMetadata(identityC, installationID, metadata)
|
|
|
|
}
|
|
|
|
|
2023-03-20 12:51:17 +00:00
|
|
|
func (s *Multidevice) SetInstallationName(identity *ecdsa.PublicKey, installationID string, name string) error {
|
|
|
|
identityC := crypto.CompressPubkey(identity)
|
|
|
|
return s.persistence.SetInstallationName(identityC, installationID, name)
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
func (s *Multidevice) EnableInstallation(identity *ecdsa.PublicKey, installationID string) error {
|
2019-05-23 08:47:20 +00:00
|
|
|
identityC := crypto.CompressPubkey(identity)
|
|
|
|
return s.persistence.EnableInstallation(identityC, installationID)
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
func (s *Multidevice) DisableInstallation(myIdentityKey *ecdsa.PublicKey, installationID string) error {
|
2019-05-23 08:47:20 +00:00
|
|
|
myIdentityKeyC := crypto.CompressPubkey(myIdentityKey)
|
|
|
|
return s.persistence.DisableInstallation(myIdentityKeyC, installationID)
|
|
|
|
}
|