Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
Use XCTUnwrap instead of force unwrapping as much as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
amika-sq committed Jan 5, 2024
1 parent d4ec978 commit 146ca4e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
6 changes: 3 additions & 3 deletions TestUtilities/Data+Hex.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation

public extension Data {
static func fromHexString(_ hexString: String) -> Self? {
extension Data {
public static func fromHexString(_ hexString: String) -> Self? {
var data = Data()
var hex = hexString

Expand All @@ -27,7 +27,7 @@ public extension Data {
return data
}

func toHexString() -> String {
public func toHexString() -> String {
return map { String(format: "%02x", $0) }.joined()
}
}
4 changes: 2 additions & 2 deletions Tests/tbDEXTests/crypto/InMemoryKeyManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ final class InMemoryKeyManagerTests: XCTestCase {

func test_signSucceedsWhenKeyIsInKeyManager() throws {
let keyAlias = try keyManager.generatePrivateKey(algorithm: .es256k)
let payload = "Hello, world!".data(using: .utf8)!
let payload = try XCTUnwrap("Hello, world!".data(using: .utf8))
XCTAssertNoThrow(try keyManager.sign(keyAlias: keyAlias, payload: payload))
}

func test_signThrowsErrorWhenKeyIsNotInKeyManager() throws {
let payload = "Hello, world!".data(using: .utf8)!
let payload = try XCTUnwrap("Hello, world!".data(using: .utf8))
XCTAssertThrowsError(try keyManager.sign(keyAlias: "InvalidAlias", payload: payload))
}
}
49 changes: 29 additions & 20 deletions Tests/tbDEXTests/crypto/Secp256k1Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ final class Secp256k1Tests: XCTestCase {
}

func test_bytesToPrivateKey_returnedInJwkFormat() throws {
let privateKeyBytes = Data.fromHexString("740ec69810de9ad1b8f298f1d2c0e6a52dd1e958dc2afc85764bec169c222e88")!
let privateKeyBytes = try XCTUnwrap(
Data.fromHexString("740ec69810de9ad1b8f298f1d2c0e6a52dd1e958dc2afc85764bec169c222e88")
)
let privateKey = try Secp256k1.shared.bytesToPrivateKey(privateKeyBytes)

XCTAssertEqual(privateKey.curve, .secp256k1)
Expand All @@ -49,9 +51,10 @@ final class Secp256k1Tests: XCTestCase {
}

func test_bytesToPublicKey_returnedInJwkFormat() throws {
let publicKeyBytes = Data.fromHexString(
"043752951274023296c8a74b0ffe42f82ff4b4d4bba4326477422703f761f59258c26a7465b9a77ac0c3f1cedb139c428b0b1fbb5516867b527636f3286f705553"
)!
let publicKeyBytes = try XCTUnwrap(
Data.fromHexString(
"043752951274023296c8a74b0ffe42f82ff4b4d4bba4326477422703f761f59258c26a7465b9a77ac0c3f1cedb139c428b0b1fbb5516867b527636f3286f705553"
))
let publicKey = try Secp256k1.shared.bytesToPublicKey(publicKeyBytes)

XCTAssertEqual(publicKey.curve, .secp256k1)
Expand All @@ -63,21 +66,25 @@ final class Secp256k1Tests: XCTestCase {
}

func test_compressPublicKey() throws {
let compressedPublicKeyBytes = Data.fromHexString(
"026bcdccc644b309921d3b0c266183a20786650c1634d34e8dfa1ed74cd66ce214")!
let uncompressedPublicKeyBytes = Data.fromHexString(
"046bcdccc644b309921d3b0c266183a20786650c1634d34e8dfa1ed74cd66ce21465062296011dd076ae4e8ce5163ccf69d01496d3147656dcc96645b95211f3c6"
)!
let compressedPublicKeyBytes = try XCTUnwrap(
Data.fromHexString("026bcdccc644b309921d3b0c266183a20786650c1634d34e8dfa1ed74cd66ce214")
)
let uncompressedPublicKeyBytes = try XCTUnwrap(
Data.fromHexString(
"046bcdccc644b309921d3b0c266183a20786650c1634d34e8dfa1ed74cd66ce21465062296011dd076ae4e8ce5163ccf69d01496d3147656dcc96645b95211f3c6"
)
)

let output = try Secp256k1.shared.compressPublicKey(publicKeyBytes: uncompressedPublicKeyBytes)
XCTAssertEqual(output.count, 33)
XCTAssertEqual(output, compressedPublicKeyBytes)
}

func test_compressPublicKey_throwsForInvalidUncompressedPublickey() throws {
let invalidUncompressedPublicKeyBytes = Data.fromHexString(
"dfebc16793a5737ac51f606a43524df8373c063e41d5a99b2f1530afd987284bd1c7cde1658a9a756e71f44a97b4783ea9dee5ccb7f1447eb4836d8de9bd4f81fd"
)!
let invalidUncompressedPublicKeyBytes = try XCTUnwrap(
Data.fromHexString(
"dfebc16793a5737ac51f606a43524df8373c063e41d5a99b2f1530afd987284bd1c7cde1658a9a756e71f44a97b4783ea9dee5ccb7f1447eb4836d8de9bd4f81fd"
))

do {
let _ = try Secp256k1.shared.compressPublicKey(publicKeyBytes: invalidUncompressedPublicKeyBytes)
Expand All @@ -88,20 +95,23 @@ final class Secp256k1Tests: XCTestCase {
}

func test_decompressPublicKey() throws {
let compressedPublicKeyBytes = Data.fromHexString(
"026bcdccc644b309921d3b0c266183a20786650c1634d34e8dfa1ed74cd66ce214")!
let uncompressedPublicKeyBytes = Data.fromHexString(
"046bcdccc644b309921d3b0c266183a20786650c1634d34e8dfa1ed74cd66ce21465062296011dd076ae4e8ce5163ccf69d01496d3147656dcc96645b95211f3c6"
)!
let compressedPublicKeyBytes = try XCTUnwrap(
Data.fromHexString(
"026bcdccc644b309921d3b0c266183a20786650c1634d34e8dfa1ed74cd66ce214"))
let uncompressedPublicKeyBytes = try XCTUnwrap(
Data.fromHexString(
"046bcdccc644b309921d3b0c266183a20786650c1634d34e8dfa1ed74cd66ce21465062296011dd076ae4e8ce5163ccf69d01496d3147656dcc96645b95211f3c6"
))

let output = try Secp256k1.shared.decompressPublicKey(publicKeyBytes: compressedPublicKeyBytes)
XCTAssertEqual(output.count, 65)
XCTAssertEqual(output, uncompressedPublicKeyBytes)
}

func test_decompressPublicKey_throwsForInvalidCompressedPublicKey() throws {
let invalidCompressedPublicKeyBytes = Data.fromHexString(
"fef0b998921eafb58f49efdeb0adc47123aa28a4042924236f08274d50c72fe7b0")!
let invalidCompressedPublicKeyBytes = try XCTUnwrap(
Data.fromHexString(
"fef0b998921eafb58f49efdeb0adc47123aa28a4042924236f08274d50c72fe7b0"))

do {
let _ = try Secp256k1.shared.decompressPublicKey(publicKeyBytes: invalidCompressedPublicKeyBytes)
Expand All @@ -111,7 +121,6 @@ final class Secp256k1Tests: XCTestCase {
}
}


func test_sign_returns64ByteSignature() throws {
let privateKey = try Secp256k1.shared.generatePrivateKey()
let data = Data([51, 52, 53])
Expand Down

0 comments on commit 146ca4e

Please sign in to comment.