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

Commit

Permalink
Run swift-format
Browse files Browse the repository at this point in the history
  • Loading branch information
amika-sq authored and github-actions[bot] committed Jan 3, 2024
1 parent d8b78d8 commit 68fd968
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 40 deletions.
10 changes: 5 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,32 @@ let package = Package(
name: "tbDEX",
platforms: [
.iOS(.v13),
.macOS(.v10_15)
.macOS(.v10_15),
],
products: [
.library(
name: "tbDEX",
targets: ["tbDEX"]
),
)
],
dependencies: [
.package(url: "https://github.com/GigaBitcoin/secp256k1.swift.git", from: "0.14.0"),
.package(url: "https://github.com/swift-extras/swift-extras-base64.git", from: "0.7.0")
.package(url: "https://github.com/swift-extras/swift-extras-base64.git", from: "0.7.0"),
],
targets: [
.target(
name: "tbDEX",
dependencies: [
.product(name: "secp256k1", package: "secp256k1.swift"),
.product(name: "ExtrasBase64", package: "swift-extras-base64")
.product(name: "ExtrasBase64", package: "swift-extras-base64"),
]
),
.testTarget(
name: "tbDEXTests",
dependencies: ["tbDEX"],
resources: [
.copy("TestVectors/ed25519"),
.copy("TestVectors/secp256k1")
.copy("TestVectors/secp256k1"),
]
),
]
Expand Down
4 changes: 2 additions & 2 deletions Sources/tbDEX/crypto/Ed25519.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public enum Ed25519: KeyGenerator, Signer {

/// Verifies an RFC8032-compliant EdDSA signature against given data using an Ed25519 public key in JSON Web Key
/// (JWK) format.
public static func verify<S,D>(publicKey: Jwk, signature: S, signedPayload: D) throws -> Bool where S: DataProtocol, D: DataProtocol {
public static func verify<S, D>(publicKey: Jwk, signature: S, signedPayload: D) throws -> Bool
where S: DataProtocol, D: DataProtocol {
guard let x = publicKey.x else {
throw Ed25519Error.invalidPublicJwk
}
Expand All @@ -89,7 +90,6 @@ public enum Ed25519: KeyGenerator, Signer {
return publicKey.isValidSignature(signature, for: signedPayload)
}


// MARK: - Private Functions

private static func generatePrivateJwk(privateKey: Curve25519.Signing.PrivateKey) throws -> Jwk {
Expand Down
2 changes: 1 addition & 1 deletion Sources/tbDEX/crypto/JWK.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation
import CryptoKit
import ExtrasBase64
import Foundation

public struct Jwk: Codable, Equatable {

Expand Down
4 changes: 2 additions & 2 deletions Sources/tbDEX/crypto/KeyManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public protocol KeyManager {

/// Generates and securely stores a private key based on the provided keyType,
/// returning a unique alias that can be utilized to reference the generated key for future operations.
///
///
/// - Parameter keyType: The `KeyType` to use for key generation
/// - Returns: A unique alias that can be used to reference the stored key.
func generatePrivateKey(keyType: KeyType) throws -> String
Expand All @@ -40,7 +40,7 @@ public protocol KeyManager {
func sign<D>(keyAlias: String, payload: D) throws -> Data where D: DataProtocol

/// Return the alias of `publicKey`, as was originally returned by `generatePrivateKey`.
///
///
/// - Parameter publicKey: A public key in JSON Web Key (JWK) format
/// - Returns: The alias belonging to `publicKey`
func getDeterministicAlias(publicKey: Jwk) -> String
Expand Down
29 changes: 15 additions & 14 deletions Sources/tbDEX/crypto/Secp256k1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public enum Secp256k1: KeyGenerator, Signer {
/// Generates an Secp256k1 private key in JSON Web Key (JWK) format.
public static func generatePrivateKey() throws -> Jwk {
return try generatePrivateJwk(
privateKey:secp256k1.Signing.PrivateKey()
privateKey: secp256k1.Signing.PrivateKey()
)
}

Expand All @@ -58,7 +58,6 @@ public enum Secp256k1: KeyGenerator, Signer {
return try generatePrivateJwk(privateKey: privateKey)
}


/// Converts a raw Secp256k1 public key in bytes to its corresponding JSON Web Key (JWK) format.
public static func bytesToPublicKey(_ bytes: Data) throws -> Jwk {
let publicKey = try secp256k1.Signing.PublicKey(
Expand All @@ -81,7 +80,7 @@ public enum Secp256k1: KeyGenerator, Signer {
/// Converts a Secp256k1 public key from JSON Web Key (JWK) format to a raw bytes.
public static func publicKeyToBytes(_ publicKey: Jwk) throws -> Data {
guard let x = publicKey.x,
let y = publicKey.y
let y = publicKey.y
else {
throw Secpsecp256k1Error.invalidPublicJwk
}
Expand All @@ -101,19 +100,20 @@ public enum Secp256k1: KeyGenerator, Signer {
/// Converts a Secp256k1 raw public key to its compressed form.
public static func compressPublicKey(publicKeyBytes: Data) throws -> Data {
guard publicKeyBytes.count == Self.uncompressedKeySize,
publicKeyBytes.first == Self.uncompressedKeyID
publicKeyBytes.first == Self.uncompressedKeyID
else {
throw Secpsecp256k1Error.internalError(reason: "Public key must be 65 bytes long an start with 0x04")
}

let xBytes = publicKeyBytes[1...32]
let yBytes = publicKeyBytes[33...64]

let prefix = if yBytes.last! % 2 == 0 {
Self.compressedKeyEvenYID
} else {
Self.compressedKeyOddYID
}
let prefix =
if yBytes.last! % 2 == 0 {
Self.compressedKeyEvenYID
} else {
Self.compressedKeyOddYID
}

var data = Data()
data.append(prefix)
Expand Down Expand Up @@ -145,7 +145,8 @@ public enum Secp256k1: KeyGenerator, Signer {

/// Verifies an RFC6979-compliant ECDSA signature against given data and a Secp256k1 public key in JSON Web Key
/// (JWK) format.
public static func verify<S,D>(publicKey: Jwk, signature: S, signedPayload: D) throws -> Bool where S: DataProtocol, D: DataProtocol {
public static func verify<S, D>(publicKey: Jwk, signature: S, signedPayload: D) throws -> Bool
where S: DataProtocol, D: DataProtocol {
let publicKeyBytes = try publicKeyToBytes(publicKey)
let publicKey = try secp256k1.Signing.PublicKey(dataRepresentation: publicKeyBytes, format: .uncompressed)

Expand Down Expand Up @@ -239,20 +240,20 @@ public enum Secpsecp256k1Error: Error {

// MARK: - Helper extensions

private extension Data {
func isCompressed() -> Bool {
extension Data {
fileprivate func isCompressed() -> Bool {
return self.count == Secp256k1.compressedKeySize
}
}

private extension secp256k1.Signing.PublicKey {
extension secp256k1.Signing.PublicKey {

/// Get the uncompressed bytes for a given public key.
///
/// With a compressed public key, there's no direct access to the y-coordinate for use within
/// a Jwk. To avoid doing manual computations along the curve to compute the y-coordinate, this
/// function offloads the work to the `secp256k1` library to compute it for us.
func uncompressedBytes() -> Data {
fileprivate func uncompressedBytes() -> Data {
switch self.format {
case .uncompressed:
return self.dataRepresentation
Expand Down
5 changes: 3 additions & 2 deletions Sources/tbDEX/crypto/Signer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation

/// Protocol defining the contract for signing and verifying signatures on payloads
protocol Signer {

/// Sign a given payload using a private key.
///
/// - Parameters:
Expand All @@ -18,5 +18,6 @@ protocol Signer {
/// - signature: The signature to be verified against the payload and public key.
/// - signedPayload: The original payload that was signed, to be verified.
/// - Returns: Boolean indicating if the publicKey and signature are valid for the given payload.
static func verify<S, D>(publicKey: Jwk, signature: S, signedPayload: D) throws -> Bool where S: DataProtocol, D: DataProtocol
static func verify<S, D>(publicKey: Jwk, signature: S, signedPayload: D) throws -> Bool
where S: DataProtocol, D: DataProtocol
}
2 changes: 1 addition & 1 deletion Sources/tbDEX/extensions/Base64URL.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Foundation
import ExtrasBase64
import Foundation

extension Collection where Element == UInt8 {
/// Encodes a collection of bytes to a Base64URL encoded string
Expand Down
12 changes: 7 additions & 5 deletions Tests/tbDEXTests/TestVector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ public func loadTestVector<Input: Codable, Output: Codable>(
fileName: String,
subdirectory: String? = nil
) throws -> TestVector<Input, Output> {
guard let url = Bundle.module.url(
forResource: fileName,
withExtension: "json",
subdirectory: subdirectory
) else {
guard
let url = Bundle.module.url(
forResource: fileName,
withExtension: "json",
subdirectory: subdirectory
)
else {
fatalError("Missing file: \(fileName).json")
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/tbDEXTests/crypto/Ed25519Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ final class Ed25519Tests: XCTestCase {
fileName: "verify",
subdirectory: "ed25519"
)

for vector in testVector.vectors {
let isValid = try Ed25519.verify(
publicKey: vector.input.key,
Expand Down
25 changes: 18 additions & 7 deletions Tests/tbDEXTests/crypto/Secp256k1Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ final class Secp256k1Tests: XCTestCase {
}

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

XCTAssertEqual(publicKey.curve, .secp256k1)
Expand All @@ -85,16 +87,21 @@ final class Secp256k1Tests: XCTestCase {
}

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

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

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

do {
let _ = try Secp256k1.compressPublicKey(publicKeyBytes: invalidUncompressedPublicKeyBytes)
Expand All @@ -105,16 +112,20 @@ final class Secp256k1Tests: XCTestCase {
}

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

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

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

do {
let _ = try Secp256k1.decompressPublicKey(publicKeyBytes: invalidCompressedPublicKeyBytes)
Expand Down

0 comments on commit 68fd968

Please sign in to comment.