|
| 1 | +import 'dart:typed_data'; |
| 2 | + |
| 3 | +import 'package:bb_mobile/core/bip85/data/bip85_datasource.dart'; |
| 4 | +import 'package:bb_mobile/core/bip85/data/bip85_repository.dart'; |
| 5 | +import 'package:bb_mobile/core/bip85/domain/derive_next_bip85_mnemonic_from_default_wallet_usecase.dart'; |
| 6 | +import 'package:bb_mobile/core/seed/data/repository/seed_repository.dart'; |
| 7 | +import 'package:bb_mobile/core/storage/sqlite_database.dart'; |
| 8 | +import 'package:bb_mobile/core/storage/tables/bip85_derivations_table.dart'; |
| 9 | +import 'package:bb_mobile/core/utils/bip32_derivation.dart'; |
| 10 | +import 'package:bb_mobile/core/wallet/data/repositories/wallet_repository.dart'; |
| 11 | +import 'package:bb_mobile/core/wallet/domain/entities/wallet.dart'; |
| 12 | +import 'package:bb_mobile/core/wallet/domain/usecases/create_default_wallets_usecase.dart'; |
| 13 | +import 'package:bb_mobile/locator.dart'; |
| 14 | +import 'package:bb_mobile/main.dart'; |
| 15 | +import 'package:bip39_mnemonic/bip39_mnemonic.dart' as bip39; |
| 16 | +import 'package:bip39_mnemonic/bip39_mnemonic.dart'; |
| 17 | +import 'package:bip85/bip85.dart' as bip85; |
| 18 | +import 'package:flutter_test/flutter_test.dart'; |
| 19 | + |
| 20 | +Future<void> main({bool isInitialized = false}) async { |
| 21 | + TestWidgetsFlutterBinding.ensureInitialized(); |
| 22 | + if (!isInitialized) await Bull.init(); |
| 23 | + |
| 24 | + final sqlite = locator<SqliteDatabase>(); |
| 25 | + final seedRepository = locator<SeedRepository>(); |
| 26 | + final walletRepository = locator<WalletRepository>(); |
| 27 | + final bip85Datasource = locator<Bip85Datasource>(); |
| 28 | + final bip85Repository = locator<Bip85Repository>(); |
| 29 | + final createDefaultWalletsUsecase = locator<CreateDefaultWalletsUsecase>(); |
| 30 | + |
| 31 | + final mnemonic = Mnemonic.fromWords( |
| 32 | + words: [ |
| 33 | + 'zoo', |
| 34 | + 'zoo', |
| 35 | + 'zoo', |
| 36 | + 'zoo', |
| 37 | + 'zoo', |
| 38 | + 'zoo', |
| 39 | + 'zoo', |
| 40 | + 'zoo', |
| 41 | + 'zoo', |
| 42 | + 'zoo', |
| 43 | + 'zoo', |
| 44 | + 'wrong', |
| 45 | + ], |
| 46 | + ); |
| 47 | + |
| 48 | + final usecase = DeriveNextBip85MnemonicFromDefaultWalletUsecase( |
| 49 | + bip85Repository: bip85Repository, |
| 50 | + walletRepository: walletRepository, |
| 51 | + seedRepository: seedRepository, |
| 52 | + ); |
| 53 | + |
| 54 | + setUpAll(() async { |
| 55 | + // Clear all relevant tables before each test |
| 56 | + await sqlite.managers.bip85Derivations.delete(); |
| 57 | + await sqlite.managers.walletMetadatas.delete(); |
| 58 | + |
| 59 | + await createDefaultWalletsUsecase.execute(mnemonicWords: mnemonic.words); |
| 60 | + }); |
| 61 | + |
| 62 | + setUp(() async { |
| 63 | + await sqlite.managers.bip85Derivations.delete(); |
| 64 | + }); |
| 65 | + |
| 66 | + group( |
| 67 | + 'DeriveNextBip85MnemonicFromDefaultWalletUsecase Integration Tests', |
| 68 | + () { |
| 69 | + test('one derivation with index 0 with empty table', () async { |
| 70 | + const length = bip39.MnemonicLength.words12; |
| 71 | + const index = 0; |
| 72 | + |
| 73 | + // Execute the usecase |
| 74 | + final result = await usecase.execute(length: length, alias: 'test'); |
| 75 | + |
| 76 | + // Verify the derivation path |
| 77 | + expect(result.derivation, "39'/0'/12'/0'"); |
| 78 | + expect(result.mnemonic, isA<bip39.Mnemonic>()); |
| 79 | + expect(result.mnemonic.length, equals(length)); |
| 80 | + |
| 81 | + // Verify the derivation was stored in the database |
| 82 | + final storedDerivations = await bip85Datasource.fetchAll(); |
| 83 | + expect(storedDerivations.length, 1); |
| 84 | + |
| 85 | + // Ensure index 0, alias and application are correct |
| 86 | + final firstDerivation = storedDerivations.first; |
| 87 | + expect(firstDerivation.index, index); |
| 88 | + expect(firstDerivation.alias, 'test'); |
| 89 | + expect(firstDerivation.application, Bip85ApplicationColumn.bip39); |
| 90 | + |
| 91 | + // Get the xprv from the seed |
| 92 | + final xprv = Bip32Derivation.getXprvFromSeed( |
| 93 | + Uint8List.fromList(mnemonic.seed), |
| 94 | + Network.bitcoinMainnet, |
| 95 | + ); |
| 96 | + |
| 97 | + // Now generate the same derivation using BIP85 library directly |
| 98 | + final directBip85Mnemonic = bip85.Bip85Entropy.deriveMnemonic( |
| 99 | + xprvBase58: xprv, |
| 100 | + language: bip39.Language.english, |
| 101 | + length: length, |
| 102 | + index: index, |
| 103 | + ); |
| 104 | + |
| 105 | + // Verify both results are identical |
| 106 | + expect(result.mnemonic.sentence, directBip85Mnemonic.sentence); |
| 107 | + }); |
| 108 | + |
| 109 | + test('Two derivations with index bump and different lengths', () async { |
| 110 | + // Execute the usecase first time |
| 111 | + final a = await usecase.execute( |
| 112 | + length: bip39.MnemonicLength.words12, |
| 113 | + alias: 'First derivation', |
| 114 | + ); |
| 115 | + |
| 116 | + // Execute the usecase second time |
| 117 | + final b = await usecase.execute( |
| 118 | + length: bip39.MnemonicLength.words24, |
| 119 | + alias: 'Second derivation', |
| 120 | + ); |
| 121 | + |
| 122 | + // Get the xprv from the seed |
| 123 | + final xprv = Bip32Derivation.getXprvFromSeed( |
| 124 | + Uint8List.fromList(mnemonic.seed), |
| 125 | + Network.bitcoinMainnet, |
| 126 | + ); |
| 127 | + |
| 128 | + // Verify database has both derivations stored |
| 129 | + final storedDerivations = await bip85Datasource.fetchAll(); |
| 130 | + expect(storedDerivations.length, equals(2)); |
| 131 | + |
| 132 | + final indices = storedDerivations.map((d) => d.index).toList()..sort(); |
| 133 | + expect(indices, equals([0, 1])); |
| 134 | + |
| 135 | + // Verify aliases are stored correctly |
| 136 | + final first = storedDerivations.firstWhere((d) => d.index == 0); |
| 137 | + expect(first.path, "39'/0'/12'/0'"); |
| 138 | + expect(first.index, 0); |
| 139 | + expect(first.alias, 'First derivation'); |
| 140 | + expect(first.application, equals(Bip85ApplicationColumn.bip39)); |
| 141 | + expect( |
| 142 | + a.mnemonic.sentence, |
| 143 | + bip85.Bip85Entropy.deriveMnemonic( |
| 144 | + xprvBase58: xprv, |
| 145 | + language: bip39.Language.english, |
| 146 | + length: bip39.MnemonicLength.words12, |
| 147 | + index: 0, |
| 148 | + ).sentence, |
| 149 | + ); |
| 150 | + |
| 151 | + final second = storedDerivations.firstWhere((d) => d.index == 1); |
| 152 | + expect(second.path, "39'/0'/24'/1'"); |
| 153 | + expect(second.index, 1); |
| 154 | + expect(second.alias, 'Second derivation'); |
| 155 | + expect(second.application, equals(Bip85ApplicationColumn.bip39)); |
| 156 | + expect( |
| 157 | + b.mnemonic.sentence, |
| 158 | + bip85.Bip85Entropy.deriveMnemonic( |
| 159 | + xprvBase58: xprv, |
| 160 | + language: bip39.Language.english, |
| 161 | + length: bip39.MnemonicLength.words24, |
| 162 | + index: 1, |
| 163 | + ).sentence, |
| 164 | + ); |
| 165 | + }); |
| 166 | + }, |
| 167 | + ); |
| 168 | +} |
0 commit comments