Skip to content

Commit

Permalink
Move Core features to Core module.
Browse files Browse the repository at this point in the history
It's building (but not testing yet)
  • Loading branch information
yaroslavyaroslav committed Jul 11, 2022
1 parent 4e48da8 commit 749829f
Show file tree
Hide file tree
Showing 151 changed files with 870 additions and 650 deletions.
7 changes: 5 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@ let package = Package(
products: [
.library(name: "web3swift", targets: ["web3swift"])
],

dependencies: [
.package(url: "https://github.com/attaswift/BigInt.git", from: "5.3.0"),
.package(url: "https://github.com/daltoniam/Starscream.git", from: "4.0.4"),
.package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", from: "1.5.1")
],
targets: [
.target(name: "secp256k1"),
.target(
name: "Core",
dependencies: ["BigInt", "secp256k1", "CryptoSwift"]
),
.target(
name: "web3swift",
dependencies: ["BigInt", "secp256k1", "Starscream", "CryptoSwift"],
dependencies: ["Core", "BigInt", "secp256k1", "Starscream", "CryptoSwift"],
exclude: excludeFiles,
resources: [
.copy("./Browser/browser.js"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,3 @@ extension Array where Element: Comparable {
return sortedArray
}
}

extension Array where Element: BinaryInteger {
// TODO: Make me generic
/// Calculates mean value of a dataset
/// - Returns: Mean value of a dataset, nil if dataset is empty
func mean() -> BigUInt? {
guard !self.isEmpty else { return nil }
return BigUInt(self.reduce(0, +)) / BigUInt(self.count)
}


/// Calculates percentile of dataset on which get called.
/// - Parameter value: Percentile value.
/// - Returns: Item from dataset that is belongs to given percentile, nil if dataset is empty.
func percentile(of value: Double) -> Element? {
guard !self.isEmpty else { return nil }

let normalizedValue = value / 100 * Double(self.count)
let index = Int(ceil(normalizedValue))

let sorted_data = self.sorted()
guard index < self.count else { return sorted_data[sorted_data.count - 1] }
return sorted_data[index]
}
}
File renamed without changes.
16 changes: 16 additions & 0 deletions Sources/Core/Convenience/BigUInt+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// RIPEMD160_SO.swift
// web3swift
//
// Created by Alexander Vlasov on 10.01.2018.
//

import Foundation
import struct BigInt.BigUInt

extension BigUInt {
init?(_ naturalUnits: String, _ ethereumUnits: Utilities.Units) {
guard let value = Utilities.parseToBigUInt(naturalUnits, units: ethereumUnits) else {return nil}
self = value
}
}
33 changes: 33 additions & 0 deletions Sources/Core/Convenience/BlockNumber.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// File.swift
//
//
// Created by Yaroslav Yashin on 11.07.2022.
//

import Foundation
import BigInt

public enum BlockNumber {
case pending
/// Latest block of a chain
case latest
/// Earliest block of a chain
case earliest
/// Exact block number
case exact(BigUInt)

/// Block number as a hex string
public var stringValue: String {
switch self {
case .pending:
return "pending"
case .latest:
return "latest"
case .earliest:
return "earliest"
case .exact(let number):
return String(number, radix: 16).addHexPrefix()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,6 @@ public extension Data {
return nil
}

static func fromHex(_ hex: String) -> Data? {
let string = hex.lowercased().stripHexPrefix()
let array = Array<UInt8>(hex: string)
if (array.count == 0) {
if (hex == "0x" || hex == "") {
return Data()
} else {
return nil
}
}
return Data(array)
}

func bitsInRange(_ startingBit: Int, _ length: Int) -> UInt64? { // return max of 8 bytes for simplicity, non-public
if startingBit + length / 8 > self.count, length > 64, startingBit > 0, length >= 1 {return nil}
let bytes = self[(startingBit/8) ..< (startingBit+length+7)/8]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation
import BigInt

extension Data {
public extension Data {
func setLengthLeft(_ toBytes: UInt64, isNegative: Bool = false) -> Data? {
let existingLength = UInt64(self.count)
if (existingLength == toBytes) {
Expand Down Expand Up @@ -43,7 +43,7 @@ extension Data {
}
}

extension BigInt {
public extension BigInt {
func toTwosComplement() -> Data {
if (self.sign == BigInt.Sign.plus) {
return self.magnitude.serialize()
Expand All @@ -56,7 +56,7 @@ extension BigInt {
}
}

extension BigUInt {
public extension BigUInt {
func abiEncode(bits: UInt64) -> Data? {
let data = self.serialize()
let paddedLength = UInt64(ceil((Double(bits)/8.0)))
Expand All @@ -65,7 +65,7 @@ extension BigUInt {
}
}

extension BigInt {
public extension BigInt {
func abiEncode(bits: UInt64) -> Data? {
let isNegative = self < (BigInt(0))
let data = self.toTwosComplement()
Expand All @@ -75,7 +75,7 @@ extension BigInt {
}
}

extension BigInt {
public extension BigInt {
static func fromTwosComplement(data: Data) -> BigInt {
let isPositive = ((data[0] & 128) >> 7) == 0
if (isPositive) {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import Foundation

extension String {
public extension String {
var fullRange: Range<Index> {
return startIndex..<endIndex
}
Expand Down
Loading

0 comments on commit 749829f

Please sign in to comment.