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

Commit

Permalink
chore: replace secp256k1.swift for web3swift
Browse files Browse the repository at this point in the history
secp256k1.swift library does not support Cocoapods anymore, which is
still being required as an alternative distribution by ImmutableXCore.

Bump Swift version to 5.7
  • Loading branch information
CassiusPacheco committed Oct 19, 2022
1 parent c4fc52b commit dea4486
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/swiftformat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v3
- uses: sinoru/actions-setup-swift@v2
with:
swift-version: '5.6.1'
swift-version: '5.7'
- name: GitHub Action for SwiftFormat
uses: CassiusPacheco/[email protected]
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v3
- uses: sinoru/actions-setup-swift@v2
with:
swift-version: '5.6.1'
swift-version: '5.7'
- name: GitHub Actions for SwiftLint with --strict
uses: sinoru/actions-swiftlint@v6
with:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ jobs:
unit_tests:
runs-on: macos-12
steps:
- name: Select Xcode 13.4.1
run: sudo xcode-select -s "/Applications/Xcode_13.4.1.app"
- name: Select Xcode 14.0.1
run: sudo xcode-select -s "/Applications/Xcode_14.0.1.app"
- name: Repository checkout
uses: actions/checkout@v2
- name: Build for iOS
Expand Down
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.5
5.7
2 changes: 1 addition & 1 deletion ImmutableXCore.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Pod::Spec.new do |spec|

spec.dependency 'AnyCodable-FlightSchool', '~> 0.6'
spec.dependency 'BigInt', '~> 5.2.0'
spec.dependency 'secp256k1.swift', '~> 0.1'
spec.dependency 'secp256k1Swift', '~> 0.7.4'

spec.resources = ['Sources/ImmutableXCore/version']
end
15 changes: 12 additions & 3 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,22 @@
"version" : "5.3.0"
}
},
{
"identity" : "secp256k1",
"kind" : "remoteSourceControl",
"location" : "https://github.com/portto/secp256k1.git",
"state" : {
"revision" : "6864a2560066cedede330c4b344689432a7300f7",
"version" : "0.0.5"
}
},
{
"identity" : "secp256k1.swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Boilertalk/secp256k1.swift",
"location" : "https://github.com/portto/secp256k1.swift.git",
"state" : {
"revision" : "45e458ec3be46cf0a6eb68bc947f797852dc65d8",
"version" : "0.1.6"
"revision" : "23aa6bab1f60e513297d0d58a863418f68534e56",
"version" : "0.7.4"
}
}
],
Expand Down
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.5
// swift-tools-version: 5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand All @@ -18,15 +18,15 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/Flight-School/AnyCodable", from: "0.6.5"),
.package(url: "https://github.com/attaswift/BigInt", from: "5.3.0"),
.package(url: "https://github.com/Boilertalk/secp256k1.swift.git", from: "0.1.6"),
.package(url: "https://github.com/portto/secp256k1.swift.git", from: "0.7.4"),
],
targets: [
.target(
name: "ImmutableXCore",
dependencies: [
.product(name: "BigInt", package: "BigInt"),
.product(name: "AnyCodable", package: "AnyCodable"),
.product(name: "secp256k1", package: "secp256k1.swift"),
.product(name: "secp256k1Swift", package: "secp256k1.swift"),
],
resources: [
.copy("version"),
Expand Down
3 changes: 1 addition & 2 deletions Sources/ImmutableXCore/Crypto/BIP32/BIP32Key.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import BigInt
import CryptoKit
import Foundation
import secp256k1

struct BIP32Key {
/// - Parameters:
Expand Down Expand Up @@ -32,7 +31,7 @@ struct BIP32Key {
data += UInt8(0).data
data += derivedPrivateKey
} else {
data += Secp256k1Encrypter.createPublicKey(privateKey: derivedPrivateKey)
data += try Secp256k1Encrypter.createPublicKey(privateKey: derivedPrivateKey)
}

let derivingIndex = CFSwapInt32BigToHost(hardened ? (edge | index) : index)
Expand Down
20 changes: 3 additions & 17 deletions Sources/ImmutableXCore/Crypto/Secp256k1/Secp256k1Encrypter.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Foundation
import secp256k1
import secp256k1Swift

/// A simple wrapper of ``secp256k1`` lib for creating public keys.
struct Secp256k1Encrypter {
Expand All @@ -9,21 +9,7 @@ struct Secp256k1Encrypter {
/// - privateKey: private key bytes
/// - compressed: whether public key should be compressed.
/// - Returns: If compression enabled, public key is 33 bytes size, otherwise it is 65 bytes.
static func createPublicKey(privateKey: Data, compressed: Bool = true) -> Data {
let context: OpaquePointer = secp256k1_context_create(UInt32(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY))!
defer { secp256k1_context_destroy(context) }

let privateKey: [UInt8] = Array(privateKey)
var publicKey = secp256k1_pubkey()
_ = secp256k1_ec_pubkey_create(context, &publicKey, privateKey)

var output = Data(count: compressed ? 33 : 65)
var outputLen: Int = output.count
let compressedFlags = compressed ? UInt32(SECP256K1_EC_COMPRESSED) : UInt32(SECP256K1_EC_UNCOMPRESSED)
output.withUnsafeMutableBytes { pointer in
guard let p = pointer.bindMemory(to: UInt8.self).baseAddress else { return }
secp256k1_ec_pubkey_serialize(context, p, &outputLen, &publicKey, compressedFlags)
}
return output
static func createPublicKey(privateKey: Data, compressed: Bool = true) throws -> Data {
try secp256k1.Signing.PrivateKey(rawRepresentation: privateKey).publicKey.rawRepresentation
}
}

0 comments on commit dea4486

Please sign in to comment.