Skip to content

IOS-5986 Validate derivation for edslip #2920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use_frameworks!
inhibit_all_warnings!

def tangem_sdk_pod
pod 'TangemSdk', :git => 'https://github.com/Tangem/tangem-sdk-ios.git', :tag => 'develop-282'
pod 'TangemSdk', :git => 'https://github.com/Tangem/tangem-sdk-ios.git', :tag => 'develop-284'
#pod 'TangemSdk', :path => '../tangem-sdk-ios'
end

Expand Down
8 changes: 4 additions & 4 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ DEPENDENCIES:
- Kingfisher (~> 7.9.0)
- Moya
- Solana.Swift (from `https://github.com/tangem/Solana.Swift`, tag `1.2.0-tangem1`)
- TangemSdk (from `https://github.com/Tangem/tangem-sdk-ios.git`, tag `develop-282`)
- TangemSdk (from `https://github.com/Tangem/tangem-sdk-ios.git`, tag `develop-284`)
- WalletConnectSwiftV2 (from `https://github.com/WalletConnect/WalletConnectSwiftV2`, tag `1.8.4`)

SPEC REPOS:
Expand Down Expand Up @@ -276,7 +276,7 @@ EXTERNAL SOURCES:
:tag: 1.2.0-tangem1
TangemSdk:
:git: https://github.com/Tangem/tangem-sdk-ios.git
:tag: develop-282
:tag: develop-284
WalletConnectSwiftV2:
:git: https://github.com/WalletConnect/WalletConnectSwiftV2
:tag: 1.8.4
Expand All @@ -299,7 +299,7 @@ CHECKOUT OPTIONS:
:tag: 1.2.0-tangem1
TangemSdk:
:git: https://github.com/Tangem/tangem-sdk-ios.git
:tag: develop-282
:tag: develop-284
WalletConnectSwiftV2:
:git: https://github.com/WalletConnect/WalletConnectSwiftV2
:tag: 1.8.4
Expand Down Expand Up @@ -346,6 +346,6 @@ SPEC CHECKSUMS:
TweetNacl: 3abf4d1d2082b0114e7a67410e300892448951e6
WalletConnectSwiftV2: c1c2c2fbd0495860baf71515be1b943f0c5dce0c

PODFILE CHECKSUM: 75971d0262ccc981143ad55dcbb8e88817f974fd
PODFILE CHECKSUM: a7018e359c7a7784791e7d860844d1b5ee793a5c

COCOAPODS: 1.15.2
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ class CommonUserTokensManager {
return tokenItem.blockchainNetwork
}

private func addInternal(_ tokenItems: [TokenItem], shouldUpload: Bool) {
let entries = tokenItems.map { tokenItem in
private func addInternal(_ tokenItems: [TokenItem], shouldUpload: Bool) throws {
let entries = try tokenItems.map { tokenItem in
let blockchainNetwork = getBlockchainNetwork(for: tokenItem)
try validateDerivation(for: tokenItem)
return StorageEntry(blockchainNetwork: blockchainNetwork, token: tokenItem.token)
}

Expand Down Expand Up @@ -92,6 +93,14 @@ class CommonUserTokensManager {
let tokenItems = converter.convertToTokenItem(nonCustomTokens)
swapAvailabilityController.loadSwapAvailability(for: tokenItems, forceReload: forceReload, userWalletId: userWalletId.stringValue)
}

private func validateDerivation(for tokenItem: TokenItem) throws {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В целом был еще вот такой метод:

addTokenItemPrecondition

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

возможно я по flow путаю где надо ошибку кинуть, но там метод как раз на проверки добавления

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

И тогда возможно не придется тянуть throws, но throws я бы оставил конечно

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не очень понял что ты имеешь в виду. Внутри addTokenItemPrecondition тоже вызывается validateDerivation

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут я сделал так, чтобы не было возможности добавить токен с неправильной деривацией, поэтому throws нужен

if let derivationPath = tokenItem.blockchainNetwork.derivationPath,
tokenItem.blockchain.curve == .ed25519_slip0010,
derivationPath.nodes.contains(where: { !$0.isHardened }) {
throw TangemSdkError.nonHardenedDerivationNotSupported
}
}
}

// MARK: - UserTokensManager protocol conformance
Expand Down Expand Up @@ -144,6 +153,8 @@ extension CommonUserTokensManager: UserTokensManager {
throw Error.failedSupportedLongHahesTokens(blockchainDisplayName: tokenItem.blockchain.displayName)
}

try validateDerivation(for: tokenItem)

return
}

Expand All @@ -168,7 +179,12 @@ extension CommonUserTokensManager: UserTokensManager {
}

func add(_ tokenItems: [TokenItem], completion: @escaping (Result<Void, TangemSdkError>) -> Void) {
addInternal(tokenItems, shouldUpload: true)
do {
try addInternal(tokenItems, shouldUpload: true)
} catch {
completion(.failure(error.toTangemSdkError()))
return
}
deriveIfNeeded(completion: completion)
}

Expand All @@ -192,16 +208,22 @@ extension CommonUserTokensManager: UserTokensManager {
}

func update(itemsToRemove: [TokenItem], itemsToAdd: [TokenItem], completion: @escaping (Result<Void, TangemSdkError>) -> Void) {
update(itemsToRemove: itemsToRemove, itemsToAdd: itemsToAdd)
do {
try update(itemsToRemove: itemsToRemove, itemsToAdd: itemsToAdd)
} catch {
completion(.failure(error.toTangemSdkError()))
return
}

deriveIfNeeded(completion: completion)
}

func update(itemsToRemove: [TokenItem], itemsToAdd: [TokenItem]) {
func update(itemsToRemove: [TokenItem], itemsToAdd: [TokenItem]) throws {
itemsToRemove.forEach {
removeInternal($0, shouldUpload: false)
}

addInternal(itemsToAdd, shouldUpload: false)
try addInternal(itemsToAdd, shouldUpload: false)
loadSwapAvailbilityStateIfNeeded(forceReload: true)
userTokenListManager.upload()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct LockedUserTokensManager: UserTokensManager {

func update(itemsToRemove: [TokenItem], itemsToAdd: [TokenItem], completion: @escaping (Result<Void, TangemSdkError>) -> Void) {}

func update(itemsToRemove: [TokenItem], itemsToAdd: [TokenItem]) {}
func update(itemsToRemove: [TokenItem], itemsToAdd: [TokenItem]) throws {}

func addTokenItemPrecondition(_ tokenItem: TokenItem) throws {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protocol UserTokensManager: UserTokensReordering {
/// Update storage with derivation
func update(itemsToRemove: [TokenItem], itemsToAdd: [TokenItem], completion: @escaping (Result<Void, TangemSdkError>) -> Void)
/// Update storage without derivtion
func update(itemsToRemove: [TokenItem], itemsToAdd: [TokenItem])
func update(itemsToRemove: [TokenItem], itemsToAdd: [TokenItem]) throws

/// Check condition for adding token
func addTokenItemPrecondition(_ tokenItem: TokenItem) throws
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ final class AddCustomTokenViewModel: ObservableObject, Identifiable {
try checkLocalStorage()

try userWalletModel.userTokensManager.addTokenItemPrecondition(tokenItem)
try userWalletModel.userTokensManager.update(itemsToRemove: [], itemsToAdd: [tokenItem])
} catch {
self.error = error.alertBinder
return
}

userWalletModel.userTokensManager.update(itemsToRemove: [], itemsToAdd: [tokenItem])
logSuccess(tokenItem: tokenItem)

closeModule()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ final class ManageTokensNetworkSelectorViewModel: Identifiable, ObservableObject
}
}

private func saveChanges() {
private func saveChanges() throws {
guard let userTokensManager = dataSource.selectedUserWalletModel?.userTokensManager else {
return
}

userTokensManager.update(
try userTokensManager.update(
itemsToRemove: pendingRemove,
itemsToAdd: pendingAdd
)
Expand Down Expand Up @@ -199,7 +199,7 @@ final class ManageTokensNetworkSelectorViewModel: Identifiable, ObservableObject
}
}

saveChanges()
try saveChanges()
}

private func bindSelection(_ tokenItem: TokenItem) -> Binding<Bool> {
Expand Down
2 changes: 1 addition & 1 deletion Tangem/Preview Content/Fakes/FakeUserTokensManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class FakeUserTokensManager: UserTokensManager {
completion(.success(()))
}

func update(itemsToRemove: [TokenItem], itemsToAdd: [TokenItem]) {}
func update(itemsToRemove: [TokenItem], itemsToAdd: [TokenItem]) throws {}

func canRemove(_ tokenItem: TokenItem) -> Bool {
false
Expand Down
2 changes: 1 addition & 1 deletion Tangem/Preview Content/Mocks/UserTokensManagerMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct UserTokensManagerMock: UserTokensManager {

func update(itemsToRemove: [TokenItem], itemsToAdd: [TokenItem], completion: @escaping (Result<Void, TangemSdkError>) -> Void) {}

func update(itemsToRemove: [TokenItem], itemsToAdd: [TokenItem]) {}
func update(itemsToRemove: [TokenItem], itemsToAdd: [TokenItem]) throws {}

func addTokenItemPrecondition(_ tokenItem: TokenItem) throws {}

Expand Down
Loading