This commit is contained in:
fosse 2023-08-24 11:20:04 -04:00
parent 9502b7ef0f
commit bbb196dba8
5 changed files with 210 additions and 56 deletions

View file

@ -0,0 +1,91 @@
import 'package:cw_core/wallet_info.dart';
Map<DerivationType, List<DerivationInfo>> bitcoin_derivations = {
DerivationType.bip39: [
DerivationInfo(
derivationType: DerivationType.bip39,
derivationPath: "m/0'/1",
description: "cake default?",
script_type: "???",
),
DerivationInfo(
derivationType: DerivationType.bip39,
derivationPath: "m/44'/0'/0'",
description: "Standard BIP44 legacy",
script_type: "p2pkh",
),
DerivationInfo(
derivationType: DerivationType.bip39,
derivationPath: "m/49'/0'/0'",
description: "Standard BIP49 compatibility segwit",
script_type: "p2wpkh-p2sh",
),
DerivationInfo(
derivationType: DerivationType.bip39,
derivationPath: "m/84'/0'/0'",
description: "Standard BIP84 native segwit",
script_type: "p2wpkh",
),
DerivationInfo(
derivationType: DerivationType.bip39,
derivationPath: "m/0'",
description: "Non-standard legacy",
script_type: "p2pkh",
),
DerivationInfo(
derivationType: DerivationType.bip39,
derivationPath: "m/0'",
description: "Non-standard compatibility segwit",
script_type: "p2wpkh-p2sh",
),
DerivationInfo(
derivationType: DerivationType.bip39,
derivationPath: "m/0'",
description: "Non-standard native segwit",
script_type: "p2wpkh",
),
DerivationInfo(
derivationType: DerivationType.bip39,
derivationPath: "m/44'/0'/0'",
description: "Copay native segwit",
script_type: "p2wpkh",
),
DerivationInfo(
derivationType: DerivationType.bip39,
derivationPath: "m/84'/0'/2147483644'",
description: "Samourai Bad Bank (toxic change)",
script_type: "p2wpkh",
),
DerivationInfo(
derivationType: DerivationType.bip39,
derivationPath: "m/84'/0'/2147483645'",
description: "Samourai Whirlpool Pre Mix",
script_type: "p2wpkh",
),
DerivationInfo(
derivationType: DerivationType.bip39,
derivationPath: "m/84'/0'/2147483646'",
description: "Samourai Whirlpool Post Mix",
script_type: "p2wpkh",
),
DerivationInfo(
derivationType: DerivationType.bip39,
derivationPath: "m/44'/0'/2147483647'",
description: "Samourai Ricochet legacy",
script_type: "p2pkh",
),
DerivationInfo(
derivationType: DerivationType.bip39,
derivationPath: "m/49'/0'/2147483647'",
description: "Samourai Ricochet compatibility segwit",
script_type: "p2wpkh-p2sh",
),
DerivationInfo(
derivationType: DerivationType.bip39,
derivationPath: "m/84'/0'/2147483647'",
description: "Samourai Ricochet native segwit",
script_type: "p2wpkh",
),
],
};

View file

@ -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!),// "m/0'/1" .derivePath(walletInfo.derivationPath!),// default: "m/0'/1"
networkType: networkType); networkType: networkType);
} }

View file

@ -17,6 +17,7 @@ import 'package:hive/hive.dart';
import 'package:collection/collection.dart'; import 'package:collection/collection.dart';
import 'package:mobx/mobx.dart'; import 'package:mobx/mobx.dart';
import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin; import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
import 'package:cw_bitcoin/bitcoin_derivations.dart';
class BitcoinWalletService extends WalletService<BitcoinNewWalletCredentials, class BitcoinWalletService extends WalletService<BitcoinNewWalletCredentials,
BitcoinRestoreWalletFromSeedCredentials, BitcoinRestoreWalletFromWIFCredentials> { BitcoinRestoreWalletFromSeedCredentials, BitcoinRestoreWalletFromWIFCredentials> {
@ -111,17 +112,49 @@ 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, DerivationType.StandardBIP44Legacy]; return [DerivationType.bip39];
} }
static Future<List<DerivationInfo>> getDerivationsFromMnemonic( static Future<List<DerivationInfo>> getDerivationsFromMnemonic(
{required String mnemonic, required Node node}) async { {required String mnemonic, required Node node}) async {
// throw UnimplementedError();
var list = []; var list = [];
final electrumClient = ElectrumClient(); final electrumClient = ElectrumClient();
await electrumClient.connectToUri(node.uri); await electrumClient.connectToUri(node.uri);
print("@@@@@@@@@@@@@@");
for (DerivationType dType in bitcoin_derivations.keys) {
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");
// get addresses:
final sh = scriptHash(wallet.address!, networkType: bitcoin.bitcoin);
final balance = await electrumClient.getBalance(sh);
final history = await electrumClient.getHistory(sh);
print("history:");
print(history);
print(history.length);
dInfo.balance = balance.entries.first.value.toString();
dInfo.address = wallet.address ?? "";
dInfo.height = history.length;
list.add(dInfo);
} catch (e) {
print(e);
}
}
}
}
// default derivation path: // default derivation path:
var wallet = var wallet =
bitcoin.HDWallet.fromSeed(await mnemonicToSeedBytes(mnemonic), network: bitcoin.bitcoin) bitcoin.HDWallet.fromSeed(await mnemonicToSeedBytes(mnemonic), network: bitcoin.bitcoin)
@ -132,6 +165,8 @@ class BitcoinWalletService extends WalletService<BitcoinNewWalletCredentials,
final balance = await electrumClient.getBalance(sh); final balance = await electrumClient.getBalance(sh);
wallet.derive(0);
print(wallet.address); print(wallet.address);
print(balance.entries); print(balance.entries);
print("@@@@@@@@@@@@@"); print("@@@@@@@@@@@@@");
@ -158,11 +193,10 @@ class BitcoinWalletService extends WalletService<BitcoinNewWalletCredentials,
// unspentCoinsInfo: unspentCoinsInfoSource); // unspentCoinsInfo: unspentCoinsInfoSource);
list.add(DerivationInfo( list.add(DerivationInfo(
"0.00000", derivationType: DerivationType.bip39,
"address", balance: "0.00000",
0, address: "address",
DerivationType.bip39, height: 0,
null,
)); ));
return []; return [];

View file

@ -15,29 +15,49 @@ enum DerivationType {
nano, nano,
@HiveField(3) @HiveField(3)
bip39, bip39,
@HiveField(4)
StandardBIP44Legacy
} }
class DerivationInfo { class DerivationInfo {
DerivationInfo(this.balance, this.address, this.height, this.derivationType, this.derivationPath); DerivationInfo({
required this.derivationType,
this.derivationPath,
this.balance = "",
this.address = "",
this.height = 0,
this.script_type,
this.description,
});
final String balance; String balance;
final String address; String address;
final int height; int height;
final DerivationType derivationType; final DerivationType derivationType;
final String? derivationPath; final String? derivationPath;
final String? script_type;
final String? description;
} }
@HiveType(typeId: WalletInfo.typeId) @HiveType(typeId: WalletInfo.typeId)
class WalletInfo extends HiveObject { class WalletInfo extends HiveObject {
WalletInfo(this.id, this.name, this.type, this.isRecovery, this.restoreHeight, WalletInfo(
this.timestamp, this.dirPath, this.path, this.address, this.yatEid, this.id,
this.yatLastUsedAddressRaw, this.showIntroCakePayCard, this.derivationType, this.derivationPath) this.name,
this.type,
this.isRecovery,
this.restoreHeight,
this.timestamp,
this.dirPath,
this.path,
this.address,
this.yatEid,
this.yatLastUsedAddressRaw,
this.showIntroCakePayCard,
this.derivationType,
this.derivationPath)
: _yatLastUsedAddressController = StreamController<String>.broadcast(); : _yatLastUsedAddressController = StreamController<String>.broadcast();
factory WalletInfo.external( factory WalletInfo.external({
{required String id, required String id,
required String name, required String name,
required WalletType type, required WalletType type,
required bool isRecovery, required bool isRecovery,
@ -50,10 +70,23 @@ class WalletInfo extends HiveObject {
String yatEid = '', String yatEid = '',
String yatLastUsedAddressRaw = '', String yatLastUsedAddressRaw = '',
DerivationType? derivationType, DerivationType? derivationType,
String? derivationPath,}) { String? derivationPath,
return WalletInfo(id, name, type, isRecovery, restoreHeight, }) {
date.millisecondsSinceEpoch, dirPath, path, address, return WalletInfo(
yatEid, yatLastUsedAddressRaw, showIntroCakePayCard, derivationType, derivationPath); id,
name,
type,
isRecovery,
restoreHeight,
date.millisecondsSinceEpoch,
dirPath,
path,
address,
yatEid,
yatLastUsedAddressRaw,
showIntroCakePayCard,
derivationType,
derivationPath);
} }
static const typeId = WALLET_INFO_TYPE_ID; static const typeId = WALLET_INFO_TYPE_ID;

View file

@ -13,8 +13,6 @@ part 'wallet_restore_choose_derivation_view_model.g.dart';
class WalletRestoreChooseDerivationViewModel = WalletRestoreChooseDerivationViewModelBase class WalletRestoreChooseDerivationViewModel = WalletRestoreChooseDerivationViewModelBase
with _$WalletRestoreChooseDerivationViewModel; with _$WalletRestoreChooseDerivationViewModel;
abstract class WalletRestoreChooseDerivationViewModelBase with Store { abstract class WalletRestoreChooseDerivationViewModelBase with Store {
WalletRestoreChooseDerivationViewModelBase({required this.credentials}) WalletRestoreChooseDerivationViewModelBase({required this.credentials})
: mode = WalletRestoreMode.seed {} : mode = WalletRestoreMode.seed {}
@ -34,7 +32,6 @@ abstract class WalletRestoreChooseDerivationViewModelBase with Store {
String? mnemonic = credentials['seed'] as String?; String? mnemonic = credentials['seed'] as String?;
await BitcoinWalletService.getDerivationsFromMnemonic(mnemonic: mnemonic!, node: node); await BitcoinWalletService.getDerivationsFromMnemonic(mnemonic: mnemonic!, node: node);
// var standardInfo = await NanoWalletService.getInfoFromSeedOrMnemonic( // var standardInfo = await NanoWalletService.getInfoFromSeedOrMnemonic(
// DerivationType.nano, // DerivationType.nano,
// mnemonic: mnemonic, // mnemonic: mnemonic,
@ -43,11 +40,10 @@ abstract class WalletRestoreChooseDerivationViewModelBase with Store {
// ); // );
list.add(DerivationInfo( list.add(DerivationInfo(
"0.00000", balance: "0.00000",
"address", address: "address",
0, height: 0,
DerivationType.bip39, derivationType: DerivationType.bip39,
null,
)); ));
// if (bip39Info["balance"] != null) { // if (bip39Info["balance"] != null) {
@ -76,27 +72,27 @@ abstract class WalletRestoreChooseDerivationViewModelBase with Store {
if (standardInfo["balance"] != null) { if (standardInfo["balance"] != null) {
list.add(DerivationInfo( list.add(DerivationInfo(
NanoUtil.getRawAsUsableString(standardInfo["balance"] as String, NanoUtil.rawPerNano), derivationType: DerivationType.nano,
standardInfo["address"] as String, balance: NanoUtil.getRawAsUsableString(
int.tryParse( standardInfo["balance"] as String, NanoUtil.rawPerNano),
address: standardInfo["address"] as String,
height: int.tryParse(
standardInfo["confirmation_height"] as String, standardInfo["confirmation_height"] as String,
) ?? ) ??
0, 0,
DerivationType.nano,
null,
)); ));
} }
if (bip39Info["balance"] != null) { if (bip39Info["balance"] != null) {
list.add(DerivationInfo( list.add(DerivationInfo(
derivationType: DerivationType.bip39,
balance:
NanoUtil.getRawAsUsableString(bip39Info["balance"] as String, NanoUtil.rawPerNano), NanoUtil.getRawAsUsableString(bip39Info["balance"] as String, NanoUtil.rawPerNano),
bip39Info["address"] as String, address: bip39Info["address"] as String,
int.tryParse( height: int.tryParse(
bip39Info["confirmation_height"] as String? ?? "", bip39Info["confirmation_height"] as String? ?? "",
) ?? ) ??
0, 0,
DerivationType.bip39,
null,
)); ));
} }