mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2024-12-23 03:59:23 +00:00
review fixes from nano that also apply here
This commit is contained in:
parent
7f017a3444
commit
5ad89f252b
5 changed files with 103 additions and 99 deletions
|
@ -124,93 +124,6 @@ class BitcoinWalletService extends WalletService<BitcoinNewWalletCredentials,
|
||||||
return wallet;
|
return wallet;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<List<DerivationType>> compareDerivationMethods(
|
|
||||||
{required String mnemonic, required Node node}) async {
|
|
||||||
if (await checkIfMnemonicIsElectrum2(mnemonic)) {
|
|
||||||
return [DerivationType.electrum2];
|
|
||||||
}
|
|
||||||
|
|
||||||
return [DerivationType.bip39, DerivationType.electrum2];
|
|
||||||
}
|
|
||||||
|
|
||||||
static Future<List<DerivationInfo>> getDerivationsFromMnemonic(
|
|
||||||
{required String mnemonic, required Node node}) async {
|
|
||||||
List<DerivationInfo> list = [];
|
|
||||||
|
|
||||||
final electrumClient = ElectrumClient();
|
|
||||||
await electrumClient.connectToUri(node.uri);
|
|
||||||
|
|
||||||
for (DerivationType dType in bitcoin_derivations.keys) {
|
|
||||||
late Uint8List seedBytes;
|
|
||||||
if (dType == DerivationType.electrum2) {
|
|
||||||
seedBytes = await mnemonicToSeedBytes(mnemonic);
|
|
||||||
} else if (dType == DerivationType.bip39) {
|
|
||||||
seedBytes = bip39.mnemonicToSeed(mnemonic);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (DerivationInfo dInfo in bitcoin_derivations[dType]!) {
|
|
||||||
try {
|
|
||||||
DerivationInfo dInfoCopy = DerivationInfo(
|
|
||||||
derivationType: dInfo.derivationType,
|
|
||||||
derivationPath: dInfo.derivationPath,
|
|
||||||
description: dInfo.description,
|
|
||||||
script_type: dInfo.script_type,
|
|
||||||
);
|
|
||||||
var node = bip32.BIP32.fromSeed(seedBytes);
|
|
||||||
|
|
||||||
String derivationPath = dInfoCopy.derivationPath!;
|
|
||||||
int derivationDepth = countOccurrences(derivationPath, "/");
|
|
||||||
if (derivationDepth == 3) {
|
|
||||||
derivationPath += "/0/0";
|
|
||||||
dInfoCopy.derivationPath = dInfoCopy.derivationPath! + "/0";
|
|
||||||
}
|
|
||||||
node = node.derivePath(derivationPath);
|
|
||||||
|
|
||||||
String? address;
|
|
||||||
switch (dInfoCopy.script_type) {
|
|
||||||
case "p2wpkh":
|
|
||||||
address = bitcoin
|
|
||||||
.P2WPKH(
|
|
||||||
data: new bitcoin.PaymentData(pubkey: node.publicKey),
|
|
||||||
network: bitcoin.bitcoin,
|
|
||||||
)
|
|
||||||
.data
|
|
||||||
.address;
|
|
||||||
break;
|
|
||||||
case "p2pkh":
|
|
||||||
// case "p2wpkh-p2sh":// TODO
|
|
||||||
default:
|
|
||||||
address = bitcoin
|
|
||||||
.P2PKH(
|
|
||||||
data: new bitcoin.PaymentData(pubkey: node.publicKey),
|
|
||||||
network: bitcoin.bitcoin,
|
|
||||||
)
|
|
||||||
.data
|
|
||||||
.address;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
final sh = scriptHash(address!, networkType: bitcoin.bitcoin);
|
|
||||||
final history = await electrumClient.getHistory(sh);
|
|
||||||
|
|
||||||
final balance = await electrumClient.getBalance(sh);
|
|
||||||
dInfoCopy.balance = balance.entries.first.value.toString();
|
|
||||||
dInfoCopy.address = address;
|
|
||||||
dInfoCopy.height = history.length;
|
|
||||||
|
|
||||||
list.add(dInfoCopy);
|
|
||||||
} catch (e) {
|
|
||||||
print(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// sort the list such that derivations with the most transactions are first:
|
|
||||||
list.sort((a, b) => b.height.compareTo(a.height));
|
|
||||||
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
static Future<dynamic> getInfoFromSeed({required String seed, required Node node}) async {
|
static Future<dynamic> getInfoFromSeed({required String seed, required Node node}) async {
|
||||||
throw UnimplementedError();
|
throw UnimplementedError();
|
||||||
}
|
}
|
||||||
|
|
|
@ -163,9 +163,91 @@ class CWBitcoin extends Bitcoin {
|
||||||
TransactionPriority getLitecoinTransactionPrioritySlow() => LitecoinTransactionPriority.slow;
|
TransactionPriority getLitecoinTransactionPrioritySlow() => LitecoinTransactionPriority.slow;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
dynamic getBitcoinWalletService() async {
|
Future<List<DerivationType>> compareDerivationMethods(
|
||||||
Box<WalletInfo> _walletInfoSource = await CakeHive.openBox<WalletInfo>(WalletInfo.boxName);
|
{required String mnemonic, required Node node}) async {
|
||||||
Box<UnspentCoinsInfo> _unspentCoinsInfoSource = await CakeHive.openBox<UnspentCoinsInfo>(UnspentCoinsInfo.boxName);
|
if (await checkIfMnemonicIsElectrum2(mnemonic)) {
|
||||||
return BitcoinWalletService(_walletInfoSource, _unspentCoinsInfoSource);
|
return [DerivationType.electrum2];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [DerivationType.bip39, DerivationType.electrum2];
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<List<DerivationInfo>> getDerivationsFromMnemonic(
|
||||||
|
{required String mnemonic, required Node node}) async {
|
||||||
|
List<DerivationInfo> list = [];
|
||||||
|
|
||||||
|
final electrumClient = ElectrumClient();
|
||||||
|
await electrumClient.connectToUri(node.uri);
|
||||||
|
|
||||||
|
for (DerivationType dType in bitcoin_derivations.keys) {
|
||||||
|
late Uint8List seedBytes;
|
||||||
|
if (dType == DerivationType.electrum2) {
|
||||||
|
seedBytes = await mnemonicToSeedBytes(mnemonic);
|
||||||
|
} else if (dType == DerivationType.bip39) {
|
||||||
|
seedBytes = bip39.mnemonicToSeed(mnemonic);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (DerivationInfo dInfo in bitcoin_derivations[dType]!) {
|
||||||
|
try {
|
||||||
|
DerivationInfo dInfoCopy = DerivationInfo(
|
||||||
|
derivationType: dInfo.derivationType,
|
||||||
|
derivationPath: dInfo.derivationPath,
|
||||||
|
description: dInfo.description,
|
||||||
|
script_type: dInfo.script_type,
|
||||||
|
);
|
||||||
|
var node = bip32.BIP32.fromSeed(seedBytes);
|
||||||
|
|
||||||
|
String derivationPath = dInfoCopy.derivationPath!;
|
||||||
|
int derivationDepth = countOccurrences(derivationPath, "/");
|
||||||
|
if (derivationDepth == 3) {
|
||||||
|
derivationPath += "/0/0";
|
||||||
|
dInfoCopy.derivationPath = dInfoCopy.derivationPath! + "/0";
|
||||||
|
}
|
||||||
|
node = node.derivePath(derivationPath);
|
||||||
|
|
||||||
|
String? address;
|
||||||
|
switch (dInfoCopy.script_type) {
|
||||||
|
case "p2wpkh":
|
||||||
|
address = btc
|
||||||
|
.P2WPKH(
|
||||||
|
data: new btc.PaymentData(pubkey: node.publicKey),
|
||||||
|
network: btc.bitcoin,
|
||||||
|
)
|
||||||
|
.data
|
||||||
|
.address;
|
||||||
|
break;
|
||||||
|
case "p2pkh":
|
||||||
|
// case "p2wpkh-p2sh":// TODO
|
||||||
|
default:
|
||||||
|
address = btc
|
||||||
|
.P2PKH(
|
||||||
|
data: new btc.PaymentData(pubkey: node.publicKey),
|
||||||
|
network: btc.bitcoin,
|
||||||
|
)
|
||||||
|
.data
|
||||||
|
.address;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
final sh = scriptHash(address!, networkType: btc.bitcoin);
|
||||||
|
final history = await electrumClient.getHistory(sh);
|
||||||
|
|
||||||
|
final balance = await electrumClient.getBalance(sh);
|
||||||
|
dInfoCopy.balance = balance.entries.first.value.toString();
|
||||||
|
dInfoCopy.address = address;
|
||||||
|
dInfoCopy.height = history.length;
|
||||||
|
|
||||||
|
list.add(dInfoCopy);
|
||||||
|
} catch (e) {
|
||||||
|
print(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// sort the list such that derivations with the most transactions are first:
|
||||||
|
list.sort((a, b) => b.height.compareTo(a.height));
|
||||||
|
|
||||||
|
return list;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import 'package:cake_wallet/bitcoin/bitcoin.dart';
|
||||||
import 'package:cake_wallet/di.dart';
|
import 'package:cake_wallet/di.dart';
|
||||||
import 'package:cake_wallet/routes.dart';
|
import 'package:cake_wallet/routes.dart';
|
||||||
import 'package:cake_wallet/store/app_store.dart';
|
import 'package:cake_wallet/store/app_store.dart';
|
||||||
|
@ -308,7 +309,7 @@ class WalletRestorePage extends BasePage {
|
||||||
switch (walletType) {
|
switch (walletType) {
|
||||||
case WalletType.bitcoin:
|
case WalletType.bitcoin:
|
||||||
String? mnemonic = credentials['seed'] as String?;
|
String? mnemonic = credentials['seed'] as String?;
|
||||||
return await BitcoinWalletService.getDerivationsFromMnemonic(
|
return bitcoin!.getDerivationsFromMnemonic(
|
||||||
mnemonic: mnemonic!, node: node);
|
mnemonic: mnemonic!, node: node);
|
||||||
case WalletType.nano:
|
case WalletType.nano:
|
||||||
String? mnemonic = credentials['seed'] as String?;
|
String? mnemonic = credentials['seed'] as String?;
|
||||||
|
|
|
@ -165,13 +165,12 @@ abstract class WalletRestoreViewModelBase extends WalletCreationVM with Store {
|
||||||
final seedKey = options['private_key'] as String?;
|
final seedKey = options['private_key'] as String?;
|
||||||
final mnemonic = options['seed'] as String?;
|
final mnemonic = options['seed'] as String?;
|
||||||
WalletType walletType = options['walletType'] as WalletType;
|
WalletType walletType = options['walletType'] as WalletType;
|
||||||
var appStore = getIt.get<AppStore>();
|
AppStore appStore = getIt.get<AppStore>();
|
||||||
var node = appStore.settingsStore.getCurrentNode(walletType);
|
Node node = appStore.settingsStore.getCurrentNode(walletType);
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case WalletType.bitcoin:
|
case WalletType.bitcoin:
|
||||||
dynamic WalletService = await bitcoin!.getBitcoinWalletService();
|
return bitcoin!.compareDerivationMethods(mnemonic: mnemonic!, node: node);
|
||||||
return WalletService.compareDerivationMethods(mnemonic: mnemonic!, node: node) as Future<List<DerivationType>>;
|
|
||||||
// case WalletType.litecoin:
|
// case WalletType.litecoin:
|
||||||
// return bitcoin!.createBitcoinRestoreWalletFromSeedCredentials(
|
// return bitcoin!.createBitcoinRestoreWalletFromSeedCredentials(
|
||||||
// name: name, mnemonic: seed, password: password);
|
// name: name, mnemonic: seed, password: password);
|
||||||
|
|
|
@ -46,8 +46,9 @@ Future<void> main(List<String> args) async {
|
||||||
Future<void> generateBitcoin(bool hasImplementation) async {
|
Future<void> generateBitcoin(bool hasImplementation) async {
|
||||||
final outputFile = File(bitcoinOutputPath);
|
final outputFile = File(bitcoinOutputPath);
|
||||||
const bitcoinCommonHeaders = """
|
const bitcoinCommonHeaders = """
|
||||||
|
import 'dart:typed_data';
|
||||||
import 'package:cake_wallet/entities/unspent_transaction_output.dart';
|
import 'package:cake_wallet/entities/unspent_transaction_output.dart';
|
||||||
import 'package:cw_core/cake_hive.dart';
|
import 'package:cw_core/node.dart';
|
||||||
import 'package:cw_core/wallet_credentials.dart';
|
import 'package:cw_core/wallet_credentials.dart';
|
||||||
import 'package:cw_core/wallet_info.dart';
|
import 'package:cw_core/wallet_info.dart';
|
||||||
import 'package:cw_core/transaction_priority.dart';
|
import 'package:cw_core/transaction_priority.dart';
|
||||||
|
@ -55,19 +56,24 @@ import 'package:cw_core/output_info.dart';
|
||||||
import 'package:cw_core/unspent_coins_info.dart';
|
import 'package:cw_core/unspent_coins_info.dart';
|
||||||
import 'package:cw_core/wallet_service.dart';
|
import 'package:cw_core/wallet_service.dart';
|
||||||
import 'package:cake_wallet/view_model/send/output.dart';
|
import 'package:cake_wallet/view_model/send/output.dart';
|
||||||
|
import 'package:bitcoin_flutter/bitcoin_flutter.dart' as btc;
|
||||||
|
import 'package:bip32/bip32.dart' as bip32;
|
||||||
|
import 'package:bip39/bip39.dart' as bip39;
|
||||||
import 'package:hive/hive.dart';""";
|
import 'package:hive/hive.dart';""";
|
||||||
const bitcoinCWHeaders = """
|
const bitcoinCWHeaders = """
|
||||||
|
import 'package:cw_bitcoin/bitcoin_derivations.dart';
|
||||||
|
import 'package:cw_bitcoin/electrum.dart';
|
||||||
import 'package:cw_bitcoin/electrum_wallet.dart';
|
import 'package:cw_bitcoin/electrum_wallet.dart';
|
||||||
import 'package:cw_bitcoin/bitcoin_unspent.dart';
|
import 'package:cw_bitcoin/bitcoin_unspent.dart';
|
||||||
import 'package:cw_bitcoin/bitcoin_mnemonic.dart';
|
import 'package:cw_bitcoin/bitcoin_mnemonic.dart';
|
||||||
import 'package:cw_bitcoin/bitcoin_transaction_priority.dart';
|
import 'package:cw_bitcoin/bitcoin_transaction_priority.dart';
|
||||||
import 'package:cw_bitcoin/bitcoin_wallet.dart';
|
|
||||||
import 'package:cw_bitcoin/bitcoin_wallet_service.dart';
|
import 'package:cw_bitcoin/bitcoin_wallet_service.dart';
|
||||||
import 'package:cw_bitcoin/bitcoin_wallet_creation_credentials.dart';
|
import 'package:cw_bitcoin/bitcoin_wallet_creation_credentials.dart';
|
||||||
import 'package:cw_bitcoin/bitcoin_amount_format.dart';
|
import 'package:cw_bitcoin/bitcoin_amount_format.dart';
|
||||||
import 'package:cw_bitcoin/bitcoin_address_record.dart';
|
import 'package:cw_bitcoin/bitcoin_address_record.dart';
|
||||||
import 'package:cw_bitcoin/bitcoin_transaction_credentials.dart';
|
import 'package:cw_bitcoin/bitcoin_transaction_credentials.dart';
|
||||||
import 'package:cw_bitcoin/litecoin_wallet_service.dart';
|
import 'package:cw_bitcoin/litecoin_wallet_service.dart';
|
||||||
|
import 'package:cw_bitcoin/script_hash.dart';
|
||||||
""";
|
""";
|
||||||
const bitcoinCwPart = "part 'cw_bitcoin.dart';";
|
const bitcoinCwPart = "part 'cw_bitcoin.dart';";
|
||||||
const bitcoinContent = """
|
const bitcoinContent = """
|
||||||
|
@ -104,7 +110,10 @@ abstract class Bitcoin {
|
||||||
TransactionPriority getLitecoinTransactionPriorityMedium();
|
TransactionPriority getLitecoinTransactionPriorityMedium();
|
||||||
TransactionPriority getBitcoinTransactionPrioritySlow();
|
TransactionPriority getBitcoinTransactionPrioritySlow();
|
||||||
TransactionPriority getLitecoinTransactionPrioritySlow();
|
TransactionPriority getLitecoinTransactionPrioritySlow();
|
||||||
dynamic getBitcoinWalletService();
|
Future<List<DerivationType>> compareDerivationMethods(
|
||||||
|
{required String mnemonic, required Node node});
|
||||||
|
Future<List<DerivationInfo>> getDerivationsFromMnemonic(
|
||||||
|
{required String mnemonic, required Node node});
|
||||||
}
|
}
|
||||||
""";
|
""";
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue