Skip to content

Commit d825822

Browse files
committed
feat: cktap-swift
1 parent 77c04a3 commit d825822

File tree

12 files changed

+178
-1
lines changed

12 files changed

+178
-1
lines changed

.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
# Rust
12
/target
23
/Cargo.lock
4+
5+
# IDE
36
/.vscode
7+
/.idea
8+
.DS_Store
9+
10+
# Swift
11+
.build/
12+
.swiftpm/
13+
/Packages/
14+
*.xcodeproj/
15+
xcuserdata/
16+
DerivedData/
17+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
18+
*.xcframework/
19+
*.xcframework.zip
20+
Info.plist
21+
Sources
22+
lib*.a

Cargo.toml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
[workspace]
2-
members = ["lib","cli"]
2+
resolver = "2"
3+
members = ["lib", "cli"]
4+
5+
[profile.release-smaller]
6+
inherits = "release"
7+
opt-level = 'z'
8+
lto = true
9+
codegen-units = 1
10+
panic = "abort"
11+
strip = true

cktap-swift/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm/configuration/registries.json
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.netrc

cktap-swift/Package.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// swift-tools-version:5.5
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "cktap-swift",
8+
platforms: [
9+
.macOS(.v12),
10+
.iOS(.v15)
11+
],
12+
products: [
13+
.library(
14+
name: "CKTap",
15+
targets: ["cktapFFI", "CKTap"]),
16+
],
17+
dependencies: [],
18+
targets: [
19+
.binaryTarget(
20+
name: "cktapFFI",
21+
path: "./cktapFFI.xcframework"),
22+
.target(
23+
name: "CKTap",
24+
dependencies: ["cktapFFI"]
25+
),
26+
.testTarget(
27+
name: "CKTapTests",
28+
dependencies: ["CKTap"]
29+
)
30+
]
31+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import XCTest
2+
import CKTap
3+
4+
final class CKTapTests: XCTestCase {
5+
func testHelloRandom() throws {
6+
print("Hello!")
7+
let nonce = randNonce()
8+
print("Random: \(nonce)")
9+
}
10+
}

cktap-swift/build-xcframework.sh

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/bin/bash
2+
3+
# This script builds local cktap Swift language bindings and corresponding cktapFFI.xcframework.
4+
5+
TARGETDIR="../target"
6+
OUTDIR="."
7+
RELDIR="release-smaller"
8+
NAME="cktapFFI"
9+
STATIC_LIB_NAME="lib${NAME}.a"
10+
NEW_HEADER_DIR="../target/include"
11+
12+
# set required rust version and install component and targets
13+
rustup default 1.77.1
14+
rustup component add rust-src
15+
rustup target add aarch64-apple-ios # iOS arm64
16+
rustup target add x86_64-apple-ios # iOS x86_64
17+
rustup target add aarch64-apple-ios-sim # simulator mac M1
18+
rustup target add aarch64-apple-darwin # mac M1
19+
rustup target add x86_64-apple-darwin # mac x86_64
20+
21+
# Create all required directories first
22+
mkdir -p Sources/CKTap
23+
mkdir -p ../target/include
24+
mkdir -p ../target/lipo-macos/release-smaller
25+
mkdir -p ../target/lipo-ios-sim/release-smaller
26+
27+
cd ../ || exit
28+
29+
# build cktap-ffi rust lib for apple targets
30+
cargo build --package rust-cktap --profile release-smaller --target x86_64-apple-darwin
31+
cargo build --package rust-cktap --profile release-smaller --target aarch64-apple-darwin
32+
cargo build --package rust-cktap --profile release-smaller --target x86_64-apple-ios
33+
cargo build --package rust-cktap --profile release-smaller --target aarch64-apple-ios
34+
cargo build --package rust-cktap --profile release-smaller --target aarch64-apple-ios-sim
35+
36+
# Then run uniffi-bindgen
37+
cargo run --bin uniffi-bindgen generate \
38+
--library target/aarch64-apple-ios/release-smaller/librust_cktap.dylib \
39+
--language swift \
40+
--out-dir cktap-swift/Sources/CKTap \
41+
--no-format
42+
43+
# combine cktap-ffi static libs for aarch64 and x86_64 targets via lipo tool
44+
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
45+
46+
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
47+
48+
cd cktap-swift || exit
49+
50+
# move cktap-ffi static lib header files to temporary directory
51+
mv "Sources/CKTap/rust_cktapFFI.h" "${NEW_HEADER_DIR}"
52+
mv "Sources/CKTap/rust_cktapFFI.modulemap" "${NEW_HEADER_DIR}/module.modulemap"
53+
54+
# remove old xcframework directory
55+
rm -rf "${OUTDIR}/${NAME}.xcframework"
56+
57+
# create new xcframework directory from cktap-ffi static libs and headers
58+
xcodebuild -create-xcframework \
59+
-library "${TARGETDIR}/lipo-macos/${RELDIR}/librust_cktap.a" \
60+
-headers "${NEW_HEADER_DIR}" \
61+
-library "${TARGETDIR}/aarch64-apple-ios/${RELDIR}/librust_cktap.a" \
62+
-headers "${NEW_HEADER_DIR}" \
63+
-library "${TARGETDIR}/lipo-ios-sim/${RELDIR}/librust_cktap.a" \
64+
-headers "${NEW_HEADER_DIR}" \
65+
-output "${OUTDIR}/${NAME}.xcframework"

cktap-swift/justfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
default:
2+
just --list
3+
4+
build:
5+
bash ./build-xcframework.sh
6+
7+
clean:
8+
rm -rf ../rust-cktap-ffi/target/
9+
10+
test:
11+
swift test

lib/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ edition = "2021"
55

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

8+
[lib]
9+
crate-type = ["lib", "staticlib", "cdylib"]
10+
name = "rust_cktap"
11+
812
[dependencies]
913
ciborium = "0.2.0"
1014
serde = "1"
@@ -13,6 +17,11 @@ secp256k1 = { version = "0.26.0", features = ["rand-std", "bitcoin-hashes-std",
1317

1418
# optional dependencies
1519
pcsc = { version = "2", optional = true }
20+
uniffi = { version = "=0.28.0", features = ["cli"] }
21+
thiserror = "1.0.58"
22+
23+
[build-dependencies]
24+
uniffi = { version = "=0.28.0", features = ["build"] }
1625

1726
[features]
1827
default = []
@@ -21,3 +30,7 @@ emulator = []
2130
[[example]]
2231
name = "pcsc"
2332
required-features = ["pcsc"]
33+
34+
[[bin]]
35+
name = "uniffi-bindgen"
36+
path = "src/uniffi-bindgen.rs"

lib/build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
uniffi::generate_scaffolding("src/rust-cktap.udl").expect("Failed to generate FFI scaffolding");
3+
}

lib/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
uniffi::include_scaffolding!("rust-cktap");
2+
13
extern crate core;
24
pub extern crate secp256k1;
35

0 commit comments

Comments
 (0)