Skip to content

Commit

Permalink
Merge pull request #790 from pharms-eth/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
yaroslavyaroslav authored Apr 2, 2023
2 parents d5cf381 + 62b7437 commit cdfb984
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 31 deletions.
71 changes: 41 additions & 30 deletions Sources/Web3Core/KeystoreManager/KeystoreParams.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,69 @@

import Foundation

public struct KdfParamsV3: Decodable, Encodable {
public struct KdfParamsV3: Codable {
var salt: String
var dklen: Int
var n: Int?
var p: Int?
var r: Int?
var c: Int?
var prf: String?

public init(salt: String, dklen: Int, n: Int? = nil, p: Int? = nil, r: Int? = nil, c: Int? = nil, prf: String? = nil) {
self.salt = salt
self.dklen = dklen
self.n = n
self.p = p
self.r = r
self.c = c
self.prf = prf
}
}

public struct CipherParamsV3: Decodable, Encodable {
public struct CipherParamsV3: Codable {
var iv: String

public init(iv: String) {
self.iv = iv
}
}

public struct CryptoParamsV3: Decodable, Encodable {
public struct CryptoParamsV3: Codable {
var ciphertext: String
var cipher: String
var cipherparams: CipherParamsV3
var kdf: String
var kdfparams: KdfParamsV3
var mac: String
var version: String?

public init(ciphertext: String, cipher: String, cipherparams: CipherParamsV3, kdf: String, kdfparams: KdfParamsV3, mac: String, version: String? = nil) {
self.ciphertext = ciphertext
self.cipher = cipher
self.cipherparams = cipherparams
self.kdf = kdf
self.kdfparams = kdfparams
self.mac = mac
self.version = version
}
}

public protocol AbstractKeystoreParams: Codable {
var crypto: CryptoParamsV3 { get }
var id: String? { get }
var version: Int { get }
var isHDWallet: Bool { get }

}

public struct PathAddressPair: Codable {
let path: String
let address: String
public let path: String
public let address: String

public init(path: String, address: String) {
self.path = path
self.address = address
}
}

public struct KeystoreParamsBIP32: AbstractKeystoreParams {
Expand All @@ -47,21 +75,7 @@ public struct KeystoreParamsBIP32: AbstractKeystoreParams {
public var version: Int
public var isHDWallet: Bool

@available(*, deprecated, message: "Please use pathAddressPairs instead")
var pathToAddress: [String: String] {
get {
return self.pathAddressPairs.reduce(into: [String: String]()) {
$0[$1.path] = $1.address
}
}
set {
for pair in newValue {
self.pathAddressPairs.append(PathAddressPair(path: pair.0, address: pair.1))
}
}
}

var pathAddressPairs: [PathAddressPair]
public internal(set) var pathAddressPairs: [PathAddressPair]
var rootPath: String?

public init(crypto cr: CryptoParamsV3, id i: String, version ver: Int = 32, rootPath: String? = nil) {
Expand All @@ -72,23 +86,20 @@ public struct KeystoreParamsBIP32: AbstractKeystoreParams {
self.rootPath = rootPath
self.isHDWallet = true
}

}

public struct KeystoreParamsV3: AbstractKeystoreParams {
public var address: String?
public var crypto: CryptoParamsV3
public var id: String?
public var version: Int
public var isHDWallet: Bool

var address: String?

public init(address ad: String?, crypto cr: CryptoParamsV3, id i: String, version ver: Int) {
address = ad
self.crypto = cr
self.id = i
self.version = ver
public init(address: String?, crypto: CryptoParamsV3, id: String, version: Int) {
self.address = address
self.crypto = crypto
self.id = id
self.version = version
self.isHDWallet = false
}

}
2 changes: 1 addition & 1 deletion Sources/Web3Core/Utility/String+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ extension String {
return String(self[start..<end])
}

func leftPadding(toLength: Int, withPad character: Character) -> String {
public func leftPadding(toLength: Int, withPad character: Character) -> String {
let stringLength = self.count
if stringLength < toLength {
return String(repeatElement(character, count: toLength - stringLength)) + self
Expand Down

0 comments on commit cdfb984

Please sign in to comment.