fix: api & create

This commit is contained in:
Rafael Saes 2024-11-23 11:35:50 -03:00
parent c35dec0d09
commit b7ff9ab32b
5 changed files with 64 additions and 25 deletions

View file

@ -150,9 +150,17 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
hdWallets[CWBitcoinDerivationType.electrum]!;
}
} else {
seedBytes = walletInfo.derivationInfo?.derivationType == DerivationType.electrum
? ElectrumV2SeedGenerator.generateFromString(mnemonic, passphrase)
: Bip39SeedGenerator.generateFromString(mnemonic, passphrase);
switch (walletInfo.derivationInfo?.derivationType) {
case DerivationType.bip39:
seedBytes = await Bip39SeedGenerator.generateFromString(mnemonic, passphrase);
hdWallets[CWBitcoinDerivationType.bip39] = Bip32Slip10Secp256k1.fromSeed(seedBytes);
break;
case DerivationType.electrum:
default:
seedBytes = await ElectrumV2SeedGenerator.generateFromString(mnemonic, passphrase);
hdWallets[CWBitcoinDerivationType.electrum] = Bip32Slip10Secp256k1.fromSeed(seedBytes);
break;
}
}
return BitcoinWallet(

View file

@ -1308,6 +1308,11 @@ abstract class ElectrumWalletBase
Future<void> onHistoriesResponse(List<AddressHistoriesResponse> histories) async {
if (histories.isEmpty || _updatingHistories) {
_updatingHistories = false;
_syncedTimes++;
if (_syncedTimes == 3) {
syncStatus = SyncedSyncStatus();
}
return;
}

View file

@ -467,7 +467,29 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store {
(element) => element.scriptType == addressType.toString(),
);
for (final derivationInfo in derivationInfos ?? <DerivationInfo>[]) {
if (derivationInfos == null || derivationInfos.isEmpty) {
final bitcoinDerivationInfo = BitcoinDerivationInfo(
derivationType: isElectrum ? BitcoinDerivationType.electrum : BitcoinDerivationType.bip39,
derivationPath: walletInfo.derivationInfo!.derivationPath!,
scriptType: addressType,
);
await discoverNewAddresses(
derivationType: derivationType,
isChange: false,
addressType: addressType,
derivationInfo: bitcoinDerivationInfo,
);
await discoverNewAddresses(
derivationType: derivationType,
isChange: true,
addressType: addressType,
derivationInfo: bitcoinDerivationInfo,
);
continue;
}
for (final derivationInfo in derivationInfos) {
final bitcoinDerivationInfo = BitcoinDerivationInfo(
derivationType: isElectrum ? BitcoinDerivationType.electrum : BitcoinDerivationType.bip39,
derivationPath: derivationInfo.derivationPath!,

View file

@ -47,6 +47,8 @@ class ElectrumWorker {
}
void handleMessage(dynamic message) async {
print("Worker received message: $message");
try {
Map<String, dynamic> messageJson;
if (message is String) {
@ -107,7 +109,6 @@ class ElectrumWorker {
ElectrumWorkerStopScanningRequest.fromJson(messageJson),
);
break;
case ElectrumRequestMethods.estimateFeeMethod:
case ElectrumRequestMethods.tweaksSubscribeMethod:
if (_isScanning) {
_stopScanRequested = false;
@ -279,12 +280,8 @@ class ElectrumWorker {
walletType: result.walletType,
);
}
return Future.value(null);
}));
}
return histories;
}));
_sendResponse(ElectrumWorkerGetHistoryResponse(
@ -411,29 +408,36 @@ class ElectrumWorker {
if (getTime) {
if (mempoolAPIEnabled) {
try {
final txVerbose = await http.get(
Uri.parse(
"http://mempool.cakewallet.com:8999/api/v1/tx/$hash/status",
),
);
// TODO: mempool api class
final txVerbose = await http
.get(
Uri.parse(
"https://mempool.cakewallet.com/api/v1/tx/$hash/status",
),
)
.timeout(const Duration(seconds: 5));
if (txVerbose.statusCode == 200 &&
txVerbose.body.isNotEmpty &&
jsonDecode(txVerbose.body) != null) {
height = jsonDecode(txVerbose.body)['block_height'] as int;
final blockHash = await http.get(
Uri.parse(
"http://mempool.cakewallet.com:8999/api/v1/block-height/$height",
),
);
final blockHash = await http
.get(
Uri.parse(
"https://mempool.cakewallet.com/api/v1/block-height/$height",
),
)
.timeout(const Duration(seconds: 5));
if (blockHash.statusCode == 200 && blockHash.body.isNotEmpty) {
final blockResponse = await http.get(
Uri.parse(
"http://mempool.cakewallet.com:8999/api/v1/block/${blockHash.body}",
),
);
final blockResponse = await http
.get(
Uri.parse(
"https://mempool.cakewallet.com/api/v1/block/${blockHash.body}",
),
)
.timeout(const Duration(seconds: 5));
if (blockResponse.statusCode == 200 &&
blockResponse.body.isNotEmpty &&

View file

@ -273,7 +273,7 @@ const bitcoinDates = {
Future<int> getBitcoinHeightByDateAPI({required DateTime date}) async {
final response = await http.get(
Uri.parse(
"http://mempool.cakewallet.com:8999/api/v1/mining/blocks/timestamp/${(date.millisecondsSinceEpoch / 1000).round()}",
"https://mempool.cakewallet.com/api/v1/mining/blocks/timestamp/${(date.millisecondsSinceEpoch / 1000).round()}",
),
);