|
1 | 1 | package syncing |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "errors" |
5 | 6 |
|
| 7 | + "go.uber.org/zap" |
| 8 | + |
6 | 9 | gethcommon "github.com/ethereum/go-ethereum/common" |
7 | 10 |
|
8 | 11 | accsmanagementtypes "github.com/status-im/status-go/accounts-management/types" |
9 | 12 | "github.com/status-im/status-go/crypto/types" |
10 | 13 | "github.com/status-im/status-go/multiaccounts/accounts" |
11 | 14 | multiaccountscommon "github.com/status-im/status-go/multiaccounts/common" |
| 15 | + maErrors "github.com/status-im/status-go/multiaccounts/errors" |
| 16 | + "github.com/status-im/status-go/multiaccounts/settings" |
12 | 17 | "github.com/status-im/status-go/pkg/pubsub" |
13 | 18 | "github.com/status-im/status-go/protocol/protobuf" |
14 | 19 | "github.com/status-im/status-go/services/accounts/accountsevent" |
15 | 20 | ) |
16 | 21 |
|
| 22 | +// TODO move this code out of the protocol package |
| 23 | +// https://github.com/status-im/status-go/pull/6967#discussion_r2391383496 |
| 24 | + |
17 | 25 | var ( |
18 | 26 | ErrNotWatchOnlyAccount = errors.New("an account is not a watch only account") |
19 | 27 | ErrTryingToStoreOldWalletAccount = errors.New("trying to store an old wallet account") |
@@ -96,3 +104,43 @@ func HandleSyncWatchOnlyAccount(accountsDB *accounts.Database, message *protobuf |
96 | 104 | } |
97 | 105 | return acc, nil |
98 | 106 | } |
| 107 | + |
| 108 | +// extractSyncSetting parses incoming *protobuf.SyncSetting and stores the setting data if needed |
| 109 | +func ExtractAndSaveSyncSetting(accountsDB *accounts.Database, logger *zap.Logger, syncSetting *protobuf.SyncSetting) (*settings.SyncSettingField, error) { |
| 110 | + sf, err := settings.GetFieldFromProtobufType(syncSetting.Type) |
| 111 | + if err != nil { |
| 112 | + logger.Error( |
| 113 | + "extractSyncSetting - settings.GetFieldFromProtobufType", |
| 114 | + zap.Error(err), |
| 115 | + zap.Any("syncSetting", syncSetting), |
| 116 | + ) |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + |
| 120 | + spf := sf.SyncProtobufFactory() |
| 121 | + if spf == nil { |
| 122 | + logger.Warn("extractSyncSetting - received protobuf for setting with no SyncProtobufFactory") |
| 123 | + return nil, nil |
| 124 | + } |
| 125 | + if spf.Inactive() { |
| 126 | + logger.Warn("extractSyncSetting - received protobuf for inactive sync setting") |
| 127 | + return nil, nil |
| 128 | + } |
| 129 | + |
| 130 | + value := spf.ExtractValueFromProtobuf()(syncSetting) |
| 131 | + |
| 132 | + err = accountsDB.SaveSyncSetting(sf, value, syncSetting.Clock) |
| 133 | + if err == maErrors.ErrNewClockOlderThanCurrent { |
| 134 | + logger.Info("extractSyncSetting - SaveSyncSetting :", zap.Error(err)) |
| 135 | + return nil, nil |
| 136 | + } |
| 137 | + if err != nil { |
| 138 | + return nil, err |
| 139 | + } |
| 140 | + |
| 141 | + if v, ok := value.([]byte); ok { |
| 142 | + value = json.RawMessage(v) |
| 143 | + } |
| 144 | + |
| 145 | + return &settings.SyncSettingField{SettingField: sf, Value: value}, nil |
| 146 | +} |
0 commit comments