Skip to content

Commit f33b07e

Browse files
committed
associated types and typealiases prettification
1 parent db9daae commit f33b07e

File tree

7 files changed

+32
-21
lines changed

7 files changed

+32
-21
lines changed

Example/web3swiftBrowser/web3swiftBrowser.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@
410410
CODE_SIGN_STYLE = Automatic;
411411
DEVELOPMENT_TEAM = "";
412412
INFOPLIST_FILE = web3swiftBrowser/Info.plist;
413+
IPHONEOS_DEPLOYMENT_TARGET = 13.7;
413414
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
414415
PRODUCT_BUNDLE_IDENTIFIER = io.thematter.web3swiftBrowser;
415416
PRODUCT_NAME = "$(TARGET_NAME)";
@@ -426,6 +427,7 @@
426427
CODE_SIGN_STYLE = Automatic;
427428
DEVELOPMENT_TEAM = "";
428429
INFOPLIST_FILE = web3swiftBrowser/Info.plist;
430+
IPHONEOS_DEPLOYMENT_TARGET = 13.7;
429431
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
430432
PRODUCT_BUNDLE_IDENTIFIER = io.thematter.web3swiftBrowser;
431433
PRODUCT_NAME = "$(TARGET_NAME)";

Sources/web3swift/KeystoreManager/AbstractKeystore.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import Foundation
99
//import EthereumAddress
1010

1111
public protocol AbstractKeystore {
12-
public func giveKeystoreParams() -> AbstractKeystoreParams
12+
public associatedtype Params: AbstractKeystoreParams
13+
func giveKeystoreParams() -> Params
1314
var addresses: [EthereumAddress]? { get }
1415
var isHDKeystore: Bool { get }
1516
func UNSAFE_getPrivateKeyData(password: String, account: EthereumAddress) throws -> Data

Sources/web3swift/KeystoreManager/BIP32Keystore.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import Foundation
1111
//import EthereumAddress
1212

1313
public class BIP32Keystore: AbstractKeystore {
14-
public func giveKeystoreParams() -> AbstractKeystoreParams {
15-
keystoreParams
14+
15+
typealias Params = KeystoreParamsBIP32
16+
public func giveKeystoreParams() -> Params {
17+
self.keystoreParams
1618
}
1719

1820
// Protocol

Sources/web3swift/KeystoreManager/BIP39.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ public class BIP39 {
107107
static public func generateMnemonics(bitsOfEntropy: Int, language: BIP39Language = BIP39Language.english) throws -> String? {
108108
guard bitsOfEntropy >= 128 && bitsOfEntropy <= 256 && bitsOfEntropy.isMultiple(of: 32) else {return nil}
109109
guard let entropy = Data.randomBytes(length: bitsOfEntropy/8) else {throw AbstractKeystoreError.noEntropyError}
110-
return BIP39.generateMnemonicsFromEntropy(entropy: entropy, language: language)
110+
return BIP39.generateMnemonicsFromEntropy(entropy: entropy, language:
111+
language)
111112

112113
}
113114

Sources/web3swift/KeystoreManager/EthereumKeystoreV3.swift

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@ import CryptoSwift
99
import Foundation
1010

1111
public class EthereumKeystoreV3: AbstractKeystore {
12+
typealias Params = KeystoreParamsV3
1213
public var keystoreParams: KeystoreParamsV3?
1314

14-
public func giveKeystoreParams() -> AbstractKeystoreParams {
15+
public func giveKeystoreParams() -> Params {
1516
keystoreParams
1617
}
1718

1819

1920
// Protocol
2021
private var address: EthereumAddress?
2122
public var isHDKeystore: Bool = false
22-
23+
2324
public var addresses: [EthereumAddress]? {
2425
get {
2526
if self.address != nil {
@@ -28,34 +29,34 @@ public class EthereumKeystoreV3: AbstractKeystore {
2829
return nil
2930
}
3031
}
31-
32+
3233
public func UNSAFE_getPrivateKeyData(password: String, account: EthereumAddress) throws -> Data {
3334
if self.addresses?.count == 1 && account == self.addresses?.last {
3435
guard let privateKey = try? self.getKeyData(password) else {throw AbstractKeystoreError.invalidPasswordError}
3536
return privateKey
3637
}
3738
throw AbstractKeystoreError.invalidAccountError
3839
}
39-
40+
4041
// Class
41-
42+
4243
public func getAddress() -> EthereumAddress? {
4344
return self.address
4445
}
45-
46+
4647
// --------------
47-
48+
4849
public convenience init?(_ jsonString: String) {
4950
let lowercaseJSON = jsonString.lowercased()
5051
guard let jsonData = lowercaseJSON.data(using: .utf8) else {return nil}
5152
self.init(jsonData)
5253
}
53-
54+
5455
public convenience init?(_ jsonData: Data) {
5556
guard let keystoreParams = try? JSONDecoder().decode(KeystoreParamsV3.self, from: jsonData) else {return nil}
5657
self.init(keystoreParams)
5758
}
58-
59+
5960
public init?(_ keystoreParams: KeystoreParamsV3) {
6061
if (keystoreParams.version != 3) {return nil}
6162
if (keystoreParams.crypto.version != nil && keystoreParams.crypto.version != "1") {return nil}
@@ -66,19 +67,19 @@ public class EthereumKeystoreV3: AbstractKeystore {
6667
return nil
6768
}
6869
}
69-
70+
7071
public init? (password: String = "web3swift", aesMode: String = "aes-128-cbc") throws {
7172
guard var newPrivateKey = SECP256K1.generatePrivateKey() else {return nil}
7273
defer {Data.zero(&newPrivateKey)}
7374
try encryptDataToStorage(password, keyData: newPrivateKey, aesMode: aesMode)
7475
}
75-
76+
7677
public init? (privateKey: Data, password: String = "web3swift", aesMode: String = "aes-128-cbc") throws {
7778
guard privateKey.count == 32 else {return nil}
7879
guard SECP256K1.verifyPrivateKey(privateKey: privateKey) else {return nil}
7980
try encryptDataToStorage(password, keyData: privateKey, aesMode: aesMode)
8081
}
81-
82+
8283
fileprivate func encryptDataToStorage(_ password: String, keyData: Data?, dkLen: Int=32, N: Int = 4096, R: Int = 6, P: Int = 1, aesMode: String = "aes-128-cbc") throws {
8384
if (keyData == nil) {
8485
throw AbstractKeystoreError.encryptionError("Encryption without key data")
@@ -117,7 +118,7 @@ public class EthereumKeystoreV3: AbstractKeystore {
117118
let keystoreparams = KeystoreParamsV3(address: addr.address.lowercased(), crypto: crypto, id: UUID().uuidString.lowercased(), version: 3)
118119
self.keystoreParams = keystoreparams
119120
}
120-
121+
121122
public func regenerate(oldPassword: String, newPassword: String, dkLen: Int=32, N: Int = 4096, R: Int = 6, P: Int = 1) throws {
122123
var keyData = try self.getKeyData(oldPassword)
123124
if keyData == nil {
@@ -126,7 +127,7 @@ public class EthereumKeystoreV3: AbstractKeystore {
126127
defer {Data.zero(&keyData!)}
127128
try self.encryptDataToStorage(newPassword, keyData: keyData!, aesMode: self.keystoreParams!.crypto.cipher)
128129
}
129-
130+
130131
fileprivate func getKeyData(_ password: String) throws -> Data? {
131132
guard let keystoreParams = self.keystoreParams else {return nil}
132133
guard let saltData = Data.fromHex(keystoreParams.crypto.kdfparams.salt) else {return nil}
@@ -187,7 +188,7 @@ public class EthereumKeystoreV3: AbstractKeystore {
187188
// return Data(bytes:decryptedPK!)
188189
return Data(decryptedPK!)
189190
}
190-
191+
191192
public func serialize() throws -> Data? {
192193
guard let params = self.keystoreParams else {return nil}
193194
let data = try JSONEncoder().encode(params)

Sources/web3swift/KeystoreManager/KeystoreManager.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import Foundation
99
//import EthereumAddress
1010

1111
public class KeystoreManager: AbstractKeystore {
12-
public func giveKeystoreParams() -> AbstractKeystoreParams {
12+
associatedtype Params = KeystoreParamsV3
13+
public func giveKeystoreParams() -> Params {
1314
fatalError("giveKeystoreParams() is not available for manager implemented")
15+
return nil
1416
}
1517

1618
public var isHDKeystore: Bool = false

Sources/web3swift/KeystoreManager/PlainKeystore.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import Foundation
1010
//import EthereumAddress
1111

1212
public class PlainKeystore: AbstractKeystore {
13+
typealias Params = KeystoreParamsV3
14+
1315
public var keystoreParams: KeystoreParamsV3?
1416

15-
public func giveKeystoreParams() -> AbstractKeystoreParams {
17+
public func giveKeystoreParams() -> T {
1618
keystoreParams
1719
}
1820

0 commit comments

Comments
 (0)