Skip to content
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

[ABW-3634] Fix bug where Account Details wouldn't show Verify Address on Ledger #1229

Merged
merged 5 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -139,4 +139,13 @@ extension AccountsClient {
public func saveVirtualAccount(_ account: Account) async throws {
try await saveVirtualAccounts([account])
}

public func isLedgerHWAccount(_ address: AccountAddress) async -> Bool {
do {
let account = try await getAccountByAddress(address)
return account.isLedgerControlled
} catch {
return false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,3 @@ private extension AddressDetails.View {
return .init(result)
}
}

private extension AddressDetails.State {
var showVerifyOnLedger: Bool {
switch address {
case let .account(_, isLedgerHWAccount):
isLedgerHWAccount
default:
false
}
}
}
22 changes: 22 additions & 0 deletions RadixWallet/Core/FeaturePrelude/AddressView/AddressDetails.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public struct AddressDetails: Sendable, FeatureReducer {
var showEnlarged = false

var showShare = false
var showVerifyOnLedger = false

public init(address: LedgerIdentifiable.Address) {
self.address = address
Expand All @@ -31,6 +32,7 @@ public struct AddressDetails: Sendable, FeatureReducer {
public enum InternalAction: Sendable, Equatable {
case loadedTitle(TaskResult<String?>)
case loadedQrImage(TaskResult<CGImage>)
case loadedShowVerifyOnLedger(Bool)
}

@Dependency(\.accountsClient) var accountsClient
Expand All @@ -48,6 +50,7 @@ public struct AddressDetails: Sendable, FeatureReducer {
case .task:
return loadTitleEffect(state: &state)
.merge(with: loadQrCodeEffect(state: &state))
.merge(with: loadShowVerifyOnLedgerEffect(state: state))
case .copyButtonTapped:
pasteboardClient.copyString(state.address.address)
return .none
Expand Down Expand Up @@ -97,6 +100,9 @@ public struct AddressDetails: Sendable, FeatureReducer {
case let .loadedQrImage(.failure(error)):
state.qrImage = .failure(error)
return .none
case let .loadedShowVerifyOnLedger(value):
state.showVerifyOnLedger = value
return .none
}
}

Expand Down Expand Up @@ -141,6 +147,22 @@ public struct AddressDetails: Sendable, FeatureReducer {
await send(.internal(.loadedQrImage(result)))
}
}

private func loadShowVerifyOnLedgerEffect(state: State) -> Effect<Action> {
switch state.address {
case let .account(address, isLedgerHWAccount):
if let isLedgerHWAccount {
.send(.internal(.loadedShowVerifyOnLedger(isLedgerHWAccount)))
} else {
.run { send in
let showVerifyOnLedger = await accountsClient.isLedgerHWAccount(address)
await send(.internal(.loadedShowVerifyOnLedger(showVerifyOnLedger)))
}
}
default:
.send(.internal(.loadedShowVerifyOnLedger(false)))
}
}
}

// MARK: - Helpers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private extension AddressView {
#if DEBUG
struct AddressView_Previews: PreviewProvider {
static var previews: some View {
AddressView(.address(.account(try! .init(validatingAddress: "account_tdx_b_1p8ahenyznrqy2w0tyg00r82rwuxys6z8kmrhh37c7maqpydx7p"))))
AddressView(.address(.account(try! .init(validatingAddress: "account_tdx_b_1p8ahenyznrqy2w0tyg00r82rwuxys6z8kmrhh37c7maqpydx7p"), isLedgerHWAccount: false)))
}
}
#endif
6 changes: 5 additions & 1 deletion RadixWallet/Core/SharedModels/LedgerIdentifiable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ public enum LedgerIdentifiable: Sendable {
// MARK: LedgerIdentifiable.Address
extension LedgerIdentifiable {
public enum Address: Hashable, Sendable, Identifiable {
case account(AccountAddress, isLedgerHWAccount: Bool = false)
/// `isLedgerHWAccount` indicates if the account is controlled by a Ledger device.
/// - `true`: we know the account is controlled by a Ledger device
/// - `false`: either the account isn't controller by a Ledger device or it is an external account and we don't care.
/// - `nil`: we don't know if the account is controller by a Ledger device, we should check if needed.
case account(AccountAddress, isLedgerHWAccount: Bool?)
GhenadieVP marked this conversation as resolved.
Show resolved Hide resolved
case package(PackageAddress)
case resource(ResourceAddress)
case resourcePool(PoolAddress)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ extension AccountOrAddressOf {
var identifer: LedgerIdentifiable {
switch self {
case let .profileAccount(value: account):
.address(.account(account.address))
.address(.account(account.address, isLedgerHWAccount: account.isLedgerControlled))
case let .addressOfExternalAccount(address):
.address(.account(address))
.address(.account(address, isLedgerHWAccount: false))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ extension NewAccountCompletion {
let subtitle: String

let accountAddress: AccountAddress
let isLedgerControlled: Bool
let appearanceID: AppearanceID

init(state: NewAccountCompletion.State) {
Expand All @@ -31,8 +32,8 @@ extension NewAccountCompletion {
}

self.isFirstOnNetwork = state.isFirstOnNetwork

self.accountAddress = state.account.address
self.isLedgerControlled = state.account.isLedgerControlled
self.appearanceID = state.account.appearanceID
self.explanation = L10n.CreateAccount.Completion.explanation

Expand Down Expand Up @@ -106,7 +107,7 @@ private extension NewAccountCompletion.View {
.textStyle(.body1Header)
.multilineTextAlignment(.center)

AddressView(.address(.account(viewStore.accountAddress)), isTappable: false)
AddressView(.address(.account(viewStore.accountAddress, isLedgerHWAccount: viewStore.isLedgerControlled)), isTappable: false)
.foregroundColor(.app.whiteTransparent)
.textStyle(.body2HighImportance)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ extension DappDetails.View {
Spacer(minLength: 0)

AddressView(
.address(.account(viewStore.address)),
.address(.account(viewStore.address, isLedgerHWAccount: false)),
imageColor: .app.gray2
)
.foregroundColor(.app.gray1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ extension PersonaDetails.View {
ForEach(viewStore.sharingAccounts) { account in
SmallAccountCard(
account.label.rawValue,
identifiable: .address(.account(account.address)),
identifiable: .address(.account(account.address, isLedgerHWAccount: nil)),
gradient: .init(account.appearanceId)
)
.cornerRadius(.small1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ extension CompletionMigrateOlympiaAccountsToBabylon {
Text(name)
.textStyle(.body1Header)
}
AddressView(.address(.account(account.address)))
AddressView(.address(.account(account.address, isLedgerHWAccount: nil)))
.opacity(0.8)
}
.foregroundColor(.app.white)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ extension SmallAccountCard where Accessory == EmptyView {
case let .external(accountAddress, _):
self.init(
L10n.TransactionReview.externalAccountName,
identifiable: .address(.account(accountAddress)),
identifiable: .address(.account(accountAddress, isLedgerHWAccount: false)),
gradient: .init(colors: [.app.gray2]),
verticalPadding: .small1
)
Expand Down
Loading