Skip to content

Commit b7c442e

Browse files
committed
fix: some version issue
1 parent 97863d9 commit b7c442e

File tree

13 files changed

+255
-98
lines changed

13 files changed

+255
-98
lines changed

Shared/HomePage.swift

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,41 @@
55
// Created by Qiwei Li on 6/8/22.
66
//
77

8-
import SwiftUI
98
import CoreData
9+
import SwiftUI
1010

1111
struct HomePage: View {
1212
@EnvironmentObject var userAccountModel: UserAccountModel
1313
@EnvironmentObject var cryptoPassModel: CryptoPassModel
1414
@EnvironmentObject var ethereumModel: EthereumModel
15-
15+
1616
@Environment(\.managedObjectContext) private var viewContext
1717

18-
1918
var body: some View {
2019
NavigationView {
21-
if let _ = userAccountModel.userAccount{
20+
if let _ = userAccountModel.userAccount {
2221
LoginRequiredView {
2322
Text("Login required")
2423
} child: {
2524
PasswordList()
2625
}
27-
2826
}
2927
TitleView(icon: .dotsAndLineVerticalAndCursorarrowRectangle, subtitle: "CryptoPass")
3028
}
31-
.onAppear{
29+
.onAppear {
3230
userAccountModel.initialize()
3331
}
3432
.onReceive(userAccountModel.$userAccount, perform: { account in
35-
if let account = account{
33+
if let account = account {
3634
cryptoPassModel.setUp(client: ethereumModel.ethereumClient, account: account, privateKey: userAccountModel.privateKey!)
3735
}
3836
})
39-
.sheet(isPresented: $userAccountModel.shouldInitAccount){
40-
IntroductionPage()
37+
.sheet(isPresented: $userAccountModel.shouldInitAccount) {
38+
IntroductionPage()
4139
}
4240
}
4341
}
4442

45-
4643
struct ContentView_Previews: PreviewProvider {
4744
static var previews: some View {
4845
HomePage().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)

Shared/common/components/LoginRequiredView.swift

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,43 @@ import SwiftUI
99

1010
struct LoginRequiredView<Content: View, Login: View>: View {
1111
@EnvironmentObject var authenticationModel: AuthenticationModel
12-
12+
1313
@ViewBuilder let loginScreen: () -> Login
1414
@ViewBuilder let child: () -> Content
1515

16-
1716
@State var hasLogined = false
1817
@State var error: AuthenticationError?
1918
@State var hasError = false
20-
19+
2120
var body: some View {
22-
Group{
23-
if hasLogined{
21+
Group {
22+
if hasLogined {
2423
child()
2524
} else {
2625
loginScreen()
2726
}
2827
}
2928
.alert(isPresented: $hasError, error: error, actions: {
3029
Button {
31-
hasError = false
30+
hasError = false
3231
} label: {
3332
Text("ok")
3433
}
3534

3635
})
37-
.onAppear{
36+
.onAppear {
37+
if hasLogined {
38+
return
39+
}
3840
authenticationModel.authenticate { isAuthenticated, hasError in
39-
if isAuthenticated{
41+
if isAuthenticated {
4042
hasLogined = true
4143
} else {
4244
self.hasError = true
4345
self.error = .invalidIdentity
4446
}
45-
46-
if hasError{
47+
48+
if hasError {
4749
self.hasError = true
4850
self.error = .invalidDevice
4951
}

Shared/common/objects/CryptoPassContract.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ struct GetSecretSizeResponse: ABIResponse {
135135

136136

137137
struct CryptoPass{
138-
let contractAddress = EthereumAddress(Environments.contractAddress!)
138+
let contractAddress = EthereumAddress(Environments.contractAddress)
139139
let client: EthereumClient
140140
let account: EthereumAccount
141141
let privateKey: Data

Shared/common/settings/Environments.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010
/**
1111
System environments
1212
*/
13-
struct Environments{
14-
static let rpc = Bundle.main.object(forInfoDictionaryKey: "RPC_ENDPOINT") as? String
15-
static let contractAddress = Bundle.main.object(forInfoDictionaryKey: "CONTRACT") as? String
13+
struct Environments {
14+
static let rpc = RPC_URL
15+
static let contractAddress = CONTRACT_ADDRESS
1616
}

Shared/models/AuthenticationModel.swift

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,21 @@
88
import Foundation
99
import LocalAuthentication
1010

11-
12-
13-
class AuthenticationModel: ObservableObject{
11+
class AuthenticationModel: ObservableObject {
1412
private var context = LAContext()
15-
16-
func authenticate(completion: @escaping (_ isAuthenticated: Bool, _ hasError: Bool) -> ()){
13+
14+
func authenticate(completion: @escaping (_ isAuthenticated: Bool, _ hasError: Bool) -> ()) {
1715
var error: NSError?
1816
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
19-
// it's possible, so go ahead and use it
20-
let reason = "We need to unlock your data."
21-
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, authenticationError in
22-
if success {
23-
completion(true, false)
24-
} else {
25-
completion(false, false)
26-
}
17+
// it's possible, so go ahead and use it
18+
let reason = "We need to unlock your data."
19+
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, _ in
20+
if success {
21+
completion(true, false)
22+
} else {
23+
completion(false, false)
2724
}
25+
}
2826
} else {
2927
completion(false, true)
3028
}

Shared/models/EthereumModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class EthereumModel: ObservableObject{
1212
@Published var ethereumClient: EthereumClient
1313

1414
init(){
15-
let clientUrl = URL(string: Environments.rpc!)
15+
let clientUrl = URL(string: Environments.rpc)
1616
self.ethereumClient = EthereumClient(url: clientUrl!)
1717
}
1818
}

Shared/models/UserAccountModel.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,44 @@
88
import Foundation
99
import web3
1010

11-
12-
class UserAccountModel: ObservableObject{
11+
class UserAccountModel: ObservableObject {
1312
@Published var userAccount: EthereumAccount?
1413
@Published var hasInitAccount: Bool = false
1514
@Published var shouldInitAccount = false
1615
@Published var privateKey: Data?
1716
private let storage = EthereumKeyChainStorage()
18-
19-
func initialize(){
17+
18+
func initialize() {
19+
if hasInitAccount {
20+
return
21+
}
2022
do {
2123
privateKey = try storage.loadPrivateKey()
22-
userAccount = try EthereumAccount.init(keyStorage: storage)
24+
userAccount = try EthereumAccount(keyStorage: storage)
2325
shouldInitAccount = false
2426
hasInitAccount = true
2527
} catch PrivateKeyError.privateKeyNotFound {
2628
shouldInitAccount = true
2729
print("No previous stored private key")
28-
} catch{
30+
} catch {
2931
print(error.localizedDescription)
3032
}
3133
}
32-
33-
func importAccount(privateKey: String) throws{
34+
35+
func importAccount(privateKey: String) throws {
3436
let keyStoreage = EthereumKeyChainStorage()
35-
//TODO: Replace 123
37+
// TODO: Replace 123
3638
userAccount = try EthereumAccount.importAccount(keyStorage: keyStoreage, privateKey: privateKey)
3739
self.privateKey = privateKey.web3.hexData
3840
}
39-
40-
func resetAccount(){
41+
42+
func resetAccount() {
4143
userAccount = nil
4244
hasInitAccount = false
4345
shouldInitAccount = true
4446
}
45-
46-
func finishSetup(){
47+
48+
func finishSetup() {
4749
hasInitAccount = true
4850
shouldInitAccount = false
4951
}

Shared/pages/password/PasswordList.swift

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
// Created by Qiwei Li on 6/8/22.
66
//
77

8+
import BigInt
89
import SwiftUI
910
import web3
10-
import BigInt
1111

1212
struct PasswordList: View {
1313
@EnvironmentObject var userAccountModel: UserAccountModel
@@ -16,14 +16,10 @@ struct PasswordList: View {
1616
@EnvironmentObject var transactionModel: TransactionModel
1717
@EnvironmentObject var passwordListModel: PasswordListModel
1818

19-
20-
2119
var body: some View {
22-
Group{
23-
NavigationLink(destination: PasswordForm(), tag: 1, selection: $passwordListModel.selection){
24-
25-
}
26-
List{
20+
Group {
21+
NavigationLink(destination: PasswordForm(), tag: 1, selection: $passwordListModel.selection) {}
22+
List {
2723
Section(header: Text("Blockchain info")) {
2824
InfoCard(title: "BlockNumber", subtitle: "\(passwordListModel.blockNumber)", color: .orange, unit: "blocks", icon: .boltBatteryblock)
2925

@@ -32,32 +28,29 @@ struct PasswordList: View {
3228
InfoCard(title: "Password Count", subtitle: "\(passwordListModel.passwordSize)", color: .orange, unit: "", icon: .boltBatteryblock)
3329
}
3430

35-
3631
Section(header: Text("Passwords")) {
37-
ForEach(passwordListModel.passwords){ password in
32+
ForEach(passwordListModel.passwords) { password in
3833
NavigationLink(destination: PasswordForm(editMode: false, password: password)) {
3934
PasswordRow(password: password)
4035
}
4136
}
42-
.onDelete{
37+
.onDelete {
4338
indexSet in
44-
Task{
39+
Task {
4540
await delete(at: indexSet)
4641
}
4742
}
48-
4943
}
50-
5144
}
5245
.listStyle(InsetGroupedListStyle())
53-
.toolbar{
54-
ToolbarItem(placement: .navigationBarLeading){
55-
Button(action: { userAccountModel.resetAccount() }){
46+
.toolbar {
47+
ToolbarItem(placement: .navigationBarLeading) {
48+
Button(action: { userAccountModel.resetAccount() }) {
5649
Image(systemSymbol: .externaldriveFill)
5750
}
5851
}
59-
ToolbarItem(placement: .navigationBarTrailing){
60-
Button(action: { passwordListModel.selection = 1 }){
52+
ToolbarItem(placement: .navigationBarTrailing) {
53+
Button(action: { passwordListModel.selection = 1 }) {
6154
Image(systemSymbol: .plus)
6255
}
6356
}
@@ -68,18 +61,18 @@ struct PasswordList: View {
6861
print("BlockNumber: \(passwordListModel.blockNumber)")
6962
await fetchBlockchainData(isRefresh: true)
7063
}
71-
.onReceive(userAccountModel.$userAccount){ _ in
64+
.onReceive(userAccountModel.$userAccount) { _ in
7265
Task {
7366
await fetchBlockchainData()
7467
}
7568
}
76-
.onReceive(transactionModel.$transactionReceipt){
69+
.onReceive(transactionModel.$transactionReceipt) {
7770
_ in
7871
Task {
7972
await fetchBlockchainData()
8073
}
8174
}
82-
.alert(isPresented: $passwordListModel.hasError){
75+
.alert(isPresented: $passwordListModel.hasError) {
8376
Alert(title: Text("Error"), message: Text(passwordListModel.error!), dismissButton: .default(Text("ok")))
8477
}
8578
.task {
@@ -92,7 +85,7 @@ struct PasswordList: View {
9285

9386
func delete(at offsets: IndexSet) async {
9487
let index = offsets.first
95-
if let cryptoPass = cryptoPassModel.client{
88+
if let cryptoPass = cryptoPassModel.client {
9689
if let index = index {
9790
let transaction = try! await cryptoPass.prepareDeleteTransaction(at: BigUInt(index))
9891
transactionModel.showConfirmation(transaction: transaction)
@@ -105,17 +98,17 @@ struct PasswordList: View {
10598
*/
10699
private func fetchBlockchainData(isRefresh: Bool = false) async {
107100
passwordListModel.update()
108-
withAnimation{
109-
Task{
110-
do{
111-
if let userAccount = userAccountModel.userAccount{
101+
withAnimation {
102+
Task {
103+
do {
104+
if let userAccount = userAccountModel.userAccount {
112105
passwordListModel.blockNumber = try await ethereumModel.ethereumClient.eth_blockNumber()
113-
passwordListModel.accountBalance = BigInt(try await ethereumModel.ethereumClient.eth_getBalance(address: userAccount.address, block: EthereumBlock.Latest))
106+
passwordListModel.accountBalance = try BigInt(await ethereumModel.ethereumClient.eth_getBalance(address: userAccount.address, block: EthereumBlock.Latest))
114107

115-
if let cryptoPass = cryptoPassModel.client{
108+
if let cryptoPass = cryptoPassModel.client {
116109
passwordListModel.passwordSize = try await cryptoPass.getSecretSize()
117-
if passwordListModel.passwordSize > 0{
118-
passwordListModel.passwords = try await cryptoPass.getSecretsInRange(start: 0, end: passwordListModel.passwordSize).map{
110+
if passwordListModel.passwordSize > 0 {
111+
passwordListModel.passwords = try await cryptoPass.getSecretsInRange(start: 0, end: passwordListModel.passwordSize).map {
119112
password in
120113
CommonPassword.from(password: password)!
121114
}
@@ -125,7 +118,7 @@ struct PasswordList: View {
125118
}
126119
}
127120

128-
} catch{
121+
} catch {
129122
print(error.localizedDescription)
130123
passwordListModel.hasError = true
131124
passwordListModel.error = error.localizedDescription

Shared/pages/transaction/ConfirmationPage.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct ConfirmationPage: View {
3434

3535
}
3636
}
37-
if let transaction = transaction {
37+
if transaction != nil {
3838
Form{
3939
Section("Transaction Summary") {
4040
FormTextField(leading: {Text("Value")}) {
@@ -147,7 +147,7 @@ struct ConfirmationPage: View {
147147
Task {
148148
if let transaction = transactionModel.transaction{
149149
do {
150-
gasFee = try await ethereumModel.ethereumClient.eth_estimateGas(transaction, withAccount: userAccountModel.userAccount!)
150+
gasFee = try await ethereumModel.ethereumClient.eth_estimateGas(transaction)
151151
gasPrice = try await ethereumModel.ethereumClient.eth_gasPrice()
152152
nonce = try await ethereumModel.ethereumClient.eth_getTransactionCount(address: userAccountModel.userAccount!.address, block: .Latest)
153153
chainId = try await ethereumModel.ethereumClient.chainId()

Shared/secrets.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//
2+
// secrets.swift
3+
// cryptoPass
4+
//
5+
// Created by Qiwei Li on 9/26/23.
6+
//
7+
8+
import Foundation
9+
10+
let RPC_URL = "https://rpc.etdchain.net"
11+
let CONTRACT_ADDRESS = "0x196016EB927A63BbCb7dA010b8fd945a356A66c0"

0 commit comments

Comments
 (0)