Restore wallet and sweep WIP

This commit is contained in:
Blazebrain 2023-05-07 08:27:54 +01:00
parent 3da8f06a52
commit 8323fc29fe
7 changed files with 129 additions and 9 deletions

View file

@ -2,6 +2,7 @@ import 'dart:io';
import 'package:cw_bitcoin/bitcoin_mnemonic.dart';
import 'package:cw_bitcoin/bitcoin_mnemonic_is_incorrect_exception.dart';
import 'package:cw_bitcoin/bitcoin_wallet_creation_credentials.dart';
import 'package:cw_core/node.dart';
import 'package:cw_core/unspent_coins_info.dart';
import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_service.dart';
@ -77,4 +78,10 @@ class BitcoinWalletService extends WalletService<
await wallet.init();
return wallet;
}
@override
Future<void> sweepAllFunds(Node node, String address, String paymentId) {
// TODO: implement sweepAllFunds
throw UnimplementedError();
}
}

View file

@ -1,4 +1,5 @@
import 'dart:io';
import 'package:cw_core/node.dart';
import 'package:cw_core/unspent_coins_info.dart';
import 'package:hive/hive.dart';
import 'package:cw_bitcoin/bitcoin_mnemonic.dart';
@ -78,4 +79,10 @@ class LitecoinWalletService extends WalletService<
await wallet.init();
return wallet;
}
@override
Future<void> sweepAllFunds(Node node, String address, String paymentId) {
// TODO: implement sweepAllFunds
throw UnimplementedError();
}
}

View file

@ -2,8 +2,11 @@ import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_credentials.dart';
import 'package:cw_core/wallet_type.dart';
import 'node.dart';
abstract class WalletService<N extends WalletCredentials,
RFS extends WalletCredentials, RFK extends WalletCredentials> {
RFS extends WalletCredentials,
RFK extends WalletCredentials> {
WalletType getType();
Future<WalletBase> create(N credentials);
@ -12,6 +15,8 @@ abstract class WalletService<N extends WalletCredentials,
Future<WalletBase> restoreFromKeys(RFK credentials);
Future<void> sweepAllFunds(Node node, String address, String paymentId);
Future<WalletBase> openWallet(String name, String password);
Future<bool> isWalletExit(String name);

View file

@ -1,4 +1,7 @@
import 'dart:convert';
import 'dart:ffi';
import 'dart:io';
import 'package:cw_core/node.dart';
import 'package:ffi/ffi.dart';
import 'package:flutter/foundation.dart';
import 'package:cw_haven/api/convert_utf8_to_string.dart';
@ -6,6 +9,8 @@ import 'package:cw_haven/api/signatures.dart';
import 'package:cw_haven/api/types.dart';
import 'package:cw_haven/api/haven_api.dart';
import 'package:cw_haven/api/wallet.dart';
import 'package:http/http.dart' as http;
import 'package:http/io_client.dart' as ioc;
import 'package:cw_haven/api/exceptions/wallet_opening_exception.dart';
import 'package:cw_haven/api/exceptions/wallet_creation_exception.dart';
import 'package:cw_haven/api/exceptions/wallet_restore_from_keys_exception.dart';
@ -245,4 +250,71 @@ Future<void> restoreFromKeys(
'restoreHeight': restoreHeight
});
Future<bool> sweepFundsToNewWallet({
required Node node,
required String address,
required String paymentId,
List subaddressIndices = const [],
int accountIndex = 0,
int priority = 0,
int ringSize = 0,
int outputs = 0,
int unlockTime = 1,
bool getTxKeys = false,
bool doNotRelay = false,
bool getTxHex = false,
bool getTxMetadata = false,
int belowAmount = 0,
}) async {
final uri = Uri.http(node.uriRaw, '');
final path = '/json_rpc';
final rpcUri = Uri.https(uri.authority, path);
final realm = 'monero-rpc';
final body = {
'method': 'sweep_all',
'params': {
'address': address,
'account_index': accountIndex,
'subaddr_indices': subaddressIndices,
'priority': priority,
'ring_size': ringSize,
'outputs': outputs,
'unlock_time': unlockTime,
'payment_id': paymentId,
'get_tx_keys': getTxKeys,
'below_amount': belowAmount,
'do_not_relay': doNotRelay,
'get_tx_hex': getTxHex,
'get_tx_metadata': getTxMetadata,
},
'jsonrpc': '2.0',
'id': '0'
};
try {
final authenticatingClient = HttpClient();
authenticatingClient.addCredentials(
rpcUri,
realm,
HttpClientDigestCredentials(node.login ?? '', node.password ?? ''),
);
final http.Client client = ioc.IOClient(authenticatingClient);
final response = await client.post(
rpcUri,
headers: {'Content-Type': 'application/json'},
body: json.encode(body),
);
client.close();
final resBody = json.decode(response.body) as Map<String, dynamic>;
return !(resBody['result']['offline'] as bool);
} catch (_) {
return false;
}
}
Future<bool> isWalletExist({required String path}) => compute(_isWalletExist, path);

View file

@ -1,5 +1,6 @@
import 'dart:io';
import 'package:collection/collection.dart';
import 'package:cw_core/node.dart';
import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/monero_wallet_utils.dart';
import 'package:hive/hive.dart';
@ -228,4 +229,19 @@ class HavenWalletService extends WalletService<
print(e.toString());
}
}
@override
Future<void> sweepAllFunds(Node node, String address, String paymentId) async {
try {
await haven_wallet_manager.sweepFundsToNewWallet(
node: node,
address: address,
paymentId: paymentId,
);
} catch (e) {
// TODO: Implement Exception for wallet list service.
print('MoneroWalletsManager Error: $e');
rethrow;
}
}
}

View file

@ -1,6 +1,7 @@
import 'dart:convert';
import 'dart:ffi';
import 'dart:io';
import 'package:cw_core/node.dart';
import 'package:ffi/ffi.dart';
import 'package:flutter/foundation.dart';
import 'package:cw_monero/api/convert_utf8_to_string.dart';
@ -232,25 +233,23 @@ Future<void> restoreFromKeys(
'restoreHeight': restoreHeight
});
Future<bool> restoreFromTxIdS({
required String uriRaw,
Future<bool> sweepFundsToNewWallet({
required Node node,
required String address,
required String paymentId,
List subaddressIndices = const [],
int accountIndex = 0,
int priority = 0,
int ringSize = 0,
int outputs = 0,
int unlockTime = 1,
required String paymentId,
bool getTxKeys = false,
bool doNotRelay = false,
bool getTxHex = false,
bool getTxMetadata = false,
int belowAmount = 0,
String? username,
String? password,
}) async {
final uri = Uri.http(uriRaw, '');
final uri = Uri.http(node.uriRaw, '');
final path = '/json_rpc';
final rpcUri = Uri.https(uri.authority, path);
final realm = 'monero-rpc';
@ -281,7 +280,7 @@ Future<bool> restoreFromTxIdS({
authenticatingClient.addCredentials(
rpcUri,
realm,
HttpClientDigestCredentials(username ?? '', password ?? ''),
HttpClientDigestCredentials(node.login ?? '', node.password ?? ''),
);
final http.Client client = ioc.IOClient(authenticatingClient);

View file

@ -1,4 +1,5 @@
import 'dart:io';
import 'package:cw_core/node.dart';
import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/monero_wallet_utils.dart';
import 'package:hive/hive.dart';
@ -192,7 +193,20 @@ class MoneroWalletService extends WalletService<
rethrow;
}
}
@override
Future<void> sweepAllFunds(Node node, String address, String paymentId) async {
try {
await monero_wallet_manager.sweepFundsToNewWallet(
node: node,
address: address,
paymentId: paymentId,
);
} catch (e) {
// TODO: Implement Exception for wallet list service.
print('MoneroWalletsManager Error: $e');
rethrow;
}
}
Future<void> repairOldAndroidWallet(String name) async {
try {
if (!Platform.isAndroid) {