diff --git a/Sources/TorusUtils/Extensions/HelperExtension.swift b/Sources/TorusUtils/Extensions/Sequence+Extension.swift similarity index 100% rename from Sources/TorusUtils/Extensions/HelperExtension.swift rename to Sources/TorusUtils/Extensions/Sequence+Extension.swift diff --git a/Sources/TorusUtils/Extensions/String+Extension.swift b/Sources/TorusUtils/Extensions/String+Extension.swift index 86d5de05..d2e5b68f 100755 --- a/Sources/TorusUtils/Extensions/String+Extension.swift +++ b/Sources/TorusUtils/Extensions/String+Extension.swift @@ -1,58 +1,17 @@ -// web3swift -// -// Created by Alex Vlasov. -// Copyright © 2018 Alex Vlasov. All rights reserved. -// - import Foundation extension String { - var fullRange: Range { - return startIndex ..< endIndex - } - - var fullNSRange: NSRange { - return NSRange(fullRange, in: self) - } - - func index(of char: Character) -> Index? { - guard let range = range(of: String(char)) else { - return nil - } - return range.lowerBound - } - - subscript(bounds: CountableClosedRange) -> String { - let start = index(startIndex, offsetBy: bounds.lowerBound) - let end = index(startIndex, offsetBy: bounds.upperBound) - return String(self[start ... end]) - } - - subscript(bounds: CountableRange) -> String { - let start = index(startIndex, offsetBy: bounds.lowerBound) - let end = index(startIndex, offsetBy: bounds.upperBound) - return String(self[start ..< end]) - } - - subscript(bounds: CountablePartialRangeFrom) -> String { - let start = index(startIndex, offsetBy: bounds.lowerBound) - let end = endIndex - return String(self[start ..< end]) - } - - func leftPadding(toLength: Int, withPad character: Character) -> String { - let stringLength = count - if stringLength < toLength { - return String(repeatElement(character, count: toLength - stringLength)) + self - } else { - return String(suffix(toLength)) - } - } - func hasHexPrefix() -> Bool { return hasPrefix("0x") } + func addHexPrefix() -> String { + if !hasPrefix("0x") { + return "0x" + self + } + return self + } + func stripHexPrefix() -> String { if hasPrefix("0x") { let indexStart = index(startIndex, offsetBy: 2) @@ -61,45 +20,36 @@ extension String { return self } - func addHexPrefix() -> String { - if !hasPrefix("0x") { - return "0x" + self + func has04Prefix() -> Bool { + return hasPrefix("04") + } + + func add04Prefix() -> String { + if !hasPrefix("04") { + return "04" + self } return self } - - func matchingStrings(regex: String) -> [[String]] { - guard let regex = try? NSRegularExpression(pattern: regex, options: []) else { return [] } - let nsString = self as NSString - let results = regex.matches(in: self, options: [], range: NSRange(location: 0, length: nsString.length)) - return results.map { result in - (0 ..< result.numberOfRanges).map { result.range(at: $0).location != NSNotFound - ? nsString.substring(with: result.range(at: $0)) - : "" - } + + func strip04Prefix() -> String { + if hasPrefix("04") { + let indexStart = index(startIndex, offsetBy: 2) + return String(self[indexStart...]) } + return self } - func range(from nsRange: NSRange) -> Range? { - guard - let from16 = utf16.index(utf16.startIndex, offsetBy: nsRange.location, limitedBy: utf16.endIndex), - let to16 = utf16.index(utf16.startIndex, offsetBy: nsRange.location + nsRange.length, limitedBy: utf16.endIndex), - let from = from16.samePosition(in: self), - let to = to16.samePosition(in: self) - else { return nil } - return from ..< to - } - - var asciiValue: Int { - let s = unicodeScalars - return Int(s[s.startIndex].value) + func addLeading0sForLength64() -> String { + if count < 64 { + let toAdd = String(repeating: "0", count: 64 - count) + return toAdd + self + } else { + return self + } } -} -extension Character { - var asciiValue: Int { - let s = String(self).unicodeScalars - return Int(s[s.startIndex].value) + func customBytes() -> Array { + data(using: String.Encoding.utf8, allowLossyConversion: true)?.bytes ?? Array(utf8) } } @@ -119,3 +69,14 @@ extension String { return result } } + +extension StringProtocol { + var hexa: [UInt8] { + var startIndex = self.startIndex + return (0 ..< count / 2).compactMap { _ in + let endIndex = index(after: startIndex) + defer { startIndex = index(after: endIndex) } + return UInt8(self[startIndex ... endIndex], radix: 16) + } + } +} diff --git a/Sources/TorusUtils/Extensions/StringExtension.swift b/Sources/TorusUtils/Extensions/StringExtension.swift deleted file mode 100644 index 6e91fc10..00000000 --- a/Sources/TorusUtils/Extensions/StringExtension.swift +++ /dev/null @@ -1,42 +0,0 @@ -import Foundation -extension String { - func strip04Prefix() -> String { - if hasPrefix("04") { - let indexStart = index(startIndex, offsetBy: 2) - return String(self[indexStart...]) - } - return self - } - - func strip0xPrefix() -> String { - if hasPrefix("0x") { - let indexStart = index(startIndex, offsetBy: 2) - return String(self[indexStart...]) - } - return self - } - - func addLeading0sForLength64() -> String { - if count < 64 { - let toAdd = String(repeating: "0", count: 64 - count) - return toAdd + self - } else { - return self - } - } - - func customBytes() -> Array { - data(using: String.Encoding.utf8, allowLossyConversion: true)?.bytes ?? Array(utf8) - } -} - -extension StringProtocol { - var hexa: [UInt8] { - var startIndex = self.startIndex - return (0 ..< count / 2).compactMap { _ in - let endIndex = index(after: startIndex) - defer { startIndex = index(after: endIndex) } - return UInt8(self[startIndex ... endIndex], radix: 16) - } - } -} diff --git a/Sources/TorusUtils/Extensions/TorusUtils+extension.swift b/Sources/TorusUtils/Extensions/TorusUtils+extension.swift index 4aa9ccc7..699198cc 100644 --- a/Sources/TorusUtils/Extensions/TorusUtils+extension.swift +++ b/Sources/TorusUtils/Extensions/TorusUtils+extension.swift @@ -190,7 +190,7 @@ extension TorusUtils { private func reconstructKey(decryptedShares: [Int: String], thresholdPublicKey: KeyAssignment.PublicKey) throws -> String? { // run lagrange interpolation on all subsets, faster in the optimistic scenario than berlekamp-welch due to early exit - let allCombis = combinations(elements: Array(0.. String let publicKeyHex = publicKeyX.addLeading0sForLength64() + publicKeyY.addLeading0sForLength64() let publicKeyData = Data(hex: publicKeyHex) let ethAddrData = publicKeyData.sha3(.keccak256).suffix(20) - let ethAddrlower = "0x" + ethAddrData.toHexString() + let ethAddrlower = ethAddrData.toHexString().addHexPrefix() return ethAddrlower.toChecksumAddress() } diff --git a/Sources/TorusUtils/Helpers/LangrangeInterpolatePoly.swift b/Sources/TorusUtils/Helpers/LangrangeInterpolatePoly.swift index f622e67a..f1b8dc2c 100644 --- a/Sources/TorusUtils/Helpers/LangrangeInterpolatePoly.swift +++ b/Sources/TorusUtils/Helpers/LangrangeInterpolatePoly.swift @@ -162,7 +162,7 @@ func generateRandomPolynomial(degree: Int, secret: BigInt? = nil, deterministicS var points = [String: Point]() for share in deterministicShares { - points[String(share.shareIndex, radix: 16).leftPadding(toLength: 64, withPad: "0")] = + points[String(share.shareIndex, radix: 16).addLeading0sForLength64()] = Point(x: share.shareIndex, y: share.share) } @@ -172,7 +172,7 @@ func generateRandomPolynomial(degree: Int, secret: BigInt? = nil, deterministicS while points[shareIndex.description.padding(toLength: 64, withPad: "0", startingAt: 0)] != nil { shareIndex = try generatePrivateExcludingIndexes(shareIndexes: [BigInt(0)]) } - points[String(shareIndex, radix: 16).leftPadding(toLength: 64, withPad: "0")] = Point(x: shareIndex, y: BigInt(try secp256k1.KeyAgreement.PrivateKey().rawRepresentation)) + points[String(shareIndex, radix: 16).addLeading0sForLength64()] = Point(x: shareIndex, y: BigInt(try secp256k1.KeyAgreement.PrivateKey().rawRepresentation)) } points["0"] = Point(x: BigInt(0), y: actualS!) diff --git a/Sources/TorusUtils/Point.swift b/Sources/TorusUtils/Point.swift index 2774eb8a..f4414ce3 100644 --- a/Sources/TorusUtils/Point.swift +++ b/Sources/TorusUtils/Point.swift @@ -33,7 +33,7 @@ public class Point: Decodable { func encode(enc: String) throws -> Data { switch enc { case "arr": - let prefix = Data(hex: "04") + let prefix = Data(hex: String().add04Prefix()) let xData = Data(hex: x.description) let yData = Data(hex: y.description) return prefix + xData + yData diff --git a/Sources/TorusUtils/TorusUtils.swift b/Sources/TorusUtils/TorusUtils.swift index a43a8d26..4c3ea98e 100644 --- a/Sources/TorusUtils/TorusUtils.swift +++ b/Sources/TorusUtils/TorusUtils.swift @@ -108,17 +108,17 @@ open class TorusUtils: AbstractTorusUtils { var pubNonce: PubNonce? if extendedVerifierId != nil { - modifiedPubKey = "04" + X.addLeading0sForLength64() + Y.addLeading0sForLength64() + modifiedPubKey = (X.addLeading0sForLength64() + Y.addLeading0sForLength64()).add04Prefix() oAuthPubKeyString = modifiedPubKey } else if isLegacyNetwork() { return try await formatLegacyPublicData(finalKeyResult: result.keyResult, enableOneKey: enableOneKey, isNewKey: result.keyResult.isNewKey) } else { - modifiedPubKey = "04" + X.addLeading0sForLength64() + Y.addLeading0sForLength64() + modifiedPubKey = (X.addLeading0sForLength64() + Y.addLeading0sForLength64()).add04Prefix() oAuthPubKeyString = modifiedPubKey let pubNonceX = (nonceResult?.pubNonce?.x ?? "0") let pubNonceY = (nonceResult?.pubNonce?.y ?? "0") - let noncePub = "04" + pubNonceX.addLeading0sForLength64() + pubNonceY.addLeading0sForLength64() + let noncePub = (pubNonceX.addLeading0sForLength64() + pubNonceY.addLeading0sForLength64()).add04Prefix() modifiedPubKey = try combinePublicKeys(keys: [modifiedPubKey, noncePub], compressed: false) pubNonce = nonceResult?.pubNonce } @@ -259,8 +259,8 @@ open class TorusUtils: AbstractTorusUtils { let nonceType = nonceResult.typeOfUser ?? "v1" typeOfUser = UserType(rawValue: nonceType) ?? UserType.v1 if typeOfUser == .v2 { - finalPubKey = "04" + oAuthKeyX.addLeading0sForLength64() + oAuthKeyY.addLeading0sForLength64() - let newkey = "04" + (nonceResult.pubNonce?.x.addLeading0sForLength64())! + (nonceResult.pubNonce?.y.addLeading0sForLength64())! + finalPubKey = (oAuthKeyX.addLeading0sForLength64() + oAuthKeyY.addLeading0sForLength64()).add04Prefix() + let newkey = ((nonceResult.pubNonce?.x.addLeading0sForLength64())! + (nonceResult.pubNonce?.y.addLeading0sForLength64())!).add04Prefix() finalPubKey = try combinePublicKeys(keys: [finalPubKey, newkey], compressed: false) pubKeyNonceResult = .init(x: nonceResult.pubNonce!.x, y: nonceResult.pubNonce!.y) } else { diff --git a/Tests/TorusUtilsTests/Helpers/EtherAddress.swift b/Tests/TorusUtilsTests/Helpers/EtherAddress.swift index f744d6c5..552a395d 100644 --- a/Tests/TorusUtilsTests/Helpers/EtherAddress.swift +++ b/Tests/TorusUtilsTests/Helpers/EtherAddress.swift @@ -4,9 +4,9 @@ import XCTest class EtherTest: XCTestCase { func testPublicToEtherAddress() { - let fullAddress = String("04238569d5e12caf57d34fb5b2a0679c7775b5f61fd18cd69db9cc600a651749c3ec13a9367380b7a024a67f5e663f3afd40175c3223da63f6024b05d0bd9f292e".dropFirst(2)) - let X: String = fullAddress[0 ..< 64] - let Y: String = fullAddress[64 ..< fullAddress.count] + let fullAddress = String("04238569d5e12caf57d34fb5b2a0679c7775b5f61fd18cd69db9cc600a651749c3ec13a9367380b7a024a67f5e663f3afd40175c3223da63f6024b05d0bd9f292e") + let X: String = String(fullAddress.suffix(128).prefix(64)) + let Y: String = String(fullAddress.suffix(64)) let etherAddress = generateAddressFromPubKey(publicKeyX: X, publicKeyY: Y) let finalAddress = "0x048975d4997D7578A3419851639c10318db430b6" XCTAssertEqual(etherAddress, finalAddress)