Swift Bitcoin – Full node library

Apr 24, 2025 • Implementation Details

Node Configuration in Swift

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?

We accepted the challenge as it would be fitting for our product to have its configuration optionally specified in Swift. This of course will only be possible if the Swift Runtime is installed on the user's system which is why we'll also allow JSON5 versions of the configuration file.

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.

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.

The end result is a pure Swift configuration file that could look like this:

let feeRate = 100
let config = NodeConfig(
    name: "My Node with fee rate of \(feeRate)",
    feeRate: feeRate
    …
}