mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2024-12-23 12:09:43 +00:00
6b0170e69a
Conflicts: cw_bitcoin/lib/bitcoin_wallet.dart cw_bitcoin/lib/bitcoin_wallet_creation_credentials.dart cw_bitcoin/lib/electrum_wallet.dart cw_bitcoin/lib/litecoin_wallet.dart cw_bitcoin/pubspec.lock cw_monero/example/pubspec.lock lib/core/wallet_loading_service.dart lib/src/screens/restore/wallet_restore_from_seed_form.dart lib/view_model/wallet_restore_view_model.dart
169 lines
6.1 KiB
Dart
169 lines
6.1 KiB
Dart
import 'package:bitcoin_base/bitcoin_base.dart';
|
|
import 'package:cw_bitcoin/bitcoin_mnemonic.dart';
|
|
import 'package:cw_core/encryption_file_utils.dart';
|
|
import 'package:cw_core/crypto_currency.dart';
|
|
import 'package:cw_core/unspent_coins_info.dart';
|
|
import 'package:hive/hive.dart';
|
|
import 'package:mobx/mobx.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
|
|
import 'package:cw_bitcoin/electrum_wallet_snapshot.dart';
|
|
import 'package:cw_bitcoin/electrum_wallet.dart';
|
|
import 'package:cw_core/wallet_info.dart';
|
|
import 'package:cw_bitcoin/bitcoin_address_record.dart';
|
|
import 'package:cw_bitcoin/electrum_balance.dart';
|
|
import 'package:cw_bitcoin/bitcoin_wallet_addresses.dart';
|
|
import 'package:bip39/bip39.dart' as bip39;
|
|
|
|
part 'bitcoin_wallet.g.dart';
|
|
|
|
class BitcoinWallet = BitcoinWalletBase with _$BitcoinWallet;
|
|
|
|
abstract class BitcoinWalletBase extends ElectrumWallet with Store {
|
|
BitcoinWalletBase({
|
|
required String mnemonic,
|
|
required String password,
|
|
required WalletInfo walletInfo,
|
|
required Box<UnspentCoinsInfo> unspentCoinsInfo,
|
|
required Uint8List seedBytes,
|
|
required EncryptionFileUtils encryptionFileUtils,
|
|
String? addressPageType,
|
|
BasedUtxoNetwork? networkParam,
|
|
List<BitcoinAddressRecord>? initialAddresses,
|
|
ElectrumBalance? initialBalance,
|
|
Map<String, int>? initialRegularAddressIndex,
|
|
Map<String, int>? initialChangeAddressIndex,
|
|
String? passphrase,
|
|
}) : super(
|
|
mnemonic: mnemonic,
|
|
passphrase: passphrase,
|
|
password: password,
|
|
walletInfo: walletInfo,
|
|
unspentCoinsInfo: unspentCoinsInfo,
|
|
networkType: networkParam == null
|
|
? bitcoin.bitcoin
|
|
: networkParam == BitcoinNetwork.mainnet
|
|
? bitcoin.bitcoin
|
|
: bitcoin.testnet,
|
|
initialAddresses: initialAddresses,
|
|
initialBalance: initialBalance,
|
|
seedBytes: seedBytes,
|
|
currency: CryptoCurrency.btc,
|
|
encryptionFileUtils: encryptionFileUtils) {
|
|
// in a standard BIP44 wallet, mainHd derivation path = m/84'/0'/0'/0 (account 0, index unspecified here)
|
|
// the sideHd derivation path = m/84'/0'/0'/1 (account 1, index unspecified here)
|
|
String derivationPath = walletInfo.derivationInfo!.derivationPath!;
|
|
String sideDerivationPath = derivationPath.substring(0, derivationPath.length - 1) + "1";
|
|
final hd = bitcoin.HDWallet.fromSeed(seedBytes, network: networkType);
|
|
walletAddresses = BitcoinWalletAddresses(
|
|
walletInfo,
|
|
electrumClient: electrumClient,
|
|
initialAddresses: initialAddresses,
|
|
initialRegularAddressIndex: initialRegularAddressIndex,
|
|
initialChangeAddressIndex: initialChangeAddressIndex,
|
|
mainHd: hd.derivePath(derivationPath),
|
|
sideHd: hd.derivePath(sideDerivationPath),
|
|
network: networkParam ?? network,
|
|
);
|
|
autorun((_) {
|
|
this.walletAddresses.isEnabledAutoGenerateSubaddress = this.isEnabledAutoGenerateSubaddress;
|
|
});
|
|
}
|
|
|
|
static Future<BitcoinWallet> create({
|
|
required String mnemonic,
|
|
required String password,
|
|
required WalletInfo walletInfo,
|
|
required Box<UnspentCoinsInfo> unspentCoinsInfo,
|
|
required EncryptionFileUtils encryptionFileUtils,
|
|
String? passphrase,
|
|
String? addressPageType,
|
|
BasedUtxoNetwork? network,
|
|
List<BitcoinAddressRecord>? initialAddresses,
|
|
ElectrumBalance? initialBalance,
|
|
Map<String, int>? initialRegularAddressIndex,
|
|
Map<String, int>? initialChangeAddressIndex,
|
|
}) async {
|
|
late Uint8List seedBytes;
|
|
|
|
switch (walletInfo.derivationInfo?.derivationType) {
|
|
case DerivationType.bip39:
|
|
seedBytes = await bip39.mnemonicToSeed(
|
|
mnemonic,
|
|
passphrase: passphrase ?? "",
|
|
);
|
|
break;
|
|
case DerivationType.electrum:
|
|
default:
|
|
seedBytes = await mnemonicToSeedBytes(mnemonic);
|
|
break;
|
|
}
|
|
return BitcoinWallet(
|
|
mnemonic: mnemonic,
|
|
passphrase: passphrase ?? "",
|
|
password: password,
|
|
walletInfo: walletInfo,
|
|
unspentCoinsInfo: unspentCoinsInfo,
|
|
initialAddresses: initialAddresses,
|
|
initialBalance: initialBalance,
|
|
encryptionFileUtils: encryptionFileUtils,
|
|
seedBytes: seedBytes,
|
|
initialRegularAddressIndex: initialRegularAddressIndex,
|
|
initialChangeAddressIndex: initialChangeAddressIndex,
|
|
addressPageType: addressPageType,
|
|
networkParam: network,
|
|
);
|
|
}
|
|
|
|
static Future<BitcoinWallet> open({
|
|
required String name,
|
|
required WalletInfo walletInfo,
|
|
required Box<UnspentCoinsInfo> unspentCoinsInfo,
|
|
required String password,
|
|
required EncryptionFileUtils encryptionFileUtils,
|
|
}) async {
|
|
final network = walletInfo.network != null
|
|
? BasedUtxoNetwork.fromName(walletInfo.network!)
|
|
: BitcoinNetwork.mainnet;
|
|
final snp = await ElectrumWalletSnapshot.load(encryptionFileUtils, name, walletInfo.type, password, network);
|
|
|
|
walletInfo.derivationInfo ??= DerivationInfo(
|
|
derivationType: snp.derivationType ?? DerivationType.electrum,
|
|
derivationPath: snp.derivationPath,
|
|
);
|
|
|
|
// set the default if not present:
|
|
walletInfo.derivationInfo!.derivationPath = snp.derivationPath ?? "m/0'/1";
|
|
|
|
late Uint8List seedBytes;
|
|
|
|
switch (walletInfo.derivationInfo!.derivationType) {
|
|
case DerivationType.electrum:
|
|
seedBytes = await mnemonicToSeedBytes(snp.mnemonic);
|
|
break;
|
|
case DerivationType.bip39:
|
|
default:
|
|
seedBytes = await bip39.mnemonicToSeed(
|
|
snp.mnemonic,
|
|
passphrase: snp.passphrase ?? '',
|
|
);
|
|
break;
|
|
}
|
|
|
|
return BitcoinWallet(
|
|
mnemonic: snp.mnemonic,
|
|
password: password,
|
|
passphrase: snp.passphrase,
|
|
walletInfo: walletInfo,
|
|
unspentCoinsInfo: unspentCoinsInfo,
|
|
initialAddresses: snp.addresses,
|
|
initialBalance: snp.balance,
|
|
encryptionFileUtils: encryptionFileUtils,
|
|
seedBytes: seedBytes,
|
|
initialRegularAddressIndex: snp.regularAddressIndex,
|
|
initialChangeAddressIndex: snp.changeAddressIndex,
|
|
addressPageType: snp.addressPageType,
|
|
networkParam: network,
|
|
);
|
|
}
|
|
}
|