cake_wallet/cw_monero/lib/wallet_manager.dart

249 lines
7.3 KiB
Dart
Raw Normal View History

2020-01-04 19:31:52 +00:00
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:flutter/foundation.dart';
import 'package:cw_monero/convert_utf8_to_string.dart';
import 'package:cw_monero/signatures.dart';
import 'package:cw_monero/types.dart';
import 'package:cw_monero/monero_api.dart';
import 'package:cw_monero/wallet.dart';
import 'package:cw_monero/exceptions/wallet_opening_exception.dart';
2020-01-04 19:31:52 +00:00
import 'package:cw_monero/exceptions/wallet_creation_exception.dart';
import 'package:cw_monero/exceptions/wallet_restore_from_keys_exception.dart';
import 'package:cw_monero/exceptions/wallet_restore_from_seed_exception.dart';
final createWalletNative = moneroApi
.lookup<NativeFunction<create_wallet>>('create_wallet')
.asFunction<CreateWallet>();
final restoreWalletFromSeedNative = moneroApi
.lookup<NativeFunction<restore_wallet_from_seed>>(
'restore_wallet_from_seed')
.asFunction<RestoreWalletFromSeed>();
final restoreWalletFromKeysNative = moneroApi
.lookup<NativeFunction<restore_wallet_from_keys>>(
'restore_wallet_from_keys')
.asFunction<RestoreWalletFromKeys>();
final isWalletExistNative = moneroApi
.lookup<NativeFunction<is_wallet_exist>>('is_wallet_exist')
.asFunction<IsWalletExist>();
final loadWalletNative = moneroApi
.lookup<NativeFunction<load_wallet>>('load_wallet')
.asFunction<LoadWallet>();
2020-12-23 12:48:04 +00:00
final errorStringNative = moneroApi
.lookup<NativeFunction<error_string>>('error_string')
.asFunction<ErrorString>();
2020-01-08 12:26:34 +00:00
void createWalletSync(
2020-05-26 15:27:10 +00:00
{String path, String password, String language, int nettype = 0}) {
2020-01-04 19:31:52 +00:00
final pathPointer = Utf8.toUtf8(path);
final passwordPointer = Utf8.toUtf8(password);
final languagePointer = Utf8.toUtf8(language);
final errorMessagePointer = allocate<Utf8>();
final isWalletCreated = createWalletNative(pathPointer, passwordPointer,
languagePointer, nettype, errorMessagePointer) !=
0;
free(pathPointer);
free(passwordPointer);
free(languagePointer);
if (!isWalletCreated) {
throw WalletCreationException(
message: convertUTF8ToString(pointer: errorMessagePointer));
}
2020-10-09 18:34:21 +00:00
// setupNodeSync(address: "node.moneroworld.com:18089");
2020-01-04 19:31:52 +00:00
}
bool isWalletExistSync({String path}) {
final pathPointer = Utf8.toUtf8(path);
final isExist = isWalletExistNative(pathPointer) != 0;
free(pathPointer);
return isExist;
}
2020-01-08 12:26:34 +00:00
void restoreWalletFromSeedSync(
2020-01-04 19:31:52 +00:00
{String path,
String password,
String seed,
int nettype = 0,
int restoreHeight = 0}) {
final pathPointer = Utf8.toUtf8(path);
final passwordPointer = Utf8.toUtf8(password);
final seedPointer = Utf8.toUtf8(seed);
final errorMessagePointer = allocate<Utf8>();
final isWalletRestored = restoreWalletFromSeedNative(
pathPointer,
passwordPointer,
seedPointer,
nettype,
restoreHeight,
errorMessagePointer) !=
0;
free(pathPointer);
free(passwordPointer);
free(seedPointer);
if (!isWalletRestored) {
throw WalletRestoreFromSeedException(
message: convertUTF8ToString(pointer: errorMessagePointer));
}
}
2020-01-08 12:26:34 +00:00
void restoreWalletFromKeysSync(
2020-01-04 19:31:52 +00:00
{String path,
String password,
String language,
2020-01-04 19:31:52 +00:00
String address,
String viewKey,
String spendKey,
int nettype = 0,
int restoreHeight = 0}) {
final pathPointer = Utf8.toUtf8(path);
final passwordPointer = Utf8.toUtf8(password);
final languagePointer = Utf8.toUtf8(language);
final addressPointer = Utf8.toUtf8(address);
final viewKeyPointer = Utf8.toUtf8(viewKey);
final spendKeyPointer = Utf8.toUtf8(spendKey);
final errorMessagePointer = allocate<Utf8>();
final isWalletRestored = restoreWalletFromKeysNative(
pathPointer,
passwordPointer,
languagePointer,
addressPointer,
viewKeyPointer,
spendKeyPointer,
nettype,
restoreHeight,
errorMessagePointer) !=
0;
free(pathPointer);
free(passwordPointer);
free(languagePointer);
free(addressPointer);
free(viewKeyPointer);
free(spendKeyPointer);
if (!isWalletRestored) {
throw WalletRestoreFromKeysException(
message: convertUTF8ToString(pointer: errorMessagePointer));
}
}
2020-01-08 12:26:34 +00:00
void loadWallet({String path, String password, int nettype = 0}) {
2020-01-04 19:31:52 +00:00
final pathPointer = Utf8.toUtf8(path);
final passwordPointer = Utf8.toUtf8(password);
2020-12-23 12:48:04 +00:00
final loaded = loadWalletNative(pathPointer, passwordPointer, nettype) != 0;
2020-01-04 19:31:52 +00:00
free(pathPointer);
free(passwordPointer);
2020-12-23 12:48:04 +00:00
if (!loaded) {
throw WalletOpeningException(
2020-12-23 12:48:04 +00:00
message: convertUTF8ToString(pointer: errorStringNative()));
}
2020-01-04 19:31:52 +00:00
}
2020-01-08 12:26:34 +00:00
void _createWallet(Map<String, dynamic> args) {
final path = args['path'] as String;
final password = args['password'] as String;
final language = args['language'] as String;
2020-01-04 19:31:52 +00:00
createWalletSync(path: path, password: password, language: language);
2020-01-08 12:26:34 +00:00
}
void _restoreFromSeed(Map<String, dynamic> args) {
final path = args['path'] as String;
final password = args['password'] as String;
final seed = args['seed'] as String;
final restoreHeight = args['restoreHeight'] as int;
2020-01-04 19:31:52 +00:00
2020-01-08 12:26:34 +00:00
restoreWalletFromSeedSync(
path: path, password: password, seed: seed, restoreHeight: restoreHeight);
}
void _restoreFromKeys(Map<String, dynamic> args) {
final path = args['path'] as String;
final password = args['password'] as String;
final language = args['language'] as String;
2020-01-08 12:26:34 +00:00
final restoreHeight = args['restoreHeight'] as int;
final address = args['address'] as String;
final viewKey = args['viewKey'] as String;
final spendKey = args['spendKey'] as String;
restoreWalletFromKeysSync(
path: path,
password: password,
language: language,
2020-01-08 12:26:34 +00:00
restoreHeight: restoreHeight,
address: address,
viewKey: viewKey,
spendKey: spendKey);
}
2020-01-04 19:31:52 +00:00
2020-01-08 12:26:34 +00:00
Future<void> _openWallet(Map<String, String> args) async =>
2020-01-04 19:31:52 +00:00
loadWallet(path: args['path'], password: args['password']);
bool _isWalletExist(String path) => isWalletExistSync(path: path);
2020-01-08 12:26:34 +00:00
void openWallet({String path, String password, int nettype = 0}) async =>
2020-07-06 20:09:03 +00:00
loadWallet(path: path, password: password, nettype: nettype);
2020-01-04 19:31:52 +00:00
2020-01-08 12:26:34 +00:00
Future<void> openWalletAsync(Map<String, String> args) async =>
compute(_openWallet, args);
2020-01-04 19:31:52 +00:00
2020-01-08 12:26:34 +00:00
Future<void> createWallet(
2020-01-04 19:31:52 +00:00
{String path,
String password,
String language,
2020-01-04 19:31:52 +00:00
int nettype = 0}) async =>
compute(_createWallet, {
'path': path,
'password': password,
'language': language,
'nettype': nettype
});
Future restoreFromSeed(
{String path,
String password,
String seed,
int nettype = 0,
int restoreHeight = 0}) async =>
2020-05-26 15:27:10 +00:00
compute<Map<String, Object>, void>(_restoreFromSeed, {
2020-01-04 19:31:52 +00:00
'path': path,
'password': password,
'seed': seed,
'nettype': nettype,
'restoreHeight': restoreHeight
});
Future restoreFromKeys(
{String path,
String password,
String language,
2020-01-04 19:31:52 +00:00
String address,
String viewKey,
String spendKey,
int nettype = 0,
int restoreHeight = 0}) async =>
2020-05-26 15:27:10 +00:00
compute<Map<String, Object>, void>(_restoreFromKeys, {
2020-01-04 19:31:52 +00:00
'path': path,
'password': password,
'language': language,
'address': address,
'viewKey': viewKey,
'spendKey': spendKey,
'nettype': nettype,
'restoreHeight': restoreHeight
});
2020-01-08 12:26:34 +00:00
Future<bool> isWalletExist({String path}) => compute(_isWalletExist, path);