Initial Commit

This commit is contained in:
Steve Myers 2021-11-22 14:17:03 -08:00
commit 38403e00b4
No known key found for this signature in database
GPG Key ID: 8105A46B22C2D051
8 changed files with 2254 additions and 0 deletions

13
.gitignore vendored Normal file
View File

@ -0,0 +1,13 @@
.DS_Store
/.build
/.swiftpm
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
/*.xcframework
!bdkFFI-umbrella.h
!module.modulemap
!info.plist
*.xcframework.zip

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "bdk-ffi"]
path = bdk-ffi
url = git@github.com:bitcoindevkit/bdk-ffi.git

36
Package.swift Normal file
View File

@ -0,0 +1,36 @@
// 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: "bdk-swift",
platforms: [
.macOS(.v12),
.iOS(.v15)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "BitcoinDevKit",
targets: ["bdkFFI", "BitcoinDevKit"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.binaryTarget(
name: "bdkFFI",
url: "https://github.com/notmandatory/bdk-swift/releases/download/0.1.0/bdkFFI.xcframework.zip",
checksum: "b34dc1dea2e53bc894f1ad61269e45de6c77dd6391bbb1318cfb0be17435c4db"),
.target(
name: "BitcoinDevKit",
dependencies: ["bdkFFI"]),
.testTarget(
name: "BitcoinDevKitTests",
dependencies: ["BitcoinDevKit"]),
]
)

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# bdk-swift
A description of this package.

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,12 @@
import XCTest
@testable import BitcoinDevKit
final class BitcoinDevKitTests: XCTestCase {
func testMemoryWalletNewAddress() throws {
let desc = "wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)"
let config = DatabaseConfig.memory(junk: "")
let wallet = try OfflineWallet.init(descriptor: desc, network: Network.regtest, databaseConfig: config)
let address = wallet.getNewAddress()
XCTAssertEqual(address, "bcrt1qzg4mckdh50nwdm9hkzq06528rsu73hjxytqkxs")
}
}

1
bdk-ffi Submodule

@ -0,0 +1 @@
Subproject commit 4e087ef21c69986090ad8157bba112842156b8b9

73
build.sh Executable file
View File

@ -0,0 +1,73 @@
#!/usr/bin/env bash
set -eo pipefail
BUILD_PROFILE=release
BDKFFI_DIR=bdk-ffi
TARGET_DIR=$BDKFFI_DIR/target
STATIC_LIB_NAME=libbdkffi.a
SWIFT_DIR="$BDKFFI_DIR/bindings/bdk-swift"
XCFRAMEWORK_NAME="bdkFFI"
XCFRAMEWORK_ROOT="$XCFRAMEWORK_NAME.xcframework"
XCFRAMEWORK_COMMON="$XCFRAMEWORK_ROOT/common/$XCFRAMEWORK_NAME.framework"
## build bdk-ffi rust libs
echo "Build bdk-ffi rust library"
pushd $BDKFFI_DIR
cargo build --release
echo "Generate bdk-ffi swift bindings"
uniffi-bindgen generate src/bdk.udl --no-format --out-dir bindings/bdk-swift/ --language swift
swiftc -module-name bdk -emit-library -o libbdkffi.dylib -emit-module -emit-module-path bindings/bdk-swift/ -parse-as-library -L target/release/ -lbdkffi -Xcc -fmodule-map-file=bindings/bdk-swift/$XCFRAMEWORK_NAME.modulemap bindings/bdk-swift/bdk.swift -suppress-warnings
## build bdk-ffi rust libs into xcframework
echo "Build bdk-ffi libs into swift xcframework"
APPLE_TRIPLES=("x86_64-apple-darwin" "x86_64-apple-ios" "aarch64-apple-ios")
for TARGET in $APPLE_TRIPLES; do
echo "Build bdk-ffi lib for target $TARGET"
cargo build --release --target $TARGET
done
popd
## Manually construct xcframework
# Cleanup prior build
rm -rf "$XCFRAMEWORK_ROOT"
rm -f $XCFRAMEWORK_ROOT.zip
# Common files
mkdir -p "$XCFRAMEWORK_COMMON/Modules"
cp "$SWIFT_DIR/$XCFRAMEWORK_NAME.modulemap" "$XCFRAMEWORK_COMMON/Modules/"
mkdir -p "$XCFRAMEWORK_COMMON/Headers"
cp "$SWIFT_DIR/$XCFRAMEWORK_NAME-umbrella.h" "$XCFRAMEWORK_COMMON/Headers"
cp "$SWIFT_DIR/$XCFRAMEWORK_NAME.h" "$XCFRAMEWORK_COMMON/Headers"
# macOS x86_64 hardware
mkdir -p "$XCFRAMEWORK_ROOT/macos-x86_64"
cp -R "$XCFRAMEWORK_COMMON" "$XCFRAMEWORK_ROOT/macos-x86_64/$XCFRAMEWORK_NAME.framework"
cp "$TARGET_DIR/x86_64-apple-darwin/$BUILD_PROFILE/$STATIC_LIB_NAME" "$XCFRAMEWORK_ROOT/macos-x86_64/$XCFRAMEWORK_NAME.framework/$XCFRAMEWORK_NAME"
# iOS hardware
mkdir -p "$XCFRAMEWORK_ROOT/ios-arm64"
cp -R "$XCFRAMEWORK_COMMON" "$XCFRAMEWORK_ROOT/ios-arm64/$XCFRAMEWORK_NAME.framework"
cp "$TARGET_DIR/aarch64-apple-ios/$BUILD_PROFILE/$STATIC_LIB_NAME" "$XCFRAMEWORK_ROOT/ios-arm64/$XCFRAMEWORK_NAME.framework/$XCFRAMEWORK_NAME"
# iOS simulator, currently x86_64 only (need to make fat binary to add M1)
mkdir -p "$XCFRAMEWORK_ROOT/ios-arm64_x86_64-simulator"
cp -R "$XCFRAMEWORK_COMMON" "$XCFRAMEWORK_ROOT/ios-arm64_x86_64-simulator/$XCFRAMEWORK_NAME.framework"
cp "$TARGET_DIR/x86_64-apple-ios/$BUILD_PROFILE/$STATIC_LIB_NAME" "$XCFRAMEWORK_ROOT/ios-arm64_x86_64-simulator/$XCFRAMEWORK_NAME.framework/$XCFRAMEWORK_NAME"
# Set up the metadata for the XCFramework as a whole.
cp "$SWIFT_DIR/Info.plist" "$XCFRAMEWORK_ROOT/Info.plist"
# TODO add license info
# Remove common
rm -rf "$XCFRAMEWORK_ROOT/common"
# Zip it all up into a bundle for distribution.
zip -9 -r "$XCFRAMEWORK_ROOT.zip" "$XCFRAMEWORK_ROOT"
swift package compute-checksum bdkFFI.xcframework.zip
# Cleanup build ?
# rm -rf "$XCFRAMEWORK_ROOT"