Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cocoapod podspec #6

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,34 @@
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "curvelibCommon"
BuildableName = "curvelibCommon"
BlueprintName = "curvelibCommon"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "curvelibSha3"
BuildableName = "curvelibSha3"
BlueprintName = "curvelibSha3"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
Expand Down
7 changes: 7 additions & 0 deletions License.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2023 Web3Auth

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 changes: 17 additions & 7 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ let package = Package(
.library(
name: "encryption_aes_cbc_sha512",
targets: ["encryption_aes_cbc_sha512"]),
.library(name: "curvelibSha3", targets: ["curvelibSha3"]),
.library(name: "curvelibCommon", targets: ["curvelibCommon"])
],
dependencies: [
// Dependencies declare other packages that this package depends on.
Expand All @@ -27,25 +29,33 @@ let package = Package(
path: "Sources/curve_secp256k1/curve_secp256k1.xcframework"
),

.target(name: "curvelib",
dependencies: ["curve_secp256k1"],
path: "Sources/curve_secp256k1"
.target(name: "curvelib_xc",
dependencies: ["curve_secp256k1"],
path: "Sources/curve_secp256k1"

),
.target(name: "curvelibCommon",
path: "Sources/curvelib/common"
),
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.target(
name: "curveSecp256k1",
dependencies: ["curvelib"],
dependencies: ["curvelib_xc", "curvelibCommon"],
path: "Sources/curvelib/secp256k1"
),
.target(
name: "encryption_aes_cbc_sha512",
dependencies: ["curvelib", "curveSecp256k1"],
dependencies: ["curvelib_xc", "curveSecp256k1", "curvelibCommon"],
path: "Sources/curvelib/encryption"
),

.target(
name: "curvelibSha3",
dependencies: ["curvelib_xc", "curvelibCommon"],
path: "Sources/curvelib/sha3"
),
.testTarget(
name: "curvelibTests",
dependencies: ["curveSecp256k1", "encryption_aes_cbc_sha512"]),
dependencies: ["curveSecp256k1", "encryption_aes_cbc_sha512", "curvelibSha3"]),
]
)
16 changes: 8 additions & 8 deletions Sources/curve_secp256k1/curve_secp256k1.xcframework/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<key>BinaryPath</key>
<string>libsecp256k1_rs.a</string>
<key>LibraryIdentifier</key>
<string>macos-arm64_x86_64</string>
<string>ios-arm64_x86_64-simulator</string>
<key>LibraryPath</key>
<string>libsecp256k1_rs.a</string>
<key>SupportedArchitectures</key>
Expand All @@ -17,38 +17,38 @@
<string>x86_64</string>
</array>
<key>SupportedPlatform</key>
<string>macos</string>
<string>ios</string>
<key>SupportedPlatformVariant</key>
<string>simulator</string>
</dict>
<dict>
<key>BinaryPath</key>
<string>libsecp256k1_rs.a</string>
<key>LibraryIdentifier</key>
<string>ios-arm64</string>
<string>macos-arm64_x86_64</string>
<key>LibraryPath</key>
<string>libsecp256k1_rs.a</string>
<key>SupportedArchitectures</key>
<array>
<string>arm64</string>
<string>x86_64</string>
</array>
<key>SupportedPlatform</key>
<string>ios</string>
<string>macos</string>
</dict>
<dict>
<key>BinaryPath</key>
<string>libsecp256k1_rs.a</string>
<key>LibraryIdentifier</key>
<string>ios-arm64_x86_64-simulator</string>
<string>ios-arm64</string>
<key>LibraryPath</key>
<string>libsecp256k1_rs.a</string>
<key>SupportedArchitectures</key>
<array>
<string>arm64</string>
<string>x86_64</string>
</array>
<key>SupportedPlatform</key>
<string>ios</string>
<key>SupportedPlatformVariant</key>
<string>simulator</string>
</dict>
</array>
<key>CFBundlePackageType</key>
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 3 additions & 1 deletion Sources/curve_secp256k1/include/curve_secp256k1.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ char *curve_secp256k1_encrypted_message_get_mac(struct EncryptedMessage *message
char *curve_secp256k1_encrypted_message_get_iv(struct EncryptedMessage *message,int* error_code);
void curve_secp256k1_encrypted_message_free(struct EncryptedMessage *message);
struct EncryptedMessage *curve_secp256k1_aes_cbc_hmac_encrypt(struct PublicKey *public_key, char *plain_text, int *error_code);
char *curve_secp256k1_aes_cbc_hmac_decrypt(struct SecretKey* secret_key, struct EncryptedMessage* encrypted, int* error_code );
char *curve_secp256k1_aes_cbc_hmac_decrypt(struct SecretKey* secret_key, struct EncryptedMessage* encrypted, bool skip_mac_check, int* error_code );

char *curve_sha3_keccak256(char *hex_data, int *error_code);

#ifdef __cplusplus
} // extern "C"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public struct CurveError: Error, LocalizedError {
case unknownStatusCode
}

private(set) var type: ErrorType
private(set) public var type: ErrorType

public init(code: Int32) {
switch code {
Expand Down
36 changes: 36 additions & 0 deletions Sources/curvelib/common/data_extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// File.swift
//
//
// Created by CW Lee on 05/04/2024.
//

import Foundation

public extension Data {
var hexString: String {
return map { String(format: "%02x", $0) }.joined()
}

init?(hexString: String) {
// Ensure the string has an even number of characters
guard hexString.count % 2 == 0 else { return nil }

var data = Data(capacity: hexString.count / 2)

// Convert each pair of characters to a byte and append to data
var index = hexString.startIndex
while index < hexString.endIndex {
let nextIndex = hexString.index(index, offsetBy: 2)
if let byte = UInt8(hexString[index..<nextIndex], radix: 16) {
data.append(byte)
} else {
return nil // Invalid hexadecimal character
}
index = nextIndex
}

self = data
}
}

6 changes: 4 additions & 2 deletions Sources/curvelib/encryption/EncryptedMessage.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import Foundation

#if canImport(curvelib)
import curvelib
#if canImport(curvelib_xc)
import curvelib_xc
#endif
import curvelibCommon
import curveSecp256k1


public final class EncryptedMessage {
private(set) var pointer: OpaquePointer?

Expand Down
21 changes: 14 additions & 7 deletions Sources/curvelib/encryption/Encryption.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import Foundation

#if canImport(curvelib)
import curvelib
#if canImport(curvelib_xc)
import curvelib_xc
#endif
import curvelibCommon
import curveSecp256k1



public final class Encryption {
public static func encrypt(pk: PublicKey, plainText: String) throws -> EncryptedMessage {
public static func encrypt(pk: PublicKey, data: Data) throws -> EncryptedMessage {
var errorCode: Int32 = -1
let stringPtr = UnsafeMutablePointer<Int8>(mutating: (plainText as NSString).utf8String)
let stringPtr = UnsafeMutablePointer<Int8>(mutating: (data.hexString as NSString).utf8String)
let result = withUnsafeMutablePointer(to: &errorCode, { error in
curve_secp256k1_aes_cbc_hmac_encrypt(pk.pointer, stringPtr, error)
})
Expand All @@ -20,10 +23,10 @@ public final class Encryption {
return EncryptedMessage(ptr: result!)
}

public static func decrypt(sk: SecretKey, encrypted: EncryptedMessage) throws -> String {
public static func decrypt(sk: SecretKey, encrypted: EncryptedMessage, skipMacCheck:Bool = false) throws -> Data {
var errorCode: Int32 = -1
let result = withUnsafeMutablePointer(to: &errorCode, { error in
curve_secp256k1_aes_cbc_hmac_decrypt(sk.pointer, encrypted.pointer, error)
curve_secp256k1_aes_cbc_hmac_decrypt(sk.pointer, encrypted.pointer, skipMacCheck, error)
})

guard errorCode == 0 else {
Expand All @@ -32,6 +35,10 @@ public final class Encryption {

let value = String(cString: result!)
curve_secp256k1_string_free(result)
return value

guard let result = Data(hexString: value) else {
throw CurveError(code: 3 )
}
return result
}
}
6 changes: 4 additions & 2 deletions Sources/curvelib/secp256k1/ECDH.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Foundation

#if canImport(curvelib)
import curvelib
#if canImport(curvelib_xc)
import curvelib_xc
#endif

import curvelibCommon

public final class ECDH {
public static func ecdh(sk: SecretKey, pk: PublicKey) throws -> String {
var errorCode: Int32 = -1
Expand Down
6 changes: 4 additions & 2 deletions Sources/curvelib/secp256k1/ECDSA.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Foundation

#if canImport(curvelib)
import curvelib
#if canImport(curvelib_xc)
import curvelib_xc
#endif

import curvelibCommon

public final class ECDSA {
public static func signRecoverable(key: SecretKey, hash: String) throws -> Signature {
var errorCode: Int32 = -1
Expand Down
7 changes: 5 additions & 2 deletions Sources/curvelib/secp256k1/PublicKey.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import Foundation

#if canImport(curvelib)
import curvelib
#if canImport(curvelib_xc)
import curvelib_xc
#endif

import curvelibCommon


public final class PublicKey {
private(set) public var pointer: OpaquePointer?

Expand Down
7 changes: 5 additions & 2 deletions Sources/curvelib/secp256k1/PublicKeyCollection.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import Foundation

#if canImport(curvelib)
import curvelib
#if canImport(curvelib_xc)
import curvelib_xc
#endif

import curvelibCommon


public final class PublicKeyCollection {
private(set) var pointer: OpaquePointer?

Expand Down
7 changes: 5 additions & 2 deletions Sources/curvelib/secp256k1/SecretKey.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import Foundation

#if canImport(curvelib)
import curvelib
#if canImport(curvelib_xc)
import curvelib_xc
#endif

import curvelibCommon


public final class SecretKey {
private(set) public var pointer: OpaquePointer?

Expand Down
7 changes: 5 additions & 2 deletions Sources/curvelib/secp256k1/Signature.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import Foundation

#if canImport(curvelib)
import curvelib
#if canImport(curvelib_xc)
import curvelib_xc
#endif

import curvelibCommon


public final class Signature {
private(set) var pointer: OpaquePointer?

Expand Down
42 changes: 42 additions & 0 deletions Sources/curvelib/sha3/sha3.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// File.swift
//
//
// Created by CW Lee on 05/04/2024.
//

import Foundation

#if canImport(curvelib_xc)
import curvelib_xc
#endif

import curvelibCommon

public func keccak256 ( data : Data ) throws -> Data {
var errorCode: Int32 = -1
let hexPtr = UnsafeMutablePointer<Int8>(mutating: (data.hexString as NSString).utf8String)
let result = withUnsafeMutablePointer(to: &errorCode, { error in
curve_sha3_keccak256(hexPtr, error)
})
guard errorCode == 0 else {
throw CurveError(code: errorCode)
}
let value = String(cString: result!)
curve_secp256k1_string_free(result)

guard let hex_data = Data(hexString: value) else {
throw CurveError(code: 3)
}
return hex_data
}

public enum Variants {
case KECCAK256
}

public extension Data {
func sha3( varient : Variants ) throws -> Data {
return try keccak256(data: self)
}
}
Loading
Loading