Skip to content

Commit

Permalink
IOS-3290 Refactor to separate settings
Browse files Browse the repository at this point in the history
  • Loading branch information
tureck1y committed Mar 29, 2023
1 parent 16856f6 commit 0e1ca6b
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 44 deletions.
2 changes: 2 additions & 0 deletions TangemSdk/TangemSdk/Common/Card/Card.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public struct Card: Codable, JSONStringConvertible {
public let issuer: Issuer
/// Card setting, that were set during the personalization process
public var settings: Settings
/// Card settings, that were set during the personalization process anc can be changed by user directly
public internal(set) var userSettings: UserSettings
/// When this value is `current`, it means that the application is linked to the card,
/// and COS will not enforce security delay if `SignCommand` will be called
/// with `TlvTag.TerminalTransactionSignature` parameter containing a correct signature of raw data
Expand Down
14 changes: 1 addition & 13 deletions TangemSdk/TangemSdk/Common/Card/CardSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public extension Card {
public let isHDWalletAllowed: Bool
/// Is allowed to create backup
public let isBackupAllowed: Bool
/// Is allowed to reset user codes
public internal(set) var isResettingUserCodesAllowed: Bool
/// Is allowed to delete wallet. COS before v4
@SkipEncoding
var isPermanentWallet: Bool
Expand All @@ -55,7 +53,7 @@ public extension Card {

@available(iOS 13.0, *)
extension Card.Settings {
init(securityDelay: Int, maxWalletsCount: Int, mask: CardSettingsMask, userSettings: UserSettings,
init(securityDelay: Int, maxWalletsCount: Int, mask: CardSettingsMask,
defaultSigningMethods: SigningMethod? = nil, defaultCurve: EllipticCurve? = nil) {
self.securityDelay = securityDelay
self.maxWalletsCount = maxWalletsCount
Expand Down Expand Up @@ -83,25 +81,15 @@ extension Card.Settings {
}

self.supportedEncryptionModes = encryptionModes

// user settings
self.isResettingUserCodesAllowed = userSettings.isResettingUserCodesAllowed
}

func updated(with mask: CardSettingsMask) -> Card.Settings {
return .init(securityDelay: self.securityDelay,
maxWalletsCount: self.maxWalletsCount,
mask: mask,
userSettings: .init(isResettingUserCodesAllowed: self.isResettingUserCodesAllowed),
defaultSigningMethods: self.defaultSigningMethods,
defaultCurve: self.defaultCurve)
}

func updated(with userSettings: UserSettings) -> Card.Settings {
var copy = self
copy.isResettingUserCodesAllowed = userSettings.isResettingUserCodesAllowed
return copy
}
}

// MARK: - CardSettingsMask
Expand Down
18 changes: 8 additions & 10 deletions TangemSdk/TangemSdk/Common/Card/UserSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@

import Foundation

// MARK: - UserSettings
@available(iOS 13.0, *)
struct UserSettings: JSONStringConvertible {
var isResettingUserCodesAllowed: Bool
public extension Card {
struct UserSettings: Codable {
/// Is allowed to reset user codes
public internal(set) var isResettingUserCodesAllowed: Bool
}
}

@available(iOS 13.0, *)
extension Card.UserSettings {
var mask: UserSettingsMask {
let builder = MaskBuilder<UserSettingsMask>()

Expand All @@ -22,17 +27,10 @@ struct UserSettings: JSONStringConvertible {

return builder.build()
}
}

@available(iOS 13.0, *)
extension UserSettings {
init(from mask: UserSettingsMask) {
self.isResettingUserCodesAllowed = !mask.contains(.forbidResetPIN)
}

init(from cardSettings: Card.Settings) {
self.isResettingUserCodesAllowed = cardSettings.isResettingUserCodesAllowed
}
}

// MARK: - UserSettingsMask
Expand Down
20 changes: 10 additions & 10 deletions TangemSdk/TangemSdk/Common/Deserialization/CardDeserializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,9 @@ struct CardDeserializer {

let securityDelayMs = securityDelay.map { $0 * 10 } ?? 0

var userSettings: UserSettings
let userSettingsMask: UserSettingsMask? = try decoder.decode(.userSettingsMask)
if let userSettingsMask {
userSettings = .init(from: userSettingsMask)
} else {
userSettings = .init(isResettingUserCodesAllowed: firmware >= .backupAvailable)
}

let settings = Card.Settings(securityDelay: securityDelayMs,
maxWalletsCount: try decoder.decode(.walletsCount) ?? 1, //Cos before v4 always has 1 wallet
mask: cardSettingsMask,
userSettings: userSettings,
defaultSigningMethods: try decoder.decode(.signingMethod),
defaultCurve: defaultCurve)

Expand All @@ -97,14 +88,23 @@ struct CardDeserializer {
let backupRawStatus: Card.BackupRawStatus? = try decoder.decode(.backupStatus)
let backupCardsCount: Int? = try decoder.decode(.backupCount)
let backupStatus: Card.BackupStatus? = try backupRawStatus.map { try Card.BackupStatus(from: $0, cardsCount: backupCardsCount) }


var userSettings: Card.UserSettings
let userSettingsMask: UserSettingsMask? = try decoder.decode(.userSettingsMask)
if let userSettingsMask {
userSettings = .init(from: userSettingsMask)
} else {
userSettings = .init(isResettingUserCodesAllowed: firmware >= .backupAvailable)
}

let card = Card(cardId: try decoder.decode(.cardId),
batchId: try cardDataDecoder.decode(.batchId),
cardPublicKey: try decoder.decode(.cardPublicKey),
firmwareVersion: firmware,
manufacturer: manufacturer,
issuer: issuer,
settings: settings,
userSettings: userSettings,
linkedTerminalStatus: terminalIsLinked ? .current : .none,
isAccessCodeSet: isAccessCodeSet ?? isAccessCodeSetLegacy,
isPasscodeSet: isPasscodeSet,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ final class GetResetPinTokenCommand: Command {
return TangemSdkError.noActiveBackup
}

guard card.settings.isResettingUserCodesAllowed else {
guard card.userSettings.isResettingUserCodesAllowed else {
return TangemSdkError.userCodeResettingDisabled
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class SignResetPinTokenCommand: Command {
return .resetPinWrongCard()
}

guard card.settings.isResettingUserCodesAllowed else {
guard card.userSettings.isResettingUserCodesAllowed else {
return TangemSdkError.userCodeResettingDisabled
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public final class SetResettingUserCodesAllowedTask: CardSessionRunnable {
return
}

var userSettings = UserSettings(from: card.settings)
var userSettings = card.userSettings
userSettings.isResettingUserCodesAllowed = isResettingUserCodesAllowed

let setUserSettingsCommand = SetUserSettingsCommand(settings: userSettings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ struct SetUserSettingsCommandResponse: JSONStringConvertible {
/// Unique Tangem card ID number.
let cardId: String
/// The mask was set
let settings: UserSettings
let settings: Card.UserSettings
}

/// Set user serrings on a card. COS v.6.10+
@available(iOS 13.0, *)
class SetUserSettingsCommand: Command {
var preflightReadMode: PreflightReadMode { .readCardOnly }

private let settings: UserSettings
private let settings: Card.UserSettings

init(settings: UserSettings) {
init(settings: Card.UserSettings) {
self.settings = settings
}

Expand All @@ -44,10 +44,7 @@ class SetUserSettingsCommand: Command {
transceive(in: session) { result in
switch result {
case .success(let response):
if let settings = session.environment.card?.settings {
session.environment.card?.settings = settings.updated(with: response.settings)
}

session.environment.card?.userSettings = response.settings
completion(.success(response))
case .failure(let error):
completion(.failure(error))
Expand Down
4 changes: 3 additions & 1 deletion TangemSdk/TangemSdkTests/Jsons/Card.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
"secp256r1"
],
"cardPublicKey" : "0400D05BCAC34B58AA48BF998FB68667A3112262275200431EA235EC4616A15287B5D21F15E45740AB6B829F415950DBC7A68493DCF5FD270C8CAAB0E975E9A0D9",
"userSettings" : {
"isResettingUserCodesAllowed" : true
},
"settings" : {
"isSettingPasscodeAllowed" : true,
"maxWalletsCount" : 36,
"isOverwritingIssuerExtraDataRestricted" : false,
"isRemovingUserCodesAllowed" : true,
"isResettingUserCodesAllowed" : true,
"isLinkedTerminalEnabled" : true,
"securityDelay" : 3000,
"isSettingAccessCodeAllowed" : false,
Expand Down

0 comments on commit 0e1ca6b

Please sign in to comment.