clean up sol

This commit is contained in:
julian 2024-05-03 09:33:59 -06:00
parent 5aa1f7b08a
commit 1101b8c932
2 changed files with 69 additions and 46 deletions

View file

@ -1,9 +1,9 @@
import 'package:solana/solana.dart'; import 'package:solana/solana.dart';
import 'package:stackwallet/models/node_model.dart'; import 'package:stackwallet/models/node_model.dart';
import 'package:stackwallet/utilities/default_nodes.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/wallets/crypto_currency/crypto_currency.dart'; import 'package:stackwallet/wallets/crypto_currency/crypto_currency.dart';
import 'package:stackwallet/wallets/crypto_currency/intermediate/bip39_currency.dart'; import 'package:stackwallet/wallets/crypto_currency/intermediate/bip39_currency.dart';
import 'package:stackwallet/utilities/default_nodes.dart';
class Solana extends Bip39Currency { class Solana extends Bip39Currency {
Solana(super.network) { Solana(super.network) {
@ -20,7 +20,8 @@ class Solana extends Bip39Currency {
switch (network) { switch (network) {
case CryptoCurrencyNetwork.main: case CryptoCurrencyNetwork.main:
return NodeModel( return NodeModel(
host: "https://api.mainnet-beta.solana.com/", // TODO: Change this to stack wallet one host:
"https://api.mainnet-beta.solana.com/", // TODO: Change this to stack wallet one
port: 443, port: 443,
name: DefaultNodes.defaultName, name: DefaultNodes.defaultName,
id: DefaultNodes.buildId(Coin.solana), id: DefaultNodes.buildId(Coin.solana),
@ -40,9 +41,18 @@ class Solana extends Bip39Currency {
@override @override
bool validateAddress(String address) { bool validateAddress(String address) {
return isPointOnEd25519Curve(Ed25519HDPublicKey.fromBase58(address).toByteArray()); return isPointOnEd25519Curve(
Ed25519HDPublicKey.fromBase58(address).toByteArray());
} }
@override @override
String get genesisHash => throw UnimplementedError(); String get genesisHash => throw UnimplementedError();
}
@override
bool operator ==(Object other) {
return other is Solana && other.network == network;
}
@override
int get hashCode => Object.hash(Solana, network);
}

View file

@ -28,24 +28,30 @@ import 'package:tuple/tuple.dart';
class SolanaWallet extends Bip39Wallet<Solana> { class SolanaWallet extends Bip39Wallet<Solana> {
SolanaWallet(CryptoCurrencyNetwork network) : super(Solana(network)); SolanaWallet(CryptoCurrencyNetwork network) : super(Solana(network));
static const String _addressDerivationPath = "m/44'/501'/0'/0'";
NodeModel? _solNode; NodeModel? _solNode;
RpcClient? _rpcClient; // The Solana RpcClient. RpcClient? _rpcClient; // The Solana RpcClient.
Future<Ed25519HDKeyPair> _getKeyPair() async { Future<Ed25519HDKeyPair> _getKeyPair() async {
return Ed25519HDKeyPair.fromMnemonic(await getMnemonic(), return Ed25519HDKeyPair.fromMnemonic(
account: 0, change: 0); await getMnemonic(),
account: 0,
change: 0,
);
} }
Future<Address> _getCurrentAddress() async { Future<Address> _generateAddress() async {
final addressStruct = Address( final addressStruct = Address(
walletId: walletId, walletId: walletId,
value: (await _getKeyPair()).address, value: (await _getKeyPair()).address,
publicKey: List<int>.empty(), publicKey: List<int>.empty(),
derivationIndex: 0, derivationIndex: 0,
derivationPath: DerivationPath()..value = "m/44'/501'/0'/0'", derivationPath: DerivationPath()..value = _addressDerivationPath,
type: cryptoCurrency.coin.primaryAddressType, type: cryptoCurrency.coin.primaryAddressType,
subType: AddressSubType.unknown); subType: AddressSubType.receiving,
);
return addressStruct; return addressStruct;
} }
@ -65,7 +71,10 @@ class SolanaWallet extends Bip39Wallet<Solana> {
recipientAccount: pubKey, recipientAccount: pubKey,
lamports: transferAmount.raw.toInt(), lamports: transferAmount.raw.toInt(),
) )
]).compile(recentBlockhash: latestBlockhash!.value.blockhash, feePayer: (await _getKeyPair()).publicKey); ]).compile(
recentBlockhash: latestBlockhash!.value.blockhash,
feePayer: pubKey,
);
return await _rpcClient?.getFeeForMessage( return await _rpcClient?.getFeeForMessage(
base64Encode(compiledMessage.toByteArray().toList()), base64Encode(compiledMessage.toByteArray().toList()),
@ -79,18 +88,13 @@ class SolanaWallet extends Bip39Wallet<Solana> {
@override @override
Future<void> checkSaveInitialReceivingAddress() async { Future<void> checkSaveInitialReceivingAddress() async {
try { try {
final address = (await _getKeyPair()).address; Address? address = await getCurrentReceivingAddress();
await mainDB.updateOrPutAddresses([ if (address == null) {
Address( address = await _generateAddress();
walletId: walletId,
value: address, await mainDB.updateOrPutAddresses([address]);
publicKey: List<int>.empty(), }
derivationIndex: 0,
derivationPath: DerivationPath()..value = "m/44'/501'/0'/0'",
type: cryptoCurrency.coin.primaryAddressType,
subType: AddressSubType.unknown)
]);
} catch (e, s) { } catch (e, s) {
Logging.instance.log( Logging.instance.log(
"$runtimeType checkSaveInitialReceivingAddress() failed: $e\n$s", "$runtimeType checkSaveInitialReceivingAddress() failed: $e\n$s",
@ -120,9 +124,10 @@ class SolanaWallet extends Bip39Wallet<Solana> {
"Failed to get fees, please check your node connection."); "Failed to get fees, please check your node connection.");
} }
final address = await getCurrentReceivingAddress();
// Rent exemption of Solana // Rent exemption of Solana
final accInfo = final accInfo = await _rpcClient?.getAccountInfo(address!.value);
await _rpcClient?.getAccountInfo((await _getKeyPair()).address);
final int minimumRent = final int minimumRent =
await _rpcClient?.getMinimumBalanceForRentExemption( await _rpcClient?.getMinimumBalanceForRentExemption(
accInfo!.value!.data.toString().length) ?? accInfo!.value!.data.toString().length) ??
@ -132,7 +137,9 @@ class SolanaWallet extends Bip39Wallet<Solana> {
txData.amount!.raw.toInt() - txData.amount!.raw.toInt() -
feeAmount)) { feeAmount)) {
throw Exception( throw Exception(
"Insufficient remaining balance for rent exemption, minimum rent: ${minimumRent / pow(10, cryptoCurrency.fractionDigits)}"); "Insufficient remaining balance for rent exemption, minimum rent: "
"${minimumRent / pow(10, cryptoCurrency.fractionDigits)}",
);
} }
return txData.copyWith( return txData.copyWith(
@ -255,7 +262,7 @@ class SolanaWallet extends Bip39Wallet<Solana> {
@override @override
Future<void> recover({required bool isRescan}) async { Future<void> recover({required bool isRescan}) async {
await refreshMutex.protect(() async { await refreshMutex.protect(() async {
final addressStruct = await _getCurrentAddress(); final addressStruct = await _generateAddress();
await mainDB.updateOrPutAddresses([addressStruct]); await mainDB.updateOrPutAddresses([addressStruct]);
@ -277,13 +284,13 @@ class SolanaWallet extends Bip39Wallet<Solana> {
@override @override
Future<void> updateBalance() async { Future<void> updateBalance() async {
try { try {
final address = await getCurrentReceivingAddress();
_checkClient(); _checkClient();
final balance = await _rpcClient?.getBalance(info.cachedReceivingAddress); final balance = await _rpcClient?.getBalance(address!.value);
// Rent exemption of Solana // Rent exemption of Solana
final accInfo = final accInfo = await _rpcClient?.getAccountInfo(address!.value);
await _rpcClient?.getAccountInfo((await _getKeyPair()).address);
// TODO [prio=low]: handle null account info. // TODO [prio=low]: handle null account info.
final int minimumRent = final int minimumRent =
await _rpcClient?.getMinimumBalanceForRentExemption( await _rpcClient?.getMinimumBalanceForRentExemption(
@ -366,6 +373,8 @@ class SolanaWallet extends Bip39Wallet<Solana> {
final txsList = final txsList =
List<Tuple2<isar.Transaction, Address>>.empty(growable: true); List<Tuple2<isar.Transaction, Address>>.empty(growable: true);
final myAddress = (await getCurrentReceivingAddress())!;
// TODO [prio=low]: Revisit null assertion below. // TODO [prio=low]: Revisit null assertion below.
for (final tx in transactionsList!) { for (final tx in transactionsList!) {
@ -380,13 +389,16 @@ class SolanaWallet extends Bip39Wallet<Solana> {
fractionDigits: cryptoCurrency.fractionDigits, fractionDigits: cryptoCurrency.fractionDigits,
); );
if ((senderAddress == (await _getKeyPair()).address) && (receiverAddress == "11111111111111111111111111111111")) { if ((senderAddress == myAddress.value) &&
// The account that is only 1's are System Program accounts which means there is no receiver except the sender, see: https://explorer.solana.com/address/11111111111111111111111111111111 (receiverAddress == "11111111111111111111111111111111")) {
// The account that is only 1's are System Program accounts which
// means there is no receiver except the sender,
// see: https://explorer.solana.com/address/11111111111111111111111111111111
txType = isar.TransactionType.sentToSelf; txType = isar.TransactionType.sentToSelf;
receiverAddress = senderAddress; receiverAddress = senderAddress;
} else if (senderAddress == (await _getKeyPair()).address) { } else if (senderAddress == myAddress.value) {
txType = isar.TransactionType.outgoing; txType = isar.TransactionType.outgoing;
} else if (receiverAddress == (await _getKeyPair()).address) { } else if (receiverAddress == myAddress.value) {
txType = isar.TransactionType.incoming; txType = isar.TransactionType.incoming;
} }
@ -411,15 +423,16 @@ class SolanaWallet extends Bip39Wallet<Solana> {
); );
final txAddress = Address( final txAddress = Address(
walletId: walletId, walletId: walletId,
value: receiverAddress, value: receiverAddress,
publicKey: List<int>.empty(), publicKey: List<int>.empty(),
derivationIndex: 0, derivationIndex: 0,
derivationPath: DerivationPath()..value = "m/44'/501'/0'/0'", derivationPath: DerivationPath()..value = _addressDerivationPath,
type: AddressType.solana, type: AddressType.solana,
subType: txType == isar.TransactionType.outgoing subType: txType == isar.TransactionType.outgoing
? AddressSubType.unknown ? AddressSubType.unknown
: AddressSubType.receiving); : AddressSubType.receiving,
);
txsList.add(Tuple2(transaction, txAddress)); txsList.add(Tuple2(transaction, txAddress));
} }