Broadcast mweb

This commit is contained in:
Hector Chu 2024-04-26 12:32:38 +01:00
parent 796c95315f
commit f0157101fb
2 changed files with 32 additions and 9 deletions

View file

@ -8,6 +8,7 @@ import 'package:cw_bitcoin/bitcoin_mnemonic.dart';
import 'package:cw_bitcoin/bitcoin_transaction_priority.dart';
import 'package:cw_bitcoin/bitcoin_unspent.dart';
import 'package:cw_bitcoin/electrum_transaction_info.dart';
import 'package:cw_bitcoin/pending_bitcoin_transaction.dart';
import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/pending_transaction.dart';
import 'package:cw_core/sync_status.dart';
@ -308,13 +309,11 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
final fee = utxos.sumOfUtxosValue() - preOutputSum;
final txb = BitcoinTransactionBuilder(utxos: utxos,
outputs: outputs, fee: fee, network: network);
final scanSecret = mwebHd.derive(0x80000000).privKey!;
final spendSecret = mwebHd.derive(0x80000001).privKey!;
final stub = await CwMweb.stub();
final resp = await stub.create(CreateRequest(
rawTx: txb.buildTransaction((a, b, c, d) => '').toBytes(),
scanSecret: hex.decode(scanSecret),
spendSecret: hex.decode(spendSecret),
scanSecret: hex.decode(mwebHd.derive(0x80000000).privKey!),
spendSecret: hex.decode(mwebHd.derive(0x80000001).privKey!),
feeRatePerKb: Int64(feeRate * 1000),
dryRun: true));
final tx = BtcTransaction.fromRaw(hex.encode(resp.rawTx));
@ -334,7 +333,14 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
@override
Future<PendingTransaction> createTransaction(Object credentials) async {
final tx = await super.createTransaction(credentials);
final tx = await super.createTransaction(credentials) as PendingBitcoinTransaction;
final stub = await CwMweb.stub();
final resp = await stub.create(CreateRequest(
rawTx: hex.decode(tx.hex),
scanSecret: hex.decode(mwebHd.derive(0x80000000).privKey!),
spendSecret: hex.decode(mwebHd.derive(0x80000001).privKey!),
feeRatePerKb: Int64.parseInt(tx.feeRate) * 1000));
tx.hexOverride = hex.encode(resp.rawTx);
return tx;
}
}

View file

@ -1,11 +1,14 @@
import 'package:cw_bitcoin/exceptions.dart';
import 'package:bitcoin_base/bitcoin_base.dart';
import 'package:blockchain_utils/blockchain_utils.dart';
import 'package:cw_core/pending_transaction.dart';
import 'package:cw_bitcoin/electrum.dart';
import 'package:cw_bitcoin/bitcoin_amount_format.dart';
import 'package:cw_bitcoin/electrum_transaction_info.dart';
import 'package:cw_core/transaction_direction.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:cw_mweb/cw_mweb.dart';
import 'package:cw_mweb/mwebd.pb.dart';
class PendingBitcoinTransaction with PendingTransaction {
PendingBitcoinTransaction(
@ -31,12 +34,14 @@ class PendingBitcoinTransaction with PendingTransaction {
final bool hasChange;
final bool isSendAll;
final bool hasTaprootInputs;
String? idOverride;
String? hexOverride;
@override
String get id => _tx.txId();
String get id => idOverride ?? _tx.txId();
@override
String get hex => _tx.serialize();
String get hex => hexOverride ?? _tx.serialize();
@override
String get amountFormatted => bitcoinAmountToString(amount: amount);
@ -49,8 +54,7 @@ class PendingBitcoinTransaction with PendingTransaction {
final List<void Function(ElectrumTransactionInfo transaction)> _listeners;
@override
Future<void> commit() async {
Future<void> _commit() async {
int? callId;
final result = await electrumClient.broadcastTransaction(
@ -78,6 +82,19 @@ class PendingBitcoinTransaction with PendingTransaction {
throw BitcoinTransactionCommitFailed();
}
}
@override
Future<void> commit() async {
if (network is LitecoinNetwork) try {
final stub = await CwMweb.stub();
final resp = await stub.broadcast(BroadcastRequest(rawTx: BytesUtils.fromHexString(hex)));
idOverride = resp.txid;
} catch (e) {
throw BitcoinTransactionCommitFailed();
} else {
await _commit();
}
_listeners.forEach((listener) => listener(transactionInfo()));
}