Skip to content

Commit

Permalink
associated types and typealiases prettification
Browse files Browse the repository at this point in the history
  • Loading branch information
skywinder committed Oct 27, 2020
1 parent db9daae commit f33b07e
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = web3swiftBrowser/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 13.7;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = io.thematter.web3swiftBrowser;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -426,6 +427,7 @@
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = web3swiftBrowser/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 13.7;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = io.thematter.web3swiftBrowser;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down
3 changes: 2 additions & 1 deletion Sources/web3swift/KeystoreManager/AbstractKeystore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import Foundation
//import EthereumAddress

public protocol AbstractKeystore {
public func giveKeystoreParams() -> AbstractKeystoreParams
public associatedtype Params: AbstractKeystoreParams
func giveKeystoreParams() -> Params
var addresses: [EthereumAddress]? { get }
var isHDKeystore: Bool { get }
func UNSAFE_getPrivateKeyData(password: String, account: EthereumAddress) throws -> Data
Expand Down
6 changes: 4 additions & 2 deletions Sources/web3swift/KeystoreManager/BIP32Keystore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import Foundation
//import EthereumAddress

public class BIP32Keystore: AbstractKeystore {
public func giveKeystoreParams() -> AbstractKeystoreParams {
keystoreParams

typealias Params = KeystoreParamsBIP32
public func giveKeystoreParams() -> Params {
self.keystoreParams
}

// Protocol
Expand Down
3 changes: 2 additions & 1 deletion Sources/web3swift/KeystoreManager/BIP39.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public class BIP39 {
static public func generateMnemonics(bitsOfEntropy: Int, language: BIP39Language = BIP39Language.english) throws -> String? {
guard bitsOfEntropy >= 128 && bitsOfEntropy <= 256 && bitsOfEntropy.isMultiple(of: 32) else {return nil}
guard let entropy = Data.randomBytes(length: bitsOfEntropy/8) else {throw AbstractKeystoreError.noEntropyError}
return BIP39.generateMnemonicsFromEntropy(entropy: entropy, language: language)
return BIP39.generateMnemonicsFromEntropy(entropy: entropy, language:
language)

}

Expand Down
31 changes: 16 additions & 15 deletions Sources/web3swift/KeystoreManager/EthereumKeystoreV3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ import CryptoSwift
import Foundation

public class EthereumKeystoreV3: AbstractKeystore {
typealias Params = KeystoreParamsV3
public var keystoreParams: KeystoreParamsV3?

public func giveKeystoreParams() -> AbstractKeystoreParams {
public func giveKeystoreParams() -> Params {
keystoreParams
}


// Protocol
private var address: EthereumAddress?
public var isHDKeystore: Bool = false

public var addresses: [EthereumAddress]? {
get {
if self.address != nil {
Expand All @@ -28,34 +29,34 @@ public class EthereumKeystoreV3: AbstractKeystore {
return nil
}
}

public func UNSAFE_getPrivateKeyData(password: String, account: EthereumAddress) throws -> Data {
if self.addresses?.count == 1 && account == self.addresses?.last {
guard let privateKey = try? self.getKeyData(password) else {throw AbstractKeystoreError.invalidPasswordError}
return privateKey
}
throw AbstractKeystoreError.invalidAccountError
}

// Class

public func getAddress() -> EthereumAddress? {
return self.address
}

// --------------

public convenience init?(_ jsonString: String) {
let lowercaseJSON = jsonString.lowercased()
guard let jsonData = lowercaseJSON.data(using: .utf8) else {return nil}
self.init(jsonData)
}

public convenience init?(_ jsonData: Data) {
guard let keystoreParams = try? JSONDecoder().decode(KeystoreParamsV3.self, from: jsonData) else {return nil}
self.init(keystoreParams)
}

public init?(_ keystoreParams: KeystoreParamsV3) {
if (keystoreParams.version != 3) {return nil}
if (keystoreParams.crypto.version != nil && keystoreParams.crypto.version != "1") {return nil}
Expand All @@ -66,19 +67,19 @@ public class EthereumKeystoreV3: AbstractKeystore {
return nil
}
}

public init? (password: String = "web3swift", aesMode: String = "aes-128-cbc") throws {
guard var newPrivateKey = SECP256K1.generatePrivateKey() else {return nil}
defer {Data.zero(&newPrivateKey)}
try encryptDataToStorage(password, keyData: newPrivateKey, aesMode: aesMode)
}

public init? (privateKey: Data, password: String = "web3swift", aesMode: String = "aes-128-cbc") throws {
guard privateKey.count == 32 else {return nil}
guard SECP256K1.verifyPrivateKey(privateKey: privateKey) else {return nil}
try encryptDataToStorage(password, keyData: privateKey, aesMode: aesMode)
}

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 {
if (keyData == nil) {
throw AbstractKeystoreError.encryptionError("Encryption without key data")
Expand Down Expand Up @@ -117,7 +118,7 @@ public class EthereumKeystoreV3: AbstractKeystore {
let keystoreparams = KeystoreParamsV3(address: addr.address.lowercased(), crypto: crypto, id: UUID().uuidString.lowercased(), version: 3)
self.keystoreParams = keystoreparams
}

public func regenerate(oldPassword: String, newPassword: String, dkLen: Int=32, N: Int = 4096, R: Int = 6, P: Int = 1) throws {
var keyData = try self.getKeyData(oldPassword)
if keyData == nil {
Expand All @@ -126,7 +127,7 @@ public class EthereumKeystoreV3: AbstractKeystore {
defer {Data.zero(&keyData!)}
try self.encryptDataToStorage(newPassword, keyData: keyData!, aesMode: self.keystoreParams!.crypto.cipher)
}

fileprivate func getKeyData(_ password: String) throws -> Data? {
guard let keystoreParams = self.keystoreParams else {return nil}
guard let saltData = Data.fromHex(keystoreParams.crypto.kdfparams.salt) else {return nil}
Expand Down Expand Up @@ -187,7 +188,7 @@ public class EthereumKeystoreV3: AbstractKeystore {
// return Data(bytes:decryptedPK!)
return Data(decryptedPK!)
}

public func serialize() throws -> Data? {
guard let params = self.keystoreParams else {return nil}
let data = try JSONEncoder().encode(params)
Expand Down
4 changes: 3 additions & 1 deletion Sources/web3swift/KeystoreManager/KeystoreManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import Foundation
//import EthereumAddress

public class KeystoreManager: AbstractKeystore {
public func giveKeystoreParams() -> AbstractKeystoreParams {
associatedtype Params = KeystoreParamsV3
public func giveKeystoreParams() -> Params {
fatalError("giveKeystoreParams() is not available for manager implemented")
return nil
}

public var isHDKeystore: Bool = false
Expand Down
4 changes: 3 additions & 1 deletion Sources/web3swift/KeystoreManager/PlainKeystore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import Foundation
//import EthereumAddress

public class PlainKeystore: AbstractKeystore {
typealias Params = KeystoreParamsV3

public var keystoreParams: KeystoreParamsV3?

public func giveKeystoreParams() -> AbstractKeystoreParams {
public func giveKeystoreParams() -> T {
keystoreParams
}

Expand Down

0 comments on commit f33b07e

Please sign in to comment.