2021-12-24 12:52:08 +00:00
|
|
|
import 'package:cw_bitcoin/bitcoin_mnemonic.dart';
|
2023-11-30 17:28:29 +00:00
|
|
|
import 'package:cw_bitcoin/electrum_transaction_history.dart';
|
2022-10-12 17:09:57 +00:00
|
|
|
import 'package:cw_core/crypto_currency.dart';
|
2021-12-24 12:52:08 +00:00
|
|
|
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(
|
2023-11-30 17:28:29 +00:00
|
|
|
{required super.mnemonic,
|
|
|
|
required super.password,
|
|
|
|
required super.walletInfo,
|
|
|
|
required super.unspentCoinsInfo,
|
2023-11-30 10:01:58 +00:00
|
|
|
bitcoin.NetworkType? networkType,
|
2022-10-12 17:09:57 +00:00
|
|
|
required Uint8List seedBytes,
|
|
|
|
List<BitcoinAddressRecord>? initialAddresses,
|
2023-12-06 14:47:14 +00:00
|
|
|
List<BitcoinAddressRecord>? initialSilentAddresses,
|
2022-10-12 17:09:57 +00:00
|
|
|
ElectrumBalance? initialBalance,
|
2022-01-18 16:27:33 +00:00
|
|
|
int initialRegularAddressIndex = 0,
|
2023-11-30 10:01:58 +00:00
|
|
|
int initialChangeAddressIndex = 0,
|
2023-12-06 14:47:14 +00:00
|
|
|
int initialSilentAddressIndex = 0,
|
2023-11-30 10:01:58 +00:00
|
|
|
bitcoin.SilentPaymentReceiver? silentAddress})
|
2021-12-24 12:52:08 +00:00
|
|
|
: super(
|
2023-11-30 17:28:29 +00:00
|
|
|
networkType: networkType ?? bitcoin.bitcoin,
|
|
|
|
initialAddresses: initialAddresses,
|
|
|
|
initialBalance: initialBalance,
|
|
|
|
seedBytes: seedBytes,
|
|
|
|
currency: CryptoCurrency.btc,
|
|
|
|
transactionHistory:
|
|
|
|
ElectrumTransactionHistory(walletInfo: walletInfo, password: password)) {
|
2023-11-30 10:01:58 +00:00
|
|
|
walletAddresses = BitcoinWalletAddresses(walletInfo,
|
2023-11-30 17:28:29 +00:00
|
|
|
transactionHistory: super.transactionHistory,
|
2021-12-24 12:52:08 +00:00
|
|
|
initialAddresses: initialAddresses,
|
2023-12-06 14:47:14 +00:00
|
|
|
initialSilentAddresses: initialSilentAddresses,
|
2022-01-18 16:27:33 +00:00
|
|
|
initialRegularAddressIndex: initialRegularAddressIndex,
|
|
|
|
initialChangeAddressIndex: initialChangeAddressIndex,
|
2023-12-06 14:47:14 +00:00
|
|
|
initialSilentAddressIndex: initialSilentAddressIndex,
|
2021-12-24 12:52:08 +00:00
|
|
|
mainHd: hd,
|
2023-11-30 10:01:58 +00:00
|
|
|
sideHd: bitcoin.HDWallet.fromSeed(seedBytes, network: networkType).derivePath("m/0'/1"),
|
|
|
|
networkType: networkType ?? bitcoin.bitcoin,
|
|
|
|
silentAddress: silentAddress);
|
2021-12-24 12:52:08 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 14:47:14 +00:00
|
|
|
static Future<BitcoinWallet> create({
|
|
|
|
required String mnemonic,
|
|
|
|
required String password,
|
|
|
|
required WalletInfo walletInfo,
|
|
|
|
required Box<UnspentCoinsInfo> unspentCoinsInfo,
|
|
|
|
bitcoin.NetworkType? networkType,
|
|
|
|
List<BitcoinAddressRecord>? initialAddresses,
|
|
|
|
List<BitcoinAddressRecord>? initialSilentAddresses,
|
|
|
|
ElectrumBalance? initialBalance,
|
|
|
|
int initialRegularAddressIndex = 0,
|
|
|
|
int initialChangeAddressIndex = 0,
|
|
|
|
int initialSilentAddressIndex = 0,
|
|
|
|
}) async {
|
2022-10-12 17:09:57 +00:00
|
|
|
return BitcoinWallet(
|
|
|
|
mnemonic: mnemonic,
|
|
|
|
password: password,
|
|
|
|
walletInfo: walletInfo,
|
|
|
|
unspentCoinsInfo: unspentCoinsInfo,
|
2023-11-30 10:01:58 +00:00
|
|
|
networkType: networkType,
|
2022-10-12 17:09:57 +00:00
|
|
|
initialAddresses: initialAddresses,
|
2023-12-06 14:47:14 +00:00
|
|
|
initialSilentAddresses: initialSilentAddresses,
|
2022-10-12 17:09:57 +00:00
|
|
|
initialBalance: initialBalance,
|
|
|
|
seedBytes: await mnemonicToSeedBytes(mnemonic),
|
|
|
|
initialRegularAddressIndex: initialRegularAddressIndex,
|
2023-11-30 10:01:58 +00:00
|
|
|
initialChangeAddressIndex: initialChangeAddressIndex,
|
2023-12-06 14:47:14 +00:00
|
|
|
initialSilentAddressIndex: initialSilentAddressIndex,
|
2023-11-30 10:01:58 +00:00
|
|
|
silentAddress: await bitcoin.SilentPaymentReceiver.fromMnemonic(mnemonic,
|
|
|
|
hrp: networkType == bitcoin.bitcoin ? 'sp' : 'tsp'));
|
2022-10-12 17:09:57 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 12:52:08 +00:00
|
|
|
static Future<BitcoinWallet> open({
|
2022-10-12 17:09:57 +00:00
|
|
|
required String name,
|
|
|
|
required WalletInfo walletInfo,
|
|
|
|
required Box<UnspentCoinsInfo> unspentCoinsInfo,
|
|
|
|
required String password,
|
2021-12-24 12:52:08 +00:00
|
|
|
}) async {
|
2023-11-30 10:01:58 +00:00
|
|
|
final snp = await ElectrumWalletSnapshot.load(name, walletInfo.type, password);
|
2021-12-24 12:52:08 +00:00
|
|
|
return BitcoinWallet(
|
|
|
|
mnemonic: snp.mnemonic,
|
|
|
|
password: password,
|
|
|
|
walletInfo: walletInfo,
|
|
|
|
unspentCoinsInfo: unspentCoinsInfo,
|
2023-11-30 10:01:58 +00:00
|
|
|
networkType: snp.networkType,
|
2021-12-24 12:52:08 +00:00
|
|
|
initialAddresses: snp.addresses,
|
2023-12-06 14:47:14 +00:00
|
|
|
initialSilentAddresses: snp.silentAddresses,
|
2021-12-24 12:52:08 +00:00
|
|
|
initialBalance: snp.balance,
|
2022-10-12 17:09:57 +00:00
|
|
|
seedBytes: await mnemonicToSeedBytes(snp.mnemonic),
|
2022-01-18 16:27:33 +00:00
|
|
|
initialRegularAddressIndex: snp.regularAddressIndex,
|
2023-11-30 10:01:58 +00:00
|
|
|
initialChangeAddressIndex: snp.changeAddressIndex,
|
2023-12-06 14:47:14 +00:00
|
|
|
initialSilentAddressIndex: snp.silentAddressIndex,
|
2023-11-30 10:01:58 +00:00
|
|
|
silentAddress: await bitcoin.SilentPaymentReceiver.fromMnemonic(snp.mnemonic,
|
|
|
|
hrp: snp.networkType == bitcoin.bitcoin ? 'sp' : 'tsp'));
|
2021-12-24 12:52:08 +00:00
|
|
|
}
|
2023-11-30 10:01:58 +00:00
|
|
|
}
|