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