Skip to content

Commit 0659da3

Browse files
committed
chore: resolve deprecation warnings
1 parent 9734e8f commit 0659da3

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

web3swift/src/Account/EthereumKeyStorage.swift

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,50 +31,59 @@ public class EthereumKeyLocalStorage: EthereumSingleKeyStorageProtocol, @uncheck
3131
private let address: ThreadSafeBox<EthereumAddress?> = ThreadSafeBox(nil)
3232
private let localFileName = "ethereumkey"
3333

34-
private var addressPath: String? {
34+
private var addressURL: URL? {
3535
guard let address = address.value else {
3636
return nil
3737
}
3838
if let url = folderPath {
39-
return url.appendingPathComponent(address.asString()).path
39+
return url.appendingPathComponent(address.asString())
4040
}
4141
return nil
4242
}
4343

44+
private var addressPath: String? { addressURL?.path }
45+
4446
private var folderPath: URL? {
4547
if let url = fileManager.urls(for: .cachesDirectory, in: .userDomainMask).first {
4648
return url
4749
}
4850
return nil
4951
}
5052

53+
private var localURL: URL? {
54+
fileManager
55+
.urls(for: .cachesDirectory, in: .userDomainMask)
56+
.first?
57+
.appendingPathComponent(localFileName)
58+
}
59+
5160
private var localPath: String? {
52-
if let url = fileManager.urls(for: .cachesDirectory, in: .userDomainMask).first {
53-
return url.appendingPathComponent(localFileName).path
54-
}
55-
return nil
61+
localURL?.path
5662
}
5763

5864
private let fileManager = FileManager.default
5965

6066
public func storePrivateKey(key: Data) throws {
61-
guard let localPath else {
67+
guard let localURL else {
6268
throw EthereumKeyStorageError.failedToSave
6369
}
6470

65-
let success = NSKeyedArchiver.archiveRootObject(key, toFile: localPath)
66-
67-
if !success {
71+
do {
72+
try NSKeyedArchiver
73+
.archivedData(withRootObject: key, requiringSecureCoding: false)
74+
.write(to: localURL)
75+
} catch {
6876
throw EthereumKeyStorageError.failedToSave
6977
}
7078
}
7179

7280
public func loadPrivateKey() throws -> Data {
73-
guard let localPath else {
81+
guard let localURL else {
7482
throw EthereumKeyStorageError.failedToLoad
7583
}
7684

77-
guard let data = NSKeyedUnarchiver.unarchiveObject(withFile: localPath) as? Data else {
85+
guard let archivedData = try? Data(contentsOf: localURL),
86+
let data = try NSKeyedUnarchiver.unarchivedObject(ofClass: NSData.self, from: archivedData) as? Data else {
7887
throw EthereumKeyStorageError.failedToLoad
7988
}
8089

@@ -112,13 +121,15 @@ extension EthereumKeyLocalStorage: EthereumMultipleKeyStorageProtocol {
112121
}
113122
}
114123

115-
guard let localPath = self.addressPath else {
124+
guard let localURL = self.addressURL else {
116125
throw EthereumKeyStorageError.failedToSave
117126
}
118127

119-
let success = NSKeyedArchiver.archiveRootObject(key, toFile: localPath)
120-
121-
if !success {
128+
do {
129+
try NSKeyedArchiver
130+
.archivedData(withRootObject: key, requiringSecureCoding: false)
131+
.write(to: localURL)
132+
} catch {
122133
throw EthereumKeyStorageError.failedToSave
123134
}
124135
}
@@ -134,11 +145,12 @@ extension EthereumKeyLocalStorage: EthereumMultipleKeyStorageProtocol {
134145
}
135146
}
136147

137-
guard let localPath = self.addressPath else {
148+
guard let localURL = self.addressURL else {
138149
throw EthereumKeyStorageError.failedToLoad
139150
}
140151

141-
guard let data = NSKeyedUnarchiver.unarchiveObject(withFile: localPath) as? Data else {
152+
guard let archivedData = try? Data(contentsOf: localURL),
153+
let data = try NSKeyedUnarchiver.unarchivedObject(ofClass: NSData.self, from: archivedData) as? Data else {
142154
throw EthereumKeyStorageError.failedToLoad
143155
}
144156

0 commit comments

Comments
 (0)