This commit is contained in:
fosse 2023-08-24 18:06:58 -04:00
parent ee062148a3
commit 47bf8cc7c1
8 changed files with 98 additions and 95 deletions

View file

@ -6,7 +6,7 @@ Map<DerivationType, List<DerivationInfo>> bitcoin_derivations = {
derivationType: DerivationType.bip39,
derivationPath: "m/0'/1",
description: "cake default?",
script_type: "???",
script_type: "p2wpkh",// TODO: not really sure what cake uses by default
),
DerivationInfo(
derivationType: DerivationType.bip39,

View file

@ -45,7 +45,7 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
initialChangeAddressIndex: initialChangeAddressIndex,
mainHd: hd,
sideHd: bitcoin.HDWallet.fromSeed(seedBytes, network: networkType)
.derivePath(walletInfo.derivationPath!),// default: "m/0'/1"
.derivePath(walletInfo.derivationPath!),
networkType: networkType);
}
@ -78,6 +78,16 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
required String password,
}) async {
final snp = await ElectrumWallletSnapshot.load(name, walletInfo.type, password);
walletInfo.derivationType = snp.derivationType;
walletInfo.derivationPath = snp.derivationPath;
// set the default if not present:
if (walletInfo.derivationPath == null) {
walletInfo.derivationPath = "m/0'/1";
}
return BitcoinWallet(
mnemonic: snp.mnemonic,
password: password,

View file

@ -1,9 +1,11 @@
import 'dart:io';
import 'package:cw_bitcoin/address_to_output_script.dart';
import 'package:cw_bitcoin/bitcoin_mnemonic.dart';
import 'package:cw_bitcoin/bitcoin_mnemonic_is_incorrect_exception.dart';
import 'package:cw_bitcoin/bitcoin_wallet_creation_credentials.dart';
import 'package:cw_bitcoin/electrum.dart';
import 'package:cw_bitcoin/script_hash.dart';
import 'package:cw_bitcoin/utils.dart';
import 'package:cw_core/node.dart';
import 'package:cw_core/unspent_coins_info.dart';
import 'package:cw_core/wallet_base.dart';
@ -112,12 +114,12 @@ class BitcoinWalletService extends WalletService<BitcoinNewWalletCredentials,
static Future<List<DerivationType>> compareDerivationMethods(
{required mnemonic, required Node node}) async {
return [DerivationType.bip39];
return [DerivationType.unknown];
}
static Future<List<DerivationInfo>> getDerivationsFromMnemonic(
{required String mnemonic, required Node node}) async {
var list = [];
List<DerivationInfo> list = [];
final electrumClient = ElectrumClient();
await electrumClient.connectToUri(node.uri);
@ -128,23 +130,44 @@ class BitcoinWalletService extends WalletService<BitcoinNewWalletCredentials,
if (dType == DerivationType.bip39) {
for (DerivationInfo dInfo in bitcoin_derivations[dType]!) {
try {
print("${dInfo.derivationType.toString()} : ${dInfo.derivationPath}");
var wallet = bitcoin.HDWallet.fromSeed(await mnemonicToSeedBytes(mnemonic),
network: bitcoin.bitcoin)
.derivePath("m/0'/1");
.derivePath(dInfo.derivationPath!);
// get addresses:
final sh = scriptHash(wallet.address!, networkType: bitcoin.bitcoin);
final balance = await electrumClient.getBalance(sh);
String? address;
switch (dInfo.script_type) {
case "p2wpkh":
address = bitcoin
.P2WPKH(
data: generatePaymentData(hd: wallet, index: 0), network: bitcoin.bitcoin)
.data
.address;
break;
case "p2pkh":
address = bitcoin
.P2PKH(
data: generatePaymentData(hd: wallet, index: 0), network: bitcoin.bitcoin)
.data
.address;
break;
case "p2wpkh-p2sh":
// address = bitcoin.P
default:
address = wallet.address;
break;
}
print(
"${dInfo.derivationType.toString()} : ${dInfo.derivationPath} : ${dInfo.script_type} : ${address}");
final sh = scriptHash(address!, networkType: bitcoin.bitcoin);
final history = await electrumClient.getHistory(sh);
print("history:");
print(history);
print(history.length);
print(history);
final balance = await electrumClient.getBalance(sh);
dInfo.balance = balance.entries.first.value.toString();
dInfo.address = wallet.address ?? "";
dInfo.address = address;
dInfo.height = history.length;
list.add(dInfo);
@ -155,51 +178,7 @@ class BitcoinWalletService extends WalletService<BitcoinNewWalletCredentials,
}
}
// default derivation path:
var wallet =
bitcoin.HDWallet.fromSeed(await mnemonicToSeedBytes(mnemonic), network: bitcoin.bitcoin)
.derivePath("m/0'/1");
// get addresses:
final sh = scriptHash(wallet.address!, networkType: bitcoin.bitcoin);
final balance = await electrumClient.getBalance(sh);
wallet.derive(0);
print(wallet.address);
print(balance.entries);
print("@@@@@@@@@@@@@");
// final wallet = await BitcoinWalletBase.create(
// password: "password",
// mnemonic: mnemonic,
// walletInfo: WalletInfo(
// "id",
// "test",
// WalletType.bitcoin,
// false,
// 0,
// 0,
// "dirPath",
// "path",
// "",
// null,
// "yatLastUsedAddressRaw",
// false,
// DerivationType.bip39,
// "derivationPath",
// ),
// unspentCoinsInfo: unspentCoinsInfoSource);
list.add(DerivationInfo(
derivationType: DerivationType.bip39,
balance: "0.00000",
address: "address",
height: 0,
));
return [];
return list;
}
static Future<dynamic> getInfoFromSeed({required String seed, required Node node}) async {

View file

@ -3,6 +3,7 @@ import 'package:cw_bitcoin/bitcoin_address_record.dart';
import 'package:cw_bitcoin/electrum_balance.dart';
import 'package:cw_bitcoin/file.dart';
import 'package:cw_core/pathForWallet.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:cw_core/wallet_type.dart';
class ElectrumWallletSnapshot {
@ -14,7 +15,10 @@ class ElectrumWallletSnapshot {
required this.addresses,
required this.balance,
required this.regularAddressIndex,
required this.changeAddressIndex});
required this.changeAddressIndex,
this.derivationType,
this.derivationPath,
});
final String name;
final String password;
@ -25,6 +29,8 @@ class ElectrumWallletSnapshot {
ElectrumBalance balance;
int regularAddressIndex;
int changeAddressIndex;
DerivationType? derivationType;
String? derivationPath;
static Future<ElectrumWallletSnapshot> load(String name, WalletType type, String password) async {
final path = await pathForWallet(name: name, type: type);
@ -41,6 +47,9 @@ class ElectrumWallletSnapshot {
var regularAddressIndex = 0;
var changeAddressIndex = 0;
final derivationType = data['derivationType'] as DerivationType;
final derivationPath = data['derivationPath'] as String?;
try {
regularAddressIndex = int.parse(data['account_index'] as String? ?? '0');
changeAddressIndex = int.parse(data['change_address_index'] as String? ?? '0');
@ -54,6 +63,8 @@ class ElectrumWallletSnapshot {
addresses: addresses,
balance: balance,
regularAddressIndex: regularAddressIndex,
changeAddressIndex: changeAddressIndex);
changeAddressIndex: changeAddressIndex,
derivationType: derivationType,
derivationPath: derivationPath);
}
}

View file

@ -370,7 +370,7 @@ abstract class NanoWalletBase
try {
balance[currency] = await _client.getBalance(_publicAddress!);
} catch (e) {
throw Exception("Failed to get balance $e");
print("Failed to get balance $e");
}
await save();
}

View file

@ -56,7 +56,7 @@ class WalletRestoreChooseDerivationPage extends BasePage {
child: InkWell(
borderRadius: BorderRadius.circular(15),
onTap: () async {
Navigator.pop(context, derivation.derivationType);
Navigator.pop(context, derivation);
},
child: ListTile(
contentPadding: EdgeInsets.all(16),
@ -73,6 +73,17 @@ class WalletRestoreChooseDerivationPage extends BasePage {
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (derivation.derivationPath != null)
Text(
derivation.derivationPath!,
style: Theme.of(context).primaryTextTheme.labelMedium!.copyWith(
fontSize: 14,
fontWeight: FontWeight.w500,
color: Theme.of(context)
.extension<CakeTextTheme>()!
.secondaryTextColor,
),
),
Text(
derivation.address,
style: Theme.of(context).primaryTextTheme.labelMedium!.copyWith(

View file

@ -72,7 +72,8 @@ class WalletRestorePage extends BasePage {
key: walletRestoreFromKeysFormKey,
walletRestoreViewModel: walletRestoreViewModel,
onPrivateKeyChange: (String seed) {
if (walletRestoreViewModel.type == WalletType.nano || walletRestoreViewModel.type == WalletType.banano) {
if (walletRestoreViewModel.type == WalletType.nano ||
walletRestoreViewModel.type == WalletType.banano) {
walletRestoreViewModel.isButtonEnabled = _isValidSeedKey();
}
},
@ -105,6 +106,7 @@ class WalletRestorePage extends BasePage {
final GlobalKey<WalletRestoreFromKeysFromState> walletRestoreFromKeysFormKey;
final FocusNode _blockHeightFocusNode;
DerivationType derivationType = DerivationType.unknown;
String? derivationPath = null;
@override
Widget body(BuildContext context) {
@ -281,8 +283,8 @@ class WalletRestorePage extends BasePage {
}
credentials['derivationType'] = this.derivationType;
credentials['derivationPath'] = this.derivationPath;
credentials['walletType'] = walletRestoreViewModel.type;
return credentials;
}
@ -319,16 +321,18 @@ class WalletRestorePage extends BasePage {
if (derivationTypes[0] == DerivationType.unknown || derivationTypes.length > 1) {
// push screen to choose the derivation type:
var derivationType = await Navigator.of(context)
var derivationInfo = await Navigator.of(context)
.pushNamed(Routes.restoreWalletChooseDerivation, arguments: _credentials())
as DerivationType?;
if (derivationType == null) {
as DerivationInfo?;
if (derivationInfo == null) {
walletRestoreViewModel.state = InitialExecutionState();
return;
}
this.derivationType = derivationType;
this.derivationType = derivationInfo.derivationType;
this.derivationPath = derivationInfo.derivationPath;
} else {
this.derivationType = derivationTypes[0];
this.derivationPath = "m/0'/1";
}
walletRestoreViewModel.state = InitialExecutionState();

View file

@ -30,7 +30,7 @@ abstract class WalletRestoreChooseDerivationViewModelBase with Store {
switch (walletType) {
case WalletType.bitcoin:
String? mnemonic = credentials['seed'] as String?;
await BitcoinWalletService.getDerivationsFromMnemonic(mnemonic: mnemonic!, node: node);
return await BitcoinWalletService.getDerivationsFromMnemonic(mnemonic: mnemonic!, node: node);
// var standardInfo = await NanoWalletService.getInfoFromSeedOrMnemonic(
// DerivationType.nano,
@ -39,24 +39,12 @@ abstract class WalletRestoreChooseDerivationViewModelBase with Store {
// node: node,
// );
list.add(DerivationInfo(
balance: "0.00000",
address: "address",
height: 0,
derivationType: DerivationType.bip39,
));
// if (bip39Info["balance"] != null) {
// list.add(Derivation(
// NanoUtil.getRawAsUsableString(bip39Info["balance"] as String, NanoUtil.rawPerNano),
// bip39Info["address"] as String,
// DerivationType.bip39,
// int.tryParse(
// bip39Info["confirmation_height"] as String? ?? "",
// ) ??
// 0,
// list.add(DerivationInfo(
// balance: "0.00000",
// address: "address",
// height: 0,
// derivationType: DerivationType.bip39,
// ));
// }
break;
case WalletType.nano:
String? mnemonic = credentials['seed'] as String?;