|
| 1 | +// Unit tests for the repository-boundary guarantees of |
| 2 | +// `BitcoinWalletRepository.buildPsbt`: |
| 3 | +// |
| 4 | +// 1. D7 defense in depth — "a frozen coin must never be spendable". |
| 5 | +// BDK's documented semantics are the opposite: a manually added utxo |
| 6 | +// (`TxBuilder.addUtxos`) overrides the `unspendable` filter, and |
| 7 | +// `BdkWalletDatasource` deliberately preserves those raw semantics |
| 8 | +// (see bdk_wallet_datasource_test.dart, which asserts them). The |
| 9 | +// repository must therefore strip any selected coin that is also in |
| 10 | +// the unspendable set before calling down, so the app-level invariant |
| 11 | +// holds for ANY caller — not only those going through |
| 12 | +// PrepareBitcoinSendUsecase's own frozen-set stripping. |
| 13 | +// |
| 14 | +// 2. RBF defaults to ENABLED when the caller omits the flag. The |
| 15 | +// previous `replaceByFee ?? false` was harmless while the datasource's |
| 16 | +// `setExactSequence` call discarded its result (a silent no-op), but |
| 17 | +// became wrong once that call was fixed: a null flag would have |
| 18 | +// started disabling RBF by default, diverging from both the |
| 19 | +// datasource's own default and BDK's default sequence (0xFFFFFFFD). |
| 20 | +import 'dart:typed_data'; |
| 21 | + |
| 22 | +import 'package:bb_mobile/core/fees/domain/fees_entity.dart'; |
| 23 | +import 'package:bb_mobile/core/seed/data/datasources/seed_datasource.dart'; |
| 24 | +import 'package:bb_mobile/core/storage/tables/wallet_metadata_table.dart'; |
| 25 | +import 'package:bb_mobile/core/wallet/data/datasources/bdk_wallet_datasource.dart'; |
| 26 | +import 'package:bb_mobile/core/wallet/data/datasources/wallet_metadata_datasource.dart'; |
| 27 | +import 'package:bb_mobile/core/wallet/data/models/wallet_metadata_model.dart'; |
| 28 | +import 'package:bb_mobile/core/wallet/data/models/wallet_model.dart'; |
| 29 | +import 'package:bb_mobile/core/wallet/data/models/wallet_utxo_model.dart'; |
| 30 | +import 'package:bb_mobile/core/wallet/data/repositories/bitcoin_wallet_repository.dart'; |
| 31 | +import 'package:bb_mobile/core/wallet/domain/entities/wallet_utxo.dart'; |
| 32 | +import 'package:flutter_test/flutter_test.dart'; |
| 33 | +import 'package:mocktail/mocktail.dart'; |
| 34 | + |
| 35 | +class _MockWalletMetadataDatasource extends Mock |
| 36 | + implements WalletMetadataDatasource {} |
| 37 | + |
| 38 | +class _MockSeedDatasource extends Mock implements SeedDatasource {} |
| 39 | + |
| 40 | +class _MockBdkWalletDatasource extends Mock implements BdkWalletDatasource {} |
| 41 | + |
| 42 | +// Encodes as a bitcoin testnet BIP84 origin so |
| 43 | +// WalletMetadataModelExtension.isBitcoin decodes to true. |
| 44 | +const _walletId = 'wpkh([73c5da0a/84h/1h/0h])'; |
| 45 | + |
| 46 | +WalletUtxo _utxo({required String txId, required int vout}) => |
| 47 | + WalletUtxo.bitcoin( |
| 48 | + walletId: _walletId, |
| 49 | + txId: txId, |
| 50 | + vout: vout, |
| 51 | + scriptPubkey: Uint8List(0), |
| 52 | + amountSat: BigInt.from(100000), |
| 53 | + address: 'tb1-test-address', |
| 54 | + ); |
| 55 | + |
| 56 | +void main() { |
| 57 | + late _MockWalletMetadataDatasource metadataDatasource; |
| 58 | + late _MockBdkWalletDatasource bdkDatasource; |
| 59 | + late BitcoinWalletRepository repository; |
| 60 | + |
| 61 | + const metadata = WalletMetadataModel( |
| 62 | + id: _walletId, |
| 63 | + masterFingerprint: '73c5da0a', |
| 64 | + xpubFingerprint: 'deadbeef', |
| 65 | + isEncryptedVaultTested: false, |
| 66 | + isPhysicalBackupTested: false, |
| 67 | + xpub: 'tpub-test', |
| 68 | + externalPublicDescriptor: 'wpkh(external)', |
| 69 | + internalPublicDescriptor: 'wpkh(internal)', |
| 70 | + signer: Signer.local, |
| 71 | + isDefault: true, |
| 72 | + ); |
| 73 | + |
| 74 | + setUpAll(() { |
| 75 | + registerFallbackValue( |
| 76 | + const WalletModel.publicBdk( |
| 77 | + id: _walletId, |
| 78 | + externalDescriptor: 'wpkh(external)', |
| 79 | + internalDescriptor: 'wpkh(internal)', |
| 80 | + isTestnet: true, |
| 81 | + ), |
| 82 | + ); |
| 83 | + registerFallbackValue(const NetworkFee.relativeSatPerKwu(1000)); |
| 84 | + }); |
| 85 | + |
| 86 | + setUp(() { |
| 87 | + metadataDatasource = _MockWalletMetadataDatasource(); |
| 88 | + bdkDatasource = _MockBdkWalletDatasource(); |
| 89 | + repository = BitcoinWalletRepository( |
| 90 | + walletMetadataDatasource: metadataDatasource, |
| 91 | + seedDatasource: _MockSeedDatasource(), |
| 92 | + bdkWalletDatasource: bdkDatasource, |
| 93 | + ); |
| 94 | + |
| 95 | + when( |
| 96 | + () => metadataDatasource.fetch(_walletId), |
| 97 | + ).thenAnswer((_) async => metadata); |
| 98 | + when( |
| 99 | + () => bdkDatasource.buildPsbt( |
| 100 | + wallet: any(named: 'wallet'), |
| 101 | + address: any(named: 'address'), |
| 102 | + amountSat: any(named: 'amountSat'), |
| 103 | + networkFee: any(named: 'networkFee'), |
| 104 | + drain: any(named: 'drain'), |
| 105 | + unspendable: any(named: 'unspendable'), |
| 106 | + selected: any(named: 'selected'), |
| 107 | + replaceByFee: any(named: 'replaceByFee'), |
| 108 | + ), |
| 109 | + ).thenAnswer((_) async => 'psbt'); |
| 110 | + }); |
| 111 | + |
| 112 | + List<WalletUtxoModel>? capturedSelected() => |
| 113 | + verify( |
| 114 | + () => bdkDatasource.buildPsbt( |
| 115 | + wallet: any(named: 'wallet'), |
| 116 | + address: any(named: 'address'), |
| 117 | + amountSat: any(named: 'amountSat'), |
| 118 | + networkFee: any(named: 'networkFee'), |
| 119 | + drain: any(named: 'drain'), |
| 120 | + unspendable: any(named: 'unspendable'), |
| 121 | + selected: captureAny(named: 'selected'), |
| 122 | + replaceByFee: any(named: 'replaceByFee'), |
| 123 | + ), |
| 124 | + ).captured.single |
| 125 | + as List<WalletUtxoModel>?; |
| 126 | + |
| 127 | + bool capturedReplaceByFee() => |
| 128 | + verify( |
| 129 | + () => bdkDatasource.buildPsbt( |
| 130 | + wallet: any(named: 'wallet'), |
| 131 | + address: any(named: 'address'), |
| 132 | + amountSat: any(named: 'amountSat'), |
| 133 | + networkFee: any(named: 'networkFee'), |
| 134 | + drain: any(named: 'drain'), |
| 135 | + unspendable: any(named: 'unspendable'), |
| 136 | + selected: any(named: 'selected'), |
| 137 | + replaceByFee: captureAny(named: 'replaceByFee'), |
| 138 | + ), |
| 139 | + ).captured.single |
| 140 | + as bool; |
| 141 | + |
| 142 | + group('D7 defense in depth — selected ∩ unspendable is stripped', () { |
| 143 | + test( |
| 144 | + 'a selected coin that is also unspendable never reaches the datasource', |
| 145 | + () async { |
| 146 | + await repository.buildPsbt( |
| 147 | + walletId: _walletId, |
| 148 | + address: 'tb1-destination', |
| 149 | + amountSat: 25000, |
| 150 | + networkFee: const NetworkFee.relativeSatPerKwu(1000), |
| 151 | + unspendable: const [(txId: 'tx-frozen', vout: 0)], |
| 152 | + selected: [ |
| 153 | + _utxo(txId: 'tx-frozen', vout: 0), // must be stripped |
| 154 | + _utxo(txId: 'tx-free', vout: 1), // must pass through |
| 155 | + ], |
| 156 | + ); |
| 157 | + |
| 158 | + final selected = capturedSelected(); |
| 159 | + expect(selected, hasLength(1)); |
| 160 | + expect(selected!.single.txId, 'tx-free'); |
| 161 | + expect(selected.single.vout, 1); |
| 162 | + }, |
| 163 | + ); |
| 164 | + |
| 165 | + test('same txId but different vout is NOT stripped', () async { |
| 166 | + await repository.buildPsbt( |
| 167 | + walletId: _walletId, |
| 168 | + address: 'tb1-destination', |
| 169 | + amountSat: 25000, |
| 170 | + networkFee: const NetworkFee.relativeSatPerKwu(1000), |
| 171 | + unspendable: const [(txId: 'tx-shared', vout: 0)], |
| 172 | + selected: [_utxo(txId: 'tx-shared', vout: 1)], |
| 173 | + ); |
| 174 | + |
| 175 | + final selected = capturedSelected(); |
| 176 | + expect(selected, hasLength(1)); |
| 177 | + expect(selected!.single.vout, 1); |
| 178 | + }); |
| 179 | + |
| 180 | + test('no unspendable set leaves the selection untouched', () async { |
| 181 | + await repository.buildPsbt( |
| 182 | + walletId: _walletId, |
| 183 | + address: 'tb1-destination', |
| 184 | + amountSat: 25000, |
| 185 | + networkFee: const NetworkFee.relativeSatPerKwu(1000), |
| 186 | + selected: [ |
| 187 | + _utxo(txId: 'tx-a', vout: 0), |
| 188 | + _utxo(txId: 'tx-b', vout: 1), |
| 189 | + ], |
| 190 | + ); |
| 191 | + |
| 192 | + expect(capturedSelected(), hasLength(2)); |
| 193 | + }); |
| 194 | + }); |
| 195 | + |
| 196 | + group('replaceByFee default', () { |
| 197 | + test('omitted flag defaults to RBF ENABLED (true)', () async { |
| 198 | + await repository.buildPsbt( |
| 199 | + walletId: _walletId, |
| 200 | + address: 'tb1-destination', |
| 201 | + amountSat: 25000, |
| 202 | + networkFee: const NetworkFee.relativeSatPerKwu(1000), |
| 203 | + ); |
| 204 | + |
| 205 | + expect(capturedReplaceByFee(), isTrue); |
| 206 | + }); |
| 207 | + |
| 208 | + test('explicit false is forwarded unchanged', () async { |
| 209 | + await repository.buildPsbt( |
| 210 | + walletId: _walletId, |
| 211 | + address: 'tb1-destination', |
| 212 | + amountSat: 25000, |
| 213 | + networkFee: const NetworkFee.relativeSatPerKwu(1000), |
| 214 | + replaceByFee: false, |
| 215 | + ); |
| 216 | + |
| 217 | + expect(capturedReplaceByFee(), isFalse); |
| 218 | + }); |
| 219 | + }); |
| 220 | +} |
0 commit comments