From 8b196186d9aeb7d17e8ac6cc22e33eab6a4d9ac9 Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Tue, 25 Jun 2024 15:01:02 +0800 Subject: [PATCH 1/6] blake2 --- Blockchain/Package.resolved | 11 ++++++++++- Node/Package.resolved | 11 ++++++++++- Utils/Package.resolved | 11 ++++++++++- Utils/Package.swift | 2 ++ Utils/Sources/Utils/Crypto.swift | 19 +++++++++++++++++++ Utils/Sources/Utils/Helpers.swift | 20 ++++++++++++++++++++ Utils/Tests/UtilsTests/Blake2Tests.swift | 17 +++++++++++++++++ 7 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 Utils/Sources/Utils/Crypto.swift create mode 100644 Utils/Sources/Utils/Helpers.swift create mode 100644 Utils/Tests/UtilsTests/Blake2Tests.swift diff --git a/Blockchain/Package.resolved b/Blockchain/Package.resolved index 0c724b53..5b4b1296 100644 --- a/Blockchain/Package.resolved +++ b/Blockchain/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "70e836bce6a34055534368d9795a06e92002ca028f0fcd154b1d037502f7060c", + "originHash" : "0ac92ee4aeacbb08a4c124d75df94d0eef8e04bbc2ecc74287b02650ec69779a", "pins" : [ { "identity" : "async-channels", @@ -10,6 +10,15 @@ "revision" : "679ee7d35e493e181be844dadbe78636cefaace4" } }, + { + "identity" : "blake2.swift", + "kind" : "remoteSourceControl", + "location" : "https://github.com/tesseract-one/Blake2.swift.git", + "state" : { + "revision" : "29c55c8fe42d6661e5a32cc5bbbad1fff64fd01e", + "version" : "0.2.0" + } + }, { "identity" : "scalecodec.swift", "kind" : "remoteSourceControl", diff --git a/Node/Package.resolved b/Node/Package.resolved index 3062c167..d28aa86a 100644 --- a/Node/Package.resolved +++ b/Node/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "82a6d0499b5b0467ed0f8512e428444b3e8626df0667aaa6217ac2055f9ae08c", + "originHash" : "9410affb9296c71e9328e458fd6424e870da866b122c00a7925ca2247508a8e9", "pins" : [ { "identity" : "async-channels", @@ -10,6 +10,15 @@ "revision" : "679ee7d35e493e181be844dadbe78636cefaace4" } }, + { + "identity" : "blake2.swift", + "kind" : "remoteSourceControl", + "location" : "https://github.com/tesseract-one/Blake2.swift.git", + "state" : { + "revision" : "29c55c8fe42d6661e5a32cc5bbbad1fff64fd01e", + "version" : "0.2.0" + } + }, { "identity" : "scalecodec.swift", "kind" : "remoteSourceControl", diff --git a/Utils/Package.resolved b/Utils/Package.resolved index 52ec90a0..f52244b6 100644 --- a/Utils/Package.resolved +++ b/Utils/Package.resolved @@ -1,6 +1,15 @@ { - "originHash" : "7e6e33157836e05375f90c70184331ac5884a4dc78788ca4ae9e201bcdf8dfed", + "originHash" : "7f9d01d4c8c5ccb64f9b9f466f7f8e61b0fd8cd435aa5c4e587e5a6e459f5d77", "pins" : [ + { + "identity" : "blake2.swift", + "kind" : "remoteSourceControl", + "location" : "https://github.com/tesseract-one/Blake2.swift.git", + "state" : { + "revision" : "29c55c8fe42d6661e5a32cc5bbbad1fff64fd01e", + "version" : "0.2.0" + } + }, { "identity" : "scalecodec.swift", "kind" : "remoteSourceControl", diff --git a/Utils/Package.swift b/Utils/Package.swift index 8daa1c93..153b4396 100644 --- a/Utils/Package.swift +++ b/Utils/Package.swift @@ -17,6 +17,7 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/tesseract-one/ScaleCodec.swift.git", from: "0.3.0"), + .package(url: "https://github.com/tesseract-one/Blake2.swift.git", from: "0.2.0"), ], targets: [ // Targets are the basic building blocks of a package, defining a module or a test suite. @@ -25,6 +26,7 @@ let package = Package( name: "Utils", dependencies: [ .product(name: "ScaleCodec", package: "ScaleCodec.swift"), + .product(name: "Blake2", package: "Blake2.swift"), ] ), .testTarget( diff --git a/Utils/Sources/Utils/Crypto.swift b/Utils/Sources/Utils/Crypto.swift new file mode 100644 index 00000000..6512c183 --- /dev/null +++ b/Utils/Sources/Utils/Crypto.swift @@ -0,0 +1,19 @@ +import Blake2 +import Foundation + +public enum HashConversionError: Error { + case invalidSize +} + +/// Computes a Blake2b 256-bit hash of the input data and returns the result as a `Data32`. +/// +/// - Parameter: The input data to hash. +/// - Throws: An error if the hashing process fails. +/// - Returns: A `Data32` containing the hash result. +public func blake2b256(_ data: Data) throws -> Data32 { + let hash = try Blake2b.hash(size: 32, data: data) + guard let data32 = Data32(hash) else { + throw HashConversionError.invalidSize + } + return data32 +} diff --git a/Utils/Sources/Utils/Helpers.swift b/Utils/Sources/Utils/Helpers.swift new file mode 100644 index 00000000..b18ba3c4 --- /dev/null +++ b/Utils/Sources/Utils/Helpers.swift @@ -0,0 +1,20 @@ +import Foundation + +public func hexStringToData(_ hexString: String) -> Data? { + var data = Data() + var index = hexString.startIndex + + while index < hexString.endIndex { + // Find the next 2 characters (1 byte) + guard let nextIndex = hexString.index(index, offsetBy: 2, limitedBy: hexString.endIndex), + let byte = UInt8(hexString[index ..< nextIndex], radix: 16) + else { + return nil + } + + data.append(byte) + index = nextIndex + } + + return data +} diff --git a/Utils/Tests/UtilsTests/Blake2Tests.swift b/Utils/Tests/UtilsTests/Blake2Tests.swift new file mode 100644 index 00000000..9e1b3ec8 --- /dev/null +++ b/Utils/Tests/UtilsTests/Blake2Tests.swift @@ -0,0 +1,17 @@ +import Blake2 +import XCTest + +@testable import Utils + +final class Blake2Tests: XCTestCase { + func testBlake2b256Test() { + let testData = Data("test".utf8) + let expected = hexStringToData("928b20366943e2afd11ebc0eae2e53a93bf177a4fcf35bcc64d503704e65e202") + do { + let actual = try blake2b256(testData) + XCTAssertEqual(expected, actual?.data) + } catch { + XCTFail("Hashing failed") + } + } +} From 7d77dd9678bee72119c9827d73f1f77426e38aa4 Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Tue, 25 Jun 2024 15:08:01 +0800 Subject: [PATCH 2/6] fix --- Boka/Package.resolved | 11 ++++++++++- Utils/Tests/UtilsTests/Blake2Tests.swift | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Boka/Package.resolved b/Boka/Package.resolved index e5aca38c..c0f8b755 100644 --- a/Boka/Package.resolved +++ b/Boka/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "bd44440d7f85d30386716bbd2cc933f21b5ebbc0e75fbfc91dc6a138b6c8ce43", + "originHash" : "a4042fe50ef710cddb88ce0dedba5a852492f62faeacb51990391e81ddcf7e06", "pins" : [ { "identity" : "async-channels", @@ -10,6 +10,15 @@ "revision" : "679ee7d35e493e181be844dadbe78636cefaace4" } }, + { + "identity" : "blake2.swift", + "kind" : "remoteSourceControl", + "location" : "https://github.com/tesseract-one/Blake2.swift.git", + "state" : { + "revision" : "29c55c8fe42d6661e5a32cc5bbbad1fff64fd01e", + "version" : "0.2.0" + } + }, { "identity" : "scalecodec.swift", "kind" : "remoteSourceControl", diff --git a/Utils/Tests/UtilsTests/Blake2Tests.swift b/Utils/Tests/UtilsTests/Blake2Tests.swift index 9e1b3ec8..9f237c99 100644 --- a/Utils/Tests/UtilsTests/Blake2Tests.swift +++ b/Utils/Tests/UtilsTests/Blake2Tests.swift @@ -9,7 +9,7 @@ final class Blake2Tests: XCTestCase { let expected = hexStringToData("928b20366943e2afd11ebc0eae2e53a93bf177a4fcf35bcc64d503704e65e202") do { let actual = try blake2b256(testData) - XCTAssertEqual(expected, actual?.data) + XCTAssertEqual(expected, actual.data) } catch { XCTFail("Hashing failed") } From f6c5c1e7faa9a21b8b8d2f42cfb5e1bceeb5f3ae Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Tue, 25 Jun 2024 16:35:11 +0800 Subject: [PATCH 3/6] fix name --- Utils/Sources/Utils/{Crypto.swift => hashing.swift} | 0 Utils/Tests/UtilsTests/Blake2Tests.swift | 1 - 2 files changed, 1 deletion(-) rename Utils/Sources/Utils/{Crypto.swift => hashing.swift} (100%) diff --git a/Utils/Sources/Utils/Crypto.swift b/Utils/Sources/Utils/hashing.swift similarity index 100% rename from Utils/Sources/Utils/Crypto.swift rename to Utils/Sources/Utils/hashing.swift diff --git a/Utils/Tests/UtilsTests/Blake2Tests.swift b/Utils/Tests/UtilsTests/Blake2Tests.swift index 9f237c99..59ff6599 100644 --- a/Utils/Tests/UtilsTests/Blake2Tests.swift +++ b/Utils/Tests/UtilsTests/Blake2Tests.swift @@ -1,4 +1,3 @@ -import Blake2 import XCTest @testable import Utils From a8ddae87c26d9185c10f04d50979a86483e720a5 Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Wed, 26 Jun 2024 14:17:02 +0800 Subject: [PATCH 4/6] fix --- Utils/Sources/Utils/hashing.swift | 8 +------- Utils/Tests/UtilsTests/Blake2Tests.swift | 10 +++------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/Utils/Sources/Utils/hashing.swift b/Utils/Sources/Utils/hashing.swift index 6512c183..3b9f988c 100644 --- a/Utils/Sources/Utils/hashing.swift +++ b/Utils/Sources/Utils/hashing.swift @@ -1,10 +1,6 @@ import Blake2 import Foundation -public enum HashConversionError: Error { - case invalidSize -} - /// Computes a Blake2b 256-bit hash of the input data and returns the result as a `Data32`. /// /// - Parameter: The input data to hash. @@ -12,8 +8,6 @@ public enum HashConversionError: Error { /// - Returns: A `Data32` containing the hash result. public func blake2b256(_ data: Data) throws -> Data32 { let hash = try Blake2b.hash(size: 32, data: data) - guard let data32 = Data32(hash) else { - throw HashConversionError.invalidSize - } + let data32 = Data32(hash)! return data32 } diff --git a/Utils/Tests/UtilsTests/Blake2Tests.swift b/Utils/Tests/UtilsTests/Blake2Tests.swift index 59ff6599..d196cb1e 100644 --- a/Utils/Tests/UtilsTests/Blake2Tests.swift +++ b/Utils/Tests/UtilsTests/Blake2Tests.swift @@ -3,14 +3,10 @@ import XCTest @testable import Utils final class Blake2Tests: XCTestCase { - func testBlake2b256Test() { + func testBlake2b256Test() throws { let testData = Data("test".utf8) let expected = hexStringToData("928b20366943e2afd11ebc0eae2e53a93bf177a4fcf35bcc64d503704e65e202") - do { - let actual = try blake2b256(testData) - XCTAssertEqual(expected, actual.data) - } catch { - XCTFail("Hashing failed") - } + let actual = try blake2b256(testData) + XCTAssertEqual(expected, actual.data) } } From 70c52184211b6d4b60408934c7e8d6ba714ed55b Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Wed, 26 Jun 2024 14:22:32 +0800 Subject: [PATCH 5/6] extension for data --- Utils/Sources/Utils/Helpers.swift | 31 ++++++++++++++---------- Utils/Tests/UtilsTests/Blake2Tests.swift | 2 +- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/Utils/Sources/Utils/Helpers.swift b/Utils/Sources/Utils/Helpers.swift index b18ba3c4..f26f1a91 100644 --- a/Utils/Sources/Utils/Helpers.swift +++ b/Utils/Sources/Utils/Helpers.swift @@ -1,20 +1,25 @@ import Foundation -public func hexStringToData(_ hexString: String) -> Data? { - var data = Data() - var index = hexString.startIndex - - while index < hexString.endIndex { - // Find the next 2 characters (1 byte) - guard let nextIndex = hexString.index(index, offsetBy: 2, limitedBy: hexString.endIndex), - let byte = UInt8(hexString[index ..< nextIndex], radix: 16) - else { +extension Data { + init?(fromHexString hexString: String) { + guard !hexString.isEmpty else { return nil } - data.append(byte) - index = nextIndex - } + var data = Data() + var index = hexString.startIndex + + while index < hexString.endIndex { + guard let nextIndex = hexString.index(index, offsetBy: 2, limitedBy: hexString.endIndex), + let byte = UInt8(hexString[index ..< nextIndex], radix: 16) + else { + return nil + } - return data + data.append(byte) + index = nextIndex + } + + self.init(data) + } } diff --git a/Utils/Tests/UtilsTests/Blake2Tests.swift b/Utils/Tests/UtilsTests/Blake2Tests.swift index d196cb1e..ab925efc 100644 --- a/Utils/Tests/UtilsTests/Blake2Tests.swift +++ b/Utils/Tests/UtilsTests/Blake2Tests.swift @@ -5,7 +5,7 @@ import XCTest final class Blake2Tests: XCTestCase { func testBlake2b256Test() throws { let testData = Data("test".utf8) - let expected = hexStringToData("928b20366943e2afd11ebc0eae2e53a93bf177a4fcf35bcc64d503704e65e202") + let expected = Data(fromHexString: "928b20366943e2afd11ebc0eae2e53a93bf177a4fcf35bcc64d503704e65e202") let actual = try blake2b256(testData) XCTAssertEqual(expected, actual.data) } From 718e2cf9df1e3bfed6cf0041aa6616b7b744f5b3 Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Wed, 26 Jun 2024 14:23:14 +0800 Subject: [PATCH 6/6] rename --- Utils/Sources/Utils/{Helpers.swift => Extensions.swift} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Utils/Sources/Utils/{Helpers.swift => Extensions.swift} (100%) diff --git a/Utils/Sources/Utils/Helpers.swift b/Utils/Sources/Utils/Extensions.swift similarity index 100% rename from Utils/Sources/Utils/Helpers.swift rename to Utils/Sources/Utils/Extensions.swift