Skip to content

Fixed wallet checksum verification issue #411

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 2 commits into from
Sep 4, 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import BigInt
import Combine
import Foundation
import CryptoKit

protocol CIS2ServiceProtocol {
func fetchTokens(contractIndex: String, contractSubindex: String) -> AnyPublisher<CIS2TokensInfo, Error>
Expand All @@ -25,11 +26,16 @@ protocol CIS2ServiceProtocol {
func fetchTokensMetadata(contractIndex: String, contractSubindex: String, tokenId: String) async throws -> CIS2TokensMetadata
func fetchTokensBalance(contractIndex: String, contractSubindex: String, accountAddress: String, tokenId: String) async throws -> [CIS2TokenBalance]
func deleteTokenFromCache(_ token: CIS2TokenSelectionRepresentable) throws
func fetchTokensMetadataDetails(url: URL) async throws -> CIS2TokenMetadataDetails
func fetchTokensMetadataDetails(url: URL, metadataChecksum: String?) async throws -> CIS2TokenMetadataDetails

func getTokenMetadataPair(metadata: CIS2TokensMetadata) async throws -> [(CIS2TokensMetadataItem, CIS2TokenMetadataDetails)]
}

enum ChecksumError: Error {
case invalidChecksum
case incorrectChecksum
}

class CIS2Service: CIS2ServiceProtocol {
let networkManager: NetworkManagerProtocol
let storageManager: StorageManagerProtocol
Expand Down Expand Up @@ -188,9 +194,13 @@ extension CIS2Service {
)
}

func fetchTokensMetadataDetails(url: URL) async throws -> CIS2TokenMetadataDetails {
func fetchTokensMetadataDetails(url: URL, metadataChecksum: String?) async throws -> CIS2TokenMetadataDetails {
let metadata: CIS2TokenMetadataDetails = try await networkManager.load(ResourceRequest(url: url))

if let metadataChecksum {
try await verifyChecksum(checksum: metadataChecksum, url: url)
}

try await MainActor.run {
try self.storageManager.storeCIS2TokenMetadataDetails(metadata, for: url.absoluteString)
}
Expand All @@ -216,7 +226,7 @@ extension CIS2Service {
for metadata in metadata.metadata {
if let url = URL(string: metadata.metadataURL) {
group.addTask {
guard let result = try? await self.fetchTokensMetadataDetails(url: url) else {
guard let result = try? await self.fetchTokensMetadataDetails(url: url, metadataChecksum: metadata.metadataChecksum) else {
return nil
}
return (metadata, result)
Expand All @@ -229,4 +239,19 @@ extension CIS2Service {
.reduce(into: []) { $0.append($1) }
}
}

/// Verifies the checksum of the response data against a provided checksum.
///
/// - Parameters:
/// - checksum: The expected checksum.
/// - responseData: The data to verify.
/// - Throws: `ChecksumError.incorrectChecksum` if the checksums do not match.
func verifyChecksum(checksum: String, url: URL) async throws {
let (data, _) = try await URLSession(configuration: .ephemeral).data(from: url)
let hash = SHA256.hash(data: data)
let hashString = hash.compactMap { String(format: "%02x", $0) }.joined()
guard hashString.localizedCaseInsensitiveCompare(checksum) == .orderedSame else {
throw ChecksumError.incorrectChecksum
}
}
}
4 changes: 0 additions & 4 deletions ConcordiumWallet/Views/UnshiedSunset/UnshiedSunsetView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
.foregroundColor(.white)
.frame(maxWidth: .infinity)
.frame(height: 48)
.contentShape(.rect)

Check failure on line 66 in ConcordiumWallet/Views/UnshiedSunset/UnshiedSunsetView.swift

View workflow job for this annotation

GitHub Actions / Test

type 'Shape' has no member 'rect'

Check failure on line 66 in ConcordiumWallet/Views/UnshiedSunset/UnshiedSunsetView.swift

View workflow job for this annotation

GitHub Actions / Test

type 'Shape' has no member 'rect'
}
.foregroundColor(.clear)
.background(Color(red: 0.27, green: 0.53, blue: 0.67))
Expand All @@ -83,7 +83,3 @@
}
}
}

#Preview {
UnshiedSunsetView()
}
Loading