Implemented structure of BasePayloadMounter.Mount()

This commit is contained in:
Samuel Hawksby-Robinson 2023-03-22 12:25:25 +00:00
parent 20a5b09433
commit e028bb8ac0
3 changed files with 31 additions and 26 deletions

View File

@ -29,10 +29,10 @@ type HandlerServer interface {
GetLogger() *zap.Logger
}
type ProtobufMarshaler interface {
type ProtobufMarshaller interface {
MarshalProtobuf() ([]byte, error)
}
type ProtobufUnmarshaler interface {
type ProtobufUnmarshaller interface {
UnmarshalProtobuf([]byte) error
}

View File

@ -25,15 +25,42 @@ type PayloadLoader interface {
type BasePayloadMounter struct {
*PayloadLockPayload
*PayloadToSend
payloadLoader PayloadLoader
payloadMarshaller ProtobufMarshaller
encryptor *PayloadEncryptor
}
func NewBasePayloadMounter(e *PayloadEncryptor) *BasePayloadMounter {
return &BasePayloadMounter{
&PayloadLockPayload{e},
&PayloadToSend{e},
PayloadLockPayload: &PayloadLockPayload{e},
PayloadToSend: &PayloadToSend{e},
}
}
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`
}
return bpm.encryptor.encrypt(p)
}
/*
|--------------------------------------------------------------------------
| AccountPayload
@ -80,26 +107,15 @@ func NewAccountPayloadMounter(pe *PayloadEncryptor, config *SenderConfig, logger
// Mount loads and prepares the payload to be stored in the AccountPayloadLoader's state ready for later access
func (apm *AccountPayloadMounter) Mount() error {
l := apm.logger.Named("Mount()")
l.Debug("fired")
err := apm.payloadLoader.Load()
if err != nil {
return err
}
l.Debug("after Load()")
pb, err := apm.accountPayloadMarshaller.MarshalProtobuf()
if err != nil {
return err
}
l.Debug(
"after MarshalProtobuf",
zap.Any("accountPayloadMarshaller.accountPayloadMarshaller.keys", apm.accountPayloadMarshaller.keys),
zap.Any("accountPayloadMarshaller.accountPayloadMarshaller.multiaccount", apm.accountPayloadMarshaller.multiaccount),
zap.String("accountPayloadMarshaller.accountPayloadMarshaller.password", apm.accountPayloadMarshaller.password),
zap.Binary("pb", pb),
)
return apm.encryptor.encrypt(pb)
}

View File

@ -87,26 +87,15 @@ func NewAccountPayloadReceiver(e *PayloadEncryptor, config *ReceiverConfig, logg
// Receive takes a []byte representing raw data, parses and stores the data
func (apr *AccountPayloadReceiver) Receive(data []byte) error {
l := apr.logger.Named("Receive()")
l.Debug("fired")
err := apr.encryptor.decrypt(data)
if err != nil {
return err
}
l.Debug("after Decrypt")
err = apr.accountPayloadMarshaller.UnmarshalProtobuf(apr.Received())
if err != nil {
return err
}
l.Debug(
"after UnmarshalProtobuf",
zap.Any("accountPayloadMarshaller.accountPayloadMarshaller.keys", apr.accountPayloadMarshaller.keys),
zap.Any("accountPayloadMarshaller.accountPayloadMarshaller.multiaccount", apr.accountPayloadMarshaller.multiaccount),
zap.String("accountPayloadMarshaller.accountPayloadMarshaller.password", apr.accountPayloadMarshaller.password),
zap.Binary("accountPayloadMarshaller.Received()", apr.Received()),
)
signal.SendLocalPairingEvent(Event{Type: EventReceivedAccount, Action: ActionPairingAccount, Data: apr.accountPayload.multiaccount})