Skip to content

Commit

Permalink
Merge pull request #39 from torusresearch/feat/revampTorusUtil
Browse files Browse the repository at this point in the history
Feat/revamp torus util
  • Loading branch information
chaitanyapotti authored May 24, 2023
2 parents c9beaa8 + 9eda317 commit acfdebc
Show file tree
Hide file tree
Showing 22 changed files with 575 additions and 399 deletions.
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ let package = Package(
dependencies: ["FetchNodeDetails", "CryptoSwift"]),
.testTarget(
name: "TorusUtilsTests",
dependencies: ["TorusUtils", "CryptoSwift", .product(name: "JWTKit", package: "jwt-kit"), "FetchNodeDetails"])
dependencies: ["TorusUtils", "CryptoSwift", .product(name: "JWTKit", package: "jwt-kit"), "FetchNodeDetails"]
)
], swiftLanguageVersions: [.v5]

)
2 changes: 1 addition & 1 deletion Sources/TorusUtils/AbstractTorusUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import FetchNodeDetails
import Foundation

public protocol AbstractTorusUtils {
func retrieveShares(torusNodePubs: [TorusNodePubModel], endpoints: [String], verifier: String, verifierId: String, idToken: String, extraParams: Data) async throws -> [String: String]
func retrieveShares(torusNodePubs: [TorusNodePubModel], endpoints: [String], verifier: String, verifierId: String, idToken: String, extraParams: Data) async throws -> RetrieveSharesResponseModel

func getPublicAddress(endpoints: [String], torusNodePubs: [TorusNodePubModel], verifier: String, verifierId: String, isExtended: Bool) async throws -> GetPublicAddressModel
}
6 changes: 3 additions & 3 deletions Sources/TorusUtils/DataModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import BigInt
import Foundation

public struct TaskGroupResponse {
public var data:Data
public var urlResponse:URLResponse
public var index:Int
public var data: Data
public var urlResponse: URLResponse
public var index: Int

public init(data: Data, urlResponse: URLResponse, index: Int) {
self.data = data
Expand Down
37 changes: 37 additions & 0 deletions Sources/TorusUtils/Extensions/HelperExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// File.swift
//
//
// Created by Dhruv Jaiswal on 09/04/23.
//

import Foundation

// Necessary for decryption

extension Sequence where Element == UInt8 {
var data: Data { .init(self) }
var hexa: String { map { .init(format: "%02x", $0) }.joined() }
}

extension Data {
init?(hexString: String) {
let length = hexString.count / 2
var data = Data(capacity: length)
for i in 0 ..< length {
let j = hexString.index(hexString.startIndex, offsetBy: i * 2)
let k = hexString.index(j, offsetBy: 2)
let bytes = hexString[j ..< k]
if var byte = UInt8(bytes, radix: 16) {
data.append(&byte, count: 1)
} else {
return nil
}
}
self = data
}

func addLeading0sForLength64() -> Data {
Data(hex: toHexString().addLeading0sForLength64())
}
}
4 changes: 2 additions & 2 deletions Sources/TorusUtils/Extensions/StringExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import Foundation
extension String {

func padLeft(padChar:Character,count:Int) -> String {
func padLeft(padChar: Character, count: Int) -> String {
let str = self
if str.count >= count {
return str
}
var resultStr = ""
while(str.count < count - str.count) {
while str.count < count - str.count {
resultStr.append(padChar)
}
resultStr.append(str)
Expand Down
311 changes: 103 additions & 208 deletions Sources/TorusUtils/Extensions/TorusUtils+extension.swift

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Sources/TorusUtils/Helpers/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension TorusUtilError: CustomDebugStringConvertible {
case .apiRequestFailed:
return "API request failed or No response from the node"
case let .decodingFailed(response):
return "JSON Decoding error \(response)"
return "JSON Decoding error \(response ?? "")"
case let .errInResponse(str):
return "API response error \(str)"
case .decryptionFailed:
Expand All @@ -60,7 +60,7 @@ extension TorusUtilError: CustomDebugStringConvertible {
case .invalidKeySize:
return "Invalid key size. Expected 32 bytes"
case let .encodingFailed(msg):
return "Could not encode data \(msg)"
return "Could not encode data \(msg ?? "")"
}
}

Expand All @@ -80,7 +80,7 @@ extension TorusUtilError: LocalizedError {
case .apiRequestFailed:
return "API request failed or No response from the node"
case let .decodingFailed(response):
return "JSON Decoding error \(response)"
return "JSON Decoding error \(response ?? "")"
case let .errInResponse(str):
return "API response error \(str)"
case .decryptionFailed:
Expand All @@ -106,7 +106,7 @@ extension TorusUtilError: LocalizedError {
case .invalidKeySize:
return "Invalid key size. Expected 32 bytes"
case let .encodingFailed(msg):
return "Could not encode data \(msg)"
return "Could not encode data \(msg ?? "")"
}
}
}
5 changes: 0 additions & 5 deletions Sources/TorusUtils/Helpers/JSONRPCRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,6 @@ public struct JSONRPCresponse: Codable {
self.error = error
}

// public struct ErrorMessage: Decodable {
// public var code: Int
// public var message: String
// }

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: JSONRPCresponseKeys.self)
let id: Int = try container.decode(Int.self, forKey: .id)
Expand Down
29 changes: 29 additions & 0 deletions Sources/TorusUtils/Helpers/NetworkingHelper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// File.swift
//
//
// Created by Dhruv Jaiswal on 30/03/23.
//

import Foundation

enum HTTPMethod {
case get
case post

var name: String {
switch self {
case .get:
return "GET"
case .post:
return "POST"
}
}
}

extension TorusUtils {
func createURLSession() -> URLSession {
let session = URLSession(configuration: urlSession.configuration)
return session
}
}
34 changes: 34 additions & 0 deletions Sources/TorusUtils/Helpers/Web3Error.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
public enum Web3Error: Error {
case transactionSerializationError
case connectionError
case dataError
case walletError
case inputError(desc: String)
case nodeError(desc: String)
case processingError(desc: String)
case generalError(err: Error)
case unknownError

public var errorDescription: String {
switch self {
case .transactionSerializationError:
return "Transaction Serialization Error"
case .connectionError:
return "Connection Error"
case .dataError:
return "Data Error"
case .walletError:
return "Wallet Error"
case let .inputError(desc):
return desc
case let .nodeError(desc):
return desc
case let .processingError(desc):
return desc
case let .generalError(err):
return err.localizedDescription
case .unknownError:
return "Unknown Error"
}
}
}
38 changes: 38 additions & 0 deletions Sources/TorusUtils/Models/CommitmentRequestResponseModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// File.swift
//
//
// Created by Dhruv Jaiswal on 03/04/23.
//

import Foundation

public struct CommitmentRequestResponseModel: Decodable {
public var data: String
public var nodepubx: String
public var nodepuby: String
public var signature: String

public init(data: String, nodepubx: String, nodepuby: String, signature: String) {
self.data = data
self.nodepubx = nodepubx
self.nodepuby = nodepuby
self.signature = signature
}
}

extension Array where Element == CommitmentRequestResponseModel {

public func tostringDict() -> [[String: String]] {
var dictArr = [[String: String]]()
for val in self {
var dict = [String: String]()
dict["data"] = val.data
dict["nodepubx"] = val.nodepubx
dict["nodepuby"] = val.nodepuby
dict["signature"] = val.signature
dictArr.append(dict)
}
return dictArr
}
}
57 changes: 57 additions & 0 deletions Sources/TorusUtils/Models/KeyLookupResponseModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// File.swift
//
//
// Created by Dhruv Jaiswal on 02/04/23.
//

import Foundation

public struct KeyLookupResponseModel: CustomStringConvertible, Hashable {

public let pubKeyX: String
public let pubKeyY: String
public let keyIndex: String
public let address: String
public var description: String {
return "public key X is \(pubKeyX) public key Y is \(pubKeyY) address is \(address)"
}

public init(pubKeyX: String, pubKeyY: String, keyIndex: String, address: String) {
self.pubKeyX = pubKeyX
self.pubKeyY = pubKeyY
self.keyIndex = keyIndex
self.address = address
}

}

public enum KeyLookupError: Error {
case verifierNotSupported
case verifierAndVerifierIdNotAssigned
case configError

static func createErrorFromString(errorString: String) -> Self {
if errorString.contains("Verifier not supported") {
return .verifierNotSupported
} else if errorString.contains("Verifier + VerifierID has not yet been assigned") {
return .verifierAndVerifierIdNotAssigned
} else {
return .configError
}
}
}

extension KeyLookupError: LocalizedError {

public var errorDescription: String? {
switch self {
case .verifierNotSupported:
return "Verifier not supported. Check if you: \n1. Are on the right network (Torus testnet/mainnet) \n2. Have setup a verifier on dashboterard.web3auth.io?"
case .verifierAndVerifierIdNotAssigned:
return "Verifier + VerifierID has not yet been assigned"
case .configError:
return "ConfigurationError"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// File.swift
//
//
// Created by Dhruv Jaiswal on 06/04/23.
//

import Foundation

public struct RetrieveDecryptAndReconstuctResponseModel {
public let iv: String
public let ephemPublicKey: String
public let share: String
public let pubKeyX: String
public let pubKeyY: String

public init(iv: String, ephemPublicKey: String, share: String, pubKeyX: String, pubKeyY: String) {
self.iv = iv
self.ephemPublicKey = ephemPublicKey
self.share = share
self.pubKeyX = pubKeyX
self.pubKeyY = pubKeyY
}
}
18 changes: 18 additions & 0 deletions Sources/TorusUtils/Models/RetrieveSharesResponseModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// File.swift
//
//
// Created by Dhruv Jaiswal on 08/04/23.
//

import Foundation

public struct RetrieveSharesResponseModel {
public let publicAddress: String
public let privateKey: String

public init(publicKey: String, privateKey: String) {
self.publicAddress = publicKey
self.privateKey = privateKey
}
}
Loading

0 comments on commit acfdebc

Please sign in to comment.