mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2024-11-17 09:47:37 +00:00
feat: update xtz
This commit is contained in:
parent
f43aeadc3f
commit
d04df00691
3 changed files with 123 additions and 26 deletions
|
@ -1,6 +1,9 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:ffi';
|
||||||
|
|
||||||
import 'package:http/http.dart';
|
import 'package:http/http.dart';
|
||||||
|
import 'package:isar/isar.dart';
|
||||||
import 'package:stackwallet/models/balance.dart';
|
import 'package:stackwallet/models/balance.dart';
|
||||||
import 'package:stackwallet/models/isar/models/blockchain_data/address.dart';
|
import 'package:stackwallet/models/isar/models/blockchain_data/address.dart';
|
||||||
import 'package:stackwallet/models/isar/models/blockchain_data/transaction.dart';
|
import 'package:stackwallet/models/isar/models/blockchain_data/transaction.dart';
|
||||||
|
@ -11,8 +14,10 @@ import 'package:stackwallet/services/node_service.dart';
|
||||||
import 'package:stackwallet/utilities/amount/amount.dart';
|
import 'package:stackwallet/utilities/amount/amount.dart';
|
||||||
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
||||||
import 'package:stackwallet/utilities/default_nodes.dart';
|
import 'package:stackwallet/utilities/default_nodes.dart';
|
||||||
|
import 'package:stackwallet/utilities/enums/fee_rate_type_enum.dart';
|
||||||
|
|
||||||
import 'package:tezart/tezart.dart';
|
import 'package:tezart/tezart.dart';
|
||||||
|
import 'package:tuple/tuple.dart';
|
||||||
|
|
||||||
import '../../../db/isar/main_db.dart';
|
import '../../../db/isar/main_db.dart';
|
||||||
import '../../../models/node_model.dart';
|
import '../../../models/node_model.dart';
|
||||||
|
@ -49,6 +54,10 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
||||||
return _xtzNode ?? NodeService(secureStorageInterface: _secureStore).getPrimaryNodeFor(coin: Coin.tezos) ?? DefaultNodes.getNodeFor(Coin.tezos);
|
return _xtzNode ?? NodeService(secureStorageInterface: _secureStore).getPrimaryNodeFor(coin: Coin.tezos) ?? DefaultNodes.getNodeFor(Coin.tezos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<Keystore> getKeystore() async {
|
||||||
|
return Keystore.fromMnemonic((await mnemonicString).toString());
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String get walletId => _walletId;
|
String get walletId => _walletId;
|
||||||
late String _walletId;
|
late String _walletId;
|
||||||
|
@ -102,10 +111,36 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
||||||
Balance? _balance;
|
Balance? _balance;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<String> confirmSend({required Map<String, dynamic> txData}) {
|
Future<Map<String, dynamic>> prepareSend({required String address, required Amount amount, Map<String, dynamic>? args}) async {
|
||||||
// TODO: implement confirmSend,
|
try {
|
||||||
// NOTE FROM DETHERMINAL: I couldnt write this function because I dont have any tezos to test with
|
if (amount.decimals != coin.decimals) {
|
||||||
throw UnimplementedError();
|
throw Exception("Amount decimals do not match coin decimals!");
|
||||||
|
}
|
||||||
|
var fee = int.parse((await estimateFeeFor(amount, (args!["feeRate"] as FeeRateType).index)).raw.toString());
|
||||||
|
Map<String, dynamic> txData = {
|
||||||
|
"fee": fee,
|
||||||
|
"address": address,
|
||||||
|
"recipientAmt": amount,
|
||||||
|
};
|
||||||
|
return Future.value(txData);
|
||||||
|
} catch (e) {
|
||||||
|
return Future.error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<String> confirmSend({required Map<String, dynamic> txData}) async {
|
||||||
|
try {
|
||||||
|
final node = getCurrentNode().host + getCurrentNode().port.toString();
|
||||||
|
final int amountInMicroTez = ((int.parse((txData["recipientAmt"] as Amount).raw.toString()) * 1000000)).round();
|
||||||
|
final int feeInMicroTez = int.parse(txData["fee"].toString());
|
||||||
|
final String destinationAddress = txData["address"] as String;
|
||||||
|
final String sourceAddress = await currentReceivingAddress;
|
||||||
|
return Future.value(""); // TODO: return tx hash
|
||||||
|
} catch (e) {
|
||||||
|
Logging.instance.log(e.toString(), level: LogLevel.Error);
|
||||||
|
return Future.error(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -114,13 +149,14 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
||||||
if (mneString == null) {
|
if (mneString == null) {
|
||||||
throw Exception("No mnemonic found!");
|
throw Exception("No mnemonic found!");
|
||||||
}
|
}
|
||||||
return Future.value(Keystore.fromMnemonic(mneString).address);
|
return Future.value((Keystore.fromMnemonic(mneString)).address);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Amount> estimateFeeFor(Amount amount, int feeRate) {
|
Future<Amount> estimateFeeFor(Amount amount, int feeRate) {
|
||||||
// TODO: implement estimateFeeFor
|
return Future.value(
|
||||||
throw UnimplementedError();
|
Amount(rawValue: BigInt.parse(100000.toString()), fractionDigits: coin.decimals),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -130,13 +166,22 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
// TODO: implement fees
|
Future<FeeObject> get fees async {
|
||||||
Future<FeeObject> get fees => throw UnimplementedError();
|
// TODO: Change this to get fees from node and fix numberOfBlocks
|
||||||
|
return FeeObject(
|
||||||
|
numberOfBlocksFast: 1,
|
||||||
|
numberOfBlocksAverage: 1,
|
||||||
|
numberOfBlocksSlow: 1,
|
||||||
|
fast: 1000000,
|
||||||
|
medium: 100000,
|
||||||
|
slow: 10000,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> fullRescan(int maxUnusedAddressGap, int maxNumberOfIndexesToCheck) {
|
Future<void> fullRescan(int maxUnusedAddressGap, int maxNumberOfIndexesToCheck) {
|
||||||
// TODO: implement fullRescan
|
refresh();
|
||||||
throw UnimplementedError();
|
return Future.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -215,13 +260,6 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
||||||
@override
|
@override
|
||||||
Future<String?> get mnemonicString => _secureStore.read(key: '${_walletId}_mnemonic');
|
Future<String?> get mnemonicString => _secureStore.read(key: '${_walletId}_mnemonic');
|
||||||
|
|
||||||
@override
|
|
||||||
Future<Map<String, dynamic>> prepareSend({required String address, required Amount amount, Map<String, dynamic>? args}) {
|
|
||||||
// TODO: implement prepareSend
|
|
||||||
// NOTE FROM DETHERMINAL: I couldnt write this function because I dont have any tezos to test with
|
|
||||||
throw UnimplementedError();
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> recoverFromMnemonic({required String mnemonic, String? mnemonicPassphrase, required int maxUnusedAddressGap, required int maxNumberOfIndexesToCheck, required int height}) async {
|
Future<void> recoverFromMnemonic({required String mnemonic, String? mnemonicPassphrase, required int maxUnusedAddressGap, required int maxNumberOfIndexesToCheck, required int height}) async {
|
||||||
if ((await mnemonicString) != null ||
|
if ((await mnemonicString) != null ||
|
||||||
|
@ -267,9 +305,66 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
||||||
await updateCachedBalance(_balance!);
|
await updateCachedBalance(_balance!);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> updateTransactions() async {
|
||||||
|
var api = "https://api.mainnet.tzkt.io/v1/accounts/${await currentReceivingAddress}/balance_history"; // TODO: Can we use current node instead of this?
|
||||||
|
var returnedTxs = await get(Uri.parse(api)).then((value) => value.body);
|
||||||
|
Logging.instance.log(
|
||||||
|
"Transactions for ${await currentReceivingAddress}: $returnedTxs",
|
||||||
|
level: LogLevel.Info);
|
||||||
|
List<Tuple2<Transaction, Address>> txs = [];
|
||||||
|
Object? jsonTxs = jsonDecode(returnedTxs);
|
||||||
|
if (jsonTxs == null) {
|
||||||
|
await db.addNewTransactionData(txs, walletId);
|
||||||
|
} else {
|
||||||
|
for (var tx in jsonTxs as List) {
|
||||||
|
var theTx = Transaction(
|
||||||
|
walletId: walletId,
|
||||||
|
txid: "",
|
||||||
|
timestamp: DateTime.parse(tx["timestamp"].toString()).toUtc().millisecondsSinceEpoch ~/ 1000,
|
||||||
|
type: TransactionType.unknown,
|
||||||
|
subType: TransactionSubType.none,
|
||||||
|
amount: int.parse(tx["balance"].toString()),
|
||||||
|
amountString: Amount(
|
||||||
|
rawValue: BigInt.parse(tx["balance"].toString()),
|
||||||
|
fractionDigits: 6
|
||||||
|
).toJsonString(),
|
||||||
|
fee: 0,
|
||||||
|
height: int.parse(tx["level"].toString()),
|
||||||
|
isCancelled: false,
|
||||||
|
isLelantus: false,
|
||||||
|
slateId: "",
|
||||||
|
otherData: "",
|
||||||
|
inputs: [],
|
||||||
|
outputs: [],
|
||||||
|
nonce: 0
|
||||||
|
);
|
||||||
|
var theAddress = Address(
|
||||||
|
walletId: walletId,
|
||||||
|
value: await currentReceivingAddress,
|
||||||
|
publicKey: [], // TODO: Add public key
|
||||||
|
derivationIndex: 0,
|
||||||
|
derivationPath: null,
|
||||||
|
type: AddressType.unknown,
|
||||||
|
subType: AddressSubType.unknown,
|
||||||
|
);
|
||||||
|
txs.add(Tuple2(theTx, theAddress));
|
||||||
|
}
|
||||||
|
await db.addNewTransactionData(txs, walletId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> updateChainHeight() async {
|
||||||
|
var api = "https://api.tzkt.io/v1/blocks/count"; // TODO: Can we use current node instead of this?
|
||||||
|
var returnedHeight = await get(Uri.parse(api)).then((value) => value.body);
|
||||||
|
final int intHeight = int.parse(returnedHeight.toString());
|
||||||
|
await updateCachedChainHeight(intHeight);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> refresh() {
|
Future<void> refresh() {
|
||||||
|
updateChainHeight();
|
||||||
updateBalance();
|
updateBalance();
|
||||||
|
updateTransactions();
|
||||||
return Future.value();
|
return Future.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -277,16 +372,17 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
||||||
int get storedChainHeight => getCachedChainHeight();
|
int get storedChainHeight => getCachedChainHeight();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<bool> testNetworkConnection() {
|
Future<bool> testNetworkConnection() async{
|
||||||
// TODO: implement testNetworkConnection
|
try {
|
||||||
throw UnimplementedError();
|
await get(Uri.parse("https://api.mainnet.tzkt.io/v1/accounts/${await currentReceivingAddress}/balance"));
|
||||||
|
return true;
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
// TODO: implement transactions
|
Future<List<Transaction>> get transactions => db.getTransactions(walletId).findAll();
|
||||||
Future<List<Transaction>> get transactions async {
|
|
||||||
// TODO: Maybe we can use this -> https://api.tzkt.io/#operation/Accounts_GetBalanceHistory
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> updateNode(bool shouldRefresh) async {
|
Future<void> updateNode(bool shouldRefresh) async {
|
||||||
|
|
|
@ -136,6 +136,7 @@ abstract class Constants {
|
||||||
case Coin.ethereum:
|
case Coin.ethereum:
|
||||||
case Coin.namecoin:
|
case Coin.namecoin:
|
||||||
case Coin.particl:
|
case Coin.particl:
|
||||||
|
case Coin.tezos:
|
||||||
values.addAll([24, 21, 18, 15, 12]);
|
values.addAll([24, 21, 18, 15, 12]);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
|
@ -173,7 +173,7 @@ abstract class DefaultNodes {
|
||||||
);
|
);
|
||||||
|
|
||||||
static NodeModel get tezos => NodeModel( // TODO: Change this to original one
|
static NodeModel get tezos => NodeModel( // TODO: Change this to original one
|
||||||
host: "https://mainnet.api.tez.ie",
|
host: "mainnet.api.tez.ie",
|
||||||
port: 443,
|
port: 443,
|
||||||
name: defaultName,
|
name: defaultName,
|
||||||
id: _nodeId(Coin.tezos),
|
id: _nodeId(Coin.tezos),
|
||||||
|
|
Loading…
Reference in a new issue