bdgwallet / bdkmanager-swift

BDKManager for iOS / Swift
9 stars 2 forks source link

Minimal SwiftUI additions to bdk-swift #9

Open danielnordh opened 1 year ago

danielnordh commented 1 year ago

Instead of creating a 'fat' BDKManager class, what is the most minimal version of SwiftUI syntactic sugar we could add to BitcoinDevKit itself? This could either be done directly in the only file at the moment, or as a separate file for clarity.

What does every SwiftUI wallet developer need?

To support these, you need to create / keep references to:

network
databaseConfig
blockchain
danielnordh commented 1 year ago

To create a Wallet, you need:

let databaseconfig ...
let network ...
let descriptor ...
// to init
let wallet = try Wallet.init(descriptor: descriptor, changeDescriptor: changeDescriptor, network: network, databaseConfig: databaseConfig)

To get balance (should happen after every sync):

self.wallet!.getBalance()

To get transactions (should happen after every sync):

self.wallet!.listTransactions()

To sync:

let url = ...
let electrumConfig = ElectrumConfig(url: url!, socks5: nil, retry: ELECTRUM_RETRY, timeout: nil, stopGap: ELECTRUM_STOPGAP)
let blockchainConfig = BlockchainConfig.electrum(config: electrumConfig)
let blockchain = try Blockchain(config: blockchainConfig)
// to sync
try wallet.sync(blockchain: blockchain, progress: nil)

To send:

let psbt = try TxBuilder().addRecipient(address: recipient, amount: amount).feeRate(satPerVbyte: feeRate).finish(wallet: self.wallet!)
let success = try self.wallet!.sign(psbt: psbt)
let blockchainConfig = self.blockchainConfig(network: self.network, syncSource: self.syncSource)
let blockchain = try Blockchain(config: blockchainConfig)
try blockchain.broadcast(psbt: psbt)
danielnordh commented 1 year ago

Proposed minimal interface:

public class BDKManager: ObservableObject {
    @Published public var wallet: Wallet
    @Published public var balance: Balance?
    @Published public var transactions: [TransactionDetails]?

    private var blockchain: Blockchain

    // Sync the loaded wallet once
    public func sync() {
       ... (also updates Balance and Transactions)
    }

    // Send an amount of bitcoin (in sats) to a recipient, optional feeRate
    public func sendBitcoin(recipient: String, amount: UInt64, feeRate: Float) -> Bool {
       ...
    }
}
danielnordh commented 1 year ago

Or should this not be part of bdk-swift at all, maybe just be a reference class?

RandyMcMillan commented 1 year ago

👀

danielnordh commented 1 year ago

I have moved this discussion to bdk-ffi: https://github.com/bitcoindevkit/bdk-ffi/issues/266