-
Notifications
You must be signed in to change notification settings - Fork 244
/
payload_management.go
188 lines (159 loc) · 5.31 KB
/
payload_management.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package pairing
import (
"encoding/json"
"errors"
"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/multiaccounts/accounts"
"github.com/status-im/status-go/multiaccounts/settings"
"github.com/status-im/status-go/protocol/protobuf"
)
const keystoreDir = "keystore"
var (
ErrKeyFileAlreadyExists = errors.New("key file already exists")
ErrKeyUIDEmptyAsSender = errors.New("keyUID must be provided as sender")
ErrNodeConfigNilAsReceiver = errors.New("node config must be provided as receiver")
ErrLoggedInKeyUIDConflict = errors.New("logged in keyUID not same as keyUID in payload")
)
// AccountPayload represents the payload structure a Server handles
type AccountPayload struct {
keys map[string][]byte
multiaccount *multiaccounts.Account
password string
//flag if account already exist before sync account
exist bool
}
// AccountPayloadMarshaller is responsible for marshalling and unmarshalling Server payload data
type AccountPayloadMarshaller struct {
logger *zap.Logger
*AccountPayload
}
func NewPairingPayloadMarshaller(ap *AccountPayload, logger *zap.Logger) *AccountPayloadMarshaller {
return &AccountPayloadMarshaller{logger: logger, AccountPayload: ap}
}
func (ppm *AccountPayloadMarshaller) MarshalProtobuf() ([]byte, error) {
return proto.Marshal(&protobuf.LocalPairingPayload{
Keys: ppm.accountKeysToProtobuf(),
Multiaccount: ppm.multiaccount.ToProtobuf(),
Password: ppm.password,
})
}
func (ppm *AccountPayloadMarshaller) accountKeysToProtobuf() []*protobuf.LocalPairingPayload_Key {
var keys []*protobuf.LocalPairingPayload_Key
for name, data := range ppm.keys {
keys = append(keys, &protobuf.LocalPairingPayload_Key{Name: name, Data: data})
}
return keys
}
func (ppm *AccountPayloadMarshaller) UnmarshalProtobuf(data []byte) error {
l := ppm.logger.Named("UnmarshalProtobuf()")
l.Debug("fired")
pb := new(protobuf.LocalPairingPayload)
err := proto.Unmarshal(data, pb)
l.Debug(
"after protobuf.LocalPairingPayload",
zap.Any("pb", pb),
zap.Any("pb.Multiaccount", pb.Multiaccount),
zap.Any("pb.Keys", pb.Keys),
)
if err != nil {
return err
}
ppm.accountKeysFromProtobuf(pb.Keys)
ppm.multiaccountFromProtobuf(pb.Multiaccount)
ppm.password = pb.Password
return nil
}
func (ppm *AccountPayloadMarshaller) accountKeysFromProtobuf(pbKeys []*protobuf.LocalPairingPayload_Key) {
l := ppm.logger.Named("accountKeysFromProtobuf()")
l.Debug("fired")
if ppm.keys == nil {
ppm.keys = make(map[string][]byte)
}
for _, key := range pbKeys {
ppm.keys[key.Name] = key.Data
}
l.Debug(
"after for _, key := range pbKeys",
zap.Any("pbKeys", pbKeys),
zap.Any("accountPayloadMarshaller.keys", ppm.keys),
)
}
func (ppm *AccountPayloadMarshaller) multiaccountFromProtobuf(pbMultiAccount *protobuf.MultiAccount) {
ppm.multiaccount = new(multiaccounts.Account)
ppm.multiaccount.FromProtobuf(pbMultiAccount)
}
type RawMessagesPayload struct {
rawMessages []*protobuf.RawMessage
subAccounts []*accounts.Account
setting *settings.Settings
}
func NewRawMessagesPayload() *RawMessagesPayload {
return &RawMessagesPayload{
setting: new(settings.Settings),
}
}
// RawMessagePayloadMarshaller is responsible for marshalling and unmarshalling raw message data
type RawMessagePayloadMarshaller struct {
payload *RawMessagesPayload
}
func NewRawMessagePayloadMarshaller(payload *RawMessagesPayload) *RawMessagePayloadMarshaller {
return &RawMessagePayloadMarshaller{
payload: payload,
}
}
func (rmm *RawMessagePayloadMarshaller) MarshalProtobuf() (data []byte, err error) {
syncRawMessage := new(protobuf.SyncRawMessage)
syncRawMessage.RawMessages = rmm.payload.rawMessages
if len(rmm.payload.subAccounts) > 0 {
syncRawMessage.SubAccountsJsonBytes, err = json.Marshal(rmm.payload.subAccounts)
if err != nil {
return nil, err
}
}
if !rmm.payload.setting.IsEmpty() {
syncRawMessage.SettingsJsonBytes, err = json.Marshal(rmm.payload.setting)
if err != nil {
return nil, err
}
}
return proto.Marshal(syncRawMessage)
}
func (rmm *RawMessagePayloadMarshaller) UnmarshalProtobuf(data []byte) error {
syncRawMessage := new(protobuf.SyncRawMessage)
err := proto.Unmarshal(data, syncRawMessage)
if err != nil {
return err
}
if syncRawMessage.SubAccountsJsonBytes != nil {
err = json.Unmarshal(syncRawMessage.SubAccountsJsonBytes, &rmm.payload.subAccounts)
if err != nil {
return err
}
}
if syncRawMessage.SettingsJsonBytes != nil {
err = json.Unmarshal(syncRawMessage.SettingsJsonBytes, rmm.payload.setting)
if err != nil {
return err
}
}
rmm.payload.rawMessages = syncRawMessage.RawMessages
return nil
}
// InstallationPayloadMounterReceiver represents an InstallationPayload Repository
type InstallationPayloadMounterReceiver struct {
PayloadMounter
PayloadReceiver
}
func NewInstallationPayloadMounterReceiver(encryptor *PayloadEncryptor, backend *api.GethStatusBackend, deviceType string) *InstallationPayloadMounterReceiver {
return &InstallationPayloadMounterReceiver{
NewInstallationPayloadMounter(encryptor, backend, deviceType),
NewInstallationPayloadReceiver(encryptor, backend, deviceType),
}
}
func (i *InstallationPayloadMounterReceiver) LockPayload() {
i.PayloadMounter.LockPayload()
i.PayloadReceiver.LockPayload()
}