Skip to content

Commit

Permalink
Making utility extensions public (#21)
Browse files Browse the repository at this point in the history
* Making utility extensions public

* Bumping release in README
  • Loading branch information
csjones authored Sep 4, 2020
1 parent a32c27d commit 097e499
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
4 changes: 3 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
]
```

Expand All @@ -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,
Expand All @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions Sources/swift-crypto/extensions/Array.swift
Original file line number Diff line number Diff line change
@@ -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())
}
}
18 changes: 18 additions & 0 deletions Sources/swift-crypto/extensions/Data.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
11 changes: 6 additions & 5 deletions Tests/secp256k1Tests/secp256k1Tests.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import XCTest
@testable import secp256k1
@testable import secp256k1_utils
import secp256k1_utils

final class secp256k1Tests: XCTestCase {
/// Uncompressed Keypair test
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -47,15 +48,15 @@ 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)
XCTAssertEqual(secp256k1_ec_pubkey_create(context, &cPubkey, privateKey), 1)
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)
Expand Down

0 comments on commit 097e499

Please sign in to comment.