Skip to content

Commit

Permalink
Merge pull request #86 from torusresearch/fix/encryption
Browse files Browse the repository at this point in the history
fix: Remove CryptoSwift
  • Loading branch information
metalurgical authored Apr 17, 2024
2 parents 9854272 + 017cb4d commit cd26539
Show file tree
Hide file tree
Showing 16 changed files with 239 additions and 188 deletions.
123 changes: 56 additions & 67 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,70 +1,59 @@
{
"object": {
"pins": [
{
"package": "AnyCodable",
"repositoryURL": "https://github.com/Flight-School/AnyCodable",
"state": {
"branch": null,
"revision": "862808b2070cd908cb04f9aafe7de83d35f81b05",
"version": "0.6.7"
}
},
{
"package": "BigInt",
"repositoryURL": "https://github.com/attaswift/BigInt",
"state": {
"branch": null,
"revision": "0ed110f7555c34ff468e72e1686e59721f2b0da6",
"version": "5.3.0"
}
},
{
"package": "CryptoSwift",
"repositoryURL": "https://github.com/krzyzanowskim/CryptoSwift",
"state": {
"branch": null,
"revision": "7892a123f7e8d0fe62f9f03728b17bbd4f94df5c",
"version": "1.8.1"
}
},
{
"package": "curvelib.swift",
"repositoryURL": "https://github.com/tkey/curvelib.swift",
"state": {
"branch": null,
"revision": "7dad3bf1793de263f83406c08c18c9316abf082f",
"version": "0.1.2"
}
},
{
"package": "FetchNodeDetails",
"repositoryURL": "https://github.com/torusresearch/fetch-node-details-swift.git",
"state": {
"branch": null,
"revision": "d591af500f32ce3c88d04af9bb74d746585acfea",
"version": "5.1.0"
}
},
{
"package": "jwt-kit",
"repositoryURL": "https://github.com/vapor/jwt-kit",
"state": {
"branch": null,
"revision": "e05513b5aec24f88012b6e3034115b6bc915356a",
"version": "4.13.2"
}
},
{
"package": "swift-crypto",
"repositoryURL": "https://github.com/apple/swift-crypto.git",
"state": {
"branch": null,
"revision": "f0525da24dc3c6cbb2b6b338b65042bc91cbc4bb",
"version": "3.3.0"
}
"pins" : [
{
"identity" : "anycodable",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Flight-School/AnyCodable",
"state" : {
"revision" : "862808b2070cd908cb04f9aafe7de83d35f81b05",
"version" : "0.6.7"
}
]
},
"version": 1
},
{
"identity" : "bigint",
"kind" : "remoteSourceControl",
"location" : "https://github.com/attaswift/BigInt",
"state" : {
"revision" : "0ed110f7555c34ff468e72e1686e59721f2b0da6",
"version" : "5.3.0"
}
},
{
"identity" : "curvelib.swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/tkey/curvelib.swift",
"state" : {
"revision" : "2df9f638fd121b445ce3b410cd79ac4d0cfa94ed",
"version" : "1.0.0"
}
},
{
"identity" : "fetch-node-details-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/torusresearch/fetch-node-details-swift",
"state" : {
"revision" : "bf2f0759da5c5c80765773b08c2756045edf608f",
"version" : "5.2.0"
}
},
{
"identity" : "jwt-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/jwt-kit",
"state" : {
"revision" : "c2595b9ad7f512d7f334830b4df1fed6e917946a",
"version" : "4.13.4"
}
},
{
"identity" : "swift-crypto",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-crypto.git",
"state" : {
"revision" : "f0525da24dc3c6cbb2b6b338b65042bc91cbc4bb",
"version" : "3.3.0"
}
}
],
"version" : 2
}
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 <[email protected]>
// 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

0 comments on commit cd26539

Please sign in to comment.