mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2024-12-22 11:29:23 +00:00
feat: add cardano
This commit is contained in:
parent
2145334152
commit
27d319cece
12 changed files with 802 additions and 5 deletions
|
@ -165,7 +165,8 @@ enum AddressType {
|
|||
tezos,
|
||||
frostMS,
|
||||
p2tr,
|
||||
solana;
|
||||
solana,
|
||||
cardanoShelley;
|
||||
|
||||
String get readableName {
|
||||
switch (this) {
|
||||
|
@ -201,6 +202,8 @@ enum AddressType {
|
|||
return "Solana";
|
||||
case AddressType.p2tr:
|
||||
return "P2TR (taproot)";
|
||||
case AddressType.cardanoShelley:
|
||||
return "Cardano Shelley";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,6 +12,8 @@ import 'dart:async';
|
|||
import 'dart:convert';
|
||||
|
||||
import 'package:bip39/bip39.dart' as bip39;
|
||||
import 'package:blockchain_utils/bip/bip/bip39/bip39_mnemonic.dart';
|
||||
import 'package:blockchain_utils/bip/bip/bip39/bip39_mnemonic_generator.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_svg/flutter_svg.dart';
|
||||
|
@ -560,11 +562,16 @@ class _NewWalletRecoveryPhraseWarningViewState
|
|||
wordCount = info
|
||||
.coin.defaultSeedPhraseLength;
|
||||
|
||||
// TODO: Refactor these to generate each coin in their respective classes
|
||||
// This code should not be in a random view page file
|
||||
if (coin is Monero ||
|
||||
coin is Wownero) {
|
||||
// currently a special case due to the
|
||||
// xmr/wow libraries handling their
|
||||
// own mnemonic generation
|
||||
} else if (coin is Cardano) {
|
||||
mnemonicPassphrase = "";
|
||||
mnemonic = Bip39MnemonicGenerator().fromWordsNumber(Bip39WordsNum.wordsNum15).toList().join(" ");
|
||||
} else if (wordCount > 0) {
|
||||
if (ref
|
||||
.read(
|
||||
|
|
|
@ -30,6 +30,7 @@ class PriceAPI {
|
|||
BitcoinFrost: "bitcoin",
|
||||
Litecoin: "litecoin",
|
||||
Bitcoincash: "bitcoin-cash",
|
||||
Cardano: "cardano",
|
||||
Dash: "dash",
|
||||
Dogecoin: "dogecoin",
|
||||
Epiccash: "epic-cash",
|
||||
|
|
|
@ -18,7 +18,8 @@ enum DerivePathType {
|
|||
eth,
|
||||
eCash44,
|
||||
solana,
|
||||
bip86;
|
||||
bip86,
|
||||
cardanoShelley;
|
||||
|
||||
AddressType getAddressType() {
|
||||
switch (this) {
|
||||
|
@ -41,6 +42,9 @@ enum DerivePathType {
|
|||
|
||||
case DerivePathType.bip86:
|
||||
return AddressType.p2tr;
|
||||
|
||||
case DerivePathType.cardanoShelley:
|
||||
return AddressType.cardanoShelley;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,11 +4,14 @@ import 'dart:io';
|
|||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:on_chain/ada/ada.dart';
|
||||
import 'package:on_chain/ada/src/provider/provider/provider.dart';
|
||||
|
||||
import '../networking/http.dart';
|
||||
import '../pages/settings_views/global_settings_view/manage_nodes_views/add_edit_node_view.dart';
|
||||
import '../providers/global/prefs_provider.dart';
|
||||
import '../services/tor_service.dart';
|
||||
import '../wallets/api/cardano/blockfrost_http_provider.dart';
|
||||
import '../wallets/api/tezos/tezos_rpc_api.dart';
|
||||
import '../wallets/crypto_currency/crypto_currency.dart';
|
||||
import '../wallets/crypto_currency/interfaces/electrumx_currency_interface.dart';
|
||||
|
@ -107,7 +110,9 @@ Future<bool> testNodeConnection({
|
|||
|
||||
case CryptonoteCurrency():
|
||||
try {
|
||||
final proxyInfo = ref.read(prefsChangeNotifierProvider).useTor
|
||||
final proxyInfo = ref
|
||||
.read(prefsChangeNotifierProvider)
|
||||
.useTor
|
||||
? ref.read(pTorService).getProxyInfo()
|
||||
: null;
|
||||
|
||||
|
@ -190,7 +195,7 @@ Future<bool> testNodeConnection({
|
|||
case Stellar():
|
||||
try {
|
||||
testPassed =
|
||||
await testStellarNodeConnection(formData.host!, formData.port!);
|
||||
await testStellarNodeConnection(formData.host!, formData.port!);
|
||||
} catch (_) {}
|
||||
break;
|
||||
|
||||
|
@ -206,7 +211,9 @@ Future<bool> testNodeConnection({
|
|||
"action": "version",
|
||||
},
|
||||
),
|
||||
proxyInfo: ref.read(prefsChangeNotifierProvider).useTor
|
||||
proxyInfo: ref
|
||||
.read(prefsChangeNotifierProvider)
|
||||
.useTor
|
||||
? ref.read(pTorService).getProxyInfo()
|
||||
: null,
|
||||
);
|
||||
|
@ -243,6 +250,29 @@ Future<bool> testNodeConnection({
|
|||
testPassed = false;
|
||||
}
|
||||
break;
|
||||
|
||||
case Cardano():
|
||||
try {
|
||||
final blockfrostProvider = BlockforestProvider(
|
||||
BlockfrostHttpProvider(
|
||||
url: "${formData.host!}:${formData.port!}/api/v0",
|
||||
),
|
||||
);
|
||||
|
||||
final health = await blockfrostProvider.request(
|
||||
BlockfrostRequestBackendHealthStatus(),
|
||||
);
|
||||
|
||||
Logging.instance.log(
|
||||
"Cardano testNodeConnection \"health=$health\"",
|
||||
level: LogLevel.Info,
|
||||
);
|
||||
|
||||
return health;
|
||||
} catch (_) {
|
||||
testPassed = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return testPassed;
|
||||
|
|
51
lib/wallets/api/cardano/blockfrost_http_provider.dart
Normal file
51
lib/wallets/api/cardano/blockfrost_http_provider.dart
Normal file
|
@ -0,0 +1,51 @@
|
|||
import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:on_chain/ada/src/provider/blockfrost/core/core.dart';
|
||||
import 'package:on_chain/ada/src/provider/service/service.dart';
|
||||
|
||||
import '../../../utilities/logger.dart';
|
||||
|
||||
class BlockfrostHttpProvider implements BlockfrostServiceProvider {
|
||||
BlockfrostHttpProvider(
|
||||
{required this.url,
|
||||
this.version = "v0",
|
||||
this.projectId,
|
||||
http.Client? client,
|
||||
this.defaultRequestTimeout = const Duration(seconds: 30)})
|
||||
: client = client ?? http.Client();
|
||||
@override
|
||||
final String url;
|
||||
final String version;
|
||||
final String? projectId;
|
||||
final http.Client client;
|
||||
final Duration defaultRequestTimeout;
|
||||
|
||||
@override
|
||||
Future<dynamic> get(BlockforestRequestDetails params,
|
||||
[Duration? timeout]) async {
|
||||
final response =
|
||||
await client.get(Uri.parse(params.url(url, "api/$version")), headers: {
|
||||
'Content-Type': 'application/json',
|
||||
"Accept": "application/json",
|
||||
if (projectId != null) ...{"project_id": projectId!},
|
||||
}).timeout(timeout ?? defaultRequestTimeout);
|
||||
final data = json.decode(response.body);
|
||||
return data;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<dynamic> post(BlockforestRequestDetails params,
|
||||
[Duration? timeout]) async {
|
||||
final response = await client
|
||||
.post(Uri.parse(params.url(url, "api/$version")),
|
||||
headers: {
|
||||
"Accept": "application/json",
|
||||
if (projectId != null) ...{"project_id": projectId!},
|
||||
...params.header
|
||||
},
|
||||
body: params.body)
|
||||
.timeout(timeout ?? defaultRequestTimeout);
|
||||
final data = json.decode(response.body);
|
||||
return data;
|
||||
}
|
||||
}
|
123
lib/wallets/crypto_currency/coins/cardano.dart
Normal file
123
lib/wallets/crypto_currency/coins/cardano.dart
Normal file
|
@ -0,0 +1,123 @@
|
|||
import '../../../models/isar/models/blockchain_data/address.dart';
|
||||
import '../../../models/node_model.dart';
|
||||
import '../../../utilities/default_nodes.dart';
|
||||
import '../../../utilities/enums/derive_path_type_enum.dart';
|
||||
import '../crypto_currency.dart';
|
||||
import '../intermediate/bip39_currency.dart';
|
||||
|
||||
class Cardano extends Bip39Currency {
|
||||
Cardano(super.network) {
|
||||
_idMain = "cardano";
|
||||
_uriScheme = "cardano";
|
||||
switch (network) {
|
||||
case CryptoCurrencyNetwork.main:
|
||||
_id = _idMain;
|
||||
_name = "Cardano";
|
||||
_ticker = "ADA";
|
||||
default:
|
||||
throw Exception("Unsupported network: $network");
|
||||
}
|
||||
}
|
||||
|
||||
late final String _id;
|
||||
|
||||
@override
|
||||
String get identifier => _id;
|
||||
|
||||
late final String _idMain;
|
||||
|
||||
@override
|
||||
String get mainNetId => _idMain;
|
||||
|
||||
late final String _name;
|
||||
|
||||
@override
|
||||
String get prettyName => _name;
|
||||
|
||||
late final String _uriScheme;
|
||||
|
||||
@override
|
||||
String get uriScheme => _uriScheme;
|
||||
|
||||
late final String _ticker;
|
||||
|
||||
@override
|
||||
String get ticker => _ticker;
|
||||
|
||||
@override
|
||||
AddressType get defaultAddressType => AddressType.cardanoShelley;
|
||||
|
||||
@override
|
||||
Uri defaultBlockExplorer(String txid) {
|
||||
switch (network) {
|
||||
case CryptoCurrencyNetwork.main:
|
||||
return Uri.parse(
|
||||
"https://explorer.cardano.org/en/transaction?id=$txid");
|
||||
default:
|
||||
throw Exception(
|
||||
"Unsupported network for defaultBlockExplorer(): $network",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
DerivePathType get defaultDerivePathType => DerivePathType.cardanoShelley;
|
||||
|
||||
@override
|
||||
NodeModel get defaultNode {
|
||||
switch (network) {
|
||||
case CryptoCurrencyNetwork.main:
|
||||
return NodeModel(
|
||||
host: "https://cardano.stackwallet.com",
|
||||
port: 443,
|
||||
name: DefaultNodes.defaultName,
|
||||
id: DefaultNodes.buildId(this),
|
||||
useSSL: true,
|
||||
enabled: true,
|
||||
coinName: identifier,
|
||||
isFailover: true,
|
||||
isDown: false,
|
||||
);
|
||||
|
||||
default:
|
||||
throw Exception("Unsupported network: $network");
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
int get defaultSeedPhraseLength => 15;
|
||||
|
||||
@override
|
||||
int get fractionDigits => 6;
|
||||
|
||||
@override
|
||||
String get genesisHash => "f0f7892b5c333cffc4b3c4344de48af4cc63f55e44936196f365a9ef2244134f";
|
||||
|
||||
@override
|
||||
bool get hasBuySupport => false;
|
||||
|
||||
@override
|
||||
bool get hasMnemonicPassphraseSupport => false;
|
||||
|
||||
@override
|
||||
int get minConfirms => 2;
|
||||
|
||||
@override
|
||||
List<int> get possibleMnemonicLengths => [defaultSeedPhraseLength];
|
||||
|
||||
@override
|
||||
BigInt get satsPerCoin => BigInt.from(1000000);
|
||||
|
||||
@override
|
||||
int get targetBlockTimeSeconds => 20;
|
||||
|
||||
@override
|
||||
bool validateAddress(String address) {
|
||||
switch (network) {
|
||||
case CryptoCurrencyNetwork.main:
|
||||
return RegExp(r"^addr1[0-9a-zA-Z]{98}$").hasMatch(address);
|
||||
default:
|
||||
throw Exception("Unsupported network: $network");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,6 +6,7 @@ export 'coins/banano.dart';
|
|||
export 'coins/bitcoin.dart';
|
||||
export 'coins/bitcoin_frost.dart';
|
||||
export 'coins/bitcoincash.dart';
|
||||
export 'coins/cardano.dart';
|
||||
export 'coins/dash.dart';
|
||||
export 'coins/dogecoin.dart';
|
||||
export 'coins/ecash.dart';
|
||||
|
|
556
lib/wallets/wallet/impl/cardano_wallet.dart
Normal file
556
lib/wallets/wallet/impl/cardano_wallet.dart
Normal file
|
@ -0,0 +1,556 @@
|
|||
import 'dart:convert';
|
||||
|
||||
import 'package:blockchain_utils/bip/bip/bip44/base/bip44_base.dart';
|
||||
import 'package:blockchain_utils/bip/cardano/bip32/cardano_icarus_bip32.dart';
|
||||
import 'package:blockchain_utils/bip/cardano/cip1852/cip1852.dart';
|
||||
import 'package:blockchain_utils/bip/cardano/cip1852/conf/cip1852_coins.dart';
|
||||
import 'package:blockchain_utils/bip/cardano/mnemonic/cardano_icarus_seed_generator.dart';
|
||||
import 'package:blockchain_utils/bip/cardano/shelley/cardano_shelley.dart';
|
||||
import 'package:on_chain/ada/ada.dart';
|
||||
import '../../../models/balance.dart';
|
||||
import '../../../models/isar/models/blockchain_data/address.dart';
|
||||
import 'package:tuple/tuple.dart';
|
||||
import '../../../models/isar/models/blockchain_data/transaction.dart' as isar;
|
||||
import '../../../models/paymint/fee_object_model.dart';
|
||||
import '../../../networking/http.dart';
|
||||
import '../../../services/tor_service.dart';
|
||||
import '../../../utilities/amount/amount.dart';
|
||||
import 'package:isar/isar.dart';
|
||||
import '../../../utilities/logger.dart';
|
||||
import '../../../utilities/prefs.dart';
|
||||
import '../../api/cardano/blockfrost_http_provider.dart';
|
||||
import '../../crypto_currency/crypto_currency.dart';
|
||||
import '../../models/tx_data.dart';
|
||||
import '../intermediate/bip39_wallet.dart';
|
||||
|
||||
class CardanoWallet extends Bip39Wallet<Cardano> {
|
||||
CardanoWallet(CryptoCurrencyNetwork network) : super(Cardano(network));
|
||||
|
||||
// Source: https://cips.cardano.org/cip/CIP-1852
|
||||
static const String _addressDerivationPath = "m/1852'/1815'/0'/0/0";
|
||||
static final HTTP _httpClient = HTTP();
|
||||
|
||||
BlockforestProvider? blockfrostProvider;
|
||||
|
||||
@override
|
||||
FilterOperation? get changeAddressFilterOperation => null;
|
||||
|
||||
@override
|
||||
FilterOperation? get receivingAddressFilterOperation => null;
|
||||
|
||||
Future<Address> _getAddress() async {
|
||||
final mnemonic = await getMnemonic();
|
||||
final seed = CardanoIcarusSeedGenerator(mnemonic).generate();
|
||||
final cip1852 = Cip1852.fromSeed(seed, Cip1852Coins.cardanoIcarus);
|
||||
final derivationAccount = cip1852.purpose.coin.account(0);
|
||||
final shelley = CardanoShelley.fromCip1852Object(derivationAccount)
|
||||
.change(Bip44Changes.chainExt)
|
||||
.addressIndex(0);
|
||||
final paymentPublicKey = shelley.bip44.publicKey.compressed;
|
||||
final stakePublicKey = shelley.bip44Sk.publicKey.compressed;
|
||||
final addressStr = ADABaseAddress.fromPublicKey(
|
||||
basePubkeyBytes: paymentPublicKey,
|
||||
stakePubkeyBytes: stakePublicKey,
|
||||
).address;
|
||||
return Address(
|
||||
walletId: walletId,
|
||||
value: addressStr,
|
||||
publicKey: paymentPublicKey,
|
||||
derivationIndex: 0,
|
||||
derivationPath: DerivationPath()..value = _addressDerivationPath,
|
||||
type: AddressType.cardanoShelley,
|
||||
subType: AddressSubType.receiving,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> checkSaveInitialReceivingAddress() async {
|
||||
try {
|
||||
final Address? address = await getCurrentReceivingAddress();
|
||||
|
||||
if (address == null) {
|
||||
final address = await _getAddress();
|
||||
|
||||
await mainDB.updateOrPutAddresses([address]);
|
||||
}
|
||||
} catch (e, s) {
|
||||
Logging.instance.log(
|
||||
"$runtimeType checkSaveInitialReceivingAddress() failed: $e\n$s",
|
||||
level: LogLevel.Error,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> pingCheck() async {
|
||||
try {
|
||||
final currentNode = getCurrentNode();
|
||||
blockfrostProvider = BlockforestProvider(
|
||||
BlockfrostHttpProvider(
|
||||
url: "${currentNode.host}:${currentNode.port}/api/v0",
|
||||
),
|
||||
);
|
||||
|
||||
final health = await blockfrostProvider!.request(
|
||||
BlockfrostRequestBackendHealthStatus(),
|
||||
);
|
||||
|
||||
return Future.value(health);
|
||||
} catch (e, s) {
|
||||
Logging.instance.log(
|
||||
"Error ping checking in cardano_wallet.dart: $e\n$s",
|
||||
level: LogLevel.Error,
|
||||
);
|
||||
return Future.value(false);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Amount> estimateFeeFor(Amount amount, int feeRate) async {
|
||||
await updateProvider();
|
||||
|
||||
if (info.cachedBalance.spendable.raw == BigInt.zero) {
|
||||
return Amount(
|
||||
rawValue: BigInt.zero,
|
||||
fractionDigits: cryptoCurrency.fractionDigits,
|
||||
);
|
||||
}
|
||||
|
||||
final params = await blockfrostProvider!.request(
|
||||
BlockfrostRequestLatestEpochProtocolParameters(),
|
||||
);
|
||||
|
||||
final fee = params.calculateFee(284);
|
||||
|
||||
return Amount(
|
||||
rawValue: fee,
|
||||
fractionDigits: cryptoCurrency.fractionDigits,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<FeeObject> get fees async {
|
||||
try {
|
||||
await updateProvider();
|
||||
|
||||
final params = await blockfrostProvider!.request(
|
||||
BlockfrostRequestLatestEpochProtocolParameters(),
|
||||
);
|
||||
|
||||
// 284 is the size of a basic transaction with one input and two outputs (change and recipient)
|
||||
final fee = params.calculateFee(284).toInt();
|
||||
|
||||
return FeeObject(
|
||||
numberOfBlocksFast: 2,
|
||||
numberOfBlocksAverage: 2,
|
||||
numberOfBlocksSlow: 2,
|
||||
fast: fee,
|
||||
medium: fee,
|
||||
slow: fee,
|
||||
);
|
||||
} catch (e, s) {
|
||||
Logging.instance.log(
|
||||
"Error getting fees in cardano_wallet.dart: $e\n$s",
|
||||
level: LogLevel.Error,
|
||||
);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<TxData> prepareSend({required TxData txData}) async {
|
||||
try {
|
||||
await updateProvider();
|
||||
|
||||
if (txData.amount!.raw < ADAHelper.toLovelaces("1")) {
|
||||
throw Exception("By network rules, you can send minimum 1 ADA");
|
||||
}
|
||||
|
||||
final utxos = await blockfrostProvider!.request(
|
||||
BlockfrostRequestAddressUTXOsOfAGivenAsset(
|
||||
address: ADAAddress.fromAddress(
|
||||
(await getCurrentReceivingAddress())!.value,
|
||||
),
|
||||
asset: "lovelace",
|
||||
),
|
||||
);
|
||||
|
||||
var leftAmountForUtxos = txData.amount!.raw;
|
||||
var listOfUtxosToBeUsed = <ADAAccountUTXOResponse>[];
|
||||
var totalBalance = BigInt.zero;
|
||||
|
||||
for (final utxo in utxos) {
|
||||
if (!(leftAmountForUtxos <= BigInt.parse("0"))) {
|
||||
leftAmountForUtxos -= BigInt.parse(utxo.amount.first.quantity);
|
||||
listOfUtxosToBeUsed.add(utxo);
|
||||
}
|
||||
totalBalance += BigInt.parse(utxo.amount.first.quantity);
|
||||
}
|
||||
|
||||
if (leftAmountForUtxos > BigInt.parse("0") || totalBalance < txData.amount!.raw) {
|
||||
throw Exception("Insufficient balance");
|
||||
}
|
||||
|
||||
final bip32 = CardanoIcarusBip32.fromSeed(CardanoIcarusSeedGenerator(await getMnemonic()).generate());
|
||||
final spend = bip32.derivePath("1852'/1815'/0'/0/0");
|
||||
final privateKey = AdaPrivateKey.fromBytes(spend.privateKey.raw);
|
||||
|
||||
// Calculate fees with example tx
|
||||
final exampleFee = ADAHelper.toLovelaces("0.10");
|
||||
final change = TransactionOutput(address: ADABaseAddress((await getCurrentReceivingAddress())!.value), amount: Value(coin: totalBalance - (txData.amount!.raw)));
|
||||
final body = TransactionBody(
|
||||
inputs: listOfUtxosToBeUsed.map((e) => TransactionInput(transactionId: TransactionHash.fromHex(e.txHash), index: e.outputIndex)).toList(),
|
||||
outputs: [change, TransactionOutput(address: ADABaseAddress(txData.recipients!.first.address), amount: Value(coin: txData.amount!.raw - exampleFee))],
|
||||
fee: exampleFee,
|
||||
);
|
||||
final exampleTx = ADATransaction(
|
||||
body: body,
|
||||
witnessSet: TransactionWitnessSet(vKeys: [
|
||||
privateKey.createSignatureWitness(body.toHash().data),
|
||||
],)
|
||||
,);
|
||||
final params = await blockfrostProvider!.request(BlockfrostRequestLatestEpochProtocolParameters());
|
||||
final fee = params.calculateFee(exampleTx.size);
|
||||
|
||||
// Check if we are sending all balance, which means no change and only one output for recipient.
|
||||
if (totalBalance == txData.amount!.raw) {
|
||||
final List<TxRecipient> newRecipients = [(
|
||||
address: txData.recipients!.first.address,
|
||||
amount: Amount(
|
||||
rawValue: txData.amount!.raw - fee,
|
||||
fractionDigits: cryptoCurrency.fractionDigits,
|
||||
),
|
||||
isChange: txData.recipients!.first.isChange,
|
||||
),];
|
||||
return txData.copyWith(
|
||||
fee: Amount(
|
||||
rawValue: fee,
|
||||
fractionDigits: cryptoCurrency.fractionDigits,
|
||||
),
|
||||
recipients: newRecipients,
|
||||
);
|
||||
} else {
|
||||
if (txData.amount!.raw + fee > totalBalance) {
|
||||
throw Exception("Insufficient balance for fee");
|
||||
}
|
||||
|
||||
// Minimum change in Cardano is 1 ADA and we need to have enough balance for that
|
||||
if (totalBalance - (txData.amount!.raw + fee) < ADAHelper.toLovelaces("1")) {
|
||||
throw Exception("Not enough balance for change. By network rules, please either send all balance or leave at least 1 ADA change.");
|
||||
}
|
||||
|
||||
return txData.copyWith(
|
||||
fee: Amount(
|
||||
rawValue: fee,
|
||||
fractionDigits: cryptoCurrency.fractionDigits,
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (e, s) {
|
||||
Logging.instance.log(
|
||||
"$runtimeType Cardano prepareSend failed: $e\n$s",
|
||||
level: LogLevel.Error,
|
||||
);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<TxData> confirmSend({required TxData txData}) async {
|
||||
try {
|
||||
await updateProvider();
|
||||
|
||||
final utxos = await blockfrostProvider!.request(
|
||||
BlockfrostRequestAddressUTXOsOfAGivenAsset(
|
||||
address: ADAAddress.fromAddress(
|
||||
(await getCurrentReceivingAddress())!.value,
|
||||
),
|
||||
asset: "lovelace",
|
||||
),
|
||||
);
|
||||
|
||||
|
||||
var leftAmountForUtxos = txData.amount!.raw + txData.fee!.raw;
|
||||
var listOfUtxosToBeUsed = <ADAAccountUTXOResponse>[];
|
||||
var totalBalance = BigInt.zero;
|
||||
|
||||
for (final utxo in utxos) {
|
||||
if (!(leftAmountForUtxos <= BigInt.parse("0"))) {
|
||||
leftAmountForUtxos -= BigInt.parse(utxo.amount.first.quantity);
|
||||
listOfUtxosToBeUsed.add(utxo);
|
||||
}
|
||||
totalBalance += BigInt.parse(utxo.amount.first.quantity);
|
||||
}
|
||||
|
||||
var totalUtxoAmount = BigInt.zero;
|
||||
|
||||
for (final utxo in listOfUtxosToBeUsed) {
|
||||
totalUtxoAmount += BigInt.parse(utxo.amount.first.quantity);
|
||||
}
|
||||
|
||||
final bip32 = CardanoIcarusBip32.fromSeed(CardanoIcarusSeedGenerator(await getMnemonic()).generate());
|
||||
final spend = bip32.derivePath("1852'/1815'/0'/0/0");
|
||||
final privateKey = AdaPrivateKey.fromBytes(spend.privateKey.raw);
|
||||
|
||||
final change = TransactionOutput(address: ADABaseAddress((await getCurrentReceivingAddress())!.value), amount: Value(coin: totalUtxoAmount - (txData.amount!.raw + txData.fee!.raw)));
|
||||
List<TransactionOutput> outputs = [];
|
||||
if (totalBalance == (txData.amount!.raw + txData.fee!.raw)) {
|
||||
outputs = [TransactionOutput(address: ADABaseAddress(txData.recipients!.first.address), amount: Value(coin: txData.amount!.raw))];
|
||||
} else {
|
||||
outputs = [change, TransactionOutput(address: ADABaseAddress(txData.recipients!.first.address), amount: Value(coin: txData.amount!.raw))];
|
||||
}
|
||||
final body = TransactionBody(
|
||||
inputs: listOfUtxosToBeUsed.map((e) => TransactionInput(transactionId: TransactionHash.fromHex(e.txHash), index: e.outputIndex)).toList(),
|
||||
outputs: outputs,
|
||||
fee: txData.fee!.raw,
|
||||
);
|
||||
final tx = ADATransaction(
|
||||
body: body,
|
||||
witnessSet: TransactionWitnessSet(vKeys: [
|
||||
privateKey.createSignatureWitness(body.toHash().data),
|
||||
],)
|
||||
,);
|
||||
|
||||
final sentTx = await blockfrostProvider!.request(BlockfrostRequestSubmitTransaction(
|
||||
transactionCborBytes: tx.serialize(),),);
|
||||
return txData.copyWith(
|
||||
txid: sentTx,
|
||||
);
|
||||
} catch (e, s) {
|
||||
Logging.instance.log(
|
||||
"$runtimeType Cardano confirmSend failed: $e\n$s",
|
||||
level: LogLevel.Error,
|
||||
);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> recover({required bool isRescan}) async {
|
||||
await refreshMutex.protect(() async {
|
||||
final addressStruct = await _getAddress();
|
||||
|
||||
await mainDB.updateOrPutAddresses([addressStruct]);
|
||||
|
||||
if (info.cachedReceivingAddress != addressStruct.value) {
|
||||
await info.updateReceivingAddress(
|
||||
newAddress: addressStruct.value,
|
||||
isar: mainDB.isar,
|
||||
);
|
||||
}
|
||||
|
||||
await Future.wait([
|
||||
updateBalance(),
|
||||
updateChainHeight(),
|
||||
updateTransactions(),
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateBalance() async {
|
||||
try {
|
||||
await updateProvider();
|
||||
|
||||
final addressUtxos = await blockfrostProvider!.request(
|
||||
BlockfrostRequestAddressUTXOs(
|
||||
ADAAddress.fromAddress(
|
||||
(await getCurrentReceivingAddress())!.value,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
BigInt totalBalanceInLovelace = BigInt.parse("0");
|
||||
for (final utxo in addressUtxos) {
|
||||
totalBalanceInLovelace += BigInt.parse(utxo.amount.first.quantity);
|
||||
}
|
||||
|
||||
final balance = Balance(
|
||||
total: Amount(
|
||||
rawValue: totalBalanceInLovelace,
|
||||
fractionDigits: cryptoCurrency.fractionDigits,),
|
||||
spendable: Amount(
|
||||
rawValue: totalBalanceInLovelace,
|
||||
fractionDigits: cryptoCurrency.fractionDigits,),
|
||||
blockedTotal: Amount(
|
||||
rawValue: BigInt.zero,
|
||||
fractionDigits: cryptoCurrency.fractionDigits,
|
||||
),
|
||||
pendingSpendable: Amount(
|
||||
rawValue: BigInt.zero,
|
||||
fractionDigits: cryptoCurrency.fractionDigits,
|
||||
),
|
||||
);
|
||||
|
||||
await info.updateBalance(newBalance: balance, isar: mainDB.isar);
|
||||
} catch (e, s) {
|
||||
Logging.instance.log(
|
||||
"Error getting balance in cardano_wallet.dart: $e\n$s",
|
||||
level: LogLevel.Error,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateChainHeight() async {
|
||||
try {
|
||||
await updateProvider();
|
||||
|
||||
final latestBlock = await blockfrostProvider!.request(
|
||||
BlockfrostRequestLatestBlock(),
|
||||
);
|
||||
|
||||
await info.updateCachedChainHeight(
|
||||
newHeight: latestBlock.height == null ? 0 : latestBlock.height!,
|
||||
isar: mainDB.isar);
|
||||
} catch (e, s) {
|
||||
Logging.instance.log(
|
||||
"Error updating transactions in cardano_wallet.dart: $e\n$s",
|
||||
level: LogLevel.Error,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateNode() async {
|
||||
await refresh();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateTransactions() async {
|
||||
try {
|
||||
await updateProvider();
|
||||
|
||||
final currentAddr = (await getCurrentReceivingAddress())!.value;
|
||||
|
||||
final txsList = await blockfrostProvider!.request(
|
||||
BlockfrostRequestAddressTransactions(
|
||||
ADAAddress.fromAddress(
|
||||
currentAddr,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
final parsedTxsList =
|
||||
List<Tuple2<isar.Transaction, Address>>.empty(growable: true);
|
||||
|
||||
for (final tx in txsList) {
|
||||
final txInfo = await blockfrostProvider!.request(
|
||||
BlockfrostRequestSpecificTransaction(tx.txHash),
|
||||
);
|
||||
final utxoInfo = await blockfrostProvider!.request(
|
||||
BlockfrostRequestTransactionUTXOs(tx.txHash),
|
||||
);
|
||||
var txType = isar.TransactionType.unknown;
|
||||
|
||||
var txOutputIndex = -1;
|
||||
|
||||
for (final input in utxoInfo.inputs) {
|
||||
if (input.address == currentAddr) {
|
||||
txType = isar.TransactionType.outgoing;
|
||||
}
|
||||
}
|
||||
|
||||
if (txType == isar.TransactionType.unknown) {
|
||||
for (final output in utxoInfo.outputs) {
|
||||
if (output.address == currentAddr) {
|
||||
txType = isar.TransactionType.incoming;
|
||||
txOutputIndex = output.outputIndex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var receiverAddr = "Unknown?";
|
||||
|
||||
if (txType == isar.TransactionType.incoming) {
|
||||
receiverAddr = currentAddr;
|
||||
} else if (txType == isar.TransactionType.outgoing) {
|
||||
for (final output in utxoInfo.outputs) {
|
||||
if (output.address != currentAddr) {
|
||||
receiverAddr = output.address;
|
||||
txOutputIndex = output.outputIndex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var amount = 0;
|
||||
|
||||
// Temporary solution until https://github.com/mrtnetwork/On_chain/issues/9 is solved.
|
||||
// Use the library when the mentioned issue is solved.
|
||||
final currentNode = getCurrentNode();
|
||||
final response = await _httpClient.get(
|
||||
url: Uri.parse(
|
||||
"${currentNode.host}:${currentNode.port}/api/v0/txs/${tx.txHash}/utxos",),
|
||||
proxyInfo: Prefs.instance.useTor
|
||||
? TorService.sharedInstance.getProxyInfo()
|
||||
: null,
|
||||
);
|
||||
final json = jsonDecode(response.body);
|
||||
if (txOutputIndex != -1) {
|
||||
amount = int.parse(json["outputs"][txOutputIndex]["amount"][0]
|
||||
["quantity"]! as String,);
|
||||
}
|
||||
// Temporary solution part end.
|
||||
|
||||
final transaction = isar.Transaction(
|
||||
walletId: walletId,
|
||||
txid: txInfo.hash,
|
||||
timestamp: tx.blockTime,
|
||||
type: txType,
|
||||
subType: isar.TransactionSubType.none,
|
||||
amount: amount,
|
||||
amountString: Amount(
|
||||
rawValue: BigInt.from(amount),
|
||||
fractionDigits: cryptoCurrency.fractionDigits,
|
||||
).toJsonString(),
|
||||
fee: int.parse(txInfo.fees),
|
||||
height: txInfo.blockHeight,
|
||||
isCancelled: false,
|
||||
isLelantus: false,
|
||||
slateId: null,
|
||||
otherData: null,
|
||||
inputs: [],
|
||||
outputs: [],
|
||||
nonce: null,
|
||||
numberOfMessages: 0,
|
||||
);
|
||||
|
||||
final txAddress = Address(
|
||||
walletId: walletId,
|
||||
value: receiverAddr,
|
||||
publicKey: List<int>.empty(),
|
||||
derivationIndex: 0,
|
||||
derivationPath: DerivationPath()..value = _addressDerivationPath,
|
||||
type: AddressType.cardanoShelley,
|
||||
subType: txType == isar.TransactionType.outgoing
|
||||
? AddressSubType.unknown
|
||||
: AddressSubType.receiving,
|
||||
);
|
||||
|
||||
parsedTxsList.add(Tuple2(transaction, txAddress));
|
||||
}
|
||||
|
||||
await mainDB.addNewTransactionData(parsedTxsList, walletId);
|
||||
} catch (e, s) {
|
||||
Logging.instance.log(
|
||||
"Error updating transactions in cardano_wallet.dart: $e\n$s",
|
||||
level: LogLevel.Error,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> updateUTXOs() async {
|
||||
// TODO: implement updateUTXOs
|
||||
return false;
|
||||
}
|
||||
|
||||
Future<void> updateProvider() async {
|
||||
final currentNode = getCurrentNode();
|
||||
blockfrostProvider = BlockforestProvider(
|
||||
BlockfrostHttpProvider(
|
||||
url: "${currentNode.host}:${currentNode.port}/",
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
|
@ -28,6 +28,7 @@ import 'impl/banano_wallet.dart';
|
|||
import 'impl/bitcoin_frost_wallet.dart';
|
||||
import 'impl/bitcoin_wallet.dart';
|
||||
import 'impl/bitcoincash_wallet.dart';
|
||||
import 'impl/cardano_wallet.dart';
|
||||
import 'impl/dash_wallet.dart';
|
||||
import 'impl/dogecoin_wallet.dart';
|
||||
import 'impl/ecash_wallet.dart';
|
||||
|
@ -324,6 +325,9 @@ abstract class Wallet<T extends CryptoCurrency> {
|
|||
case const (Bitcoincash):
|
||||
return BitcoincashWallet(net);
|
||||
|
||||
case const (Cardano):
|
||||
return CardanoWallet(net);
|
||||
|
||||
case const (Dash):
|
||||
return DashWallet(net);
|
||||
|
||||
|
|
16
pubspec.lock
16
pubspec.lock
|
@ -150,6 +150,14 @@ packages:
|
|||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "0.1.0"
|
||||
blockchain_utils:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: blockchain_utils
|
||||
sha256: aebc3a32b927b34f638817c4bfdb85f86a97e6ad35f0cd962660b0c6e8d5c56b
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "3.3.0"
|
||||
boolean_selector:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
@ -1277,6 +1285,14 @@ packages:
|
|||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.0.2"
|
||||
on_chain:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: on_chain
|
||||
sha256: "53a17cc19c722e3cdef7a34931e7cd07c490443a92bf538f08cc755b854dfb59"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "3.9.0"
|
||||
package_config:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
|
|
@ -57,6 +57,7 @@ final List<CryptoCurrency> _supportedCoins = List.unmodifiable([
|
|||
Banano(CryptoCurrencyNetwork.main),
|
||||
Bitcoincash(CryptoCurrencyNetwork.main),
|
||||
BitcoinFrost(CryptoCurrencyNetwork.main),
|
||||
Cardano(CryptoCurrencyNetwork.main),
|
||||
Dash(CryptoCurrencyNetwork.main),
|
||||
Dogecoin(CryptoCurrencyNetwork.main),
|
||||
Ecash(CryptoCurrencyNetwork.main),
|
||||
|
|
Loading…
Reference in a new issue