Skip to content

Commit

Permalink
decred: Allow unsynced unused addresses.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeGruffins committed Feb 13, 2025
1 parent 5bc43cb commit 2a2096f
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 73 deletions.
58 changes: 16 additions & 42 deletions cw_decred/lib/api/libdcrwallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,66 +26,40 @@ void initLibdcrwallet(String logDir) {

/// createWalletAsync calls the libdcrwallet's createWallet function
/// asynchronously.
Future<void> createWalletAsync(
{required String name,
required String dataDir,
required String password,
required String network,
String? mnemonic}) {
final args = <String, String>{
"name": name,
"dataDir": dataDir,
"password": password,
"network": network,
"mnemonic": mnemonic ?? "",
};
return compute(createWalletSync, args);
Future<void> createWalletAsync(String config) {
return compute(createWalletSync, config);
}

/// createWalletSync calls the libdcrwallet's createWallet function
/// synchronously.
void createWalletSync(Map<String, String> args) {
final name = args["name"]!.toCString();
final dataDir = args["dataDir"]!.toCString();
final password = args["password"]!.toCString();
final mnemonic = args["mnemonic"]!.toCString();
final network = args["network"]!.toCString();
void createWalletSync(String config) {
final cConfig = config.toCString();

executePayloadFn(
fn: () => dcrwalletApi.createWallet(name, dataDir, network, password, mnemonic),
ptrsToFree: [name, dataDir, network, password, mnemonic],
fn: () => dcrwalletApi.createWallet(cConfig),
ptrsToFree: [cConfig],
);
}

void createWatchOnlyWallet(String walletName, String datadir, String pubkey, String network) {
final cName = walletName.toCString();
final cDataDir = datadir.toCString();
final cPub = pubkey.toCString();
final cNet = network.toCString();
void createWatchOnlyWallet(String config) {
final cConfig = config.toCString();
executePayloadFn(
fn: () => dcrwalletApi.createWatchOnlyWallet(cName, cDataDir, cNet, cPub),
ptrsToFree: [cName, cDataDir, cNet, cPub],
fn: () => dcrwalletApi.createWatchOnlyWallet(cConfig),
ptrsToFree: [cConfig],
);
}

/// loadWalletAsync calls the libdcrwallet's loadWallet function asynchronously.
Future<void> loadWalletAsync({required String name, required String dataDir, required String net}) {
final args = <String, String>{
"name": name,
"dataDir": dataDir,
"network": net,
};
return compute(loadWalletSync, args);
Future<void> loadWalletAsync(String config) {
return compute(loadWalletSync, config);
}

/// loadWalletSync calls the libdcrwallet's loadWallet function synchronously.
void loadWalletSync(Map<String, String> args) {
final name = args["name"]!.toCString();
final dataDir = args["dataDir"]!.toCString();
final network = args["network"]!.toCString();
void loadWalletSync(String config) {
final cConfig = config.toCString();
executePayloadFn(
fn: () => dcrwalletApi.loadWallet(name, dataDir, network),
ptrsToFree: [name, dataDir, network],
fn: () => dcrwalletApi.loadWallet(cConfig),
ptrsToFree: [cConfig],
);
}

Expand Down
13 changes: 8 additions & 5 deletions cw_decred/lib/wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,13 @@ abstract class DecredWalletBase
persistantPeer = addr;
libdcrwallet.closeWallet(walletInfo.name);
final network = isTestnet ? "testnet" : "mainnet";
libdcrwallet.loadWalletSync({
final config = {
"name": walletInfo.name,
"dataDir": walletInfo.dirPath,
"network": network,
});
"datadir": walletInfo.dirPath,
"net": network,
"unsyncedaddrs": true,
};
libdcrwallet.loadWalletSync(jsonEncode(config));
}
await this._startSync();
connecting = false;
Expand Down Expand Up @@ -445,14 +447,15 @@ abstract class DecredWalletBase
final fee = (feeDouble * 1e8).toInt().abs();
final confs = d["confirmations"] ?? 0;
final sendTime = d["time"] ?? 0;
final height = d["height"] ?? 0;
final txInfo = DecredTransactionInfo(
id: txid,
amount: amount,
fee: fee,
direction: direction,
isPending: confs == 0,
date: DateTime.fromMillisecondsSinceEpoch(sendTime * 1000, isUtc: true),
height: 0,
height: height,
confirmations: confs,
to: d["address"] ?? "",
);
Expand Down
56 changes: 33 additions & 23 deletions cw_decred/lib/wallet_service.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:convert';
import 'dart:io';
import 'package:cw_decred/api/libdcrwallet.dart';
import 'package:cw_decred/wallet_creation_credentials.dart';
Expand Down Expand Up @@ -43,12 +44,14 @@ class DecredWalletService extends WalletService<

@override
Future<DecredWallet> create(DecredNewWalletCredentials credentials, {bool? isTestnet}) async {
await createWalletAsync(
name: credentials.walletInfo!.name,
dataDir: credentials.walletInfo!.dirPath,
password: credentials.password!,
network: isTestnet == true ? testnet : mainnet,
);
final config = {
"name": credentials.walletInfo!.name,
"datadir": credentials.walletInfo!.dirPath,
"pass": credentials.password!,
"net": isTestnet == true ? testnet : mainnet,
"unsyncedaddrs": true,
};
await createWalletAsync(jsonEncode(config));
final di = DerivationInfo(
derivationPath: isTestnet == true ? seedRestorePathTestnet : seedRestorePath);
credentials.walletInfo!.derivationInfo = di;
Expand All @@ -72,11 +75,13 @@ class DecredWalletService extends WalletService<
walletInfo.dirPath = await pathForWalletDir(name: name, type: getType());
}

await loadWalletAsync(
name: walletInfo.name,
dataDir: walletInfo.dirPath,
net: network,
);
final config = {
"name": walletInfo.name,
"datadir": walletInfo.dirPath,
"net": network,
"unsyncedaddrs": true,
};
await loadWalletAsync(jsonEncode(config));
final wallet = DecredWallet(walletInfo, password, this.unspentCoinsInfoSource);
await wallet.init();
return wallet;
Expand Down Expand Up @@ -115,12 +120,15 @@ class DecredWalletService extends WalletService<
@override
Future<DecredWallet> restoreFromSeed(DecredRestoreWalletFromSeedCredentials credentials,
{bool? isTestnet}) async {
await createWalletAsync(
name: credentials.walletInfo!.name,
dataDir: credentials.walletInfo!.dirPath,
password: credentials.password!,
mnemonic: credentials.mnemonic,
network: isTestnet == true ? testnet : mainnet);
final config = {
"name": credentials.walletInfo!.name,
"datadir": credentials.walletInfo!.dirPath,
"pass": credentials.password!,
"mnemonic": credentials.mnemonic,
"net": isTestnet == true ? testnet : mainnet,
"unsyncedaddrs": true,
};
await createWalletAsync(jsonEncode(config));
final di = DerivationInfo(
derivationPath: isTestnet == true ? seedRestorePathTestnet : seedRestorePath);
credentials.walletInfo!.derivationInfo = di;
Expand All @@ -135,12 +143,14 @@ class DecredWalletService extends WalletService<
@override
Future<DecredWallet> restoreFromKeys(DecredRestoreWalletFromPubkeyCredentials credentials,
{bool? isTestnet}) async {
createWatchOnlyWallet(
credentials.walletInfo!.name,
credentials.walletInfo!.dirPath,
credentials.pubkey,
isTestnet == true ? testnet : mainnet,
);
final config = {
"name": credentials.walletInfo!.name,
"datadir": credentials.walletInfo!.dirPath,
"pubkey": credentials.pubkey,
"net": isTestnet == true ? testnet : mainnet,
"unsyncedaddrs": true,
};
createWatchOnlyWallet(jsonEncode(config));
final di = DerivationInfo(
derivationPath: isTestnet == true ? pubkeyRestorePathTestnet : pubkeyRestorePath);
credentials.walletInfo!.derivationInfo = di;
Expand Down
2 changes: 1 addition & 1 deletion scripts/android/build_decred.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ cd "$(dirname "$0")"
CW_DECRED_DIR=$(realpath ../..)/cw_decred
LIBWALLET_PATH="${PWD}/decred/libwallet"
LIBWALLET_URL="https://github.com/decred/libwallet.git"
LIBWALLET_VERSION="82ed8a80ae9fa3b15a2d5609bc748fc663be7e37" # v2.2.1
LIBWALLET_VERSION="e02273ad75a029a4f020f11c4575025f4e4eb132"

if [ -e $LIBWALLET_PATH ]; then
rm -fr $LIBWALLET_PATH/{*,.*} || true
Expand Down
2 changes: 1 addition & 1 deletion scripts/ios/build_decred.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -e
. ./config.sh
LIBWALLET_PATH="${EXTERNAL_IOS_SOURCE_DIR}/libwallet"
LIBWALLET_URL="https://github.com/decred/libwallet.git"
LIBWALLET_VERSION="82ed8a80ae9fa3b15a2d5609bc748fc663be7e37" # v2.2.1
LIBWALLET_VERSION="e02273ad75a029a4f020f11c4575025f4e4eb132"

if [ -e $LIBWALLET_PATH ]; then
rm -fr $LIBWALLET_PATH
Expand Down
2 changes: 1 addition & 1 deletion scripts/macos/build_decred.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

LIBWALLET_PATH="${EXTERNAL_MACOS_SOURCE_DIR}/libwallet"
LIBWALLET_URL="https://github.com/decred/libwallet.git"
LIBWALLET_VERSION="82ed8a80ae9fa3b15a2d5609bc748fc663be7e37" # v2.2.1
LIBWALLET_VERSION="e02273ad75a029a4f020f11c4575025f4e4eb132"

echo "======================= DECRED LIBWALLET ========================="

Expand Down

0 comments on commit 2a2096f

Please sign in to comment.