2023-03-23 11:44:15 +00:00
|
|
|
package pairing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/api"
|
|
|
|
"github.com/status-im/status-go/multiaccounts"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PayloadMounter interface {
|
|
|
|
PayloadLocker
|
|
|
|
|
|
|
|
// Mount Loads the payload into the PayloadManager's state
|
|
|
|
Mount() error
|
|
|
|
|
|
|
|
// ToSend returns an outbound safe (encrypted) payload
|
|
|
|
ToSend() []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type PayloadLoader interface {
|
|
|
|
Load() error
|
|
|
|
}
|
|
|
|
|
2023-03-21 16:07:41 +00:00
|
|
|
type BasePayloadMounter struct {
|
|
|
|
*PayloadLockPayload
|
|
|
|
*PayloadToSend
|
2023-03-22 12:25:25 +00:00
|
|
|
|
2023-04-02 23:08:29 +00:00
|
|
|
loader PayloadLoader
|
|
|
|
marshaller ProtobufMarshaller
|
|
|
|
encryptor *PayloadEncryptor
|
2023-03-21 16:07:41 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 17:12:27 +00:00
|
|
|
func NewBasePayloadMounter(loader PayloadLoader, marshaller ProtobufMarshaller, e *PayloadEncryptor) *BasePayloadMounter {
|
2023-03-21 16:07:41 +00:00
|
|
|
return &BasePayloadMounter{
|
2023-03-22 12:25:25 +00:00
|
|
|
PayloadLockPayload: &PayloadLockPayload{e},
|
|
|
|
PayloadToSend: &PayloadToSend{e},
|
2023-04-02 23:08:29 +00:00
|
|
|
loader: loader,
|
|
|
|
marshaller: marshaller,
|
2023-03-29 17:12:27 +00:00
|
|
|
encryptor: e,
|
2023-03-21 16:07:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-29 17:12:27 +00:00
|
|
|
// Mount loads and prepares the payload to be stored in the PayloadLoader's state ready for later access
|
2023-03-22 12:25:25 +00:00
|
|
|
func (bpm *BasePayloadMounter) Mount() error {
|
2023-04-02 23:08:29 +00:00
|
|
|
err := bpm.loader.Load()
|
2023-03-22 12:25:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-02 23:08:29 +00:00
|
|
|
p, err := bpm.marshaller.MarshalProtobuf()
|
2023-03-29 17:12:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-03-22 12:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bpm.encryptor.encrypt(p)
|
|
|
|
}
|
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| AccountPayload
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
| AccountPayloadMounter, AccountPayloadLoader and AccountPayloadMarshaller
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2023-04-02 23:08:29 +00:00
|
|
|
// NewAccountPayloadMounter generates a new and initialised AccountPayload flavoured BasePayloadMounter
|
2023-03-30 10:00:32 +00:00
|
|
|
// responsible for the whole lifecycle of an AccountPayload
|
|
|
|
func NewAccountPayloadMounter(pe *PayloadEncryptor, config *SenderConfig, logger *zap.Logger) (*BasePayloadMounter, error) {
|
2023-03-23 11:44:15 +00:00
|
|
|
l := logger.Named("AccountPayloadLoader")
|
|
|
|
l.Debug("fired", zap.Any("config", config))
|
|
|
|
|
2023-03-21 16:07:41 +00:00
|
|
|
pe = pe.Renew()
|
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
// A new SHARED AccountPayload
|
|
|
|
p := new(AccountPayload)
|
|
|
|
apl, err := NewAccountPayloadLoader(p, config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-03-30 10:00:32 +00:00
|
|
|
return NewBasePayloadMounter(
|
|
|
|
apl,
|
|
|
|
NewPairingPayloadMarshaller(p, l),
|
|
|
|
pe,
|
|
|
|
), nil
|
2023-03-23 11:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AccountPayloadLoader is responsible for loading, parsing and validating AccountPayload data
|
|
|
|
type AccountPayloadLoader struct {
|
|
|
|
*AccountPayload
|
|
|
|
|
|
|
|
multiaccountsDB *multiaccounts.Database
|
|
|
|
keystorePath string
|
|
|
|
keyUID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAccountPayloadLoader(p *AccountPayload, config *SenderConfig) (*AccountPayloadLoader, error) {
|
|
|
|
ppr := &AccountPayloadLoader{
|
|
|
|
AccountPayload: p,
|
|
|
|
}
|
|
|
|
|
|
|
|
if config == nil {
|
|
|
|
return ppr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ppr.multiaccountsDB = config.DB
|
|
|
|
ppr.keyUID = config.KeyUID
|
|
|
|
ppr.password = config.Password
|
|
|
|
ppr.keystorePath = config.KeystorePath
|
|
|
|
return ppr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (apl *AccountPayloadLoader) Load() error {
|
2023-03-29 15:51:01 +00:00
|
|
|
apl.keys = make(map[string][]byte)
|
|
|
|
err := loadKeys(apl.keys, apl.keystorePath)
|
2023-03-23 11:44:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = validateKeys(apl.keys, apl.password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
apl.multiaccount, err = apl.multiaccountsDB.GetAccount(apl.keyUID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| RawMessagePayload
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
| RawMessagePayloadMounter and RawMessageLoader
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2023-04-02 23:08:29 +00:00
|
|
|
// NewRawMessagePayloadMounter generates a new and initialised RawMessagePayload flavoured BasePayloadMounter
|
2023-03-30 10:00:32 +00:00
|
|
|
// responsible for the whole lifecycle of an RawMessagePayload
|
|
|
|
func NewRawMessagePayloadMounter(logger *zap.Logger, pe *PayloadEncryptor, backend *api.GethStatusBackend, config *SenderConfig) *BasePayloadMounter {
|
2023-03-21 16:07:41 +00:00
|
|
|
pe = pe.Renew()
|
2023-04-02 23:08:29 +00:00
|
|
|
payload := NewRawMessagesPayload()
|
2023-03-21 16:07:41 +00:00
|
|
|
|
2023-03-30 10:00:32 +00:00
|
|
|
return NewBasePayloadMounter(
|
|
|
|
NewRawMessageLoader(backend, payload, config),
|
|
|
|
NewRawMessagePayloadMarshaller(payload),
|
|
|
|
pe,
|
|
|
|
)
|
2023-03-23 11:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RawMessageLoader struct {
|
2023-04-02 23:08:29 +00:00
|
|
|
payload *RawMessagesPayload
|
2023-03-23 11:44:15 +00:00
|
|
|
syncRawMessageHandler *SyncRawMessageHandler
|
|
|
|
keyUID string
|
|
|
|
deviceType string
|
|
|
|
}
|
|
|
|
|
2023-04-02 23:08:29 +00:00
|
|
|
func NewRawMessageLoader(backend *api.GethStatusBackend, payload *RawMessagesPayload, config *SenderConfig) *RawMessageLoader {
|
2023-03-23 11:44:15 +00:00
|
|
|
return &RawMessageLoader{
|
|
|
|
syncRawMessageHandler: NewSyncRawMessageHandler(backend),
|
2023-03-29 17:12:27 +00:00
|
|
|
payload: payload,
|
2023-03-23 11:44:15 +00:00
|
|
|
keyUID: config.KeyUID,
|
|
|
|
deviceType: config.DeviceType,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RawMessageLoader) Load() (err error) {
|
2023-04-02 23:08:29 +00:00
|
|
|
r.payload.rawMessages, r.payload.subAccounts, r.payload.setting, err = r.syncRawMessageHandler.PrepareRawMessage(r.keyUID, r.deviceType)
|
2023-03-23 11:44:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| InstallationPayload
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
| InstallationPayloadMounter and InstallationPayloadLoader
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2023-04-02 23:08:29 +00:00
|
|
|
// NewInstallationPayloadMounter generates a new and initialised InstallationPayload flavoured BasePayloadMounter
|
2023-03-30 10:00:32 +00:00
|
|
|
// responsible for the whole lifecycle of an InstallationPayload
|
2023-04-02 23:08:29 +00:00
|
|
|
func NewInstallationPayloadMounter(pe *PayloadEncryptor, backend *api.GethStatusBackend, deviceType string) *BasePayloadMounter {
|
2023-03-21 16:07:41 +00:00
|
|
|
pe = pe.Renew()
|
2023-04-02 23:08:29 +00:00
|
|
|
payload := NewRawMessagesPayload()
|
2023-03-21 16:07:41 +00:00
|
|
|
|
2023-03-30 10:00:32 +00:00
|
|
|
return NewBasePayloadMounter(
|
|
|
|
NewInstallationPayloadLoader(backend, payload, deviceType),
|
|
|
|
NewRawMessagePayloadMarshaller(payload),
|
|
|
|
pe,
|
|
|
|
)
|
2023-03-23 11:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type InstallationPayloadLoader struct {
|
2023-04-02 23:08:29 +00:00
|
|
|
payload *RawMessagesPayload
|
2023-03-23 11:44:15 +00:00
|
|
|
syncRawMessageHandler *SyncRawMessageHandler
|
|
|
|
deviceType string
|
|
|
|
}
|
|
|
|
|
2023-04-02 23:08:29 +00:00
|
|
|
func NewInstallationPayloadLoader(backend *api.GethStatusBackend, payload *RawMessagesPayload, deviceType string) *InstallationPayloadLoader {
|
2023-03-23 11:44:15 +00:00
|
|
|
return &InstallationPayloadLoader{
|
2023-03-29 17:12:27 +00:00
|
|
|
payload: payload,
|
2023-03-23 11:44:15 +00:00
|
|
|
syncRawMessageHandler: NewSyncRawMessageHandler(backend),
|
|
|
|
deviceType: deviceType,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *InstallationPayloadLoader) Load() error {
|
|
|
|
rawMessageCollector := new(RawMessageCollector)
|
|
|
|
err := r.syncRawMessageHandler.CollectInstallationData(rawMessageCollector, r.deviceType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-02 23:08:29 +00:00
|
|
|
rms := rawMessageCollector.convertToSyncRawMessage()
|
|
|
|
r.payload.rawMessages = rms.RawMessages
|
2023-03-29 17:12:27 +00:00
|
|
|
return nil
|
2023-03-23 11:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| PayloadMounters
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
| Funcs for all PayloadMounters AccountPayloadMounter, RawMessagePayloadMounter and InstallationPayloadMounter
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2023-03-30 10:00:32 +00:00
|
|
|
// NewPayloadMounters returns PayloadMounter s configured to handle local pairing transfers of:
|
|
|
|
// - AccountPayload, RawMessagePayload and InstallationPayload
|
2023-04-02 23:08:29 +00:00
|
|
|
func NewPayloadMounters(logger *zap.Logger, pe *PayloadEncryptor, backend *api.GethStatusBackend, config *SenderConfig) (PayloadMounter, PayloadMounter, PayloadMounterReceiver, error) {
|
2023-03-23 11:44:15 +00:00
|
|
|
am, err := NewAccountPayloadMounter(pe, config, logger)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
rmm := NewRawMessagePayloadMounter(logger, pe, backend, config)
|
2023-04-02 23:08:29 +00:00
|
|
|
imr := NewInstallationPayloadMounterReceiver(pe, backend, config.DeviceType)
|
2023-03-23 11:44:15 +00:00
|
|
|
return am, rmm, imr, nil
|
|
|
|
}
|