2024-02-07 19:33:56 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
2024-02-06 18:53:38 +00:00
|
|
|
import 'package:breez_sdk/breez_sdk.dart';
|
|
|
|
import 'package:breez_sdk/bridge_generated.dart';
|
2021-12-24 12:52:08 +00:00
|
|
|
import 'package:cw_bitcoin/bitcoin_mnemonic.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';
|
2024-02-07 19:33:56 +00:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
import 'package:cw_bitcoin/.secrets.g.dart' as secrets;
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
required Uint8List seedBytes,
|
|
|
|
List<BitcoinAddressRecord>? initialAddresses,
|
|
|
|
ElectrumBalance? initialBalance,
|
2022-01-18 16:27:33 +00:00
|
|
|
int initialRegularAddressIndex = 0,
|
|
|
|
int initialChangeAddressIndex = 0})
|
2021-12-24 12:52:08 +00:00
|
|
|
: super(
|
|
|
|
mnemonic: mnemonic,
|
|
|
|
password: password,
|
|
|
|
walletInfo: walletInfo,
|
|
|
|
unspentCoinsInfo: unspentCoinsInfo,
|
|
|
|
networkType: bitcoin.bitcoin,
|
|
|
|
initialAddresses: initialAddresses,
|
2022-10-12 17:09:57 +00:00
|
|
|
initialBalance: initialBalance,
|
|
|
|
seedBytes: seedBytes,
|
|
|
|
currency: CryptoCurrency.btc) {
|
2024-02-06 18:53:38 +00:00
|
|
|
walletAddresses = BitcoinWalletAddresses(walletInfo,
|
2022-01-18 16:27:33 +00:00
|
|
|
electrumClient: electrumClient,
|
2021-12-24 12:52:08 +00:00
|
|
|
initialAddresses: initialAddresses,
|
2022-01-18 16:27:33 +00:00
|
|
|
initialRegularAddressIndex: initialRegularAddressIndex,
|
|
|
|
initialChangeAddressIndex: initialChangeAddressIndex,
|
2021-12-24 12:52:08 +00:00
|
|
|
mainHd: hd,
|
2024-02-06 18:53:38 +00:00
|
|
|
sideHd: bitcoin.HDWallet.fromSeed(seedBytes, network: networkType).derivePath("m/0'/1"),
|
2021-12-24 12:52:08 +00:00
|
|
|
networkType: networkType);
|
2024-02-06 18:53:38 +00:00
|
|
|
|
|
|
|
// initialize breeze:
|
2024-02-07 19:33:56 +00:00
|
|
|
setupBreeze(seedBytes);
|
2024-02-06 18:53:38 +00:00
|
|
|
|
2024-01-23 05:15:24 +00:00
|
|
|
autorun((_) {
|
|
|
|
this.walletAddresses.isEnabledAutoGenerateSubaddress = this.isEnabledAutoGenerateSubaddress;
|
|
|
|
});
|
2021-12-24 12:52:08 +00:00
|
|
|
}
|
|
|
|
|
2024-02-06 18:53:38 +00:00
|
|
|
static Future<BitcoinWallet> create(
|
|
|
|
{required String mnemonic,
|
|
|
|
required String password,
|
|
|
|
required WalletInfo walletInfo,
|
|
|
|
required Box<UnspentCoinsInfo> unspentCoinsInfo,
|
|
|
|
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,
|
|
|
|
initialAddresses: initialAddresses,
|
|
|
|
initialBalance: initialBalance,
|
|
|
|
seedBytes: await mnemonicToSeedBytes(mnemonic),
|
|
|
|
initialRegularAddressIndex: initialRegularAddressIndex,
|
|
|
|
initialChangeAddressIndex: initialChangeAddressIndex);
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-10-12 17:09:57 +00:00
|
|
|
final snp = await ElectrumWallletSnapshot.load(name, walletInfo.type, password);
|
2021-12-24 12:52:08 +00:00
|
|
|
return BitcoinWallet(
|
|
|
|
mnemonic: snp.mnemonic,
|
|
|
|
password: password,
|
|
|
|
walletInfo: walletInfo,
|
|
|
|
unspentCoinsInfo: unspentCoinsInfo,
|
|
|
|
initialAddresses: snp.addresses,
|
|
|
|
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,
|
|
|
|
initialChangeAddressIndex: snp.changeAddressIndex);
|
2021-12-24 12:52:08 +00:00
|
|
|
}
|
2024-02-07 19:33:56 +00:00
|
|
|
|
|
|
|
void printDirectoryTree(Directory directory, {String prefix = ''}) {
|
|
|
|
try {
|
|
|
|
final files = directory.listSync();
|
|
|
|
for (var i = 0; i < files.length; i++) {
|
|
|
|
final isLast = i == files.length - 1;
|
|
|
|
if (files[i] is File) {
|
|
|
|
print(
|
|
|
|
'${prefix}${isLast ? '└─' : '├─'} ${files[i].path.split(Platform.pathSeparator).last}');
|
|
|
|
} else if (files[i] is Directory) {
|
|
|
|
print(
|
|
|
|
'${prefix}${isLast ? '└─' : '├─'} ${files[i].path.split(Platform.pathSeparator).last}');
|
|
|
|
printDirectoryTree(files[i] as Directory, prefix: '${prefix}${isLast ? ' ' : '│ '}');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
print('Error: $e');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> setupBreeze(Uint8List seedBytes) async {
|
|
|
|
// Initialize SDK logs listener
|
|
|
|
final sdk = BreezSDK();
|
|
|
|
sdk.initialize();
|
|
|
|
|
|
|
|
NodeConfig breezNodeConfig = NodeConfig.greenlight(
|
|
|
|
config: GreenlightNodeConfig(
|
|
|
|
partnerCredentials: null,
|
|
|
|
inviteCode: secrets.breezInviteCode,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
Config breezConfig = await sdk.defaultConfig(
|
|
|
|
envType: EnvironmentType.Production,
|
|
|
|
apiKey: secrets.breezApiKey,
|
|
|
|
nodeConfig: breezNodeConfig,
|
|
|
|
);
|
|
|
|
|
|
|
|
printDirectoryTree(Directory((await getApplicationDocumentsDirectory()).path));
|
|
|
|
// Customize the config object according to your needs
|
|
|
|
String workingDir = (await getApplicationDocumentsDirectory()).path;
|
|
|
|
workingDir = "$workingDir/wallets/bitcoin/${walletInfo.name}/breez/";
|
|
|
|
new Directory(workingDir).createSync(recursive: true);
|
|
|
|
breezConfig = breezConfig.copyWith(workingDir: workingDir);
|
|
|
|
|
|
|
|
print(workingDir);
|
|
|
|
|
|
|
|
// await sdk.connect(config: breezConfig, seed: seedBytes);
|
|
|
|
|
|
|
|
print(await sdk.isInitialized());
|
|
|
|
print(await sdk.listLsps());
|
|
|
|
}
|
2024-02-06 18:53:38 +00:00
|
|
|
}
|