From 097e4991f1d5ebc52dc709f403c1a890849e88bb Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 3 Sep 2020 19:24:56 -0700 Subject: [PATCH] Making utility extensions public (#21) * Making utility extensions public * Bumping release in README --- Package.swift | 4 +++- README.md | 6 +++--- Sources/swift-crypto/extensions/Array.swift | 20 ++++++++++++++++++++ Sources/swift-crypto/extensions/Data.swift | 18 ++++++++++++++++++ Tests/secp256k1Tests/secp256k1Tests.swift | 11 ++++++----- 5 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 Sources/swift-crypto/extensions/Array.swift create mode 100644 Sources/swift-crypto/extensions/Data.swift diff --git a/Package.swift b/Package.swift index de5e5f1..16dd61c 100644 --- a/Package.swift +++ b/Package.swift @@ -53,7 +53,9 @@ let package = Package( "swift-crypto/Sources", ], sources: [ - "swift-crypto/Tests/CryptoTests/Utils/BytesUtil.swift" + "swift-crypto/Tests/CryptoTests/Utils/BytesUtil.swift", + "extensions/Array.swift", + "extensions/Data.swift" ] ), .testTarget( diff --git a/README.md b/README.md index 01e0bb6..7124bc5 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ In your `Package.swift`: ```swift dependencies: [ - .package(name: "secp256k1", url: "https://github.com/GigaBitcoin/secp256k1.swift.git", from: "0.0.11"), + .package(name: "secp256k1", url: "https://github.com/GigaBitcoin/secp256k1.swift.git", from: "0.0.12"), ] ``` @@ -31,7 +31,7 @@ let context = secp256k1_context_create(UInt32(SECP256K1_CONTEXT_SIGN | SECP256K1 var pubkeyLen = 33 var cPubkey = secp256k1_pubkey() var pubkey = [UInt8](repeating: 0, count: pubkeyLen) -let privkey = try! Array(hexString: "14e4a74438858920d8a35fb2d88677580b6a2ee9be4e711ae34ec6b396d87b5c") +let privkey = try! Array(hex: "14e4a74438858920d8a35fb2d88677580b6a2ee9be4e711ae34ec6b396d87b5c") // Verify the context and keys are setup correctly guard secp256k1_context_randomize(context, privkey) == 1, @@ -42,7 +42,7 @@ guard secp256k1_context_randomize(context, privkey) == 1, return } -print(pubkey.hexString) // 02734b3511150a60fc8cac329cd5ff804555728740f2f2e98bc4242135ef5d5e4e +print(pubkey.hex) // 02734b3511150a60fc8cac329cd5ff804555728740f2f2e98bc4242135ef5d5e4e // Destory context after creation secp256k1_context_destroy(context) diff --git a/Sources/swift-crypto/extensions/Array.swift b/Sources/swift-crypto/extensions/Array.swift new file mode 100644 index 0000000..251d4b6 --- /dev/null +++ b/Sources/swift-crypto/extensions/Array.swift @@ -0,0 +1,20 @@ +// +// Array.swift +// GigaBitcoin/secp256k1.swift +// +// Copyright (c) 2020 GigaBitcoin LLC +// Distributed under the MIT software license +// +// See the accompanying file LICENSE for information +// + +public extension Array where Element == UInt8 { + /// Public initializer backed by the `BytesUtil.swift` Array extension + /// - Parameter hex: hexadecimal string to initialize + /// - Throws: `ByteHexEncodingErrors` for invalid string or hex value + init(hex: String) throws { + self.init() + // The `BytesUtil.swift` Array extension expects lowercase strings + self = try Array(hexString: hex.lowercased()) + } +} diff --git a/Sources/swift-crypto/extensions/Data.swift b/Sources/swift-crypto/extensions/Data.swift new file mode 100644 index 0000000..f70c109 --- /dev/null +++ b/Sources/swift-crypto/extensions/Data.swift @@ -0,0 +1,18 @@ +// +// Data.swift +// GigaBitcoin/secp256k1.swift +// +// Copyright (c) 2020 GigaBitcoin LLC +// Distributed under the MIT software license +// +// See the accompanying file LICENSE for information +// + +import Foundation + +public extension DataProtocol { + /// Public property backed by the `BytesUtil.swift` DataProtocol extension property `hexString` + var hex: String { + hexString + } +} diff --git a/Tests/secp256k1Tests/secp256k1Tests.swift b/Tests/secp256k1Tests/secp256k1Tests.swift index 24dbe9e..4897df1 100644 --- a/Tests/secp256k1Tests/secp256k1Tests.swift +++ b/Tests/secp256k1Tests/secp256k1Tests.swift @@ -1,6 +1,6 @@ import XCTest @testable import secp256k1 -@testable import secp256k1_utils +import secp256k1_utils final class secp256k1Tests: XCTestCase { /// Uncompressed Keypair test @@ -16,7 +16,7 @@ final class secp256k1Tests: XCTestCase { var cPubkey = secp256k1_pubkey() var publicKey = [UInt8](repeating: 0, count: pubkeyLen) - let privateKey = try! Array(hexString: "14E4A74438858920D8A35FB2D88677580B6A2EE9BE4E711AE34EC6B396D87B5C".lowercased()) + let privateKey = try! Array(hex: "14E4A74438858920D8A35FB2D88677580B6A2EE9BE4E711AE34EC6B396D87B5C") // Verify the context and keys are setup correctly XCTAssertEqual(secp256k1_context_randomize(context, privateKey), 1) @@ -28,10 +28,11 @@ final class secp256k1Tests: XCTestCase { """ // Define the expected public key - let expectedPublicKey = try! Array(hexString: hexString.lowercased()) + let expectedPublicKey = try! Array(hex: hexString) // Verify the generated public key matches the expected public key XCTAssertEqual(expectedPublicKey, publicKey) + XCTAssertEqual(hexString.lowercased(), publicKey.hex) } /// Compressed Keypair test @@ -47,7 +48,7 @@ final class secp256k1Tests: XCTestCase { var cPubkey = secp256k1_pubkey() var publicKey = [UInt8](repeating: 0, count: pubkeyLen) - let privateKey = try! Array(hexString: "B035FCFC6ABF660856C5F3A6F9AC51FCA897BB4E76AD9ACA3EFD40DA6B9C864B".lowercased()) + let privateKey = try! Array(hex: "B035FCFC6ABF660856C5F3A6F9AC51FCA897BB4E76AD9ACA3EFD40DA6B9C864B") // Verify the context and keys are setup correctly XCTAssertEqual(secp256k1_context_randomize(context, privateKey), 1) @@ -55,7 +56,7 @@ final class secp256k1Tests: XCTestCase { XCTAssertEqual(secp256k1_ec_pubkey_serialize(context, &publicKey, &pubkeyLen, &cPubkey, UInt32(SECP256K1_EC_COMPRESSED)), 1) // Define the expected public key - let expectedPublicKey = try! Array(hexString: "02EA724B70B48B61FB87E4310871A48C65BF38BF3FDFEFE73C2B90F8F32F9C1794".lowercased()) + let expectedPublicKey = try! Array(hex: "02EA724B70B48B61FB87E4310871A48C65BF38BF3FDFEFE73C2B90F8F32F9C1794") // Verify the generated public key matches the expected public key XCTAssertEqual(expectedPublicKey, publicKey)