fix: missing places, fix polygon wallet creation

This commit is contained in:
Rafael Saes 2024-01-31 10:54:42 -03:00
parent e8dff0773a
commit 268dc3ca9f
9 changed files with 93 additions and 118 deletions

View file

@ -67,6 +67,7 @@ abstract class EthereumWalletBase
walletInfo: walletInfo,
password: password,
encryptionFileUtils: encryptionFileUtils,
isFlatpak: isFlatpak,
);
if (!CakeHive.isAdapterRegistered(Erc20Token.typeId)) {
@ -407,6 +408,7 @@ abstract class EthereumWalletBase
privateKey: privateKey,
initialBalance: balance,
encryptionFileUtils: encryptionFileUtils,
isFlatpak: isFlatpak,
);
}

View file

@ -113,6 +113,15 @@ packages:
url: "https://pub.dev"
source: hosted
version: "8.4.3"
cake_backup:
dependency: transitive
description:
path: "."
ref: main
resolved-ref: "3aba867dcab6737f6707782f5db15d71f303db38"
url: "https://github.com/cake-tech/cake_backup.git"
source: git
version: "1.0.0+1"
characters:
dependency: transitive
description:
@ -169,6 +178,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.2"
cryptography:
dependency: transitive
description:
name: cryptography
sha256: df156c5109286340817d21fa7b62f9140f17915077127dd70f8bd7a2a0997a35
url: "https://pub.dev"
source: hosted
version: "2.5.0"
cupertino_icons:
dependency: transitive
description:
name: cupertino_icons
sha256: d57953e10f9f8327ce64a508a355f0b1ec902193f66288e8cb5070e7c47eeb2d
url: "https://pub.dev"
source: hosted
version: "1.0.6"
cw_core:
dependency: "direct main"
description:
@ -623,6 +648,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.1"
tuple:
dependency: transitive
description:
name: tuple
sha256: a97ce2013f240b2f3807bcbaf218765b6f301c3eff91092bcfa23a039e7dd151
url: "https://pub.dev"
source: hosted
version: "2.0.2"
typed_data:
dependency: transitive
description:

View file

@ -59,6 +59,7 @@ abstract class NanoWalletBase
walletInfo: walletInfo,
password: password,
encryptionFileUtils: encryptionFileUtils,
isFlatpak: isFlatpak,
);
if (!CakeHive.isAdapterRegistered(NanoAccount.typeId)) {
CakeHive.registerAdapter(NanoAccountAdapter());
@ -410,6 +411,7 @@ abstract class NanoWalletBase
mnemonic: mnemonic,
initialBalance: balance,
encryptionFileUtils: encryptionFileUtils,
isFlatpak: isFlatpak,
);
// init() should always be run after this!
}

View file

@ -137,6 +137,15 @@ packages:
url: "https://pub.dev"
source: hosted
version: "8.6.1"
cake_backup:
dependency: transitive
description:
path: "."
ref: main
resolved-ref: "3aba867dcab6737f6707782f5db15d71f303db38"
url: "https://github.com/cake-tech/cake_backup.git"
source: git
version: "1.0.0+1"
characters:
dependency: transitive
description:
@ -193,6 +202,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.3"
cryptography:
dependency: transitive
description:
name: cryptography
sha256: df156c5109286340817d21fa7b62f9140f17915077127dd70f8bd7a2a0997a35
url: "https://pub.dev"
source: hosted
version: "2.5.0"
cupertino_icons:
dependency: transitive
description:
name: cupertino_icons
sha256: d57953e10f9f8327ce64a508a355f0b1ec902193f66288e8cb5070e7c47eeb2d
url: "https://pub.dev"
source: hosted
version: "1.0.6"
cw_core:
dependency: "direct main"
description:
@ -773,6 +798,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.1"
tuple:
dependency: transitive
description:
name: tuple
sha256: a97ce2013f240b2f3807bcbaf218765b6f301c3eff91092bcfa23a039e7dd151
url: "https://pub.dev"
source: hosted
version: "2.0.2"
typed_data:
dependency: transitive
description:

View file

@ -11,7 +11,6 @@ import 'package:cw_core/pending_transaction.dart';
import 'package:cw_core/sync_status.dart';
import 'package:cw_core/transaction_direction.dart';
import 'package:cw_core/transaction_priority.dart';
import 'package:cw_core/utils/file.dart';
import 'package:cw_core/wallet_addresses.dart';
import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_info.dart';
@ -19,6 +18,7 @@ import 'package:cw_ethereum/erc20_balance.dart';
import 'package:cw_ethereum/ethereum_formatter.dart';
import 'package:cw_core/erc20_token.dart';
import 'package:cw_polygon/default_polygon_erc20_tokens.dart';
import 'package:cw_core/encryption_file_utils.dart';
import 'package:cw_polygon/polygon_client.dart';
import 'package:cw_polygon/polygon_exceptions.dart';
import 'package:cw_polygon/polygon_formatter.dart';
@ -48,6 +48,7 @@ abstract class PolygonWalletBase
String? mnemonic,
String? privateKey,
required String password,
required EncryptionFileUtils encryptionFileUtils,
ERC20Balance? initialBalance,
required this.isFlatpak,
}) : syncStatus = const NotConnectedSyncStatus(),
@ -55,13 +56,15 @@ abstract class PolygonWalletBase
_mnemonic = mnemonic,
_hexPrivateKey = privateKey,
_isTransactionUpdating = false,
_encryptionFileUtils = encryptionFileUtils,
_client = PolygonClient(),
walletAddresses = PolygonWalletAddresses(walletInfo),
balance = ObservableMap<CryptoCurrency, ERC20Balance>.of(
{CryptoCurrency.maticpoly: initialBalance ?? ERC20Balance(BigInt.zero)}),
super(walletInfo) {
this.walletInfo = walletInfo;
transactionHistory = PolygonTransactionHistory(walletInfo: walletInfo, password: password);
transactionHistory =
PolygonTransactionHistory(walletInfo: walletInfo, password: password, isFlatpak: isFlatpak);
if (!CakeHive.isAdapterRegistered(Erc20Token.typeId)) {
CakeHive.registerAdapter(Erc20TokenAdapter());
@ -76,6 +79,8 @@ abstract class PolygonWalletBase
final String? _hexPrivateKey;
final String _password;
final EncryptionFileUtils _encryptionFileUtils;
late final Box<Erc20Token> polygonErc20TokensBox;
late final EthPrivateKey _polygonPrivateKey;
@ -314,7 +319,7 @@ abstract class PolygonWalletBase
Future<void> save() async {
await walletAddresses.updateAddressesInBox();
final path = await makePath();
await write(path: path, password: _password, data: toJSON());
await _encryptionFileUtils.write(path: path, password: _password, data: toJSON());
await transactionHistory.save();
}
@ -358,9 +363,10 @@ abstract class PolygonWalletBase
{required String name,
required String password,
required WalletInfo walletInfo,
required EncryptionFileUtils encryptionFileUtils,
required bool isFlatpak}) async {
final path = await pathForWallet(name: name, type: walletInfo.type, isFlatpak: isFlatpak);
final jsonSource = await read(path: path, password: password);
final jsonSource = await encryptionFileUtils.read(path: path, password: password);
final data = json.decode(jsonSource) as Map;
final mnemonic = data['mnemonic'] as String?;
final privateKey = data['private_key'] as String?;
@ -372,6 +378,8 @@ abstract class PolygonWalletBase
mnemonic: mnemonic,
privateKey: privateKey,
initialBalance: balance,
encryptionFileUtils: encryptionFileUtils,
isFlatpak: isFlatpak,
);
}

View file

@ -5,6 +5,7 @@ import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:cw_core/wallet_service.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:cw_core/encryption_file_utils.dart';
import 'package:cw_ethereum/ethereum_mnemonics.dart';
import 'package:cw_polygon/polygon_wallet.dart';
import 'package:bip39/bip39.dart' as bip39;
@ -14,9 +15,10 @@ import 'package:collection/collection.dart';
class PolygonWalletService extends WalletService<PolygonNewWalletCredentials,
PolygonRestoreWalletFromSeedCredentials, PolygonRestoreWalletFromPrivateKey> {
PolygonWalletService(this.walletInfoSource, this.isFlatpak);
PolygonWalletService(this.walletInfoSource, this.isDirect, this.isFlatpak);
final Box<WalletInfo> walletInfoSource;
final bool isDirect;
final bool isFlatpak;
@override
@ -28,6 +30,7 @@ class PolygonWalletService extends WalletService<PolygonNewWalletCredentials,
walletInfo: credentials.walletInfo!,
mnemonic: mnemonic,
password: credentials.password!,
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
isFlatpak: isFlatpak,
);
@ -53,6 +56,7 @@ class PolygonWalletService extends WalletService<PolygonNewWalletCredentials,
name: name,
password: password,
walletInfo: walletInfo,
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
isFlatpak: isFlatpak,
);
@ -77,6 +81,7 @@ class PolygonWalletService extends WalletService<PolygonNewWalletCredentials,
password: credentials.password!,
privateKey: credentials.privateKey,
walletInfo: credentials.walletInfo!,
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
isFlatpak: isFlatpak,
);
@ -97,6 +102,7 @@ class PolygonWalletService extends WalletService<PolygonNewWalletCredentials,
password: credentials.password!,
mnemonic: credentials.mnemonic,
walletInfo: credentials.walletInfo!,
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
isFlatpak: isFlatpak,
);
@ -115,6 +121,7 @@ class PolygonWalletService extends WalletService<PolygonNewWalletCredentials,
password: password,
name: currentName,
walletInfo: currentWalletInfo,
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
isFlatpak: isFlatpak,
);

View file

@ -137,116 +137,6 @@ class CWBitcoin extends Bitcoin {
@override
int formatterStringDoubleToBitcoinAmount(String amount) => stringDoubleToBitcoinAmount(amount);
@override
WalletCredentials createBitcoinRestoreWalletFromSeedCredentials(
{required String name, required String mnemonic, required String password}) =>
BitcoinRestoreWalletFromSeedCredentials(name: name, mnemonic: mnemonic, password: password);
@override
WalletCredentials createBitcoinRestoreWalletFromWIFCredentials(
{required String name,
required String password,
required String wif,
WalletInfo? walletInfo}) =>
BitcoinRestoreWalletFromWIFCredentials(
name: name, password: password, wif: wif, walletInfo: walletInfo);
@override
WalletCredentials createBitcoinNewWalletCredentials(
{required String name, WalletInfo? walletInfo, String? password}) =>
BitcoinNewWalletCredentials(name: name, walletInfo: walletInfo, password: password);
@override
List<String> getWordList() => wordlist;
@override
Map<String, String> getWalletKeys(Object wallet) {
final bitcoinWallet = wallet as ElectrumWallet;
final keys = bitcoinWallet.keys;
return <String, String>{
'wif': keys.wif,
'privateKey': keys.privateKey,
'publicKey': keys.publicKey
};
}
@override
List<TransactionPriority> getTransactionPriorities() => BitcoinTransactionPriority.all;
@override
List<TransactionPriority> getLitecoinTransactionPriorities() => LitecoinTransactionPriority.all;
@override
TransactionPriority deserializeBitcoinTransactionPriority(int raw) =>
BitcoinTransactionPriority.deserialize(raw: raw);
@override
TransactionPriority deserializeLitecoinTransactionPriority(int raw) =>
LitecoinTransactionPriority.deserialize(raw: raw);
@override
int getFeeRate(Object wallet, TransactionPriority priority) {
final bitcoinWallet = wallet as ElectrumWallet;
return bitcoinWallet.feeRate(priority);
}
@override
Future<void> generateNewAddress(Object wallet) async {
final bitcoinWallet = wallet as ElectrumWallet;
await bitcoinWallet.walletAddresses.generateNewAddress();
}
@override
Object createBitcoinTransactionCredentials(List<Output> outputs,
{required TransactionPriority priority, int? feeRate}) =>
BitcoinTransactionCredentials(
outputs
.map((out) => OutputInfo(
fiatAmount: out.fiatAmount,
cryptoAmount: out.cryptoAmount,
address: out.address,
note: out.note,
sendAll: out.sendAll,
extractedAddress: out.extractedAddress,
isParsedAddress: out.isParsedAddress,
formattedCryptoAmount: out.formattedCryptoAmount))
.toList(),
priority: priority as BitcoinTransactionPriority,
feeRate: feeRate);
@override
Object createBitcoinTransactionCredentialsRaw(List<OutputInfo> outputs,
{TransactionPriority? priority, required int feeRate}) =>
BitcoinTransactionCredentials(outputs,
priority: priority != null ? priority as BitcoinTransactionPriority : null,
feeRate: feeRate);
@override
List<String> getAddresses(Object wallet) {
final bitcoinWallet = wallet as ElectrumWallet;
return bitcoinWallet.walletAddresses.addresses
.map((BitcoinAddressRecord addr) => addr.address)
.toList();
}
@override
String getAddress(Object wallet) {
final bitcoinWallet = wallet as ElectrumWallet;
return bitcoinWallet.walletAddresses.address;
}
@override
String formatterBitcoinAmountToString({required int amount}) =>
bitcoinAmountToString(amount: amount);
@override
double formatterBitcoinAmountToDouble({required int amount}) =>
bitcoinAmountToDouble(amount: amount);
@override
int formatterStringDoubleToBitcoinAmount(String amount) => stringDoubleToBitcoinAmount(amount);
@override
String bitcoinTransactionPriorityWithLabel(TransactionPriority priority, int rate) =>
(priority as BitcoinTransactionPriority).labelWithRate(rate);

View file

@ -5,8 +5,8 @@ class CWPolygon extends Polygon {
List<String> getPolygonWordList(String language) => EthereumMnemonics.englishWordlist;
WalletService createPolygonWalletService(
Box<WalletInfo> walletInfoSource, bool isDirec, bool isFlatpak) =>
PolygonWalletService(walletInfoSource, isFlatpak);
Box<WalletInfo> walletInfoSource, bool isDirect, bool isFlatpak) =>
PolygonWalletService(walletInfoSource, isDirect, isFlatpak);
@override
WalletCredentials createPolygonNewWalletCredentials(

View file

@ -637,7 +637,7 @@ import 'package:cw_ethereum/ethereum_mnemonics.dart';
const polygonContent = """
abstract class Polygon {
List<String> getPolygonWordList(String language);
WalletService createPolygonWalletService(Box<WalletInfo> walletInfoSource, bool isDirec, bool isFlatpak);
WalletService createPolygonWalletService(Box<WalletInfo> walletInfoSource, bool isDirect, bool isFlatpak);
WalletCredentials createPolygonNewWalletCredentials({required String name, WalletInfo? walletInfo, String? password});
WalletCredentials createPolygonRestoreWalletFromSeedCredentials({required String name, required String mnemonic, required String password});
WalletCredentials createPolygonRestoreWalletFromPrivateKey({required String name, required String privateKey, required String password});