stack_wallet/lib/wallets/api/lelantus_ffi_wrapper.dart

555 lines
17 KiB
Dart
Raw Normal View History

import 'package:bip32/bip32.dart';
2023-11-20 15:15:36 +00:00
import 'package:bitcoindart/bitcoindart.dart' as bitcoindart;
import 'package:flutter/foundation.dart';
import 'package:lelantus/lelantus.dart' as lelantus;
2024-05-27 23:56:22 +00:00
import '../../models/isar/models/isar_models.dart' as isar_models;
import '../../models/isar/models/isar_models.dart';
import '../../models/lelantus_fee_data.dart';
import '../../utilities/amount/amount.dart';
import '../../utilities/extensions/impl/string.dart';
import '../../utilities/extensions/impl/uint8_list.dart';
import '../../utilities/format.dart';
import '../../utilities/logger.dart';
import '../crypto_currency/intermediate/bip39_hd_currency.dart';
import '../models/tx_data.dart';
2023-11-20 15:15:36 +00:00
abstract final class LelantusFfiWrapper {
static const MINT_LIMIT = 5001 * 100000000;
static const MINT_LIMIT_TESTNET = 1001 * 100000000;
static const JMINT_INDEX = 5;
static const MINT_INDEX = 2;
static const TRANSACTION_LELANTUS = 8;
static const ANONYMITY_SET_EMPTY_ID = 0;
// partialDerivationPath should be something like "m/$purpose'/$coinType'/$account'/"
static Future<({List<String> spendTxIds, List<LelantusCoin> lelantusCoins})>
restore({
required final String hexRootPrivateKey,
required final Uint8List chaincode,
required final Bip39HDCurrency cryptoCurrency,
required final int latestSetId,
required final Map<dynamic, dynamic> setDataMap,
required final Set<String> usedSerialNumbers,
2023-11-20 15:15:36 +00:00
required final String walletId,
required final String partialDerivationPath,
}) async {
final args = (
hexRootPrivateKey: hexRootPrivateKey,
chaincode: chaincode,
cryptoCurrency: cryptoCurrency,
latestSetId: latestSetId,
setDataMap: setDataMap,
usedSerialNumbers: usedSerialNumbers,
walletId: walletId,
partialDerivationPath: partialDerivationPath,
);
try {
return await compute(_restore, args);
} catch (e, s) {
Logging.instance.log(
"Exception rethrown from _restore(): $e\n$s",
level: LogLevel.Info,
);
rethrow;
}
2023-11-20 15:15:36 +00:00
}
// partialDerivationPath should be something like "m/$purpose'/$coinType'/$account'/"
static Future<({List<String> spendTxIds, List<LelantusCoin> lelantusCoins})>
_restore(
({
String hexRootPrivateKey,
Uint8List chaincode,
Bip39HDCurrency cryptoCurrency,
int latestSetId,
Map<dynamic, dynamic> setDataMap,
Set<String> usedSerialNumbers,
String walletId,
String partialDerivationPath,
}) args,
) async {
2024-05-27 23:56:22 +00:00
final List<int> jindexes = [];
final List<isar_models.LelantusCoin> lelantusCoins = [];
2023-11-20 15:15:36 +00:00
final List<String> spendTxIds = [];
int lastFoundIndex = 0;
int currentIndex = 0;
final root = BIP32.fromPrivateKey(
args.hexRootPrivateKey.toUint8ListFromHex,
args.chaincode,
);
2023-11-20 15:15:36 +00:00
while (currentIndex < lastFoundIndex + 50) {
final mintKeyPair = root.derivePath(
"${args.partialDerivationPath}$MINT_INDEX/$currentIndex",
);
2023-11-20 15:15:36 +00:00
final String mintTag = lelantus.CreateTag(
mintKeyPair.privateKey!.toHex,
currentIndex,
mintKeyPair.identifier.toHex,
2024-06-19 15:15:49 +00:00
isTestnet: args.cryptoCurrency.network.isTestNet,
);
2023-11-20 15:15:36 +00:00
for (int setId = 1; setId <= args.latestSetId; setId++) {
final setData = args.setDataMap[setId] as Map;
final foundCoin = (setData["coins"] as List).firstWhere(
(e) => e[1] == mintTag,
orElse: () => <Object>[],
);
2023-11-20 15:15:36 +00:00
if (foundCoin.length == 4) {
lastFoundIndex = currentIndex;
final String publicCoin = foundCoin[0] as String;
final String txId = foundCoin[3] as String;
// this value will either be an int or a String
final dynamic thirdValue = foundCoin[2];
if (thirdValue is int) {
final int amount = thirdValue;
final String serialNumber = lelantus.GetSerialNumber(
amount,
mintKeyPair.privateKey!.toHex,
currentIndex,
2024-06-19 15:15:49 +00:00
isTestnet: args.cryptoCurrency.network.isTestNet,
);
final bool isUsed = args.usedSerialNumbers.contains(serialNumber);
2024-05-27 23:56:22 +00:00
lelantusCoins.removeWhere(
(e) =>
e.txid == txId &&
e.mintIndex == currentIndex &&
e.anonymitySetId != setId,
);
lelantusCoins.add(
isar_models.LelantusCoin(
walletId: args.walletId,
mintIndex: currentIndex,
value: amount.toString(),
txid: txId,
anonymitySetId: setId,
isUsed: isUsed,
isJMint: false,
otherData:
publicCoin, // not really needed but saved just in case
),
);
debugPrint("serial=$serialNumber amount=$amount used=$isUsed");
} else if (thirdValue is String) {
final int keyPath = lelantus.GetAesKeyPath(publicCoin);
final aesKeyPair = root.derivePath(
"${args.partialDerivationPath}$JMINT_INDEX/$keyPath",
);
try {
final String aesPrivateKey = aesKeyPair.privateKey!.toHex;
final int amount = lelantus.decryptMintAmount(
aesPrivateKey,
thirdValue,
);
2023-11-20 15:15:36 +00:00
final String serialNumber = lelantus.GetSerialNumber(
amount,
aesPrivateKey,
2023-11-20 15:15:36 +00:00
currentIndex,
2024-06-19 15:15:49 +00:00
isTestnet: args.cryptoCurrency.network.isTestNet,
2023-11-20 15:15:36 +00:00
);
final bool isUsed = args.usedSerialNumbers.contains(serialNumber);
2024-05-27 23:56:22 +00:00
lelantusCoins.removeWhere(
(e) =>
e.txid == txId &&
e.mintIndex == currentIndex &&
e.anonymitySetId != setId,
);
2023-11-20 15:15:36 +00:00
lelantusCoins.add(
isar_models.LelantusCoin(
walletId: args.walletId,
mintIndex: currentIndex,
value: amount.toString(),
txid: txId,
anonymitySetId: setId,
isUsed: isUsed,
isJMint: true,
2023-11-20 15:15:36 +00:00
otherData:
publicCoin, // not really needed but saved just in case
),
);
jindexes.add(currentIndex);
spendTxIds.add(txId);
} catch (_) {
debugPrint("AES keypair derivation issue for key path: $keyPath");
2023-11-20 15:15:36 +00:00
}
} else {
debugPrint("Unexpected coin found: $foundCoin");
2023-11-20 15:15:36 +00:00
}
}
}
currentIndex++;
2023-11-20 15:15:36 +00:00
}
return (spendTxIds: spendTxIds, lelantusCoins: lelantusCoins);
}
static Future<LelantusFeeData> estimateJoinSplitFee({
required Amount spendAmount,
required bool subtractFeeFromAmount,
required List<lelantus.DartLelantusEntry> lelantusEntries,
required bool isTestNet,
}) async {
return await compute(
LelantusFfiWrapper._estimateJoinSplitFee,
(
spendAmount: spendAmount.raw.toInt(),
subtractFeeFromAmount: subtractFeeFromAmount,
lelantusEntries: lelantusEntries,
isTestNet: isTestNet,
),
);
}
static Future<LelantusFeeData> _estimateJoinSplitFee(
2024-05-27 23:56:22 +00:00
({
int spendAmount,
bool subtractFeeFromAmount,
List<lelantus.DartLelantusEntry> lelantusEntries,
bool isTestNet,
}) data,
) async {
debugPrint("estimateJoinSplit fee");
2023-11-20 15:15:36 +00:00
// for (int i = 0; i < lelantusEntries.length; i++) {
// Logging.instance.log(lelantusEntries[i], addToDebugMessagesDB: false);
// }
debugPrint(
2023-11-20 15:15:36 +00:00
"${data.spendAmount} ${data.subtractFeeFromAmount}",
);
2024-05-27 23:56:22 +00:00
final List<int> changeToMint = List.empty(growable: true);
final List<int> spendCoinIndexes = List.empty(growable: true);
2023-11-20 15:15:36 +00:00
// Logging.instance.log(lelantusEntries, addToDebugMessagesDB: false);
final fee = lelantus.estimateFee(
data.spendAmount,
data.subtractFeeFromAmount,
data.lelantusEntries,
changeToMint,
spendCoinIndexes,
isTestnet: data.isTestNet,
);
final estimateFeeData = LelantusFeeData(
changeToMint[0],
fee,
spendCoinIndexes,
);
debugPrint(
2023-11-20 15:15:36 +00:00
"estimateFeeData ${estimateFeeData.changeToMint}"
" ${estimateFeeData.fee}"
" ${estimateFeeData.spendCoinIndexes}",
);
return estimateFeeData;
}
static Future<TxData> createJoinSplitTransaction({
required TxData txData,
required bool subtractFeeFromAmount,
required int nextFreeMintIndex,
required int locktime, // set to current chain height
required List<lelantus.DartLelantusEntry> lelantusEntries,
required List<Map<String, dynamic>> anonymitySets,
required Bip39HDCurrency cryptoCurrency,
required String partialDerivationPath,
required String hexRootPrivateKey,
required Uint8List chaincode,
}) async {
final arg = (
txData: txData,
subtractFeeFromAmount: subtractFeeFromAmount,
index: nextFreeMintIndex,
lelantusEntries: lelantusEntries,
locktime: locktime,
cryptoCurrency: cryptoCurrency,
anonymitySetsArg: anonymitySets,
partialDerivationPath: partialDerivationPath,
hexRootPrivateKey: hexRootPrivateKey,
chaincode: chaincode,
);
return await compute(_createJoinSplitTransaction, arg);
}
static Future<TxData> _createJoinSplitTransaction(
2024-05-27 23:56:22 +00:00
({
TxData txData,
bool subtractFeeFromAmount,
int index,
List<lelantus.DartLelantusEntry> lelantusEntries,
int locktime,
Bip39HDCurrency cryptoCurrency,
List<Map<dynamic, dynamic>> anonymitySetsArg,
String partialDerivationPath,
String hexRootPrivateKey,
Uint8List chaincode,
}) arg,
) async {
2023-11-20 15:15:36 +00:00
final spendAmount = arg.txData.recipients!.first.amount.raw.toInt();
final address = arg.txData.recipients!.first.address;
2024-01-05 19:45:42 +00:00
final isChange = arg.txData.recipients!.first.isChange;
2023-11-20 15:15:36 +00:00
final estimateJoinSplitFee = await _estimateJoinSplitFee(
(
spendAmount: spendAmount,
subtractFeeFromAmount: arg.subtractFeeFromAmount,
lelantusEntries: arg.lelantusEntries,
2024-06-19 15:15:49 +00:00
isTestNet: arg.cryptoCurrency.network.isTestNet,
2023-11-20 15:15:36 +00:00
),
);
final changeToMint = estimateJoinSplitFee.changeToMint;
final fee = estimateJoinSplitFee.fee;
final spendCoinIndexes = estimateJoinSplitFee.spendCoinIndexes;
debugPrint("$changeToMint $fee $spendCoinIndexes");
2023-11-20 15:15:36 +00:00
if (spendCoinIndexes.isEmpty) {
throw Exception("Error, Not enough funds.");
}
final params = arg.cryptoCurrency.networkParams;
final _network = bitcoindart.NetworkType(
messagePrefix: params.messagePrefix,
bech32: params.bech32Hrp,
bip32: bitcoindart.Bip32Type(
public: params.pubHDPrefix,
private: params.privHDPrefix,
),
pubKeyHash: params.p2pkhPrefix,
scriptHash: params.p2shPrefix,
wif: params.wifPrefix,
);
final tx = bitcoindart.TransactionBuilder(network: _network);
tx.setLockTime(arg.locktime);
tx.setVersion(3 | (TRANSACTION_LELANTUS << 16));
tx.addInput(
'0000000000000000000000000000000000000000000000000000000000000000',
4294967295,
4294967295,
Uint8List(0),
);
final derivePath = "${arg.partialDerivationPath}$MINT_INDEX/${arg.index}";
final root = BIP32.fromPrivateKey(
arg.hexRootPrivateKey.toUint8ListFromHex,
2023-11-20 15:15:36 +00:00
arg.chaincode,
);
final jmintKeyPair = root.derivePath(derivePath);
final String jmintprivatekey = jmintKeyPair.privateKey!.toHex;
2023-11-20 15:15:36 +00:00
final keyPath = lelantus.getMintKeyPath(
changeToMint,
jmintprivatekey,
arg.index,
2024-06-19 15:15:49 +00:00
isTestnet: arg.cryptoCurrency.network.isTestNet,
2023-11-20 15:15:36 +00:00
);
final _derivePath = "${arg.partialDerivationPath}$JMINT_INDEX/$keyPath";
final aesKeyPair = root.derivePath(_derivePath);
final aesPrivateKey = aesKeyPair.privateKey!.toHex;
2023-11-20 15:15:36 +00:00
final jmintData = lelantus.createJMintScript(
changeToMint,
jmintprivatekey,
arg.index,
Format.uint8listToString(jmintKeyPair.identifier),
aesPrivateKey,
2024-06-19 15:15:49 +00:00
isTestnet: arg.cryptoCurrency.network.isTestNet,
2023-11-20 15:15:36 +00:00
);
tx.addOutput(
Format.stringToUint8List(jmintData),
0,
);
int amount = spendAmount;
if (arg.subtractFeeFromAmount) {
amount -= fee;
}
tx.addOutput(
address,
amount,
);
final extractedTx = tx.buildIncomplete();
extractedTx.setPayload(Uint8List(0));
final txHash = extractedTx.getId();
final List<int> setIds = [];
final List<List<String>> anonymitySets = [];
final List<String> anonymitySetHashes = [];
final List<String> groupBlockHashes = [];
for (var i = 0; i < arg.lelantusEntries.length; i++) {
final anonymitySetId = arg.lelantusEntries[i].anonymitySetId;
if (!setIds.contains(anonymitySetId)) {
setIds.add(anonymitySetId);
final anonymitySet = arg.anonymitySetsArg.firstWhere(
2024-05-27 23:56:22 +00:00
(element) => element["setId"] == anonymitySetId,
orElse: () => <String, dynamic>{},
);
2023-11-20 15:15:36 +00:00
if (anonymitySet.isNotEmpty) {
anonymitySetHashes.add(anonymitySet['setHash'] as String);
groupBlockHashes.add(anonymitySet['blockHash'] as String);
2024-05-27 23:56:22 +00:00
final List<String> list = [];
2023-11-20 15:15:36 +00:00
for (int i = 0; i < (anonymitySet['coins'] as List).length; i++) {
list.add(anonymitySet['coins'][i][0] as String);
}
anonymitySets.add(list);
}
}
}
final String spendScript = lelantus.createJoinSplitScript(
txHash,
spendAmount,
arg.subtractFeeFromAmount,
jmintprivatekey,
arg.index,
arg.lelantusEntries,
setIds,
anonymitySets,
anonymitySetHashes,
groupBlockHashes,
2024-06-19 15:15:49 +00:00
isTestnet: arg.cryptoCurrency.network.isTestNet,
2023-11-20 15:15:36 +00:00
);
final finalTx = bitcoindart.TransactionBuilder(network: _network);
finalTx.setLockTime(arg.locktime);
finalTx.setVersion(3 | (TRANSACTION_LELANTUS << 16));
finalTx.addOutput(
Format.stringToUint8List(jmintData),
0,
);
finalTx.addOutput(
address,
amount,
);
final extTx = finalTx.buildIncomplete();
extTx.addInput(
Format.stringToUint8List(
2024-05-27 23:56:22 +00:00
'0000000000000000000000000000000000000000000000000000000000000000',
),
2023-11-20 15:15:36 +00:00
4294967295,
4294967295,
Format.stringToUint8List("c9"),
);
// debugPrint("spendscript: $spendScript");
extTx.setPayload(Format.stringToUint8List(spendScript));
final txHex = extTx.toHex();
final txId = extTx.getId();
final amountAmount = Amount(
rawValue: BigInt.from(amount),
fractionDigits: arg.cryptoCurrency.fractionDigits,
);
return arg.txData.copyWith(
txid: txId,
raw: txHex,
2024-01-05 19:45:42 +00:00
recipients: [
2024-05-27 23:56:22 +00:00
(address: address, amount: amountAmount, isChange: isChange),
2024-01-05 19:45:42 +00:00
],
2023-11-20 15:15:36 +00:00
fee: Amount(
rawValue: BigInt.from(fee),
fractionDigits: arg.cryptoCurrency.fractionDigits,
),
vSize: extTx.virtualSize(),
jMintValue: changeToMint,
spendCoinIndexes: spendCoinIndexes,
height: arg.locktime,
txType: TransactionType.outgoing,
txSubType: TransactionSubType.join,
// "confirmed_status": false,
// "timestamp": DateTime.now().millisecondsSinceEpoch ~/ 1000,
);
// return {
// "txid": txId,
// "txHex": txHex,
// "value": amount,
// "fees": Amount(
// rawValue: BigInt.from(fee),
// fractionDigits: arg.cryptoCurrency.fractionDigits,
// ).decimal.toDouble(),
// "fee": fee,
// "vSize": extTx.virtualSize(),
// "jmintValue": changeToMint,
// "spendCoinIndexes": spendCoinIndexes,
// "height": arg.locktime,
// "txType": "Sent",
// "confirmed_status": false,
// "amount": amountAmount.decimal.toDouble(),
// "recipientAmt": amountAmount,
// "address": arg.address,
// "timestamp": DateTime.now().millisecondsSinceEpoch ~/ 1000,
// "subType": "join",
// };
}
// ===========================================================================
static Future<String> _getMintScriptWrapper(
2024-05-27 23:56:22 +00:00
({
int amount,
String privateKeyHex,
int index,
String seedId,
bool isTestNet
}) data,
) async {
final String mintHex = lelantus.getMintScript(
2023-11-20 15:15:36 +00:00
data.amount,
data.privateKeyHex,
data.index,
data.seedId,
isTestnet: data.isTestNet,
);
return mintHex;
}
static Future<String> getMintScript({
required Amount amount,
required String privateKeyHex,
required int index,
required String seedId,
required bool isTestNet,
}) async {
return await compute(
LelantusFfiWrapper._getMintScriptWrapper,
(
amount: amount.raw.toInt(),
privateKeyHex: privateKeyHex,
index: index,
seedId: seedId,
isTestNet: isTestNet
),
);
}
}