mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2025-04-07 23:07:41 +00:00
WIP spark scanning
This commit is contained in:
parent
9ff323393e
commit
71e89b489f
1 changed files with 67 additions and 43 deletions
|
@ -4,7 +4,7 @@ import 'package:flutter_libsparkmobile/flutter_libsparkmobile.dart';
|
|||
import 'package:isar/isar.dart';
|
||||
import 'package:stackwallet/models/isar/models/blockchain_data/address.dart';
|
||||
import 'package:stackwallet/utilities/amount/amount.dart';
|
||||
import 'package:stackwallet/utilities/extensions/impl/uint8_list.dart';
|
||||
import 'package:stackwallet/utilities/extensions/extensions.dart';
|
||||
import 'package:stackwallet/wallets/crypto_currency/crypto_currency.dart';
|
||||
import 'package:stackwallet/wallets/isar/models/spark_coin.dart';
|
||||
import 'package:stackwallet/wallets/models/tx_data.dart';
|
||||
|
@ -76,7 +76,12 @@ mixin SparkInterface on Bip39HDWallet, ElectrumXInterface {
|
|||
const index = 1;
|
||||
|
||||
final root = await getRootHDNode();
|
||||
const derivationPath = "$kSparkBaseDerivationPath$index";
|
||||
final String derivationPath;
|
||||
if (cryptoCurrency.network == CryptoCurrencyNetwork.test) {
|
||||
derivationPath = "$kSparkBaseDerivationPathTestnet$index";
|
||||
} else {
|
||||
derivationPath = "$kSparkBaseDerivationPath$index";
|
||||
}
|
||||
final keys = root.derivePath(derivationPath);
|
||||
|
||||
final String addressString = await LibSpark.getAddress(
|
||||
|
@ -152,9 +157,20 @@ mixin SparkInterface on Bip39HDWallet, ElectrumXInterface {
|
|||
// recoverSparkWallet but only fetch and check anonymity set data that we
|
||||
// have not yet parsed.
|
||||
Future<void> refreshSparkData() async {
|
||||
final sparkAddresses = await mainDB.isar.addresses
|
||||
.where()
|
||||
.walletIdEqualTo(walletId)
|
||||
.filter()
|
||||
.typeEqualTo(AddressType.spark)
|
||||
.findAll();
|
||||
|
||||
final Set<String> paths =
|
||||
sparkAddresses.map((e) => e.derivationPath!.value).toSet();
|
||||
|
||||
try {
|
||||
final privateKeyHex = "TODO";
|
||||
final index = 0;
|
||||
const index = 1;
|
||||
|
||||
final root = await getRootHDNode();
|
||||
|
||||
final latestSparkCoinId = await electrumXClient.getSparkLatestCoinId();
|
||||
|
||||
|
@ -175,50 +191,58 @@ mixin SparkInterface on Bip39HDWallet, ElectrumXInterface {
|
|||
|
||||
// find our coins
|
||||
final List<SparkCoin> myCoins = [];
|
||||
for (final data
|
||||
in List<List<String>>.from(anonymitySet["coin"] as List)) {
|
||||
if (data.length != 2) {
|
||||
throw Exception("Unexpected serialized coin info found");
|
||||
}
|
||||
|
||||
final serializedCoin = data.first;
|
||||
final txHash = data.last;
|
||||
for (final path in paths) {
|
||||
final keys = root.derivePath(path);
|
||||
|
||||
final coin = LibSpark.identifyAndRecoverCoin(
|
||||
serializedCoin,
|
||||
privateKeyHex: privateKeyHex,
|
||||
index: index,
|
||||
);
|
||||
final privateKeyHex = keys.privateKey.data.toHex;
|
||||
|
||||
// its ours
|
||||
if (coin != null) {
|
||||
final SparkCoinType coinType;
|
||||
switch (coin.type.value) {
|
||||
case 0:
|
||||
coinType = SparkCoinType.mint;
|
||||
case 1:
|
||||
coinType = SparkCoinType.spend;
|
||||
default:
|
||||
throw Exception("Unknown spark coin type detected");
|
||||
for (final dynData in anonymitySet["coins"] as List) {
|
||||
final data = List<String>.from(dynData as List);
|
||||
|
||||
if (data.length != 2) {
|
||||
throw Exception("Unexpected serialized coin info found");
|
||||
}
|
||||
myCoins.add(
|
||||
SparkCoin(
|
||||
walletId: walletId,
|
||||
type: coinType,
|
||||
isUsed: spentCoinTags
|
||||
.contains(coin.lTagHash!.toHex), // TODO: is hex right?
|
||||
address: coin.address!,
|
||||
txHash: txHash,
|
||||
valueIntString: coin.value!.toString(),
|
||||
lTagHash: coin.lTagHash!.toHex, // TODO: is hex right?
|
||||
tag: coin.tag,
|
||||
memo: coin.memo,
|
||||
serial: coin.serial,
|
||||
serialContext: coin.serialContext,
|
||||
diversifierIntString: coin.diversifier!.toString(),
|
||||
encryptedDiversifier: coin.encryptedDiversifier,
|
||||
),
|
||||
|
||||
final serializedCoin = data.first;
|
||||
final txHash = data.last;
|
||||
|
||||
final coin = LibSpark.identifyAndRecoverCoin(
|
||||
serializedCoin,
|
||||
privateKeyHex: privateKeyHex,
|
||||
index: index,
|
||||
isTestNet: cryptoCurrency.network == CryptoCurrencyNetwork.test,
|
||||
);
|
||||
|
||||
// its ours
|
||||
if (coin != null) {
|
||||
final SparkCoinType coinType;
|
||||
switch (coin.type.value) {
|
||||
case 0:
|
||||
coinType = SparkCoinType.mint;
|
||||
case 1:
|
||||
coinType = SparkCoinType.spend;
|
||||
default:
|
||||
throw Exception("Unknown spark coin type detected");
|
||||
}
|
||||
myCoins.add(
|
||||
SparkCoin(
|
||||
walletId: walletId,
|
||||
type: coinType,
|
||||
isUsed: spentCoinTags.contains(coin.lTagHash!),
|
||||
address: coin.address!,
|
||||
txHash: txHash,
|
||||
valueIntString: coin.value!.toString(),
|
||||
lTagHash: coin.lTagHash!,
|
||||
tag: coin.tag,
|
||||
memo: coin.memo,
|
||||
serial: coin.serial,
|
||||
serialContext: coin.serialContext,
|
||||
diversifierIntString: coin.diversifier!.toString(),
|
||||
encryptedDiversifier: coin.encryptedDiversifier,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue