cake_wallet/cw_bitcoin/lib/bitcoin_wallet.dart

107 lines
4.4 KiB
Dart
Raw Normal View History

import 'package:cw_bitcoin/bitcoin_mnemonic.dart';
import 'package:cw_core/encryption_file_utils.dart';
2022-10-12 17:09:57 +00:00
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';
part 'bitcoin_wallet.g.dart';
class BitcoinWallet = BitcoinWalletBase with _$BitcoinWallet;
abstract class BitcoinWalletBase extends ElectrumWallet with Store {
BitcoinWalletBase(
2022-10-12 17:09:57 +00:00
{required String mnemonic,
required String password,
required WalletInfo walletInfo,
required Box<UnspentCoinsInfo> unspentCoinsInfo,
2023-11-14 23:17:15 +00:00
bitcoin.NetworkType? networkType,
2022-10-12 17:09:57 +00:00
required Uint8List seedBytes,
required EncryptionFileUtils encryptionFileUtils,
2022-10-12 17:09:57 +00:00
List<BitcoinAddressRecord>? initialAddresses,
ElectrumBalance? initialBalance,
int initialRegularAddressIndex = 0,
2023-11-14 23:17:15 +00:00
int initialChangeAddressIndex = 0,
bitcoin.SilentPaymentReceiver? silentAddress})
: super(
mnemonic: mnemonic,
password: password,
walletInfo: walletInfo,
unspentCoinsInfo: unspentCoinsInfo,
2023-11-14 23:17:15 +00:00
networkType: networkType ?? bitcoin.bitcoin,
initialAddresses: initialAddresses,
2022-10-12 17:09:57 +00:00
initialBalance: initialBalance,
seedBytes: seedBytes,
currency: CryptoCurrency.btc,
encryptionFileUtils: encryptionFileUtils) {
2023-11-14 23:17:15 +00:00
walletAddresses = BitcoinWalletAddresses(walletInfo,
electrumClient: electrumClient,
initialAddresses: initialAddresses,
initialRegularAddressIndex: initialRegularAddressIndex,
initialChangeAddressIndex: initialChangeAddressIndex,
mainHd: hd,
2023-11-14 23:17:15 +00:00
sideHd: bitcoin.HDWallet.fromSeed(seedBytes, network: networkType).derivePath("m/0'/1"),
networkType: networkType ?? bitcoin.bitcoin,
silentAddress: silentAddress);
}
2023-11-14 23:17:15 +00:00
static Future<BitcoinWallet> create(
{required String mnemonic,
required String password,
required WalletInfo walletInfo,
required Box<UnspentCoinsInfo> unspentCoinsInfo,
bitcoin.NetworkType? networkType,
required EncryptionFileUtils encryptionFileUtils,
List<BitcoinAddressRecord>? initialAddresses,
ElectrumBalance? initialBalance,
int initialRegularAddressIndex = 0,
int initialChangeAddressIndex = 0}) async {
2022-10-12 17:09:57 +00:00
return BitcoinWallet(
mnemonic: mnemonic,
password: password,
walletInfo: walletInfo,
unspentCoinsInfo: unspentCoinsInfo,
2023-11-14 23:17:15 +00:00
networkType: networkType,
2022-10-12 17:09:57 +00:00
initialAddresses: initialAddresses,
initialBalance: initialBalance,
encryptionFileUtils: encryptionFileUtils,
2022-10-12 17:09:57 +00:00
seedBytes: await mnemonicToSeedBytes(mnemonic),
initialRegularAddressIndex: initialRegularAddressIndex,
2023-11-14 23:17:15 +00:00
initialChangeAddressIndex: initialChangeAddressIndex,
silentAddress: await bitcoin.SilentPaymentReceiver.fromMnemonic(mnemonic,
hrp: networkType == bitcoin.bitcoin ? 'sp' : 'tsp'));
2022-10-12 17:09:57 +00:00
}
2023-11-14 23:17:15 +00:00
static Future<BitcoinWallet> open(
{required String name,
required WalletInfo walletInfo,
required Box<UnspentCoinsInfo> unspentCoinsInfo,
required String password,
required EncryptionFileUtils encryptionFileUtils}) async {
final snp =
await ElectrumWallletSnapshot.load(encryptionFileUtils, name, walletInfo.type, password);
return BitcoinWallet(
mnemonic: snp.mnemonic,
password: password,
walletInfo: walletInfo,
unspentCoinsInfo: unspentCoinsInfo,
2023-11-14 23:17:15 +00:00
networkType: snp.networkType,
initialAddresses: snp.addresses,
initialBalance: snp.balance,
2022-10-12 17:09:57 +00:00
seedBytes: await mnemonicToSeedBytes(snp.mnemonic),
encryptionFileUtils: encryptionFileUtils,
initialRegularAddressIndex: snp.regularAddressIndex,
2023-11-14 23:17:15 +00:00
initialChangeAddressIndex: snp.changeAddressIndex,
silentAddress: await bitcoin.SilentPaymentReceiver.fromMnemonic(snp.mnemonic,
hrp: snp.networkType == bitcoin.bitcoin ? 'sp' : 'tsp'));
}
2023-11-14 23:17:15 +00:00
}