Skip to content

Commit

Permalink
feat: quickstart in swift is now runnable
Browse files Browse the repository at this point in the history
  • Loading branch information
riverKanies committed Oct 30, 2024
1 parent 308fce9 commit 34876cc
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 42 deletions.
30 changes: 22 additions & 8 deletions docs/cookbook/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

This page provides an overview of how BDK can be leveraged to create and sync a wallet using an Esplora client. You can find [working code examples](https://github.com/bitcoindevkit/book-of-bdk/tree/master/examples) of this workflow in three programming languages: [Rust](https://github.com/bitcoindevkit/book-of-bdk/tree/master/examples/rust), [Swift](https://github.com/bitcoindevkit/book-of-bdk/tree/master/examples/swift), and [Kotlin](https://github.com/bitcoindevkit/book-of-bdk/tree/master/examples/kotlin).

Note that these examples are meant to be run at the command line. If you're building a mobile app in an IDE like xcode or androidstudio the process (of project creation, dependency selection and running the code) may be different.

!!! tip
This page is up-to-date with version `1.0.0-beta.5` of `bdk_wallet`.

Expand All @@ -16,7 +18,10 @@ This page provides an overview of how BDK can be leveraged to create and sync a

=== "Swift"

Create a new Swift project in Xcode.
```shell
swift package init --type executable
```
Or, if you're building an iOS app, create a new Swift project in Xcode.

=== "Kotlin"

Expand All @@ -32,8 +37,14 @@ This page provides an overview of how BDK can be leveraged to create and sync a

=== "Swift"

```toml title="Package.swift"
--8<-- "examples/swift/quickstart/Package.swift"
```
Or, if you're building an iOS app:

1. From the Xcode File menu, select Add Package Dependencies...
2. Enter `https://github.com/bitcoindevkit/bdk-swift` into the package repository URL text field
2. Enter `https://github.com/bitcoindevkit/bdk-swift` into the package repository URL search field and bdk-swift should come up
3. For the Dependency Rule select `Exact Version`, enter the version number (same as Package.swift) and click Add Package

=== "Kotlin"

Expand All @@ -52,8 +63,8 @@ We'll give a breakdown of the key pieces of this code in the next section.

=== "Swift"

```swift title="examples/swift/quickstart/main.swift"
--8<-- "examples/swift/quickstart/main.swift:file"
```swift title="examples/swift/quickstart/Sources/main.swift"
--8<-- "examples/swift/quickstart/Sources/main.swift:file"
```

=== "Kotlin"
Expand All @@ -75,7 +86,10 @@ The wallet will take a few seconds to sync, then you should see the wallet balan

=== "Swift"

Run the project in Xcode.
```shell
swift run
```
Or run the project in Xcode.

=== "Kotlin"

Expand All @@ -96,7 +110,7 @@ First we need some <a href="https://github.com/bitcoin/bitcoin/blob/master/doc/d
=== "Swift"

```swift
--8<-- "examples/swift/quickstart/main.swift:descriptors"
--8<-- "examples/swift/quickstart/Sources/main.swift:descriptors"
```

=== "Kotlin"
Expand All @@ -119,7 +133,7 @@ This example is using an <a href="https://github.com/Blockstream/esplora" target
=== "Swift"

```swift
--8<-- "examples/swift/quickstart/main.swift:client"
--8<-- "examples/swift/quickstart/Sources/main.swift:client"
```

=== "Kotlin"
Expand All @@ -145,7 +159,7 @@ Once we have our wallet setup and connected to the network, we scan the network
=== "Swift"

```swift
--8<-- "examples/swift/quickstart/main.swift:scan"
--8<-- "examples/swift/quickstart/Sources/main.swift:scan"
```

=== "Kotlin"
Expand Down
8 changes: 8 additions & 0 deletions examples/swift/quickstart/.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
15 changes: 15 additions & 0 deletions examples/swift/quickstart/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"originHash" : "80a7f511dc9201d59f1672d17a6a92dca3d40856c08e16f03e8087f59734809a",
"pins" : [
{
"identity" : "bdk-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/bitcoindevkit/bdk-swift",
"state" : {
"revision" : "875877e5c5eb753df2673958ee2f54a5360581f7",
"version" : "1.0.0-beta.5"
}
}
],
"version" : 3
}
22 changes: 22 additions & 0 deletions examples/swift/quickstart/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// swift-tools-version: 5.10

import PackageDescription

let package = Package(
name: "QuickstartExample",
platforms: [
.macOS(.v12)
],
dependencies: [
.package(url: "https://github.com/bitcoindevkit/bdk-swift", from: "1.0.0-beta.5")
],
targets: [
.executableTarget(
name: "QuickstartExample",
dependencies: [
.product(name: "BitcoinDevKit", package: "bdk-swift")
],
path: "Sources"
)
]
)
34 changes: 34 additions & 0 deletions examples/swift/quickstart/Sources/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// --8<-- [start:file]
import BitcoinDevKit

// --8<-- [start:descriptors]
let descriptor = try Descriptor(descriptor: "tr([12071a7c/86'/1'/0']tpubDCaLkqfh67Qr7ZuRrUNrCYQ54sMjHfsJ4yQSGb3aBr1yqt3yXpamRBUwnGSnyNnxQYu7rqeBiPfw3mjBcFNX4ky2vhjj9bDrGstkfUbLB9T/0/*)#z3x5097m", network: Network.signet)
let changeDescriptor = try Descriptor(descriptor: "tr([12071a7c/86'/1'/0']tpubDCaLkqfh67Qr7ZuRrUNrCYQ54sMjHfsJ4yQSGb3aBr1yqt3yXpamRBUwnGSnyNnxQYu7rqeBiPfw3mjBcFNX4ky2vhjj9bDrGstkfUbLB9T/1/*)#n9r4jswr", network: Network.signet)
// --8<-- [end:descriptors]

let wallet = try Wallet(
descriptor: descriptor,
changeDescriptor: changeDescriptor,
network: Network.signet,
connection: Connection.newInMemory()
)

let addressInfo = wallet.revealNextAddress(keychain: .external)
print("Generated address \(addressInfo.address) at index \(addressInfo.index)")

// --8<-- [start:client]
let esploraClient = EsploraClient(url: "https://mutinynet.com/api")
// --8<-- [end:client]

// --8<-- [start:scan]
let syncRequest = try wallet.startSyncWithRevealedSpks().build()
let update = try esploraClient.sync(
syncRequest: syncRequest,
parallelRequests: UInt64(5)
)
try wallet.applyUpdate(update: update)
let balance = wallet.balance()
print("Wallet balance: \(balance.total.toSat()) sat")
// --8<-- [end:scan]

// --8<-- [end:file]
34 changes: 0 additions & 34 deletions examples/swift/quickstart/main.swift

This file was deleted.

0 comments on commit 34876cc

Please sign in to comment.