github.com/status-im/status-go@v1.1.0/protocol/encryption/multidevice/multidevice.go (about)

     1  package multidevice
     2  
     3  import (
     4  	"crypto/ecdsa"
     5  	"database/sql"
     6  
     7  	"github.com/google/uuid"
     8  
     9  	"github.com/status-im/status-go/eth-node/crypto"
    10  )
    11  
    12  type InstallationMetadata struct {
    13  	// The name of the device
    14  	Name string `json:"name"`
    15  	// The type of device
    16  	DeviceType string `json:"deviceType"`
    17  	// The FCMToken for mobile devices
    18  	FCMToken string `json:"fcmToken"`
    19  }
    20  
    21  type Installation struct {
    22  	// Identity is the string identity of the owner
    23  	Identity string `json:"identity"`
    24  	// The installation-id of the device
    25  	ID string `json:"id"`
    26  	// The last known protocol version of the device
    27  	Version uint32 `json:"version"`
    28  	// Enabled is whether the installation is enabled
    29  	Enabled bool `json:"enabled"`
    30  	// Timestamp is the last time we saw this device
    31  	Timestamp int64 `json:"timestamp"`
    32  	// InstallationMetadata
    33  	InstallationMetadata *InstallationMetadata `json:"metadata"`
    34  }
    35  
    36  func (i *Installation) UniqueKey() string {
    37  	return i.ID + i.Identity
    38  }
    39  
    40  type Config struct {
    41  	MaxInstallations int
    42  	ProtocolVersion  uint32
    43  	InstallationID   string
    44  }
    45  
    46  type Multidevice struct {
    47  	persistence *sqlitePersistence
    48  	config      *Config
    49  }
    50  
    51  func New(db *sql.DB, config *Config) *Multidevice {
    52  	return &Multidevice{
    53  		config:      config,
    54  		persistence: newSQLitePersistence(db),
    55  	}
    56  }
    57  
    58  func (s *Multidevice) InstallationID() string {
    59  	return s.config.InstallationID
    60  }
    61  
    62  func (s *Multidevice) GetActiveInstallations(identity *ecdsa.PublicKey) ([]*Installation, error) {
    63  	identityC := crypto.CompressPubkey(identity)
    64  	return s.persistence.GetActiveInstallations(s.config.MaxInstallations, identityC)
    65  }
    66  
    67  func (s *Multidevice) GetOurActiveInstallations(identity *ecdsa.PublicKey) ([]*Installation, error) {
    68  	identityC := crypto.CompressPubkey(identity)
    69  	installations, err := s.persistence.GetActiveInstallations(s.config.MaxInstallations-1, identityC)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	installations = append(installations, &Installation{
    75  		ID:      s.config.InstallationID,
    76  		Version: s.config.ProtocolVersion,
    77  	})
    78  
    79  	return installations, nil
    80  }
    81  
    82  func (s *Multidevice) GetOurInstallations(identity *ecdsa.PublicKey) ([]*Installation, error) {
    83  	var found bool
    84  	identityC := crypto.CompressPubkey(identity)
    85  	installations, err := s.persistence.GetInstallations(identityC)
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  
    90  	for _, installation := range installations {
    91  		if installation.ID == s.config.InstallationID {
    92  			found = true
    93  			installation.Enabled = true
    94  			installation.Version = s.config.ProtocolVersion
    95  		}
    96  
    97  	}
    98  	if !found {
    99  		installations = append(installations, &Installation{
   100  			ID:      s.config.InstallationID,
   101  			Enabled: true,
   102  			Version: s.config.ProtocolVersion,
   103  		})
   104  	}
   105  
   106  	return installations, nil
   107  }
   108  
   109  func (s *Multidevice) AddInstallations(identity []byte, timestamp int64, installations []*Installation, defaultEnabled bool) ([]*Installation, error) {
   110  	return s.persistence.AddInstallations(identity, timestamp, installations, defaultEnabled)
   111  }
   112  
   113  func (s *Multidevice) SetInstallationMetadata(identity *ecdsa.PublicKey, installationID string, metadata *InstallationMetadata) error {
   114  	identityC := crypto.CompressPubkey(identity)
   115  	return s.persistence.SetInstallationMetadata(identityC, installationID, metadata)
   116  }
   117  
   118  func (s *Multidevice) SetInstallationName(identity *ecdsa.PublicKey, installationID string, name string) error {
   119  	identityC := crypto.CompressPubkey(identity)
   120  	return s.persistence.SetInstallationName(identityC, installationID, name)
   121  }
   122  
   123  func (s *Multidevice) EnableInstallation(identity *ecdsa.PublicKey, installationID string) error {
   124  	identityC := crypto.CompressPubkey(identity)
   125  	return s.persistence.EnableInstallation(identityC, installationID)
   126  }
   127  
   128  func (s *Multidevice) DisableInstallation(myIdentityKey *ecdsa.PublicKey, installationID string) error {
   129  	myIdentityKeyC := crypto.CompressPubkey(myIdentityKey)
   130  	return s.persistence.DisableInstallation(myIdentityKeyC, installationID)
   131  }
   132  
   133  func GenerateInstallationID() string {
   134  	return uuid.New().String()
   135  }