Rolled LockPayload, ToSend and Received funcs into dedicated types

This commit is contained in:
Samuel Hawksby-Robinson 2023-03-21 16:07:41 +00:00
parent 5bad40c8be
commit 20a5b09433
3 changed files with 98 additions and 66 deletions

View File

@ -92,3 +92,27 @@ func (pem *PayloadEncryptor) getDecrypted() []byte {
func (pem *PayloadEncryptor) lockPayload() {
pem.payload.lock()
}
type PayloadLockPayload struct {
*PayloadEncryptor
}
func (pl *PayloadLockPayload) LockPayload() {
pl.lockPayload()
}
type PayloadToSend struct {
*PayloadEncryptor
}
func (pts *PayloadToSend) ToSend() []byte {
return pts.getEncrypted()
}
type PayloadReceived struct {
*PayloadEncryptor
}
func (pr *PayloadReceived) Received() []byte {
return pr.getDecrypted()
}

View File

@ -22,6 +22,18 @@ type PayloadLoader interface {
Load() error
}
type BasePayloadMounter struct {
*PayloadLockPayload
*PayloadToSend
}
func NewBasePayloadMounter(e *PayloadEncryptor) *BasePayloadMounter {
return &BasePayloadMounter{
&PayloadLockPayload{e},
&PayloadToSend{e},
}
}
/*
|--------------------------------------------------------------------------
| AccountPayload
@ -33,6 +45,8 @@ type PayloadLoader interface {
// AccountPayloadMounter is responsible for the whole lifecycle of an AccountPayload
type AccountPayloadMounter struct {
*BasePayloadMounter
logger *zap.Logger
accountPayload *AccountPayload
encryptor *PayloadEncryptor
@ -45,6 +59,8 @@ func NewAccountPayloadMounter(pe *PayloadEncryptor, config *SenderConfig, logger
l := logger.Named("AccountPayloadLoader")
l.Debug("fired", zap.Any("config", config))
pe = pe.Renew()
// A new SHARED AccountPayload
p := new(AccountPayload)
apl, err := NewAccountPayloadLoader(p, config)
@ -53,9 +69,10 @@ func NewAccountPayloadMounter(pe *PayloadEncryptor, config *SenderConfig, logger
}
return &AccountPayloadMounter{
BasePayloadMounter: NewBasePayloadMounter(pe),
logger: l,
accountPayload: p,
encryptor: pe.Renew(),
encryptor: pe,
accountPayloadMarshaller: NewPairingPayloadMarshaller(p, l),
payloadLoader: apl,
}, nil
@ -87,14 +104,6 @@ func (apm *AccountPayloadMounter) Mount() error {
return apm.encryptor.encrypt(pb)
}
func (apm *AccountPayloadMounter) ToSend() []byte {
return apm.encryptor.getEncrypted()
}
func (apm *AccountPayloadMounter) LockPayload() {
apm.encryptor.lockPayload()
}
// AccountPayloadLoader is responsible for loading, parsing and validating AccountPayload data
type AccountPayloadLoader struct {
*AccountPayload
@ -150,15 +159,20 @@ func (apl *AccountPayloadLoader) Load() error {
*/
type RawMessagePayloadMounter struct {
logger *zap.Logger
*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()
return &RawMessagePayloadMounter{
BasePayloadMounter: NewBasePayloadMounter(pe),
logger: l,
encryptor: pe.Renew(),
loader: NewRawMessageLoader(backend, config),
@ -173,14 +187,6 @@ func (r *RawMessagePayloadMounter) Mount() error {
return r.encryptor.encrypt(r.loader.payload)
}
func (r *RawMessagePayloadMounter) ToSend() []byte {
return r.encryptor.getEncrypted()
}
func (r *RawMessagePayloadMounter) LockPayload() {
r.encryptor.lockPayload()
}
type RawMessageLoader struct {
payload []byte
syncRawMessageHandler *SyncRawMessageHandler
@ -212,13 +218,18 @@ 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()
return &InstallationPayloadMounter{
BasePayloadMounter: NewBasePayloadMounter(pe),
logger: logger.Named("InstallationPayloadManager"),
encryptor: pe.Renew(),
loader: NewInstallationPayloadLoader(backend, deviceType),
@ -233,14 +244,6 @@ func (i *InstallationPayloadMounter) Mount() error {
return i.encryptor.encrypt(i.loader.payload)
}
func (i *InstallationPayloadMounter) ToSend() []byte {
return i.encryptor.getEncrypted()
}
func (i *InstallationPayloadMounter) LockPayload() {
i.encryptor.lockPayload()
}
type InstallationPayloadLoader struct {
payload []byte
syncRawMessageHandler *SyncRawMessageHandler

View File

@ -29,6 +29,18 @@ type PayloadStorer interface {
Store() error
}
type BasePayloadReceiver struct {
*PayloadLockPayload
*PayloadReceived
}
func NewBaseBasePayloadReceiver(e *PayloadEncryptor) *BasePayloadReceiver {
return &BasePayloadReceiver{
&PayloadLockPayload{e},
&PayloadReceived{e},
}
}
/*
|--------------------------------------------------------------------------
| AccountPayload
@ -40,6 +52,8 @@ type PayloadStorer interface {
// AccountPayloadReceiver is responsible for the whole lifecycle of a AccountPayload
type AccountPayloadReceiver struct {
*BasePayloadReceiver
logger *zap.Logger
accountPayload *AccountPayload
encryptor *PayloadEncryptor
@ -48,10 +62,12 @@ type AccountPayloadReceiver struct {
}
// NewAccountPayloadReceiver generates a new and initialised AccountPayloadManager
func NewAccountPayloadReceiver(encryptor *PayloadEncryptor, config *ReceiverConfig, logger *zap.Logger) (*AccountPayloadReceiver, error) {
func NewAccountPayloadReceiver(e *PayloadEncryptor, config *ReceiverConfig, logger *zap.Logger) (*AccountPayloadReceiver, error) {
l := logger.Named("AccountPayloadManager")
l.Debug("fired", zap.Any("config", config))
e = e.Renew()
// A new SHARED AccountPayload
p := new(AccountPayload)
accountPayloadRepository, err := NewAccountPayloadStorer(p, config)
@ -60,9 +76,10 @@ func NewAccountPayloadReceiver(encryptor *PayloadEncryptor, config *ReceiverConf
}
return &AccountPayloadReceiver{
BasePayloadReceiver: NewBaseBasePayloadReceiver(e),
logger: l,
accountPayload: p,
encryptor: encryptor.Renew(),
encryptor: e,
accountPayloadMarshaller: NewPairingPayloadMarshaller(p, l),
accountStorer: accountPayloadRepository,
}, nil
@ -96,14 +113,6 @@ func (apr *AccountPayloadReceiver) Receive(data []byte) error {
return apr.accountStorer.Store()
}
func (apr *AccountPayloadReceiver) Received() []byte {
return apr.encryptor.getDecrypted()
}
func (apr *AccountPayloadReceiver) LockPayload() {
apr.encryptor.lockPayload()
}
// AccountPayloadStorer is responsible for parsing, validating and storing AccountPayload data
type AccountPayloadStorer struct {
*AccountPayload
@ -220,16 +229,22 @@ func (aps *AccountPayloadStorer) storeMultiAccount() error {
*/
type RawMessagePayloadReceiver struct {
*BasePayloadReceiver
logger *zap.Logger
encryptor *PayloadEncryptor
storer *RawMessageStorer
}
func NewRawMessagePayloadReceiver(logger *zap.Logger, accountPayload *AccountPayload, encryptor *PayloadEncryptor, backend *api.GethStatusBackend, config *ReceiverConfig) *RawMessagePayloadReceiver {
func NewRawMessagePayloadReceiver(logger *zap.Logger, accountPayload *AccountPayload, e *PayloadEncryptor, backend *api.GethStatusBackend, config *ReceiverConfig) *RawMessagePayloadReceiver {
l := logger.Named("RawMessagePayloadManager")
e = e.Renew()
return &RawMessagePayloadReceiver{
BasePayloadReceiver: NewBaseBasePayloadReceiver(e),
logger: l,
encryptor: encryptor.Renew(),
encryptor: e,
storer: NewRawMessageStorer(backend, accountPayload, config),
}
}
@ -243,14 +258,6 @@ func (r *RawMessagePayloadReceiver) Receive(data []byte) error {
return r.storer.Store()
}
func (r *RawMessagePayloadReceiver) Received() []byte {
return r.encryptor.getDecrypted()
}
func (r *RawMessagePayloadReceiver) LockPayload() {
r.encryptor.lockPayload()
}
type RawMessageStorer struct {
payload []byte
syncRawMessageHandler *SyncRawMessageHandler
@ -289,16 +296,22 @@ func (r *RawMessageStorer) Store() error {
*/
type InstallationPayloadReceiver struct {
*BasePayloadReceiver
logger *zap.Logger
encryptor *PayloadEncryptor
storer *InstallationPayloadStorer
}
func NewInstallationPayloadReceiver(logger *zap.Logger, encryptor *PayloadEncryptor, backend *api.GethStatusBackend, deviceType string) *InstallationPayloadReceiver {
func NewInstallationPayloadReceiver(logger *zap.Logger, e *PayloadEncryptor, backend *api.GethStatusBackend, deviceType string) *InstallationPayloadReceiver {
l := logger.Named("InstallationPayloadManager")
e = e.Renew()
return &InstallationPayloadReceiver{
BasePayloadReceiver: NewBaseBasePayloadReceiver(e),
logger: l,
encryptor: encryptor.Renew(),
encryptor: e,
storer: NewInstallationPayloadStorer(backend, deviceType),
}
}
@ -312,14 +325,6 @@ func (i *InstallationPayloadReceiver) Receive(data []byte) error {
return i.storer.Store()
}
func (i *InstallationPayloadReceiver) Received() []byte {
return i.encryptor.getDecrypted()
}
func (i *InstallationPayloadReceiver) LockPayload() {
i.encryptor.lockPayload()
}
type InstallationPayloadStorer struct {
payload []byte
syncRawMessageHandler *SyncRawMessageHandler