Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 56 additions & 67 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 8 additions & 12 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.3
// swift-tools-version:5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand All @@ -11,29 +11,25 @@ let package = Package(
targets: ["TorusUtils"])
],
dependencies: [
.package(name: "curvelib.swift", url: "https://github.com/tkey/curvelib.swift", from: "0.1.2"),
.package(name:"FetchNodeDetails", url: "https://github.com/torusresearch/fetch-node-details-swift", from: "5.1.0"),
.package(name:"CryptoSwift", url: "https://github.com/krzyzanowskim/CryptoSwift",from: "1.5.1"),
.package(name:"jwt-kit", url: "https://github.com/vapor/jwt-kit", from: "4.0.0"),
.package(url: "https://github.com/tkey/curvelib.swift", from: "1.0.0"),
.package(url: "https://github.com/torusresearch/fetch-node-details-swift", from: "5.2.0"),
.package(url: "https://github.com/vapor/jwt-kit", from: "4.0.0"),
.package(
name:"AnyCodable",
url: "https://github.com/Flight-School/AnyCodable",
from: "0.6.0"
),
],
targets: [
.target(
name: "TorusUtils",
dependencies: ["FetchNodeDetails", "CryptoSwift", "AnyCodable",
dependencies: ["AnyCodable",
.product(name: "FetchNodeDetails", package: "fetch-node-details-swift"),
.product(name: "curveSecp256k1", package: "curvelib.swift"),
.product(name: "encryption_aes_cbc_sha512", package: "curvelib.swift"),
]
),
]),
.testTarget(
name: "TorusUtilsTests",
dependencies: ["TorusUtils", .product(name: "JWTKit", package: "jwt-kit")]
)
],
swiftLanguageVersions: [.v5]
]
)

3 changes: 2 additions & 1 deletion Sources/TorusUtils/AbstractTorusUtils.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import BigInt
//import BigInt
import CommonSources
import BigInt
import FetchNodeDetails
import Foundation

Expand Down
82 changes: 82 additions & 0 deletions Sources/TorusUtils/Extensions/Array+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//
// CryptoSwift
//
// Copyright (C) 2014-2022 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
// This software is provided 'as-is', without any express or implied warranty.
//
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
//
// - The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required.
// - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
// - This notice may not be removed or altered from any source or binary distribution.
//

extension Array {
@inlinable
init(reserveCapacity: Int) {
self = Array<Element>()
self.reserveCapacity(reserveCapacity)
}

@inlinable
var slice: ArraySlice<Element> {
self[self.startIndex ..< self.endIndex]
}

@inlinable
subscript (safe index: Index) -> Element? {
return indices.contains(index) ? self[index] : nil
}
}

extension Array where Element == UInt8 {
public init(hex: String) {
self.init(reserveCapacity: hex.unicodeScalars.lazy.underestimatedCount)
var buffer: UInt8?
var skip = hex.hasPrefix("0x") ? 2 : 0
for char in hex.unicodeScalars.lazy {
guard skip == 0 else {
skip -= 1
continue
}
guard char.value >= 48 && char.value <= 102 else {
removeAll()
return
}
let v: UInt8
let c: UInt8 = UInt8(char.value)
switch c {
case let c where c <= 57:
v = c - 48
case let c where c >= 65 && c <= 70:
v = c - 55
case let c where c >= 97:
v = c - 87
default:
removeAll()
return
}
if let b = buffer {
append(b << 4 | v)
buffer = nil
} else {
buffer = v
}
}
if let b = buffer {
append(b)
}
}

public func toHexString() -> String {
`lazy`.reduce(into: "") {
var s = String($1, radix: 16)
if s.count == 1 {
s = "0" + s
}
$0 += s
}
}
}
12 changes: 12 additions & 0 deletions Sources/TorusUtils/Extensions/Data+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,16 @@ public extension Data {
func addLeading0sForLength64() -> Data {
Data(hex: hexString.addLeading0sForLength64())
}

init(hex: String) {
self.init(Array<UInt8>(hex: hex))
}

var bytes: Array<UInt8> {
Array(self)
}

func toHexString() -> String {
self.bytes.toHexString()
}
}
6 changes: 4 additions & 2 deletions Sources/TorusUtils/Extensions/String+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ extension String {
func toChecksumAddress() -> String {
let lowerCaseAddress = stripHexPrefix().lowercased()
let arr = Array(lowerCaseAddress)
let hash = Array(lowerCaseAddress.sha3(.keccak256))
let hash = keccak256Data(lowerCaseAddress.data(using: .utf8) ?? Data() ).toHexString()

var result = String()
for i in 0 ... lowerCaseAddress.count - 1 {
if let val = Int(String(hash[i]), radix: 16), val >= 8 {
let iIndex = hash.index(hash.startIndex, offsetBy: i)
if let val = hash[iIndex].hexDigitValue , val >= 8 {
result.append(arr[i].uppercased())
} else {
result.append(arr[i])
Expand Down
Loading