mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-01-11 05:14:46 +00:00
updates
This commit is contained in:
parent
ee062148a3
commit
47bf8cc7c1
8 changed files with 98 additions and 95 deletions
|
@ -6,7 +6,7 @@ Map<DerivationType, List<DerivationInfo>> bitcoin_derivations = {
|
||||||
derivationType: DerivationType.bip39,
|
derivationType: DerivationType.bip39,
|
||||||
derivationPath: "m/0'/1",
|
derivationPath: "m/0'/1",
|
||||||
description: "cake default?",
|
description: "cake default?",
|
||||||
script_type: "???",
|
script_type: "p2wpkh",// TODO: not really sure what cake uses by default
|
||||||
),
|
),
|
||||||
DerivationInfo(
|
DerivationInfo(
|
||||||
derivationType: DerivationType.bip39,
|
derivationType: DerivationType.bip39,
|
||||||
|
|
|
@ -45,7 +45,7 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
|
||||||
initialChangeAddressIndex: initialChangeAddressIndex,
|
initialChangeAddressIndex: initialChangeAddressIndex,
|
||||||
mainHd: hd,
|
mainHd: hd,
|
||||||
sideHd: bitcoin.HDWallet.fromSeed(seedBytes, network: networkType)
|
sideHd: bitcoin.HDWallet.fromSeed(seedBytes, network: networkType)
|
||||||
.derivePath(walletInfo.derivationPath!),// default: "m/0'/1"
|
.derivePath(walletInfo.derivationPath!),
|
||||||
networkType: networkType);
|
networkType: networkType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,6 +78,16 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
|
||||||
required String password,
|
required String password,
|
||||||
}) async {
|
}) async {
|
||||||
final snp = await ElectrumWallletSnapshot.load(name, walletInfo.type, password);
|
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(
|
return BitcoinWallet(
|
||||||
mnemonic: snp.mnemonic,
|
mnemonic: snp.mnemonic,
|
||||||
password: password,
|
password: password,
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
import 'dart:io';
|
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.dart';
|
||||||
import 'package:cw_bitcoin/bitcoin_mnemonic_is_incorrect_exception.dart';
|
import 'package:cw_bitcoin/bitcoin_mnemonic_is_incorrect_exception.dart';
|
||||||
import 'package:cw_bitcoin/bitcoin_wallet_creation_credentials.dart';
|
import 'package:cw_bitcoin/bitcoin_wallet_creation_credentials.dart';
|
||||||
import 'package:cw_bitcoin/electrum.dart';
|
import 'package:cw_bitcoin/electrum.dart';
|
||||||
import 'package:cw_bitcoin/script_hash.dart';
|
import 'package:cw_bitcoin/script_hash.dart';
|
||||||
|
import 'package:cw_bitcoin/utils.dart';
|
||||||
import 'package:cw_core/node.dart';
|
import 'package:cw_core/node.dart';
|
||||||
import 'package:cw_core/unspent_coins_info.dart';
|
import 'package:cw_core/unspent_coins_info.dart';
|
||||||
import 'package:cw_core/wallet_base.dart';
|
import 'package:cw_core/wallet_base.dart';
|
||||||
|
@ -112,12 +114,12 @@ class BitcoinWalletService extends WalletService<BitcoinNewWalletCredentials,
|
||||||
|
|
||||||
static Future<List<DerivationType>> compareDerivationMethods(
|
static Future<List<DerivationType>> compareDerivationMethods(
|
||||||
{required mnemonic, required Node node}) async {
|
{required mnemonic, required Node node}) async {
|
||||||
return [DerivationType.bip39];
|
return [DerivationType.unknown];
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<List<DerivationInfo>> getDerivationsFromMnemonic(
|
static Future<List<DerivationInfo>> getDerivationsFromMnemonic(
|
||||||
{required String mnemonic, required Node node}) async {
|
{required String mnemonic, required Node node}) async {
|
||||||
var list = [];
|
List<DerivationInfo> list = [];
|
||||||
|
|
||||||
final electrumClient = ElectrumClient();
|
final electrumClient = ElectrumClient();
|
||||||
await electrumClient.connectToUri(node.uri);
|
await electrumClient.connectToUri(node.uri);
|
||||||
|
@ -128,23 +130,44 @@ class BitcoinWalletService extends WalletService<BitcoinNewWalletCredentials,
|
||||||
if (dType == DerivationType.bip39) {
|
if (dType == DerivationType.bip39) {
|
||||||
for (DerivationInfo dInfo in bitcoin_derivations[dType]!) {
|
for (DerivationInfo dInfo in bitcoin_derivations[dType]!) {
|
||||||
try {
|
try {
|
||||||
print("${dInfo.derivationType.toString()} : ${dInfo.derivationPath}");
|
|
||||||
|
|
||||||
var wallet = bitcoin.HDWallet.fromSeed(await mnemonicToSeedBytes(mnemonic),
|
var wallet = bitcoin.HDWallet.fromSeed(await mnemonicToSeedBytes(mnemonic),
|
||||||
network: bitcoin.bitcoin)
|
network: bitcoin.bitcoin)
|
||||||
.derivePath("m/0'/1");
|
.derivePath(dInfo.derivationPath!);
|
||||||
|
|
||||||
// get addresses:
|
String? address;
|
||||||
final sh = scriptHash(wallet.address!, networkType: bitcoin.bitcoin);
|
switch (dInfo.script_type) {
|
||||||
final balance = await electrumClient.getBalance(sh);
|
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);
|
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.balance = balance.entries.first.value.toString();
|
||||||
dInfo.address = wallet.address ?? "";
|
dInfo.address = address;
|
||||||
dInfo.height = history.length;
|
dInfo.height = history.length;
|
||||||
|
|
||||||
list.add(dInfo);
|
list.add(dInfo);
|
||||||
|
@ -155,51 +178,7 @@ class BitcoinWalletService extends WalletService<BitcoinNewWalletCredentials,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// default derivation path:
|
return list;
|
||||||
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 [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<dynamic> getInfoFromSeed({required String seed, required Node node}) async {
|
static Future<dynamic> getInfoFromSeed({required String seed, required Node node}) async {
|
||||||
|
|
|
@ -3,6 +3,7 @@ import 'package:cw_bitcoin/bitcoin_address_record.dart';
|
||||||
import 'package:cw_bitcoin/electrum_balance.dart';
|
import 'package:cw_bitcoin/electrum_balance.dart';
|
||||||
import 'package:cw_bitcoin/file.dart';
|
import 'package:cw_bitcoin/file.dart';
|
||||||
import 'package:cw_core/pathForWallet.dart';
|
import 'package:cw_core/pathForWallet.dart';
|
||||||
|
import 'package:cw_core/wallet_info.dart';
|
||||||
import 'package:cw_core/wallet_type.dart';
|
import 'package:cw_core/wallet_type.dart';
|
||||||
|
|
||||||
class ElectrumWallletSnapshot {
|
class ElectrumWallletSnapshot {
|
||||||
|
@ -14,7 +15,10 @@ class ElectrumWallletSnapshot {
|
||||||
required this.addresses,
|
required this.addresses,
|
||||||
required this.balance,
|
required this.balance,
|
||||||
required this.regularAddressIndex,
|
required this.regularAddressIndex,
|
||||||
required this.changeAddressIndex});
|
required this.changeAddressIndex,
|
||||||
|
this.derivationType,
|
||||||
|
this.derivationPath,
|
||||||
|
});
|
||||||
|
|
||||||
final String name;
|
final String name;
|
||||||
final String password;
|
final String password;
|
||||||
|
@ -25,6 +29,8 @@ class ElectrumWallletSnapshot {
|
||||||
ElectrumBalance balance;
|
ElectrumBalance balance;
|
||||||
int regularAddressIndex;
|
int regularAddressIndex;
|
||||||
int changeAddressIndex;
|
int changeAddressIndex;
|
||||||
|
DerivationType? derivationType;
|
||||||
|
String? derivationPath;
|
||||||
|
|
||||||
static Future<ElectrumWallletSnapshot> load(String name, WalletType type, String password) async {
|
static Future<ElectrumWallletSnapshot> load(String name, WalletType type, String password) async {
|
||||||
final path = await pathForWallet(name: name, type: type);
|
final path = await pathForWallet(name: name, type: type);
|
||||||
|
@ -41,6 +47,9 @@ class ElectrumWallletSnapshot {
|
||||||
var regularAddressIndex = 0;
|
var regularAddressIndex = 0;
|
||||||
var changeAddressIndex = 0;
|
var changeAddressIndex = 0;
|
||||||
|
|
||||||
|
final derivationType = data['derivationType'] as DerivationType;
|
||||||
|
final derivationPath = data['derivationPath'] as String?;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
regularAddressIndex = int.parse(data['account_index'] as String? ?? '0');
|
regularAddressIndex = int.parse(data['account_index'] as String? ?? '0');
|
||||||
changeAddressIndex = int.parse(data['change_address_index'] as String? ?? '0');
|
changeAddressIndex = int.parse(data['change_address_index'] as String? ?? '0');
|
||||||
|
@ -54,6 +63,8 @@ class ElectrumWallletSnapshot {
|
||||||
addresses: addresses,
|
addresses: addresses,
|
||||||
balance: balance,
|
balance: balance,
|
||||||
regularAddressIndex: regularAddressIndex,
|
regularAddressIndex: regularAddressIndex,
|
||||||
changeAddressIndex: changeAddressIndex);
|
changeAddressIndex: changeAddressIndex,
|
||||||
|
derivationType: derivationType,
|
||||||
|
derivationPath: derivationPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -370,7 +370,7 @@ abstract class NanoWalletBase
|
||||||
try {
|
try {
|
||||||
balance[currency] = await _client.getBalance(_publicAddress!);
|
balance[currency] = await _client.getBalance(_publicAddress!);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw Exception("Failed to get balance $e");
|
print("Failed to get balance $e");
|
||||||
}
|
}
|
||||||
await save();
|
await save();
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,7 +56,7 @@ class WalletRestoreChooseDerivationPage extends BasePage {
|
||||||
child: InkWell(
|
child: InkWell(
|
||||||
borderRadius: BorderRadius.circular(15),
|
borderRadius: BorderRadius.circular(15),
|
||||||
onTap: () async {
|
onTap: () async {
|
||||||
Navigator.pop(context, derivation.derivationType);
|
Navigator.pop(context, derivation);
|
||||||
},
|
},
|
||||||
child: ListTile(
|
child: ListTile(
|
||||||
contentPadding: EdgeInsets.all(16),
|
contentPadding: EdgeInsets.all(16),
|
||||||
|
@ -73,6 +73,17 @@ class WalletRestoreChooseDerivationPage extends BasePage {
|
||||||
subtitle: Column(
|
subtitle: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
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(
|
Text(
|
||||||
derivation.address,
|
derivation.address,
|
||||||
style: Theme.of(context).primaryTextTheme.labelMedium!.copyWith(
|
style: Theme.of(context).primaryTextTheme.labelMedium!.copyWith(
|
||||||
|
|
|
@ -72,7 +72,8 @@ class WalletRestorePage extends BasePage {
|
||||||
key: walletRestoreFromKeysFormKey,
|
key: walletRestoreFromKeysFormKey,
|
||||||
walletRestoreViewModel: walletRestoreViewModel,
|
walletRestoreViewModel: walletRestoreViewModel,
|
||||||
onPrivateKeyChange: (String seed) {
|
onPrivateKeyChange: (String seed) {
|
||||||
if (walletRestoreViewModel.type == WalletType.nano || walletRestoreViewModel.type == WalletType.banano) {
|
if (walletRestoreViewModel.type == WalletType.nano ||
|
||||||
|
walletRestoreViewModel.type == WalletType.banano) {
|
||||||
walletRestoreViewModel.isButtonEnabled = _isValidSeedKey();
|
walletRestoreViewModel.isButtonEnabled = _isValidSeedKey();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -105,6 +106,7 @@ class WalletRestorePage extends BasePage {
|
||||||
final GlobalKey<WalletRestoreFromKeysFromState> walletRestoreFromKeysFormKey;
|
final GlobalKey<WalletRestoreFromKeysFromState> walletRestoreFromKeysFormKey;
|
||||||
final FocusNode _blockHeightFocusNode;
|
final FocusNode _blockHeightFocusNode;
|
||||||
DerivationType derivationType = DerivationType.unknown;
|
DerivationType derivationType = DerivationType.unknown;
|
||||||
|
String? derivationPath = null;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget body(BuildContext context) {
|
Widget body(BuildContext context) {
|
||||||
|
@ -281,8 +283,8 @@ class WalletRestorePage extends BasePage {
|
||||||
}
|
}
|
||||||
|
|
||||||
credentials['derivationType'] = this.derivationType;
|
credentials['derivationType'] = this.derivationType;
|
||||||
|
credentials['derivationPath'] = this.derivationPath;
|
||||||
credentials['walletType'] = walletRestoreViewModel.type;
|
credentials['walletType'] = walletRestoreViewModel.type;
|
||||||
|
|
||||||
return credentials;
|
return credentials;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -319,16 +321,18 @@ class WalletRestorePage extends BasePage {
|
||||||
|
|
||||||
if (derivationTypes[0] == DerivationType.unknown || derivationTypes.length > 1) {
|
if (derivationTypes[0] == DerivationType.unknown || derivationTypes.length > 1) {
|
||||||
// push screen to choose the derivation type:
|
// push screen to choose the derivation type:
|
||||||
var derivationType = await Navigator.of(context)
|
var derivationInfo = await Navigator.of(context)
|
||||||
.pushNamed(Routes.restoreWalletChooseDerivation, arguments: _credentials())
|
.pushNamed(Routes.restoreWalletChooseDerivation, arguments: _credentials())
|
||||||
as DerivationType?;
|
as DerivationInfo?;
|
||||||
if (derivationType == null) {
|
if (derivationInfo == null) {
|
||||||
walletRestoreViewModel.state = InitialExecutionState();
|
walletRestoreViewModel.state = InitialExecutionState();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.derivationType = derivationType;
|
this.derivationType = derivationInfo.derivationType;
|
||||||
|
this.derivationPath = derivationInfo.derivationPath;
|
||||||
} else {
|
} else {
|
||||||
this.derivationType = derivationTypes[0];
|
this.derivationType = derivationTypes[0];
|
||||||
|
this.derivationPath = "m/0'/1";
|
||||||
}
|
}
|
||||||
|
|
||||||
walletRestoreViewModel.state = InitialExecutionState();
|
walletRestoreViewModel.state = InitialExecutionState();
|
||||||
|
|
|
@ -30,7 +30,7 @@ abstract class WalletRestoreChooseDerivationViewModelBase with Store {
|
||||||
switch (walletType) {
|
switch (walletType) {
|
||||||
case WalletType.bitcoin:
|
case WalletType.bitcoin:
|
||||||
String? mnemonic = credentials['seed'] as String?;
|
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(
|
// var standardInfo = await NanoWalletService.getInfoFromSeedOrMnemonic(
|
||||||
// DerivationType.nano,
|
// DerivationType.nano,
|
||||||
|
@ -39,24 +39,12 @@ abstract class WalletRestoreChooseDerivationViewModelBase with Store {
|
||||||
// node: node,
|
// node: node,
|
||||||
// );
|
// );
|
||||||
|
|
||||||
list.add(DerivationInfo(
|
// list.add(DerivationInfo(
|
||||||
balance: "0.00000",
|
// balance: "0.00000",
|
||||||
address: "address",
|
// address: "address",
|
||||||
height: 0,
|
// height: 0,
|
||||||
derivationType: DerivationType.bip39,
|
// 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,
|
|
||||||
// ));
|
// ));
|
||||||
// }
|
|
||||||
break;
|
break;
|
||||||
case WalletType.nano:
|
case WalletType.nano:
|
||||||
String? mnemonic = credentials['seed'] as String?;
|
String? mnemonic = credentials['seed'] as String?;
|
||||||
|
|
Loading…
Reference in a new issue