Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimal SwiftUI additions to bdk-swift #9

Open
danielnordh opened this issue Nov 29, 2022 · 5 comments
Open

Minimal SwiftUI additions to bdk-swift #9

danielnordh opened this issue Nov 29, 2022 · 5 comments

Comments

@danielnordh
Copy link
Contributor

danielnordh commented Nov 29, 2022

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?

  • @wallet
  • @balance (don't think wallet.getBalance will be enough to automatically update balance)
  • @Transactions (ideally with all required info, to / from etc)
  • Sync (once and continuously)
  • Send (needs blockchainconfig?)

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

network
databaseConfig
blockchain
@danielnordh
Copy link
Contributor Author

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
Copy link
Contributor Author

danielnordh commented Nov 29, 2022

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
Copy link
Contributor Author

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

@RandyMcMillan
Copy link

👀

@danielnordh
Copy link
Contributor Author

I have moved this discussion to bdk-ffi: bitcoindevkit/bdk-ffi#266

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants