Skip to content
This repository has been archived by the owner on Aug 30, 2022. It is now read-only.

Converted code to Swift 5 #228

Merged
merged 3 commits into from
Oct 21, 2019
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
11 changes: 6 additions & 5 deletions EosioSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -440,10 +440,11 @@
TargetAttributes = {
6B80D25E21FA6DDF00716A7B = {
CreatedOnToolsVersion = 10.1;
LastSwiftMigration = 1010;
LastSwiftMigration = 1110;
};
6B80D26721FA6DDF00716A7B = {
CreatedOnToolsVersion = 10.1;
LastSwiftMigration = 1110;
};
};
};
Expand Down Expand Up @@ -789,7 +790,7 @@
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand Down Expand Up @@ -817,7 +818,7 @@
PRODUCT_BUNDLE_IDENTIFIER = one.block.EosioSwift;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
Expand All @@ -838,7 +839,7 @@
);
PRODUCT_BUNDLE_IDENTIFIER = one.block.EosioSwiftTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand All @@ -859,7 +860,7 @@
);
PRODUCT_BUNDLE_IDENTIFIER = one.block.EosioSwiftTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
Expand Down
2 changes: 1 addition & 1 deletion EosioSwift/Crypto/Base58String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public extension Data {
let byteString = [UInt8](string.utf8)

for ch in byteString.reversed() {
if let index = alphabet.index(of: ch) {
if let index = alphabet.firstIndex(of: ch) {
answer = answer + (j * BigUInt(index))
j *= radix
} else {
Expand Down
35 changes: 22 additions & 13 deletions EosioSwift/Crypto/RIPEMD160.swift
Original file line number Diff line number Diff line change
Expand Up @@ -306,16 +306,20 @@ public struct RIPEMD160 {
}

public mutating func update(data: Data) {
data.withUnsafeBytes { (ptr: UnsafePointer<UInt8>) in
var ptr = ptr
data.withUnsafeBytes { ptr in
let typedPtr = ptr.bindMemory(to: UInt8.self)
guard var ptr = typedPtr.baseAddress else { return }
var length = data.count
var X = [UInt32](repeating: 0, count: 16)

// Process remaining bytes from last call:
if buffer.count > 0 && buffer.count + length >= 64 {
let amount = 64 - buffer.count
buffer.append(ptr, count: amount)
buffer.withUnsafeBytes { _ = memcpy(&X, $0, 64) }
buffer.withUnsafeBytes { ptr in
guard let baseAddress = ptr.baseAddress else { return }
_ = memcpy(&X, baseAddress, 64)
}
compress(X)
ptr += amount
length -= amount
Expand All @@ -329,15 +333,18 @@ public struct RIPEMD160 {
}
// Save remaining unprocessed bytes:
buffer = Data(bytes: ptr, count: length)
count += Int64(data.count)
}
count += Int64(data.count)
}

public mutating func finalize() -> Data {
var X = [UInt32](repeating: 0, count: 16)
/* append the bit m_n == 1 */
buffer.append(0x80)
buffer.withUnsafeBytes { _ = memcpy(&X, $0, buffer.count) }
buffer.withUnsafeBytes { ptr in
guard let baseAddress = ptr.baseAddress else { return }
_ = memcpy(&X, baseAddress, buffer.count)
}

if (count & 63) > 55 {
/* length goes to next block */
Expand All @@ -353,12 +360,14 @@ public struct RIPEMD160 {
compress(X)

var data = Data(count: 20)
data.withUnsafeMutableBytes { (ptr: UnsafeMutablePointer<UInt32>) in
ptr[0] = MDbuf.0
ptr[1] = MDbuf.1
ptr[2] = MDbuf.2
ptr[3] = MDbuf.3
ptr[4] = MDbuf.4
data.withUnsafeMutableBytes { ptr in
let typedPtr = ptr.bindMemory(to: UInt32.self)
guard let baseAddress = typedPtr.baseAddress else { return }
baseAddress[0] = MDbuf.0
baseAddress[1] = MDbuf.1
baseAddress[2] = MDbuf.2
baseAddress[3] = MDbuf.3
baseAddress[4] = MDbuf.4
}

buffer = Data()
Expand Down Expand Up @@ -386,8 +395,8 @@ public extension RIPEMD160 {
var key = key
key.count = 64 // Truncate to 64 bytes or fill-up with zeros.

let outerKeyPad = Data(bytes: key.map { $0 ^ 0x5c })
let innerKeyPad = Data(bytes: key.map { $0 ^ 0x36 })
let outerKeyPad = Data(key.map { $0 ^ 0x5c })
let innerKeyPad = Data(key.map { $0 ^ 0x36 })

var innerMd = RIPEMD160()
innerMd.update(data: innerKeyPad)
Expand Down
7 changes: 4 additions & 3 deletions EosioSwift/Extensions/DataExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ public extension Data {
/// Returns the SHA256 hash of the data.
var sha256: Data {
var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
self.withUnsafeBytes {
_ = CC_SHA256($0, CC_LONG(self.count), &hash)
self.withUnsafeBytes { ptr in
guard let baseAddress = ptr.baseAddress else { return }
_ = CC_SHA256(baseAddress, CC_LONG(self.count), &hash)
}
return Data(bytes: hash)
return Data(hash)
}

/// Returns the current Data as a base58 encoded String.
Expand Down
4 changes: 2 additions & 2 deletions EosioSwift/Extensions/EosioKeySignatureExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public extension Data {
let x = uncompressedKey[1...32] // swiftlint:disable:this identifier_name
let yLastByte = uncompressedKey[64]
let flag: UInt8 = 2 + (yLastByte % 2)
let compressedKey = Data(bytes: [flag]) + x
let compressedKey = Data([flag]) + x
return compressedKey
}

Expand Down Expand Up @@ -229,7 +229,7 @@ public extension Data {
}

if key.count == 33 {
guard key.prefix(1) == Data(bytes: [0x80]) else {
guard key.prefix(1) == Data([0x80]) else {
throw EosioError(.signatureProviderError, reason: "33 byte private key: \(key.hex) does not begin with 80")
}
key = key.suffix(key.count-1)
Expand Down
4 changes: 2 additions & 2 deletions EosioSwift/Extensions/StringExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ public extension String {
/// Converts the string to a dictionary if it is convertible to a dictionary (i.e., it is a JSON string.) and returns the dictionary. Otherwise, returns `nil`.
var toJsonDictionary: [String: Any]? {
guard let data = self.data(using: .utf8) else { return nil }
let dict = (try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as? [String: Any]) ?? nil
let dict = (try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments)) as? [String: Any]
return dict
}

/// Converts the string to a array if it is convertible to a array (i.e., it is a JSON string.) and returns the array. Otherwise, returns `nil`.
var toJsonArray: [Any]? {
guard let data = self.data(using: .utf8) else { return nil }
let array = (try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as? [Any]) ?? nil
let array = (try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments)) as? [Any]
return array
}

Expand Down
4 changes: 2 additions & 2 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ target 'EosioSwift' do
target 'EosioSwiftTests' do
inherit! :search_paths
pod 'OHHTTPStubs/Swift'
pod 'BigInt', '~> 3.1', :inhibit_warnings => true
pod 'BigInt', '~> 5.0'
pod 'PromiseKit', '~> 6.8'
end

pod 'BigInt', '~> 3.1', :inhibit_warnings => true
pod 'BigInt', '~> 5.0'
pod 'SwiftLint'
pod 'PromiseKit', '~> 6.8'
end
12 changes: 4 additions & 8 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
PODS:
- BigInt (3.1.0):
- SipHash (~> 1.2)
- BigInt (5.0.0)
- OHHTTPStubs/Core (8.0.0)
- OHHTTPStubs/Default (8.0.0):
- OHHTTPStubs/Core
Expand All @@ -23,11 +22,10 @@ PODS:
- PromiseKit/CorePromise
- PromiseKit/UIKit (6.8.4):
- PromiseKit/CorePromise
- SipHash (1.2.2)
- SwiftLint (0.32.0)

DEPENDENCIES:
- BigInt (~> 3.1)
- BigInt (~> 5.0)
- OHHTTPStubs/Swift
- PromiseKit (~> 6.8)
- SwiftLint
Expand All @@ -37,16 +35,14 @@ SPEC REPOS:
- BigInt
- OHHTTPStubs
- PromiseKit
- SipHash
- SwiftLint

SPEC CHECKSUMS:
BigInt: 76b5dfdfa3e2e478d4ffdf161aeede5502e2742f
BigInt: 74b4d88367b0e819d9f77393549226d36faeb0d8
OHHTTPStubs: 9cbce6364bec557cc3439aa6bb7514670d780881
PromiseKit: 51794a832647e7b819336dc2279039ce9f1cc49b
SipHash: fad90a4683e420c52ef28063063dbbce248ea6d4
SwiftLint: 009a898ef2a1c851f45e1b59349bf6ff2ddc990d

PODFILE CHECKSUM: 1d10bec70f8d2560be35e67e47b034772bba25f7
PODFILE CHECKSUM: c2e188f1ae1612a01b8b39f8a779631954878ead

COCOAPODS: 1.8.3