Implemented base Mount() func
This uses Load, MarshalProtobuf and encrypt process flow for any PayloadMounter type. In fact the concept of a PayloadMounter type is only useful now in the instantiation context.
This commit is contained in:
parent
e028bb8ac0
commit
d0396cdd35
|
@ -103,6 +103,21 @@ func (ppm *AccountPayloadMarshaller) multiaccountFromProtobuf(pbMultiAccount *pr
|
|||
ppm.multiaccount.FromProtobuf(pbMultiAccount)
|
||||
}
|
||||
|
||||
// RawMessagePayloadMarshaller is responsible for marshalling and unmarshalling raw message data
|
||||
type RawMessagePayloadMarshaller struct {
|
||||
payload *protobuf.SyncRawMessage
|
||||
}
|
||||
|
||||
func NewRawMessagePayloadMarshaller(payload *protobuf.SyncRawMessage) *RawMessagePayloadMarshaller {
|
||||
return &RawMessagePayloadMarshaller{
|
||||
payload: payload,
|
||||
}
|
||||
}
|
||||
|
||||
func (rmm *RawMessagePayloadMarshaller) MarshalProtobuf() ([]byte, error) {
|
||||
return proto.Marshal(rmm.payload)
|
||||
}
|
||||
|
||||
// InstallationPayloadMounterReceiver represents an InstallationPayload Repository
|
||||
type InstallationPayloadMounterReceiver struct {
|
||||
*InstallationPayloadMounter
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package pairing
|
||||
|
||||
import (
|
||||
"github.com/golang/protobuf/proto"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/status-im/status-go/api"
|
||||
"github.com/status-im/status-go/multiaccounts"
|
||||
"github.com/status-im/status-go/protocol/protobuf"
|
||||
)
|
||||
|
||||
type PayloadMounter interface {
|
||||
|
@ -31,31 +31,26 @@ type BasePayloadMounter struct {
|
|||
encryptor *PayloadEncryptor
|
||||
}
|
||||
|
||||
func NewBasePayloadMounter(e *PayloadEncryptor) *BasePayloadMounter {
|
||||
func NewBasePayloadMounter(loader PayloadLoader, marshaller ProtobufMarshaller, e *PayloadEncryptor) *BasePayloadMounter {
|
||||
return &BasePayloadMounter{
|
||||
PayloadLockPayload: &PayloadLockPayload{e},
|
||||
PayloadToSend: &PayloadToSend{e},
|
||||
payloadLoader: loader,
|
||||
payloadMarshaller: marshaller,
|
||||
encryptor: e,
|
||||
}
|
||||
}
|
||||
|
||||
// Mount loads and prepares the payload to be stored in the PayloadLoader's state ready for later access
|
||||
func (bpm *BasePayloadMounter) Mount() error {
|
||||
var p []byte
|
||||
|
||||
err := bpm.payloadLoader.Load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if bpm.payloadMarshaller != nil {
|
||||
p, err = bpm.payloadMarshaller.MarshalProtobuf()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if len(p) == 0 {
|
||||
// TODO get payload from loader ... or make all mounters require a marshaller that implements:
|
||||
// `ProtobufMarshaller` and `ProtobufUnmarshaller`
|
||||
p, err := bpm.payloadMarshaller.MarshalProtobuf()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return bpm.encryptor.encrypt(p)
|
||||
|
@ -73,12 +68,6 @@ func (bpm *BasePayloadMounter) Mount() error {
|
|||
// AccountPayloadMounter is responsible for the whole lifecycle of an AccountPayload
|
||||
type AccountPayloadMounter struct {
|
||||
*BasePayloadMounter
|
||||
|
||||
logger *zap.Logger
|
||||
accountPayload *AccountPayload
|
||||
encryptor *PayloadEncryptor
|
||||
accountPayloadMarshaller *AccountPayloadMarshaller
|
||||
payloadLoader PayloadLoader
|
||||
}
|
||||
|
||||
// NewAccountPayloadMounter generates a new and initialised AccountPayloadMounter
|
||||
|
@ -96,30 +85,14 @@ func NewAccountPayloadMounter(pe *PayloadEncryptor, config *SenderConfig, logger
|
|||
}
|
||||
|
||||
return &AccountPayloadMounter{
|
||||
BasePayloadMounter: NewBasePayloadMounter(pe),
|
||||
logger: l,
|
||||
accountPayload: p,
|
||||
encryptor: pe,
|
||||
accountPayloadMarshaller: NewPairingPayloadMarshaller(p, l),
|
||||
payloadLoader: apl,
|
||||
BasePayloadMounter: NewBasePayloadMounter(
|
||||
apl,
|
||||
NewPairingPayloadMarshaller(p, l),
|
||||
pe,
|
||||
),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Mount loads and prepares the payload to be stored in the AccountPayloadLoader's state ready for later access
|
||||
func (apm *AccountPayloadMounter) Mount() error {
|
||||
err := apm.payloadLoader.Load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pb, err := apm.accountPayloadMarshaller.MarshalProtobuf()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return apm.encryptor.encrypt(pb)
|
||||
}
|
||||
|
||||
// AccountPayloadLoader is responsible for loading, parsing and validating AccountPayload data
|
||||
type AccountPayloadLoader struct {
|
||||
*AccountPayload
|
||||
|
@ -176,51 +149,39 @@ func (apl *AccountPayloadLoader) Load() error {
|
|||
|
||||
type RawMessagePayloadMounter struct {
|
||||
*BasePayloadMounter
|
||||
|
||||
logger *zap.Logger
|
||||
encryptor *PayloadEncryptor
|
||||
loader *RawMessageLoader
|
||||
}
|
||||
|
||||
func NewRawMessagePayloadMounter(logger *zap.Logger, pe *PayloadEncryptor, backend *api.GethStatusBackend, config *SenderConfig) *RawMessagePayloadMounter {
|
||||
l := logger.Named("RawMessagePayloadManager")
|
||||
|
||||
pe = pe.Renew()
|
||||
payload := new(protobuf.SyncRawMessage)
|
||||
|
||||
return &RawMessagePayloadMounter{
|
||||
BasePayloadMounter: NewBasePayloadMounter(pe),
|
||||
logger: l,
|
||||
encryptor: pe.Renew(),
|
||||
loader: NewRawMessageLoader(backend, config),
|
||||
BasePayloadMounter: NewBasePayloadMounter(
|
||||
NewRawMessageLoader(backend, payload, config),
|
||||
NewRawMessagePayloadMarshaller(payload),
|
||||
pe,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RawMessagePayloadMounter) Mount() error {
|
||||
err := r.loader.Load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return r.encryptor.encrypt(r.loader.payload)
|
||||
}
|
||||
|
||||
type RawMessageLoader struct {
|
||||
payload []byte
|
||||
payload *protobuf.SyncRawMessage
|
||||
syncRawMessageHandler *SyncRawMessageHandler
|
||||
keyUID string
|
||||
deviceType string
|
||||
}
|
||||
|
||||
func NewRawMessageLoader(backend *api.GethStatusBackend, config *SenderConfig) *RawMessageLoader {
|
||||
func NewRawMessageLoader(backend *api.GethStatusBackend, payload *protobuf.SyncRawMessage, config *SenderConfig) *RawMessageLoader {
|
||||
return &RawMessageLoader{
|
||||
syncRawMessageHandler: NewSyncRawMessageHandler(backend),
|
||||
payload: make([]byte, 0),
|
||||
payload: payload,
|
||||
keyUID: config.KeyUID,
|
||||
deviceType: config.DeviceType,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RawMessageLoader) Load() (err error) {
|
||||
r.payload, err = r.syncRawMessageHandler.PrepareRawMessage(r.keyUID, r.deviceType)
|
||||
*r.payload, err = r.syncRawMessageHandler.PrepareRawMessage(r.keyUID, r.deviceType)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -235,39 +196,30 @@ func (r *RawMessageLoader) Load() (err error) {
|
|||
|
||||
type InstallationPayloadMounter struct {
|
||||
*BasePayloadMounter
|
||||
|
||||
logger *zap.Logger
|
||||
encryptor *PayloadEncryptor
|
||||
loader *InstallationPayloadLoader
|
||||
}
|
||||
|
||||
func NewInstallationPayloadMounter(logger *zap.Logger, pe *PayloadEncryptor, backend *api.GethStatusBackend, deviceType string) *InstallationPayloadMounter {
|
||||
pe = pe.Renew()
|
||||
payload := new(protobuf.SyncRawMessage)
|
||||
|
||||
return &InstallationPayloadMounter{
|
||||
BasePayloadMounter: NewBasePayloadMounter(pe),
|
||||
logger: logger.Named("InstallationPayloadManager"),
|
||||
encryptor: pe.Renew(),
|
||||
loader: NewInstallationPayloadLoader(backend, deviceType),
|
||||
BasePayloadMounter: NewBasePayloadMounter(
|
||||
NewInstallationPayloadLoader(backend, payload, deviceType),
|
||||
NewRawMessagePayloadMarshaller(payload),
|
||||
pe,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
func (i *InstallationPayloadMounter) Mount() error {
|
||||
err := i.loader.Load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return i.encryptor.encrypt(i.loader.payload)
|
||||
}
|
||||
|
||||
type InstallationPayloadLoader struct {
|
||||
payload []byte
|
||||
payload *protobuf.SyncRawMessage
|
||||
syncRawMessageHandler *SyncRawMessageHandler
|
||||
deviceType string
|
||||
}
|
||||
|
||||
func NewInstallationPayloadLoader(backend *api.GethStatusBackend, deviceType string) *InstallationPayloadLoader {
|
||||
func NewInstallationPayloadLoader(backend *api.GethStatusBackend, payload *protobuf.SyncRawMessage, deviceType string) *InstallationPayloadLoader {
|
||||
return &InstallationPayloadLoader{
|
||||
payload: payload,
|
||||
syncRawMessageHandler: NewSyncRawMessageHandler(backend),
|
||||
deviceType: deviceType,
|
||||
}
|
||||
|
@ -279,8 +231,8 @@ func (r *InstallationPayloadLoader) Load() error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.payload, err = proto.Marshal(rawMessageCollector.convertToSyncRawMessage())
|
||||
return err
|
||||
*r.payload = rawMessageCollector.convertToSyncRawMessage()
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -21,7 +21,7 @@ func (r *RawMessageCollector) getRawMessages() []*common.RawMessage {
|
|||
return r.rawMessages
|
||||
}
|
||||
|
||||
func (r *RawMessageCollector) convertToSyncRawMessage() *protobuf.SyncRawMessage {
|
||||
func (r *RawMessageCollector) convertToSyncRawMessage() protobuf.SyncRawMessage {
|
||||
syncRawMessage := new(protobuf.SyncRawMessage)
|
||||
for _, m := range r.getRawMessages() {
|
||||
rawMessage := new(protobuf.RawMessage)
|
||||
|
@ -29,5 +29,5 @@ func (r *RawMessageCollector) convertToSyncRawMessage() *protobuf.SyncRawMessage
|
|||
rawMessage.MessageType = m.MessageType
|
||||
syncRawMessage.RawMessages = append(syncRawMessage.RawMessages, rawMessage)
|
||||
}
|
||||
return syncRawMessage
|
||||
return *syncRawMessage
|
||||
}
|
||||
|
|
|
@ -38,18 +38,18 @@ func (s *SyncRawMessageHandler) CollectInstallationData(rawMessageCollector *Raw
|
|||
return err
|
||||
}
|
||||
|
||||
func (s *SyncRawMessageHandler) PrepareRawMessage(keyUID, deviceType string) ([]byte, error) {
|
||||
func (s *SyncRawMessageHandler) PrepareRawMessage(keyUID, deviceType string) (rsm protobuf.SyncRawMessage, err error) {
|
||||
messenger := s.backend.Messenger()
|
||||
if messenger == nil {
|
||||
return nil, fmt.Errorf("messenger is nil when PrepareRawMessage")
|
||||
return rsm, fmt.Errorf("messenger is nil when PrepareRawMessage")
|
||||
}
|
||||
|
||||
currentAccount, err := s.backend.GetActiveAccount()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
if keyUID != currentAccount.KeyUID {
|
||||
return nil, fmt.Errorf("keyUID not equal")
|
||||
return rsm, fmt.Errorf("keyUID not equal")
|
||||
}
|
||||
|
||||
messenger.SetLocalPairing(true)
|
||||
|
@ -59,15 +59,15 @@ func (s *SyncRawMessageHandler) PrepareRawMessage(keyUID, deviceType string) ([]
|
|||
rawMessageCollector := new(RawMessageCollector)
|
||||
err = messenger.SyncDevices(context.TODO(), currentAccount.Name, currentAccount.Identicon, rawMessageCollector.dispatchMessage)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
|
||||
err = s.CollectInstallationData(rawMessageCollector, deviceType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
|
||||
syncRawMessage := rawMessageCollector.convertToSyncRawMessage()
|
||||
rsm = rawMessageCollector.convertToSyncRawMessage()
|
||||
|
||||
accountService := s.backend.StatusNode().AccountService()
|
||||
var (
|
||||
|
@ -76,22 +76,22 @@ func (s *SyncRawMessageHandler) PrepareRawMessage(keyUID, deviceType string) ([]
|
|||
)
|
||||
subAccounts, err = accountService.GetAccountsByKeyUID(keyUID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
syncRawMessage.SubAccountsJsonBytes, err = json.Marshal(subAccounts)
|
||||
rsm.SubAccountsJsonBytes, err = json.Marshal(subAccounts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
setting, err = accountService.GetSettings()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
syncRawMessage.SettingsJsonBytes, err = json.Marshal(setting)
|
||||
rsm.SettingsJsonBytes, err = json.Marshal(setting)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
|
||||
return proto.Marshal(syncRawMessage)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *SyncRawMessageHandler) HandleRawMessage(accountPayload *AccountPayload, nodeConfig *params.NodeConfig, settingCurrentNetwork, deviceType string, rawMessagePayload []byte) error {
|
||||
|
|
Loading…
Reference in New Issue