From 3bba3ccd531447cdd6b46f71e9a82ba362e0e21b Mon Sep 17 00:00:00 2001 From: DanGould Date: Wed, 18 Dec 2024 18:07:29 -0500 Subject: [PATCH] BROADCAST PAYJOIN BABY --- lib/_pkg/payjoin/manager.dart | 4 ++-- lib/_pkg/wallet/bdk/sensitive_create.dart | 16 ++++++++++++--- lib/_pkg/wallet/bdk/transaction.dart | 9 +++++++-- lib/_pkg/wallet/transaction.dart | 24 ++++++++++++++++++++++- 4 files changed, 45 insertions(+), 8 deletions(-) diff --git a/lib/_pkg/payjoin/manager.dart b/lib/_pkg/payjoin/manager.dart index 49497c76..73dc9052 100644 --- a/lib/_pkg/payjoin/manager.dart +++ b/lib/_pkg/payjoin/manager.dart @@ -235,9 +235,9 @@ class PayjoinManager { return; } final signedPayjoin = result!.$1; - final broadcastedTx = (await blockchain(isTestnet)) + await (await blockchain(isTestnet)) .broadcast(transaction: signedPayjoin); - print('Broadcasted transaction: $broadcastedTx'); + print('Broadcasted transaction: $signedPayjoin'); await _cleanupSession(sessionId); } else if (message is Err) { print('err: $message'); diff --git a/lib/_pkg/wallet/bdk/sensitive_create.dart b/lib/_pkg/wallet/bdk/sensitive_create.dart index 2cedba6f..8dbe7e62 100644 --- a/lib/_pkg/wallet/bdk/sensitive_create.dart +++ b/lib/_pkg/wallet/bdk/sensitive_create.dart @@ -386,12 +386,24 @@ class BDKSensitiveCreate { Future<(bdk.Wallet?, Err?)> loadPrivateBdkWallet( Wallet wallet, Seed seed, + ) async { + return await loadPrivateBdkWalletWithDb( + wallet, + seed, + const bdk.DatabaseConfig.memory(), + ); + } + + Future<(bdk.Wallet?, Err?)> loadPrivateBdkWalletWithDb( + Wallet wallet, + Seed seed, + bdk.DatabaseConfig dbConfig, ) async { try { final network = wallet.network == BBNetwork.Testnet ? bdk.Network.testnet : bdk.Network.bitcoin; - + print('network: $network'); final mn = await bdk.Mnemonic.fromString(seed.mnemonic); final pp = wallet.hasPassphrase() ? seed.passphrases.firstWhere( @@ -449,8 +461,6 @@ class BDKSensitiveCreate { ); } - const dbConfig = bdk.DatabaseConfig.memory(); - final bdkWallet = await bdk.Wallet.create( descriptor: external, changeDescriptor: internal, diff --git a/lib/_pkg/wallet/bdk/transaction.dart b/lib/_pkg/wallet/bdk/transaction.dart index d724822b..6c7aa016 100644 --- a/lib/_pkg/wallet/bdk/transaction.dart +++ b/lib/_pkg/wallet/bdk/transaction.dart @@ -593,7 +593,7 @@ class BDKTransactions { // required String address, }) async { try { - print('signTx psbt: $psbt'); + printWrapped('signTx psbt: $psbt'); final psbtStruct = await bdk.PartiallySignedTransaction.fromString(psbt); final tx = psbtStruct.extractTx(); final _ = await bdkWallet.sign( @@ -610,7 +610,7 @@ class BDKTransactions { ); // final extracted = await finalized; final psbtStr = psbtStruct.serialize(); - print('signTx psbtStr: ${base64Encode(psbtStr)}'); + printWrapped('signTx psbtStr: ${base64Encode(psbtStr)}'); return ((tx, base64Encode(psbtStr)), null); } on Exception catch (e) { return ( @@ -854,3 +854,8 @@ class BDKTransactions { return false; } } + +void printWrapped(String text) { + final pattern = new RegExp('.{1,800}'); // 800 is the size of each chunk + pattern.allMatches(text).forEach((match) => print(match.group(0))); +} diff --git a/lib/_pkg/wallet/transaction.dart b/lib/_pkg/wallet/transaction.dart index 78bb7fdd..84cbdb5e 100644 --- a/lib/_pkg/wallet/transaction.dart +++ b/lib/_pkg/wallet/transaction.dart @@ -16,6 +16,7 @@ import 'package:bb_mobile/_pkg/wallet/repository/sensitive_storage.dart'; import 'package:bb_mobile/_pkg/wallet/repository/wallets.dart'; import 'package:bb_mobile/_pkg/wallet/update.dart'; import 'package:bdk_flutter/bdk_flutter.dart' as bdk; +import 'package:path_provider/path_provider.dart'; class WalletTx implements IWalletTransactions { WalletTx({ @@ -169,6 +170,9 @@ class WalletTx implements IWalletTransactions { final (bdkSignerWallet, errSigner) = await _bdkSensitiveCreate.loadPrivateBdkWallet(wallet, seed!); if (errSigner != null) throw errSigner; + final signerTxs = + await bdkSignerWallet!.listTransactions(includeRaw: false); + print('signer txs: ${signerTxs.length}'); final (signed, errSign) = await _bdkTransactions.signTx( psbt: psbt, bdkWallet: bdkSignerWallet!, @@ -235,13 +239,31 @@ class WalletTx implements IWalletTransactions { try { final (bdkWallet, errWallet) = _walletsRepository.getBdkWallet(wallet.id); if (errWallet != null) throw errWallet; + final walletTxs = await bdkWallet!.listTransactions(includeRaw: false); + print('wallet txs: ${walletTxs.length}'); final (seed, errSeed) = await _walletSensitiveStorageRepository.readSeed( fingerprintIndex: wallet.getRelatedSeedStorageString(), ); if (errSeed != null) throw errSeed; + final appDocDir = await getApplicationDocumentsDirectory(); + final String dbDir = + '${appDocDir.path}/${wallet.getWalletStorageString()}'; + + final dbConfig = bdk.DatabaseConfig.sqlite( + config: bdk.SqliteDbConfiguration(path: dbDir), + ); final (bdkSignerWallet, errSigner) = - await _bdkSensitiveCreate.loadPrivateBdkWallet(wallet, seed!); + await _bdkSensitiveCreate.loadPrivateBdkWalletWithDb( + wallet, + seed!, + dbConfig, + ); + // final (blockchain, errNetwork) = _networkRepository.bdkBlockchain; + // await bdkSignerWallet!.sync(blockchain: blockchain!); if (errSigner != null) throw errSigner; + final signerTxs = + await bdkSignerWallet!.listTransactions(includeRaw: false); + print('signer txs: ${signerTxs.length}'); return await _bdkTransactions.signTx( psbt: psbt, bdkWallet: bdkSignerWallet!,