Skip to content

Commit

Permalink
Merge pull request #1151 from novasamatech/develop
Browse files Browse the repository at this point in the history
8.2.2 Security improvements
  • Loading branch information
ERussel authored Jul 22, 2024
2 parents 64f9969 + 50169d0 commit 14fc22c
Show file tree
Hide file tree
Showing 137 changed files with 2,908 additions and 657 deletions.
96 changes: 95 additions & 1 deletion novawallet.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions novawallet/Common/Configs/ApplicationConfigs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ extension ApplicationConfig: ApplicationConfigProtocol {
URL(string: "https://polkadot.js.org/phishing/all.json")!
}

var phishingDAppsTopLevelSet: Set<String> {
["top"]
}

var chainListURL: URL {
#if F_RELEASE
URL(string: "https://raw.githubusercontent.com/novasamatech/nova-utils/master/chains/v20/chains.json")!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ protocol NPoolsLocalSubscriptionFactoryProtocol {
chainId: ChainModel.Id
) throws -> AnyDataProvider<DecodedPoolMember>

func getDelegatedStakingDelegatorProvider(
for accountId: AccountId,
chainId: ChainModel.Id
) throws -> AnyDataProvider<DecodedDelegatedStakingDelegator>

func getBondedPoolProvider(
for poolId: NominationPools.PoolId,
chainId: ChainModel.Id
Expand Down Expand Up @@ -91,6 +96,17 @@ extension NPoolsLocalSubscriptionFactory: NPoolsLocalSubscriptionFactoryProtocol
)
}

func getDelegatedStakingDelegatorProvider(
for accountId: AccountId,
chainId: ChainModel.Id
) throws -> AnyDataProvider<DecodedDelegatedStakingDelegator> {
try getNoFallbackAccountProvider(
for: DelegatedStakingPallet.delegatorsPath,
accountId: accountId,
chainId: chainId
)
}

func getBondedPoolProvider(
for poolId: NominationPools.PoolId,
chainId: ChainModel.Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ typealias DecodedAccountInfo = ChainStorageDecodedItem<AccountInfo>
typealias DecodedCrowdloanFunds = ChainStorageDecodedItem<CrowdloanFunds>
typealias DecodedBagListNode = ChainStorageDecodedItem<BagList.Node>
typealias DecodedPoolMember = ChainStorageDecodedItem<NominationPools.PoolMember>
typealias DecodedDelegatedStakingDelegator = ChainStorageDecodedItem<DelegatedStakingPallet.Delegation>
typealias DecodedBondedPool = ChainStorageDecodedItem<NominationPools.BondedPool>
typealias DecodedRewardPool = ChainStorageDecodedItem<NominationPools.RewardPool>
typealias DecodedSubPools = ChainStorageDecodedItem<NominationPools.SubPools>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ protocol NPoolsLocalSubscriptionHandler {
chainId: ChainModel.Id
)

func handleDelegatedStaking(
result: Result<DelegatedStakingPallet.Delegation?, Error>,
accountId: AccountId,
chainId: ChainModel.Id
)

func handlePoolMetadata(
result: Result<Data?, Error>,
poolId: NominationPools.PoolId,
Expand Down Expand Up @@ -71,6 +77,12 @@ extension NPoolsLocalSubscriptionHandler {
chainId _: ChainModel.Id
) {}

func handleDelegatedStaking(
result _: Result<DelegatedStakingPallet.Delegation?, Error>,
accountId _: AccountId,
chainId _: ChainModel.Id
) {}

func handlePoolMetadata(
result _: Result<Data?, Error>,
poolId _: NominationPools.PoolId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ protocol NPoolsLocalStorageSubscriber: LocalStorageProviderObserving where Self:
callbackQueue: DispatchQueue
) -> AnyDataProvider<DecodedPoolMember>?

func subscribeDelegatedStaking(
for accountId: AccountId,
chainId: ChainModel.Id
) -> AnyDataProvider<DecodedDelegatedStakingDelegator>?

func subscribeBondedPool(
for poolId: NominationPools.PoolId,
chainId: ChainModel.Id
Expand Down Expand Up @@ -111,6 +116,39 @@ extension NPoolsLocalStorageSubscriber where Self: NPoolsLocalSubscriptionHandle
return provider
}

func subscribeDelegatedStaking(
for accountId: AccountId,
chainId: ChainModel.Id
) -> AnyDataProvider<DecodedDelegatedStakingDelegator>? {
guard
let provider = try? npoolsLocalSubscriptionFactory.getDelegatedStakingDelegatorProvider(
for: accountId,
chainId: chainId
) else {
return nil
}

addDataProviderObserver(
for: provider,
updateClosure: { [weak self] value in
self?.npoolsLocalSubscriptionHandler.handleDelegatedStaking(
result: .success(value),
accountId: accountId,
chainId: chainId
)
},
failureClosure: { [weak self] error in
self?.npoolsLocalSubscriptionHandler.handleDelegatedStaking(
result: .failure(error),
accountId: accountId,
chainId: chainId
)
}
)

return provider
}

func subscribeBondedPool(
for poolId: NominationPools.PoolId,
chainId: ChainModel.Id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import Operation_iOS

protocol WalletLocalStorageSubscriber where Self: AnyObject {
protocol WalletLocalStorageSubscriber: LocalStorageProviderObserving where Self: AnyObject {
var walletLocalSubscriptionFactory: WalletLocalSubscriptionFactoryProtocol { get }

var walletLocalSubscriptionHandler: WalletLocalSubscriptionHandler { get }
Expand All @@ -27,6 +27,16 @@ protocol WalletLocalStorageSubscriber where Self: AnyObject {
chainId: ChainModel.Id,
assetId: AssetModel.Id
) -> StreamableProvider<AssetLock>?

func subscribeToAllHoldsProvider(
for accountId: AccountId
) -> StreamableProvider<AssetHold>?

func subscribeToHoldsProvider(
for accountId: AccountId,
chainId: ChainModel.Id,
assetId: AssetModel.Id
) -> StreamableProvider<AssetHold>?
}

extension WalletLocalStorageSubscriber {
Expand Down Expand Up @@ -254,6 +264,69 @@ extension WalletLocalStorageSubscriber {

return locksProvider
}

func subscribeToAllHoldsProvider(
for accountId: AccountId
) -> StreamableProvider<AssetHold>? {
guard let holdsProvider = try? walletLocalSubscriptionFactory.getHoldsProvider(for: accountId) else {
return nil
}

addStreamableProviderObserver(
for: holdsProvider,
updateClosure: { [weak self] changes in
self?.walletLocalSubscriptionHandler.handleAccountHolds(
result: .success(changes),
accountId: accountId
)
},
failureClosure: { [weak self] error in
self?.walletLocalSubscriptionHandler.handleAccountHolds(
result: .failure(error),
accountId: accountId
)
}
)

return holdsProvider
}

func subscribeToHoldsProvider(
for accountId: AccountId,
chainId: ChainModel.Id,
assetId: AssetModel.Id
) -> StreamableProvider<AssetHold>? {
guard
let holdsProvider = try? walletLocalSubscriptionFactory.getHoldsProvider(
for: accountId,
chainId: chainId,
assetId: assetId
) else {
return nil
}

addStreamableProviderObserver(
for: holdsProvider,
updateClosure: { [weak self] changes in
self?.walletLocalSubscriptionHandler.handleAccountHolds(
result: .success(changes),
accountId: accountId,
chainId: chainId,
assetId: assetId
)
},
failureClosure: { [weak self] error in
self?.walletLocalSubscriptionHandler.handleAccountHolds(
result: .failure(error),
accountId: accountId,
chainId: chainId,
assetId: assetId
)
}
)

return holdsProvider
}
}

extension WalletLocalStorageSubscriber where Self: WalletLocalSubscriptionHandler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ protocol WalletLocalSubscriptionHandler {
chainId: ChainModel.Id,
assetId: AssetModel.Id
)

func handleAccountHolds(
result: Result<[DataProviderChange<AssetHold>], Error>,
accountId: AccountId
)

func handleAccountHolds(
result: Result<[DataProviderChange<AssetHold>], Error>,
accountId: AccountId,
chainId: ChainModel.Id,
assetId: AssetModel.Id
)
}

extension WalletLocalSubscriptionHandler {
Expand Down Expand Up @@ -55,4 +67,16 @@ extension WalletLocalSubscriptionHandler {
chainId _: ChainModel.Id,
assetId _: AssetModel.Id
) {}

func handleAccountHolds(
result _: Result<[DataProviderChange<AssetHold>], Error>,
accountId _: AccountId
) {}

func handleAccountHolds(
result _: Result<[DataProviderChange<AssetHold>], Error>,
accountId _: AccountId,
chainId _: ChainModel.Id,
assetId _: AssetModel.Id
) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ protocol WalletLocalSubscriptionFactoryProtocol {
chainId: ChainModel.Id,
assetId: AssetModel.Id
) throws -> StreamableProvider<AssetLock>

func getHoldsProvider(for accountId: AccountId) throws -> StreamableProvider<AssetHold>

func getHoldsProvider(
for accountId: AccountId,
chainId: ChainModel.Id,
assetId: AssetModel.Id
) throws -> StreamableProvider<AssetHold>
}

final class WalletLocalSubscriptionFactory: SubstrateLocalSubscriptionFactory,
Expand Down Expand Up @@ -256,4 +264,89 @@ final class WalletLocalSubscriptionFactory: SubstrateLocalSubscriptionFactory,
serialQueue: processingQueue
)
}

func getHoldsProvider(
for accountId: AccountId,
chainId: ChainModel.Id,
assetId: AssetModel.Id
) throws -> StreamableProvider<AssetHold> {
let cacheKey = "holds-\(accountId.toHex())-\(chainId)-\(assetId)"

if let provider = getProvider(for: cacheKey) as? StreamableProvider<AssetHold> {
return provider
}

let filter = NSPredicate.assetHold(
for: accountId,
chainAssetId: ChainAssetId(chainId: chainId, assetId: assetId)
)

let provider = createAssetHoldsProvider(for: filter) { entity in
accountId.toHex() == entity.chainAccountId &&
chainId == entity.chainId &&
assetId == entity.assetId
}

saveProvider(provider, for: cacheKey)

return provider
}

func getHoldsProvider(for accountId: AccountId) throws -> StreamableProvider<AssetHold> {
let cacheKey = "holds-\(accountId.toHex())"

if let provider = getProvider(for: cacheKey) as? StreamableProvider<AssetHold> {
return provider
}

let filter = NSPredicate.assetHold(for: accountId)

let provider = createAssetHoldsProvider(for: filter) { entity in
accountId.toHex() == entity.chainAccountId
}

saveProvider(provider, for: cacheKey)

return provider
}

private func createAssetHoldsProvider(
for repositoryFilter: NSPredicate,
observingFilter: @escaping (CDAssetHold) -> Bool
) -> StreamableProvider<AssetHold> {
let source = EmptyStreamableSource<AssetHold>()

let mapper = AssetHoldMapper()

let repository = storageFacade.createRepository(
filter: repositoryFilter,
sortDescriptors: [],
mapper: AnyCoreDataMapper(mapper)
)

let processingQueue = createStreamableProcessingQueue()

let observable = CoreDataContextObservable(
service: storageFacade.databaseService,
mapper: AnyCoreDataMapper(mapper),
predicate: { entity in
observingFilter(entity)
},
processingQueue: processingQueue
)

observable.start { [weak self] error in
if let error = error {
self?.logger.error("Did receive error: \(error)")
}
}

return StreamableProvider(
source: AnyStreamableSource(source),
repository: AnyDataProviderRepository(repository),
observable: AnyDataProviderRepositoryObservable(observable),
operationManager: operationManager,
serialQueue: processingQueue
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation
import BigInt

extension BigUInt {
func saturatingSub(_ value: BigUInt) -> BigUInt {
func subtractOrZero(_ value: BigUInt) -> BigUInt {
self > value ? self - value : 0
}
}
Loading

0 comments on commit 14fc22c

Please sign in to comment.