Skip to content

Commit e2cc318

Browse files
committed
Three new posts: Configuration in Swift, PSBT and Testnet 4
1 parent b0b7072 commit e2cc318

File tree

3 files changed

+98
-6
lines changed

3 files changed

+98
-6
lines changed

.swiftpm/xcode/xcshareddata/xcschemes/SwiftBitcoin.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
3737
launchStyle = "0"
3838
useCustomWorkingDirectory = "YES"
39-
customWorkingDirectory = "/Users/d/Developer/CraigWrong/swift-bitcoin.github.io"
39+
customWorkingDirectory = "/Users/d/Developer/CraigWrong/swiftbitcoin.org"
4040
ignoresPersistentStateOnLaunch = "NO"
4141
debugDocumentVersioning = "YES"
4242
debugServiceExtension = "internal"

src/content/post/posts2025.swift

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,92 @@
1-
let post11 = Post("/post/2025-03-27-psbt", "Partially Signed Bitcoin Transactions", "2025-03-27T12:00:00Z", .specification) { """
1+
let post12 = Post("/post/2025-06-17-start-testnet4-effort", "Start of Testnet 4 Compatibility Effort", "2025-06-17T12:00:00Z", .announcements) { """
2+
Testnet compatibility is a mandatory milestone for any piece of Bitcoin-related software aiming to eventually reach production stage.
23
3-
One popular request for Swift Bitcoin is to support the Partially Signed Bitcoin Transactions (PSBT) format defined in BIP174 et al. Making PSBT flows available early on is great not only for users but as a trial for the entire framework.
4+
It's also great timing that the old testnet 3 network is being decommissioned in favor of a more robust version 4.
45
5-
The PSBT version 0 and 2 specifications cover pretty much all standard interactions with Bitcoin transactions from when they are first created and funded, until they are broadcasted to the network.
6+
As of the start of this week the primary objective of the Swift Bitcoin project will be to support Testnet 4. This includes connecting to nodes and sync'ing the blockchain, as well as compatibility with all preexisting functionality like wallet and PSBT.
7+
8+
The completion of this phase will likely justify the release of the first beta. We have set a deadline of 6 months to try to get there with as much quality as possible.
9+
10+
Stay tuned for more announcements regarding this effort, coming soon.
11+
""" }
12+
13+
let post11 = Post("/post/2025-06-13-psbt", "Partially Signed Bitcoin Transactions", "2025-06-13T12:00:00Z", .specification) { """
14+
15+
PSBT support as defined by BIP174 is an important feature to have for any Bitcoin SDK. It serves as a compatibility layer between different software implementations as well as dedicated hardware devices such as wallets and signers.
16+
17+
While we hesitated at first to include this feature before Swift Bitcoin 0.1.0 is even out, a consideration was made that it would put the entire framework to the test – in a good way.
18+
19+
Given that PSBTs cover the entire lifecycle of a Bitcoin transaction it serves as validation that creation and signing flows can be built on top of the wallet API.
20+
21+
So far most of the effort was concentrated in passing the BIP's official test vectors. This ensures correct parsing and serialization as well as verifying preconditions for each role: creator, updater, signer, combiner, finalizer and extractor.
22+
23+
There's still room for improvement – specially on the ergonomics of the public interface – but the main functionality is already present.
24+
25+
```swift
26+
import BitcoinPSBT
27+
28+
// Creator
29+
// `tx` is a multisig transaction with with P2SH and P2SH-P2WSH inputs and 2 outputs.
30+
var psbt = try PartiallySignedTx(tx) // Create from unsigned transaction
31+
let psbtData = psbt.data // Serialize to pass it around
32+
33+
// Updater 1
34+
psbt = try PartiallySignedTx(psbtData) // Parse from serialized format
35+
psbt.update(input: 0, fund1)
36+
psbt.update(input: 0, redeemScript: redeem0)
37+
psbt.update(input: 0, pubkey0, path0)
38+
psbt.update(input: 0, pubkey1, path1)
39+
40+
psbt.update(input: 1, fund0.outs[1])
41+
psbt.update(input: 1, redeemScript: redeem1)
42+
psbt.update(input: 1, witnessScript: witness)
43+
psbt.update(input: 1, pubkey2, path2)
44+
psbt.update(input: 1, pubkey3, path3)
45+
46+
psbt.update(out: 0, pubkey4, path4)
47+
psbt.update(out: 1, pubkey5, path5)
48+
49+
let psbtData1 = psbt.data
50+
51+
// Second updater
52+
psbt = try PartiallySignedTx(psbtData1)
53+
psbt.update(input: 0, SighashType.all)
54+
psbt.update(input: 1, SighashType.all)
55+
let psbtData = psbt.data2
56+
57+
// Signer
58+
psbt = try PartiallySignedTx(psbtData2)
59+
try psbt.sign(input: 0, using: secretKey0)
60+
try psbt.sign(input: 1, using: secretKey1)
61+
let psbtData3 = psbt.data
62+
63+
// Second signer
64+
psbt = try PartiallySignedTx(psbtData3)
65+
try psbt.sign(input: 0, using: secretKey0)
66+
try psbt.sign(input: 1, using: secretKey1)
67+
let psbtData4 = psbt.data
68+
69+
// Combiner
70+
psbt = try PartiallySignedTx(psbtData4)
71+
psbt.combine(with: psbt2)
72+
let psbtData5 = psbt.data
73+
74+
// Finalizer
75+
psbt = try PartiallySignedTx(psbtData5)
76+
psbt.finalize()
77+
let psbtData6 = psbt.data
78+
79+
// Extractor
80+
psbt = try PartiallySignedTx(psbtData6)
81+
let tx = psbt.extractTx()
82+
83+
// Assuming we have a configured node.
84+
85+
node.sendTransaction(tx) // Broadcast signed transaction
86+
```
687
""" }
788

8-
let post10 = Post("/post/2025-03-24-node-config-swift", "Node Configuration in Swift", "2025-03-24T12:00:00Z", .implementation) { """
89+
let post10 = Post("/post/2025-04-24-node-config-swift", "Node Configuration in Swift", "2025-04-24T12:00:00Z", .implementation) { """
990
1091
A cool feature of the Swift Package Manager (SPM) is how its package manifest format is itself part of the Swift Language. This is easier before compilation when the project's source code and the Swift compiler are at hand but can that approach be extended for compiled tools like `bcnode`?
1192
@@ -14,6 +95,17 @@ We accepted the challenge as it would be fitting for our product to have its con
1495
Actually why not make the JSON format precisely match the serialization of the Swift Bitcoin configuration format in Swift? This way we can decode a JSON configuration and get the internal representation which we can work with directly in our source code. Conversely, if we take a `config.swift` file and serialize the object defined inside, we will end up with the JSON version of the same parameters.
1596
1697
So how can we achieve this versatility? Swift can act as both a compiled or an interpreted language which definitely can come in handy. One thing it cannot do though is evaluate Swift code from within a compiled program so we need to be creative. Our solution involves reading the configuration file containing a variable declaration. We will prepend that declaration with the necessary type definition.
98+
99+
The end result is a pure Swift configuration file that could look like this:
100+
101+
```swift
102+
let feeRate = 100
103+
let config = NodeConfig(
104+
name: "My Node with fee rate of \\(feeRate)",
105+
feeRate: feeRate
106+
107+
}
108+
```
17109
""" }
18110

19111
let post09 = Post("/post/2025-03-21-miniscript-dsl", "Miniscript DSL", "2025-03-21T12:00:00Z", .specification) { """

src/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import SwiftySites
33
let posts = [
44
post01, post02, post03,
55
post04,
6-
post05, post06, post07, post08, post09 /*, post10, post11*/
6+
post05, post06, post07, post08, post09, post10, post11, post12
77
]
88

99
let site = Site(

0 commit comments

Comments
 (0)