2023-05-19 10:20:16 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:isar/isar.dart';
|
|
|
|
import 'package:nanodart/nanodart.dart';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
|
|
|
|
import 'package:stackwallet/models/balance.dart';
|
|
|
|
import 'package:stackwallet/models/isar/models/blockchain_data/transaction.dart';
|
|
|
|
import 'package:stackwallet/models/isar/models/blockchain_data/utxo.dart';
|
|
|
|
import 'package:stackwallet/models/paymint/fee_object_model.dart';
|
|
|
|
import 'package:stackwallet/services/coins/coin_service.dart';
|
|
|
|
import 'package:stackwallet/services/mixins/coin_control_interface.dart';
|
|
|
|
import 'package:stackwallet/services/mixins/wallet_cache.dart';
|
|
|
|
import 'package:stackwallet/services/mixins/wallet_db.dart';
|
|
|
|
import 'package:stackwallet/utilities/amount/amount.dart';
|
|
|
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
2023-05-24 15:56:44 +00:00
|
|
|
import 'package:stackwallet/utilities/enums/log_level_enum.dart';
|
|
|
|
import 'package:stackwallet/utilities/logger.dart';
|
2023-05-19 10:20:16 +00:00
|
|
|
|
|
|
|
import '../../../db/isar/main_db.dart';
|
|
|
|
import '../../../models/isar/models/blockchain_data/address.dart';
|
|
|
|
import '../../../models/node_model.dart';
|
|
|
|
import '../../../utilities/default_nodes.dart';
|
|
|
|
import '../../../utilities/flutter_secure_storage_interface.dart';
|
|
|
|
import '../../../utilities/prefs.dart';
|
|
|
|
import '../../node_service.dart';
|
|
|
|
import '../../transaction_notification_tracker.dart';
|
|
|
|
|
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:stackwallet/models/isar/models/isar_models.dart';
|
|
|
|
|
|
|
|
const int MINIMUM_CONFIRMATIONS = 1;
|
|
|
|
|
2023-05-24 18:19:49 +00:00
|
|
|
class NanoWallet extends CoinServiceAPI
|
|
|
|
with WalletCache, WalletDB, CoinControlInterface {
|
2023-05-19 10:20:16 +00:00
|
|
|
NanoWallet({
|
|
|
|
required String walletId,
|
|
|
|
required String walletName,
|
|
|
|
required Coin coin,
|
|
|
|
required TransactionNotificationTracker tracker,
|
|
|
|
required SecureStorageInterface secureStore,
|
|
|
|
MainDB? mockableOverride,
|
|
|
|
}) {
|
|
|
|
txTracker = tracker;
|
|
|
|
_walletId = walletId;
|
|
|
|
_walletName = walletName;
|
|
|
|
_coin = coin;
|
|
|
|
_secureStore = secureStore;
|
|
|
|
initCache(walletId, coin);
|
|
|
|
initWalletDB(mockableOverride: mockableOverride);
|
|
|
|
}
|
|
|
|
|
|
|
|
NodeModel? _xnoNode;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<String?> get mnemonicPassphrase => _secureStore.read(
|
2023-05-24 15:56:44 +00:00
|
|
|
key: '${_walletId}_mnemonicPassphrase',
|
|
|
|
);
|
2023-05-19 10:20:16 +00:00
|
|
|
|
|
|
|
@override
|
2023-05-24 18:19:49 +00:00
|
|
|
Future<String?> get mnemonicString =>
|
|
|
|
_secureStore.read(key: '${_walletId}_mnemonic');
|
2023-05-19 10:20:16 +00:00
|
|
|
|
|
|
|
Future<String> getSeedFromMnemonic() async {
|
|
|
|
var mnemonic = await mnemonicString;
|
|
|
|
return NanoMnemomics.mnemonicListToSeed(mnemonic!.split(" "));
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<String> getPrivateKeyFromMnemonic() async {
|
|
|
|
var mnemonic = await mnemonicString;
|
|
|
|
var seed = NanoMnemomics.mnemonicListToSeed(mnemonic!.split(" "));
|
|
|
|
return NanoKeys.seedToPrivate(seed, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<String> getAddressFromMnemonic() async {
|
|
|
|
var mnemonic = await mnemonicString;
|
|
|
|
var seed = NanoMnemomics.mnemonicListToSeed(mnemonic!.split(' '));
|
2023-05-24 18:19:49 +00:00
|
|
|
var address = NanoAccounts.createAccount(NanoAccountType.NANO,
|
|
|
|
NanoKeys.createPublicKey(NanoKeys.seedToPrivate(seed, 0)));
|
2023-05-19 10:20:16 +00:00
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<String> getPublicKeyFromMnemonic() async {
|
|
|
|
var mnemonic = await mnemonicString;
|
|
|
|
if (mnemonic == null) {
|
|
|
|
return "";
|
|
|
|
} else {
|
|
|
|
var seed = NanoMnemomics.mnemonicListToSeed(mnemonic.split(" "));
|
|
|
|
return NanoKeys.createPublicKey(NanoKeys.seedToPrivate(seed, 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get walletId => _walletId;
|
|
|
|
late String _walletId;
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get walletName => _walletName;
|
|
|
|
late String _walletName;
|
|
|
|
|
|
|
|
@override
|
|
|
|
set walletName(String name) => _walletName = name;
|
|
|
|
|
|
|
|
@override
|
|
|
|
set isFavorite(bool markFavorite) {
|
|
|
|
_isFavorite = markFavorite;
|
|
|
|
updateCachedIsFavorite(markFavorite);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get isFavorite => _isFavorite ??= getCachedIsFavorite();
|
|
|
|
bool? _isFavorite;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Coin get coin => _coin;
|
|
|
|
late Coin _coin;
|
|
|
|
|
|
|
|
late SecureStorageInterface _secureStore;
|
|
|
|
late final TransactionNotificationTracker txTracker;
|
|
|
|
final _prefs = Prefs.instance;
|
|
|
|
|
|
|
|
bool _shouldAutoSync = false;
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get shouldAutoSync => _shouldAutoSync;
|
|
|
|
|
|
|
|
@override
|
|
|
|
set shouldAutoSync(bool shouldAutoSync) => _shouldAutoSync = shouldAutoSync;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Balance get balance => _balance ??= getCachedBalance();
|
|
|
|
Balance? _balance;
|
|
|
|
|
2023-05-24 18:19:49 +00:00
|
|
|
Future<String?> requestWork(String hash) async {
|
2023-05-24 15:56:44 +00:00
|
|
|
return http
|
|
|
|
.post(
|
2023-05-24 18:19:49 +00:00
|
|
|
Uri.parse("https://rpc.nano.to"),
|
2023-05-24 15:56:44 +00:00
|
|
|
headers: {'Content-type': 'application/json'},
|
|
|
|
body: json.encode(
|
|
|
|
{
|
|
|
|
"action": "work_generate",
|
|
|
|
"hash": hash,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.then((http.Response response) {
|
|
|
|
if (response.statusCode == 200) {
|
2023-05-24 18:19:49 +00:00
|
|
|
final Map<String, dynamic> decoded =
|
|
|
|
json.decode(response.body) as Map<String, dynamic>;
|
2023-05-24 15:56:44 +00:00
|
|
|
if (decoded.containsKey("error")) {
|
|
|
|
throw Exception("Received error ${decoded["error"]}");
|
|
|
|
}
|
|
|
|
return decoded["work"] as String?;
|
|
|
|
} else {
|
|
|
|
throw Exception("Received error ${response.statusCode}");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-19 10:20:16 +00:00
|
|
|
@override
|
2023-05-24 15:56:44 +00:00
|
|
|
Future<String> confirmSend({required Map<String, dynamic> txData}) async {
|
|
|
|
try {
|
2023-05-24 18:19:49 +00:00
|
|
|
// our address:
|
|
|
|
final String publicAddress = await getAddressFromMnemonic();
|
|
|
|
|
2023-05-24 15:56:44 +00:00
|
|
|
// first get the account balance:
|
|
|
|
final balanceBody = jsonEncode({
|
|
|
|
"action": "account_balance",
|
2023-05-24 18:19:49 +00:00
|
|
|
"account": publicAddress,
|
2023-05-24 15:56:44 +00:00
|
|
|
});
|
|
|
|
final headers = {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
};
|
|
|
|
final balanceResponse = await http.post(
|
|
|
|
Uri.parse(getCurrentNode().host),
|
|
|
|
headers: headers,
|
|
|
|
body: balanceBody,
|
|
|
|
);
|
|
|
|
|
|
|
|
final balanceData = jsonDecode(balanceResponse.body);
|
2023-05-24 18:19:49 +00:00
|
|
|
final BigInt currentBalance =
|
|
|
|
BigInt.parse(balanceData["balance"].toString());
|
2023-05-24 15:56:44 +00:00
|
|
|
final BigInt txAmount = txData["recipientAmt"].raw as BigInt;
|
|
|
|
final BigInt balanceAfterTx = currentBalance - txAmount;
|
|
|
|
|
|
|
|
// get the account info (we need the frontier and representative):
|
|
|
|
final infoBody = jsonEncode({
|
|
|
|
"action": "account_info",
|
|
|
|
"representative": "true",
|
2023-05-24 18:19:49 +00:00
|
|
|
"account": publicAddress,
|
2023-05-24 15:56:44 +00:00
|
|
|
});
|
|
|
|
final infoResponse = await http.post(
|
|
|
|
Uri.parse(getCurrentNode().host),
|
|
|
|
headers: headers,
|
|
|
|
body: infoBody,
|
|
|
|
);
|
|
|
|
|
2023-05-24 18:19:49 +00:00
|
|
|
final String frontier =
|
|
|
|
jsonDecode(infoResponse.body)["frontier"].toString();
|
|
|
|
final String representative =
|
|
|
|
jsonDecode(infoResponse.body)["representative"].toString();
|
2023-05-24 15:56:44 +00:00
|
|
|
// link = destination address:
|
2023-05-24 18:19:49 +00:00
|
|
|
final String link =
|
|
|
|
NanoAccounts.extractPublicKey(txData["address"].toString());
|
2023-05-24 15:56:44 +00:00
|
|
|
final String linkAsAccount = txData["address"].toString();
|
|
|
|
|
|
|
|
// construct the send block:
|
2023-05-24 19:09:00 +00:00
|
|
|
Map<String, String> sendBlock = {
|
2023-05-24 15:56:44 +00:00
|
|
|
"type": "state",
|
|
|
|
"account": publicAddress,
|
|
|
|
"previous": frontier,
|
|
|
|
"representative": representative,
|
|
|
|
"balance": balanceAfterTx.toString(),
|
|
|
|
"link": link,
|
|
|
|
};
|
|
|
|
|
|
|
|
// sign the send block:
|
|
|
|
final String hash = NanoBlocks.computeStateHash(
|
|
|
|
NanoAccountType.NANO,
|
|
|
|
sendBlock["account"]!,
|
|
|
|
sendBlock["previous"]!,
|
|
|
|
sendBlock["representative"]!,
|
|
|
|
BigInt.parse(sendBlock["balance"]!),
|
|
|
|
sendBlock["link"]!,
|
|
|
|
);
|
|
|
|
final String privateKey = await getPrivateKeyFromMnemonic();
|
|
|
|
final String signature = NanoSignatures.signBlock(hash, privateKey);
|
|
|
|
|
|
|
|
// get PoW for the send block:
|
2023-05-24 18:19:49 +00:00
|
|
|
final String? work = await requestWork(frontier);
|
2023-05-24 15:56:44 +00:00
|
|
|
if (work == null) {
|
|
|
|
throw Exception("Failed to get PoW for send block");
|
|
|
|
}
|
|
|
|
|
2023-05-24 19:09:00 +00:00
|
|
|
sendBlock["link_as_account"] = linkAsAccount;
|
|
|
|
sendBlock["signature"] = signature;
|
|
|
|
sendBlock["work"] = work;
|
2023-05-24 15:56:44 +00:00
|
|
|
|
|
|
|
final processBody = jsonEncode({
|
|
|
|
"action": "process",
|
|
|
|
"json_block": "true",
|
|
|
|
"subtype": "send",
|
2023-05-24 19:09:00 +00:00
|
|
|
"block": sendBlock,
|
2023-05-24 15:56:44 +00:00
|
|
|
});
|
|
|
|
final processResponse = await http.post(
|
|
|
|
Uri.parse(getCurrentNode().host),
|
|
|
|
headers: headers,
|
|
|
|
body: processBody,
|
|
|
|
);
|
|
|
|
|
2023-05-24 18:19:49 +00:00
|
|
|
final Map<String, dynamic> decoded =
|
|
|
|
json.decode(processResponse.body) as Map<String, dynamic>;
|
2023-05-24 15:56:44 +00:00
|
|
|
if (decoded.containsKey("error")) {
|
|
|
|
throw Exception("Received error ${decoded["error"]}");
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the hash of the transaction:
|
|
|
|
return decoded["hash"].toString();
|
|
|
|
} catch (e, s) {
|
2023-05-24 18:19:49 +00:00
|
|
|
Logging.instance
|
|
|
|
.log("Error sending transaction $e - $s", level: LogLevel.Error);
|
2023-05-24 15:56:44 +00:00
|
|
|
rethrow;
|
|
|
|
}
|
2023-05-19 10:20:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<String> get currentReceivingAddress => getAddressFromMnemonic();
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<Amount> estimateFeeFor(Amount amount, int feeRate) {
|
2023-05-24 15:56:44 +00:00
|
|
|
// fees are always 0 :)
|
|
|
|
return Future.value(Amount(rawValue: BigInt.from(0), fractionDigits: 7));
|
2023-05-19 10:20:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> exit() async {
|
|
|
|
_hasCalledExit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
// TODO: implement fees
|
|
|
|
Future<FeeObject> get fees => throw UnimplementedError();
|
|
|
|
|
|
|
|
Future<void> updateBalance() async {
|
|
|
|
final body = jsonEncode({
|
|
|
|
"action": "account_balance",
|
|
|
|
"account": await getAddressFromMnemonic(),
|
|
|
|
});
|
|
|
|
final headers = {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
};
|
2023-05-24 18:19:49 +00:00
|
|
|
final response = await http.post(Uri.parse(getCurrentNode().host),
|
|
|
|
headers: headers, body: body);
|
2023-05-19 10:20:16 +00:00
|
|
|
final data = jsonDecode(response.body);
|
|
|
|
_balance = Balance(
|
2023-05-24 15:56:44 +00:00
|
|
|
total: Amount(
|
2023-05-24 18:19:49 +00:00
|
|
|
rawValue: (BigInt.parse(data["balance"]
|
|
|
|
.toString()) /*+ BigInt.parse(data["receivable"].toString())*/) ~/
|
|
|
|
BigInt.from(10).pow(23),
|
|
|
|
fractionDigits: 7),
|
|
|
|
spendable: Amount(
|
|
|
|
rawValue: BigInt.parse(data["balance"].toString()) ~/
|
2023-05-24 15:56:44 +00:00
|
|
|
BigInt.from(10).pow(23),
|
|
|
|
fractionDigits: 7),
|
2023-05-19 10:20:16 +00:00
|
|
|
blockedTotal: Amount(rawValue: BigInt.parse("0"), fractionDigits: 30),
|
2023-05-24 18:19:49 +00:00
|
|
|
pendingSpendable: Amount(
|
|
|
|
rawValue: BigInt.parse(data["receivable"].toString()) ~/
|
|
|
|
BigInt.from(10).pow(23),
|
|
|
|
fractionDigits: 7),
|
2023-05-19 10:20:16 +00:00
|
|
|
);
|
|
|
|
await updateCachedBalance(_balance!);
|
|
|
|
}
|
|
|
|
|
2023-05-24 18:19:49 +00:00
|
|
|
Future<void> receiveBlock(
|
|
|
|
String blockHash, String source, String amountRaw) async {
|
2023-05-24 19:09:00 +00:00
|
|
|
// TODO: the opening block of an account is a special case
|
|
|
|
bool openBlock = false;
|
|
|
|
|
2023-05-24 18:19:49 +00:00
|
|
|
// our address:
|
|
|
|
final String publicAddress = await getAddressFromMnemonic();
|
|
|
|
|
|
|
|
// first get the account balance:
|
|
|
|
final balanceBody = jsonEncode({
|
|
|
|
"action": "account_balance",
|
|
|
|
"account": publicAddress,
|
|
|
|
});
|
|
|
|
final headers = {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
};
|
|
|
|
final balanceResponse = await http.post(
|
|
|
|
Uri.parse(getCurrentNode().host),
|
|
|
|
headers: headers,
|
|
|
|
body: balanceBody,
|
|
|
|
);
|
|
|
|
|
|
|
|
final balanceData = jsonDecode(balanceResponse.body);
|
|
|
|
final BigInt currentBalance =
|
|
|
|
BigInt.parse(balanceData["balance"].toString());
|
|
|
|
final BigInt txAmount = BigInt.parse(amountRaw);
|
|
|
|
final BigInt balanceAfterTx = currentBalance + txAmount;
|
|
|
|
|
|
|
|
// get the account info (we need the frontier and representative):
|
|
|
|
final infoBody = jsonEncode({
|
|
|
|
"action": "account_info",
|
|
|
|
"representative": "true",
|
|
|
|
"account": publicAddress,
|
|
|
|
});
|
|
|
|
final infoResponse = await http.post(
|
|
|
|
Uri.parse(getCurrentNode().host),
|
|
|
|
headers: headers,
|
|
|
|
body: infoBody,
|
|
|
|
);
|
|
|
|
|
2023-05-24 19:09:00 +00:00
|
|
|
String frontier = jsonDecode(infoResponse.body)["frontier"].toString();
|
2023-05-24 18:19:49 +00:00
|
|
|
final String representative =
|
|
|
|
jsonDecode(infoResponse.body)["representative"].toString();
|
|
|
|
|
2023-05-24 19:09:00 +00:00
|
|
|
// link = send block hash:
|
|
|
|
final String link = blockHash;
|
|
|
|
// this "linkAsAccount" is meaningless:
|
|
|
|
final String linkAsAccount =
|
|
|
|
NanoAccounts.createAccount(NanoAccountType.NANO, blockHash);
|
2023-05-24 18:19:49 +00:00
|
|
|
|
2023-05-24 19:09:00 +00:00
|
|
|
// construct the receive block:
|
|
|
|
Map<String, String> receiveBlock = {
|
2023-05-24 18:19:49 +00:00
|
|
|
"type": "state",
|
|
|
|
"account": publicAddress,
|
2023-05-24 19:09:00 +00:00
|
|
|
"previous": openBlock
|
|
|
|
? "0000000000000000000000000000000000000000000000000000000000000000"
|
|
|
|
: frontier,
|
2023-05-24 18:19:49 +00:00
|
|
|
"representative": representative,
|
|
|
|
"balance": balanceAfterTx.toString(),
|
|
|
|
"link": link,
|
2023-05-24 19:09:00 +00:00
|
|
|
"link_as_account": linkAsAccount,
|
2023-05-24 18:19:49 +00:00
|
|
|
};
|
|
|
|
|
2023-05-24 19:09:00 +00:00
|
|
|
// sign the receive block:
|
2023-05-24 18:19:49 +00:00
|
|
|
final String hash = NanoBlocks.computeStateHash(
|
|
|
|
NanoAccountType.NANO,
|
2023-05-24 19:09:00 +00:00
|
|
|
receiveBlock["account"]!,
|
|
|
|
receiveBlock["previous"]!,
|
|
|
|
receiveBlock["representative"]!,
|
|
|
|
BigInt.parse(receiveBlock["balance"]!),
|
|
|
|
receiveBlock["link"]!,
|
2023-05-24 18:19:49 +00:00
|
|
|
);
|
|
|
|
final String privateKey = await getPrivateKeyFromMnemonic();
|
|
|
|
final String signature = NanoSignatures.signBlock(hash, privateKey);
|
|
|
|
|
|
|
|
// get PoW for the receive block:
|
2023-05-24 19:09:00 +00:00
|
|
|
String? work;
|
|
|
|
if (openBlock) {
|
|
|
|
work = await requestWork(NanoAccounts.extractPublicKey(publicAddress));
|
|
|
|
} else {
|
|
|
|
work = await requestWork(frontier);
|
|
|
|
}
|
2023-05-24 18:19:49 +00:00
|
|
|
if (work == null) {
|
|
|
|
throw Exception("Failed to get PoW for receive block");
|
|
|
|
}
|
2023-05-24 19:09:00 +00:00
|
|
|
receiveBlock["link_as_account"] = linkAsAccount;
|
|
|
|
receiveBlock["signature"] = signature;
|
2023-05-24 18:19:49 +00:00
|
|
|
receiveBlock["work"] = work;
|
|
|
|
|
|
|
|
// process the receive block:
|
|
|
|
|
|
|
|
final processBody = jsonEncode({
|
|
|
|
"action": "process",
|
|
|
|
"json_block": "true",
|
|
|
|
"subtype": "receive",
|
2023-05-24 19:09:00 +00:00
|
|
|
"block": receiveBlock,
|
2023-05-24 18:19:49 +00:00
|
|
|
});
|
|
|
|
final processResponse = await http.post(
|
|
|
|
Uri.parse(getCurrentNode().host),
|
|
|
|
headers: headers,
|
|
|
|
body: processBody,
|
|
|
|
);
|
|
|
|
|
|
|
|
final Map<String, dynamic> decoded =
|
|
|
|
json.decode(processResponse.body) as Map<String, dynamic>;
|
|
|
|
if (decoded.containsKey("error")) {
|
|
|
|
throw Exception("Received error ${decoded["error"]}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-19 10:20:16 +00:00
|
|
|
Future<void> confirmAllReceivable() async {
|
2023-05-24 18:19:49 +00:00
|
|
|
final receivableResponse = await http.post(Uri.parse(getCurrentNode().host),
|
|
|
|
headers: {"Content-Type": "application/json"},
|
|
|
|
body: jsonEncode({
|
|
|
|
"action": "receivable",
|
2023-05-24 19:09:00 +00:00
|
|
|
"source": "true",
|
2023-05-24 18:19:49 +00:00
|
|
|
"account": await getAddressFromMnemonic(),
|
|
|
|
"count": "-1",
|
|
|
|
}));
|
|
|
|
|
|
|
|
final receivableData = await jsonDecode(receivableResponse.body);
|
2023-05-24 19:09:00 +00:00
|
|
|
if (receivableData["blocks"] == "") {
|
|
|
|
return;
|
|
|
|
}
|
2023-05-24 18:19:49 +00:00
|
|
|
final blocks = receivableData["blocks"] as Map<String, dynamic>;
|
|
|
|
// confirm all receivable blocks:
|
|
|
|
for (final blockHash in blocks.keys) {
|
|
|
|
final block = blocks[blockHash];
|
|
|
|
final String amountRaw = block["amount"] as String;
|
|
|
|
final String source = block["source"] as String;
|
|
|
|
await receiveBlock(blockHash, source, amountRaw);
|
2023-05-24 19:09:00 +00:00
|
|
|
// a bit of a hack:
|
|
|
|
await Future<void>.delayed(const Duration(seconds: 1));
|
2023-05-24 18:19:49 +00:00
|
|
|
}
|
2023-05-19 10:20:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> updateTransactions() async {
|
|
|
|
await confirmAllReceivable();
|
2023-05-24 15:56:44 +00:00
|
|
|
final response = await http.post(Uri.parse(getCurrentNode().host),
|
|
|
|
headers: {"Content-Type": "application/json"},
|
|
|
|
body: jsonEncode({
|
|
|
|
"action": "account_history",
|
|
|
|
"account": await getAddressFromMnemonic(),
|
|
|
|
"count": "-1",
|
|
|
|
}));
|
2023-05-19 10:20:16 +00:00
|
|
|
final data = await jsonDecode(response.body);
|
|
|
|
final transactions = data["history"] as List<dynamic>;
|
|
|
|
if (transactions.isEmpty) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
List<Transaction> transactionList = [];
|
|
|
|
for (var tx in transactions) {
|
|
|
|
var typeString = tx["type"].toString();
|
|
|
|
TransactionType type = TransactionType.unknown;
|
|
|
|
if (typeString == "send") {
|
|
|
|
type = TransactionType.outgoing;
|
|
|
|
} else if (typeString == "receive") {
|
|
|
|
type = TransactionType.incoming;
|
|
|
|
}
|
2023-05-24 18:11:33 +00:00
|
|
|
final amount = Amount(
|
|
|
|
rawValue: BigInt.parse(tx["amount"].toString()),
|
|
|
|
fractionDigits: coin.decimals,
|
|
|
|
);
|
|
|
|
|
2023-05-19 10:20:16 +00:00
|
|
|
var transaction = Transaction(
|
|
|
|
walletId: walletId,
|
|
|
|
txid: tx["hash"].toString(),
|
|
|
|
timestamp: int.parse(tx["local_timestamp"].toString()),
|
|
|
|
type: type,
|
|
|
|
subType: TransactionSubType.none,
|
2023-05-24 18:11:33 +00:00
|
|
|
amount: 0,
|
|
|
|
amountString: amount.toJsonString(),
|
2023-05-24 15:56:44 +00:00
|
|
|
fee: 0,
|
2023-05-19 10:20:16 +00:00
|
|
|
height: int.parse(tx["height"].toString()),
|
|
|
|
isCancelled: false,
|
|
|
|
isLelantus: false,
|
|
|
|
slateId: "",
|
|
|
|
otherData: "",
|
|
|
|
inputs: [],
|
|
|
|
outputs: [],
|
2023-05-24 15:56:44 +00:00
|
|
|
nonce: 0);
|
2023-05-19 10:20:16 +00:00
|
|
|
transactionList.add(transaction);
|
|
|
|
}
|
|
|
|
await db.putTransactions(transactionList);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-05-24 18:19:49 +00:00
|
|
|
Future<void> fullRescan(
|
|
|
|
int maxUnusedAddressGap, int maxNumberOfIndexesToCheck) async {
|
2023-05-19 10:20:16 +00:00
|
|
|
await _prefs.init();
|
|
|
|
await updateBalance();
|
|
|
|
await updateTransactions();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<bool> generateNewAddress() {
|
|
|
|
// TODO: implement generateNewAddress
|
|
|
|
throw UnimplementedError();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get hasCalledExit => _hasCalledExit;
|
|
|
|
bool _hasCalledExit = false;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> initializeExisting() async {
|
|
|
|
await _prefs.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> initializeNew() async {
|
|
|
|
if ((await mnemonicString) != null || (await mnemonicPassphrase) != null) {
|
2023-05-24 18:19:49 +00:00
|
|
|
throw Exception(
|
|
|
|
"Attempted to overwrite mnemonic on generate new wallet!");
|
2023-05-19 10:20:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await _prefs.init();
|
|
|
|
|
|
|
|
String seed = NanoSeeds.generateSeed();
|
|
|
|
final mnemonic = NanoMnemomics.seedToMnemonic(seed);
|
|
|
|
await _secureStore.write(
|
2023-05-24 15:56:44 +00:00
|
|
|
key: '${_walletId}_mnemonic',
|
|
|
|
value: mnemonic.join(' '),
|
2023-05-19 10:20:16 +00:00
|
|
|
);
|
|
|
|
await _secureStore.write(
|
|
|
|
key: '${_walletId}_mnemonicPassphrase',
|
|
|
|
value: "",
|
|
|
|
);
|
|
|
|
String privateKey = NanoKeys.seedToPrivate(seed, 0);
|
|
|
|
String publicKey = NanoKeys.createPublicKey(privateKey);
|
2023-05-24 18:19:49 +00:00
|
|
|
String publicAddress =
|
|
|
|
NanoAccounts.createAccount(NanoAccountType.NANO, publicKey);
|
2023-05-19 10:20:16 +00:00
|
|
|
|
|
|
|
final address = Address(
|
|
|
|
walletId: walletId,
|
|
|
|
value: publicAddress,
|
|
|
|
publicKey: [], // TODO: add public key
|
|
|
|
derivationIndex: 0,
|
|
|
|
derivationPath: DerivationPath(),
|
|
|
|
type: AddressType.unknown,
|
|
|
|
subType: AddressSubType.receiving,
|
|
|
|
);
|
|
|
|
|
|
|
|
await db.putAddress(address);
|
|
|
|
|
2023-05-24 18:19:49 +00:00
|
|
|
await Future.wait(
|
|
|
|
[updateCachedId(walletId), updateCachedIsFavorite(false)]);
|
2023-05-19 10:20:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get isConnected => _isConnected;
|
|
|
|
|
|
|
|
bool _isConnected = false;
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get isRefreshing => refreshMutex;
|
|
|
|
|
|
|
|
bool refreshMutex = false;
|
|
|
|
|
|
|
|
@override
|
2023-05-24 15:56:44 +00:00
|
|
|
Future<int> get maxFee => Future.value(0);
|
2023-05-19 10:20:16 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<List<String>> get mnemonic => _getMnemonicList();
|
|
|
|
|
|
|
|
Future<List<String>> _getMnemonicList() async {
|
|
|
|
final _mnemonicString = await mnemonicString;
|
|
|
|
if (_mnemonicString == null) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
final List<String> data = _mnemonicString.split(' ');
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-05-24 15:56:44 +00:00
|
|
|
Future<Map<String, dynamic>> prepareSend({
|
|
|
|
required String address,
|
|
|
|
required Amount amount,
|
|
|
|
Map<String, dynamic>? args,
|
|
|
|
}) async {
|
|
|
|
try {
|
|
|
|
int satAmount = amount.raw.toInt();
|
|
|
|
int realfee = 0;
|
|
|
|
|
|
|
|
if (balance.spendable == amount) {
|
|
|
|
satAmount = balance.spendable.raw.toInt() - realfee;
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, dynamic> txData = {
|
|
|
|
"fee": realfee,
|
|
|
|
"addresss": address,
|
|
|
|
"recipientAmt": Amount(
|
|
|
|
rawValue: BigInt.from(satAmount),
|
|
|
|
fractionDigits: coin.decimals,
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
Logging.instance.log("prepare send: $txData", level: LogLevel.Info);
|
|
|
|
return txData;
|
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log("Error getting fees $e - $s", level: LogLevel.Error);
|
|
|
|
rethrow;
|
|
|
|
}
|
2023-05-19 10:20:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-05-24 15:56:44 +00:00
|
|
|
Future<void> recoverFromMnemonic(
|
|
|
|
{required String mnemonic,
|
|
|
|
String? mnemonicPassphrase,
|
|
|
|
required int maxUnusedAddressGap,
|
|
|
|
required int maxNumberOfIndexesToCheck,
|
|
|
|
required int height}) async {
|
2023-05-19 10:20:16 +00:00
|
|
|
try {
|
2023-05-24 18:19:49 +00:00
|
|
|
if ((await mnemonicString) != null ||
|
|
|
|
(await this.mnemonicPassphrase) != null) {
|
2023-05-19 10:20:16 +00:00
|
|
|
throw Exception("Attempted to overwrite mnemonic on restore!");
|
|
|
|
}
|
2023-05-24 15:56:44 +00:00
|
|
|
|
2023-05-24 18:19:49 +00:00
|
|
|
await _secureStore.write(
|
|
|
|
key: '${_walletId}_mnemonic', value: mnemonic.trim());
|
2023-05-19 10:20:16 +00:00
|
|
|
await _secureStore.write(
|
|
|
|
key: '${_walletId}_mnemonicPassphrase',
|
|
|
|
value: mnemonicPassphrase ?? "",
|
|
|
|
);
|
2023-05-24 15:56:44 +00:00
|
|
|
|
2023-05-19 10:20:16 +00:00
|
|
|
String seed = NanoMnemomics.mnemonicListToSeed(mnemonic.split(" "));
|
|
|
|
String privateKey = NanoKeys.seedToPrivate(seed, 0);
|
|
|
|
String publicKey = NanoKeys.createPublicKey(privateKey);
|
2023-05-24 18:19:49 +00:00
|
|
|
String publicAddress =
|
|
|
|
NanoAccounts.createAccount(NanoAccountType.NANO, publicKey);
|
2023-05-19 10:20:16 +00:00
|
|
|
|
|
|
|
final address = Address(
|
|
|
|
walletId: walletId,
|
|
|
|
value: publicAddress,
|
|
|
|
publicKey: [], // TODO: add public key
|
|
|
|
derivationIndex: 0,
|
2023-05-24 18:19:49 +00:00
|
|
|
derivationPath: DerivationPath()
|
|
|
|
..value = "0/0", // TODO: Check if this is true
|
2023-05-19 10:20:16 +00:00
|
|
|
type: AddressType.unknown,
|
|
|
|
subType: AddressSubType.receiving,
|
|
|
|
);
|
|
|
|
|
|
|
|
await db.putAddress(address);
|
|
|
|
|
2023-05-24 18:19:49 +00:00
|
|
|
await Future.wait(
|
|
|
|
[updateCachedId(walletId), updateCachedIsFavorite(false)]);
|
2023-05-19 10:20:16 +00:00
|
|
|
} catch (e) {
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> refresh() async {
|
|
|
|
await _prefs.init();
|
|
|
|
await updateBalance();
|
|
|
|
await updateTransactions();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
int get storedChainHeight => getCachedChainHeight();
|
|
|
|
|
|
|
|
NodeModel getCurrentNode() {
|
|
|
|
return _xnoNode ??
|
2023-05-24 18:19:49 +00:00
|
|
|
NodeService(secureStorageInterface: _secureStore)
|
|
|
|
.getPrimaryNodeFor(coin: coin) ??
|
2023-05-19 10:20:16 +00:00
|
|
|
DefaultNodes.getNodeFor(coin);
|
|
|
|
}
|
2023-05-24 15:56:44 +00:00
|
|
|
|
2023-05-19 10:20:16 +00:00
|
|
|
@override
|
|
|
|
Future<bool> testNetworkConnection() {
|
2023-05-24 18:19:49 +00:00
|
|
|
http
|
|
|
|
.get(Uri.parse("${getCurrentNode().host}?action=version"))
|
|
|
|
.then((response) {
|
2023-05-19 10:20:16 +00:00
|
|
|
if (response.statusCode == 200) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return Future.value(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-05-24 18:19:49 +00:00
|
|
|
Future<List<Transaction>> get transactions =>
|
|
|
|
db.getTransactions(walletId).findAll();
|
2023-05-19 10:20:16 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> updateNode(bool shouldRefresh) async {
|
2023-05-24 18:19:49 +00:00
|
|
|
_xnoNode = NodeService(secureStorageInterface: _secureStore)
|
|
|
|
.getPrimaryNodeFor(coin: coin) ??
|
2023-05-19 10:20:16 +00:00
|
|
|
DefaultNodes.getNodeFor(coin);
|
|
|
|
|
|
|
|
if (shouldRefresh) {
|
|
|
|
unawaited(refresh());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> updateSentCachedTxData(Map<String, dynamic> txData) {
|
|
|
|
// TODO: implement updateSentCachedTxData
|
|
|
|
throw UnimplementedError();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
// TODO: implement utxos
|
|
|
|
Future<List<UTXO>> get utxos => throw UnimplementedError();
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool validateAddress(String address) {
|
|
|
|
return NanoAccounts.isValid(NanoAccountType.NANO, address);
|
|
|
|
}
|
2023-05-24 15:56:44 +00:00
|
|
|
}
|