Skip to content

Commit

Permalink
Merge pull request #147 from bitcoinppl/130-allow-pin-to-wipe-all-dat…
Browse files Browse the repository at this point in the history
…a-on-device

Allow PIN to wipe all data on device
  • Loading branch information
praveenperera authored Dec 18, 2024
2 parents db6f418 + ee499e9 commit 3a4894a
Show file tree
Hide file tree
Showing 22 changed files with 1,767 additions and 374 deletions.
16 changes: 16 additions & 0 deletions ios/Cove/AlertBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
//
import SwiftUI

struct AnyAlertBuilder: AlertBuilderProtocol {
let title: String
let message: AnyView
let actions: AnyView

init(_ alert: some AlertBuilderProtocol) {
title = alert.title
message = AnyView(alert.message)
actions = AnyView(alert.actions)
}
}

protocol AlertBuilderProtocol {
associatedtype Message: View
associatedtype Actions: View
Expand Down Expand Up @@ -39,4 +51,8 @@ struct AlertBuilder<Actions: View, Message: View>: AlertBuilderProtocol {
self.message = Text(message)
self.actions = actions()
}

func eraseToAny() -> AnyAlertBuilder {
AnyAlertBuilder(self)
}
}
28 changes: 6 additions & 22 deletions ios/Cove/AppManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,15 @@ import SwiftUI

var colorSchemeSelection = Database().globalConfig().colorScheme()
var selectedNode = Database().globalConfig().selectedNode()
var authType = Database().globalConfig().authType()

var nfcReader = NFCReader()

var prices: PriceResponse?
var fees: FeeResponse?

var lockState: LockState = .locked

// changed when route is reset, to clear lifecycle view state
var routeId = UUID()

@MainActor
var isUsingBiometrics: Bool = false

@ObservationIgnored
weak var walletManager: WalletManager?

Expand Down Expand Up @@ -79,17 +73,13 @@ import SwiftUI
walletManager = vm
}

public func lock() {
guard isAuthEnabled else { return }
lockState = .locked
}

public func checkPin(_ pin: String) -> Bool {
AuthPin().check(pin: pin)
}
/// Reset the manager state
public func reset() {
rust = FfiApp()
database = Database()

var isAuthEnabled: Bool {
authType != AuthType.none
let state = rust.state()
router = state.router
}

var currentRoute: Route {
Expand Down Expand Up @@ -167,12 +157,6 @@ import SwiftUI

case let .feesChanged(fees):
self.fees = fees

case let .authTypeChanged(authType):
self.authType = authType

case .hashedPinCodeChanged:
()
}
}
}
Expand Down
76 changes: 76 additions & 0 deletions ios/Cove/AuthManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import SwiftUI

@Observable class AuthManager: AuthManagerReconciler {
private let logger = Log(id: "AuthManager")
var rust: RustAuthManager
var type = Database().globalConfig().authType()
var lockState: LockState = .locked
var isWipeDataPinEnabled: Bool

@MainActor
var isUsingBiometrics: Bool = false

public init() {
let rust = RustAuthManager()
self.rust = rust
isWipeDataPinEnabled = rust.isWipeDataPinEnabled()

rust.listenForUpdates(reconciler: self)
}

public func lock() {
guard isAuthEnabled else { return }
lockState = .locked
}

public var isAuthEnabled: Bool {
type != AuthType.none
}

public func checkPin(_ pin: String) -> Bool {
if AuthPin().check(pin: pin) {
return true
}

if checkWipeDataPin(pin) {
AppManager().rust.dangerousWipeAllData()

// reset auth maanger
rust = RustAuthManager()
lockState = .unlocked
type = .none

// reset app manager
AppManager().reset()

return true
}

return false
}

public func checkWipeDataPin(_ pin: String) -> Bool {
rust.checkWipeDataPin(pin: pin)
}

func reconcile(message: AuthManagerReconcileMessage) {
logger.debug("reconcile: \(message)")

Task {
await MainActor.run {
switch message {
case let .authTypeChanged(authType):
self.type = authType

case .wipeDataPinChanged:
self.isWipeDataPinEnabled = self.rust.isWipeDataPinEnabled()
}
}
}
}

public func dispatch(action: AuthManagerAction) {
logger.debug("dispatch: \(action)")
rust.dispatch(action: action)
}
}
Loading

0 comments on commit 3a4894a

Please sign in to comment.