Skip to content

Commit

Permalink
feat: cktap-swift
Browse files Browse the repository at this point in the history
  • Loading branch information
reez committed Jan 14, 2025
1 parent 77c04a3 commit d825822
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 1 deletion.
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# Rust
/target
/Cargo.lock

# IDE
/.vscode
/.idea
.DS_Store

# Swift
.build/
.swiftpm/
/Packages/
*.xcodeproj/
xcuserdata/
DerivedData/
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
*.xcframework/
*.xcframework.zip
Info.plist
Sources
lib*.a
11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
[workspace]
members = ["lib","cli"]
resolver = "2"
members = ["lib", "cli"]

[profile.release-smaller]
inherits = "release"
opt-level = 'z'
lto = true
codegen-units = 1
panic = "abort"
strip = true
8 changes: 8 additions & 0 deletions cktap-swift/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
31 changes: 31 additions & 0 deletions cktap-swift/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "cktap-swift",
platforms: [
.macOS(.v12),
.iOS(.v15)
],
products: [
.library(
name: "CKTap",
targets: ["cktapFFI", "CKTap"]),
],
dependencies: [],
targets: [
.binaryTarget(
name: "cktapFFI",
path: "./cktapFFI.xcframework"),
.target(
name: "CKTap",
dependencies: ["cktapFFI"]
),
.testTarget(
name: "CKTapTests",
dependencies: ["CKTap"]
)
]
)
10 changes: 10 additions & 0 deletions cktap-swift/Tests/CKTapTests/CKTapTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import XCTest
import CKTap

final class CKTapTests: XCTestCase {
func testHelloRandom() throws {
print("Hello!")
let nonce = randNonce()
print("Random: \(nonce)")
}
}
65 changes: 65 additions & 0 deletions cktap-swift/build-xcframework.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/bin/bash

# This script builds local cktap Swift language bindings and corresponding cktapFFI.xcframework.

TARGETDIR="../target"
OUTDIR="."
RELDIR="release-smaller"
NAME="cktapFFI"
STATIC_LIB_NAME="lib${NAME}.a"
NEW_HEADER_DIR="../target/include"

# set required rust version and install component and targets
rustup default 1.77.1
rustup component add rust-src
rustup target add aarch64-apple-ios # iOS arm64
rustup target add x86_64-apple-ios # iOS x86_64
rustup target add aarch64-apple-ios-sim # simulator mac M1
rustup target add aarch64-apple-darwin # mac M1
rustup target add x86_64-apple-darwin # mac x86_64

# Create all required directories first
mkdir -p Sources/CKTap
mkdir -p ../target/include
mkdir -p ../target/lipo-macos/release-smaller
mkdir -p ../target/lipo-ios-sim/release-smaller

cd ../ || exit

# build cktap-ffi rust lib for apple targets
cargo build --package rust-cktap --profile release-smaller --target x86_64-apple-darwin
cargo build --package rust-cktap --profile release-smaller --target aarch64-apple-darwin
cargo build --package rust-cktap --profile release-smaller --target x86_64-apple-ios
cargo build --package rust-cktap --profile release-smaller --target aarch64-apple-ios
cargo build --package rust-cktap --profile release-smaller --target aarch64-apple-ios-sim

# Then run uniffi-bindgen
cargo run --bin uniffi-bindgen generate \
--library target/aarch64-apple-ios/release-smaller/librust_cktap.dylib \
--language swift \
--out-dir cktap-swift/Sources/CKTap \
--no-format

# combine cktap-ffi static libs for aarch64 and x86_64 targets via lipo tool
lipo target/aarch64-apple-ios-sim/release-smaller/librust_cktap.a target/x86_64-apple-ios/release-smaller/librust_cktap.a -create -output target/lipo-ios-sim/release-smaller/libcktapFFI.a

lipo target/aarch64-apple-darwin/release-smaller/librust_cktap.a target/x86_64-apple-darwin/release-smaller/librust_cktap.a -create -output target/lipo-macos/release-smaller/libcktapFFI.a

cd cktap-swift || exit

# move cktap-ffi static lib header files to temporary directory
mv "Sources/CKTap/rust_cktapFFI.h" "${NEW_HEADER_DIR}"
mv "Sources/CKTap/rust_cktapFFI.modulemap" "${NEW_HEADER_DIR}/module.modulemap"

# remove old xcframework directory
rm -rf "${OUTDIR}/${NAME}.xcframework"

# create new xcframework directory from cktap-ffi static libs and headers
xcodebuild -create-xcframework \
-library "${TARGETDIR}/lipo-macos/${RELDIR}/librust_cktap.a" \
-headers "${NEW_HEADER_DIR}" \
-library "${TARGETDIR}/aarch64-apple-ios/${RELDIR}/librust_cktap.a" \
-headers "${NEW_HEADER_DIR}" \
-library "${TARGETDIR}/lipo-ios-sim/${RELDIR}/librust_cktap.a" \
-headers "${NEW_HEADER_DIR}" \
-output "${OUTDIR}/${NAME}.xcframework"
11 changes: 11 additions & 0 deletions cktap-swift/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
default:
just --list

build:
bash ./build-xcframework.sh

clean:
rm -rf ../rust-cktap-ffi/target/

test:
swift test
13 changes: 13 additions & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = ["lib", "staticlib", "cdylib"]
name = "rust_cktap"

[dependencies]
ciborium = "0.2.0"
serde = "1"
Expand All @@ -13,6 +17,11 @@ secp256k1 = { version = "0.26.0", features = ["rand-std", "bitcoin-hashes-std",

# optional dependencies
pcsc = { version = "2", optional = true }
uniffi = { version = "=0.28.0", features = ["cli"] }
thiserror = "1.0.58"

[build-dependencies]
uniffi = { version = "=0.28.0", features = ["build"] }

[features]
default = []
Expand All @@ -21,3 +30,7 @@ emulator = []
[[example]]
name = "pcsc"
required-features = ["pcsc"]

[[bin]]
name = "uniffi-bindgen"
path = "src/uniffi-bindgen.rs"
3 changes: 3 additions & 0 deletions lib/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
uniffi::generate_scaffolding("src/rust-cktap.udl").expect("Failed to generate FFI scaffolding");
}
2 changes: 2 additions & 0 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
uniffi::include_scaffolding!("rust-cktap");

extern crate core;
pub extern crate secp256k1;

Expand Down
3 changes: 3 additions & 0 deletions lib/src/rust-cktap.udl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace rust_cktap {
sequence<u8> rand_nonce();
};
3 changes: 3 additions & 0 deletions lib/src/uniffi-bindgen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
uniffi::uniffi_bindgen_main()
}

0 comments on commit d825822

Please sign in to comment.