Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mnemonic data #791

Merged
merged 12 commits into from
Nov 7, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ extension Data: LiteralInitiableFromString {
public static func fromHex(_ hex: String) -> Data? {
let string = hex.lowercased().stripHexPrefix()
let array = [UInt8](hex: string)
if array.count == 0 {
if hex == "0x" || hex == "" {
return Data()
} else {
return nil
}
if (array.count == 0) {
return (hex == "0x" || hex.isEmpty) ? Data() : nil
}
return Data(array)
}
Expand Down
38 changes: 20 additions & 18 deletions Sources/Web3Core/Utility/Data+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

import Foundation

extension Data {
public extension Data {

init<T>(fromArray values: [T]) {
let values = values
let ptrUB = values.withUnsafeBufferPointer { (ptr: UnsafeBufferPointer) in return ptr }
Expand Down Expand Up @@ -33,32 +34,33 @@ extension Data {
return difference == UInt8(0x00)
}

public static func zero(_ data: inout Data) {
static func zero(_ data: inout Data) {
let count = data.count
data.withUnsafeMutableBytes { (body: UnsafeMutableRawBufferPointer) in
body.baseAddress?.assumingMemoryBound(to: UInt8.self).initialize(repeating: 0, count: count)
}
}

public static func randomBytes(length: Int) -> Data? {
for _ in 0...1024 {
pharms-eth marked this conversation as resolved.
Show resolved Hide resolved
var data = Data(repeating: 0, count: length)
let result = data.withUnsafeMutableBytes { (body: UnsafeMutableRawBufferPointer) -> Int32? in
if let bodyAddress = body.baseAddress, body.count > 0 {
let pointer = bodyAddress.assumingMemoryBound(to: UInt8.self)
return SecRandomCopyBytes(kSecRandomDefault, length, pointer)
} else {
return nil
}
}
if let notNilResult = result, notNilResult == errSecSuccess {
return data
}
static func randomBytes(length: Int) -> Data? {
let entropy_bit_size = length//128
JeneaVranceanu marked this conversation as resolved.
Show resolved Hide resolved
JeneaVranceanu marked this conversation as resolved.
Show resolved Hide resolved
//# valid_entropy_bit_sizes = [128, 160, 192, 224, 256], count: [12, 15, 18, 21, 24]
JeneaVranceanu marked this conversation as resolved.
Show resolved Hide resolved
var entropy_bytes = [UInt8](repeating: 0, count: entropy_bit_size)// / 8)
JeneaVranceanu marked this conversation as resolved.
Show resolved Hide resolved

let status = SecRandomCopyBytes(kSecRandomDefault, entropy_bytes.count, &entropy_bytes)

if status != errSecSuccess { // Always test the status.
JeneaVranceanu marked this conversation as resolved.
Show resolved Hide resolved

JeneaVranceanu marked this conversation as resolved.
Show resolved Hide resolved
} else {
entropy_bytes = [UInt8](repeating: 0, count: entropy_bit_size)// / 8)
JeneaVranceanu marked this conversation as resolved.
Show resolved Hide resolved
arc4random_buf(&entropy_bytes, entropy_bytes.count)
JeneaVranceanu marked this conversation as resolved.
Show resolved Hide resolved
}
return nil

let entropyData = entropy_bytes.shuffled()
pharms-eth marked this conversation as resolved.
Show resolved Hide resolved

return Data(entropyData)
}

public func bitsInRange(_ startingBit: Int, _ length: Int) -> UInt64? { // return max of 8 bytes for simplicity, non-public
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]
let padding = Data(repeating: 0, count: 8 - bytes.count)
Expand Down
22 changes: 21 additions & 1 deletion Sources/Web3Core/Utility/String+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ extension String {
guard let matcher = try? NSRegularExpression(pattern: "^(?<prefix>0x)(?<leadingZeroes>0+)(?<end>[0-9a-fA-F]*)$",
options: .dotMatchesLineSeparators)
else {
NSLog("stripLeadingZeroes(): failed to parse regex pattern.")
print("stripLeadingZeroes(): failed to parse regex pattern.")
JeneaVranceanu marked this conversation as resolved.
Show resolved Hide resolved
return self
}
let match = matcher.captureGroups(string: hex, options: .anchored)
Expand Down Expand Up @@ -135,6 +135,26 @@ extension String {
func trim() -> String {
trimmingCharacters(in: .whitespacesAndNewlines)
}

/// Splits a string into groups of `every` n characters, grouping from left-to-right by default. If `backwards` is true, right-to-left.
public func split(every: Int, backwards: Bool = false) -> [String] {
var result = [String]()

for i in stride(from: 0, to: self.count, by: every) {
switch backwards {
case true:
let endIndex = self.index(self.endIndex, offsetBy: -i)
let startIndex = self.index(endIndex, offsetBy: -every, limitedBy: self.startIndex) ?? self.startIndex
result.insert(String(self[startIndex..<endIndex]), at: 0)
case false:
let startIndex = self.index(self.startIndex, offsetBy: i)
let endIndex = self.index(startIndex, offsetBy: every, limitedBy: self.endIndex) ?? self.endIndex
result.append(String(self[startIndex..<endIndex]))
}
}

return result
}
pharms-eth marked this conversation as resolved.
Show resolved Hide resolved
}

extension Character {
Expand Down