Skip to content

Commit

Permalink
fix: api & create
Browse files Browse the repository at this point in the history
  • Loading branch information
rafael-xmr committed Nov 23, 2024
1 parent c35dec0 commit b7ff9ab
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 25 deletions.
14 changes: 11 additions & 3 deletions cw_bitcoin/lib/bitcoin_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions cw_bitcoin/lib/electrum_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
24 changes: 23 additions & 1 deletion cw_bitcoin/lib/electrum_wallet_addresses.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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!,
Expand Down
44 changes: 24 additions & 20 deletions cw_bitcoin/lib/electrum_worker/electrum_worker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -107,7 +109,6 @@ class ElectrumWorker {
ElectrumWorkerStopScanningRequest.fromJson(messageJson),
);
break;
case ElectrumRequestMethods.estimateFeeMethod:
case ElectrumRequestMethods.tweaksSubscribeMethod:
if (_isScanning) {
_stopScanRequested = false;
Expand Down Expand Up @@ -279,12 +280,8 @@ class ElectrumWorker {
walletType: result.walletType,
);
}

return Future.value(null);
}));
}

return histories;
}));

_sendResponse(ElectrumWorkerGetHistoryResponse(
Expand Down Expand Up @@ -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 &&
Expand Down
2 changes: 1 addition & 1 deletion cw_core/lib/get_height_by_date.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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()}",
),
);

Expand Down

0 comments on commit b7ff9ab

Please sign in to comment.