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,
mainHd: hd,
sideHd: bitcoin.HDWallet.fromSeed(seedBytes, network: networkType)
.derivePath(walletInfo.derivationPath!),// "m/0'/1"
.derivePath(walletInfo.derivationPath!),// default: "m/0'/1"
networkType: networkType);
}

View file

@ -17,6 +17,7 @@ import 'package:hive/hive.dart';
import 'package:collection/collection.dart';
import 'package:mobx/mobx.dart';
import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
import 'package:cw_bitcoin/bitcoin_derivations.dart';
class BitcoinWalletService extends WalletService<BitcoinNewWalletCredentials,
BitcoinRestoreWalletFromSeedCredentials, BitcoinRestoreWalletFromWIFCredentials> {
@ -111,17 +112,49 @@ class BitcoinWalletService extends WalletService<BitcoinNewWalletCredentials,
static Future<List<DerivationType>> compareDerivationMethods(
{required mnemonic, required Node node}) async {
return [DerivationType.bip39, DerivationType.StandardBIP44Legacy];
return [DerivationType.bip39];
}
static Future<List<DerivationInfo>> getDerivationsFromMnemonic(
{required String mnemonic, required Node node}) async {
// throw UnimplementedError();
var list = [];
final electrumClient = ElectrumClient();
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:
var wallet =
bitcoin.HDWallet.fromSeed(await mnemonicToSeedBytes(mnemonic), network: bitcoin.bitcoin)
@ -132,6 +165,8 @@ class BitcoinWalletService extends WalletService<BitcoinNewWalletCredentials,
final balance = await electrumClient.getBalance(sh);
wallet.derive(0);
print(wallet.address);
print(balance.entries);
print("@@@@@@@@@@@@@");
@ -158,11 +193,10 @@ class BitcoinWalletService extends WalletService<BitcoinNewWalletCredentials,
// unspentCoinsInfo: unspentCoinsInfoSource);
list.add(DerivationInfo(
"0.00000",
"address",
0,
DerivationType.bip39,
null,
derivationType: DerivationType.bip39,
balance: "0.00000",
address: "address",
height: 0,
));
return [];

View file

@ -10,34 +10,54 @@ enum DerivationType {
@HiveField(0)
unknown,
@HiveField(1)
def,// default is a reserved word
def, // default is a reserved word
@HiveField(2)
nano,
@HiveField(3)
bip39,
@HiveField(4)
StandardBIP44Legacy
}
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;
final String address;
final int height;
String balance;
String address;
int height;
final DerivationType derivationType;
final String? derivationPath;
final String? script_type;
final String? description;
}
@HiveType(typeId: WalletInfo.typeId)
class WalletInfo extends HiveObject {
WalletInfo(this.id, 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)
WalletInfo(
this.id,
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();
factory WalletInfo.external(
{required String id,
factory WalletInfo.external({
required String id,
required String name,
required WalletType type,
required bool isRecovery,
@ -50,10 +70,23 @@ class WalletInfo extends HiveObject {
String yatEid = '',
String yatLastUsedAddressRaw = '',
DerivationType? derivationType,
String? derivationPath,}) {
return WalletInfo(id, name, type, isRecovery, restoreHeight,
date.millisecondsSinceEpoch, dirPath, path, address,
yatEid, yatLastUsedAddressRaw, showIntroCakePayCard, derivationType, derivationPath);
String? derivationPath,
}) {
return WalletInfo(
id,
name,
type,
isRecovery,
restoreHeight,
date.millisecondsSinceEpoch,
dirPath,
path,
address,
yatEid,
yatLastUsedAddressRaw,
showIntroCakePayCard,
derivationType,
derivationPath);
}
static const typeId = WALLET_INFO_TYPE_ID;
@ -114,7 +147,7 @@ class WalletInfo extends HiveObject {
String get yatEmojiId => yatEid ?? '';
bool get isShowIntroCakePayCard {
if(showIntroCakePayCard == null) {
if (showIntroCakePayCard == null) {
return type != WalletType.haven;
}
return showIntroCakePayCard!;

View file

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