LtbLightning / bdk-flutter

Bitcoin Development Kit - Flutter Package
MIT License
60 stars 27 forks source link

How to get output tx (UTXO)? #62

Open awaik opened 1 year ago

awaik commented 1 year ago

Can you please clarify, if is it possible to get output tx for the transaction?

For example the code from here https://github.com/LtbLightning/bdk-flutter-quickstart/blob/master/lib/screens/home.dart

      final txBuilder = TxBuilder();
      final address = await Address.create(address: addressStr);
      final script = await address.scriptPubKey();
      final psbt = await txBuilder
          .addRecipient(script, amount)
          .feeRate(1.0)
          .finish(wallet);
      final sbt = await wallet.sign(psbt);

Can we get output tx (UTXO) before broadcasting?

BitcoinZavior commented 1 year ago

Hi @awaik if I understand correctly, you want to know the UTXO that the receiver wallet will have after the current transaction is generated, but you want to know it before the current transaction is generated?

Isn't it the current tx which will become the UTXO for the reciever?

awaik commented 1 year ago

Hi @BitcoinZavior I have to make a broadcast with 2 transactions.

For example the code from the library https://pub.dev/packages/bitcoin_flutter

      final alice = ECPair.fromWIF('L1Knwj9W3qK3qMKdTvmg3VfzUs3ij2LETTFhxza9LfD5dngnoLG1');
      final bob = ECPair.fromWIF('KwcN2pT3wnRAurhy7qMczzbkpY5nXMW2ubh696UBc1bcwctTx26z');

      final txb = new TransactionBuilder();
      txb.setVersion(1);
      txb.addInput('b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c',
          6); // Alice's previous transaction output, has 200000 satoshis
      txb.addInput('7d865e959b2466918c9863afca942d0fb89d7c9ac0c99bafc3749504ded97730',
          0); // Bob's previous transaction output, has 300000 satoshis
      txb.addOutput('1CUNEBjYrCn2y1SdiUMohaKUi4wpP326Lb', 180000);
      txb.addOutput('1JtK9CQw1syfWj1WtFMWomrYdV3W2tWBF9', 170000);
      // (in)(200000 + 300000) - (out)(180000 + 170000) = (fee)150000, this is the miner fee

      txb.sign(vin: 1, keyPair: bob); // Bob signs his input, which was the second input (1th)
      txb.sign(vin: 0, keyPair: alice); // Alice signs her input, which was the first input (0th)

      // prepare for broadcast to the Bitcoin network, see 'can broadcast a Transaction' below
      txb.build().toHex(),
Muniru0 commented 4 months ago

Hi, guys has anyone fixed this ?

Muniru0 commented 4 months ago

Can you please clarify, if is it possible to get output tx for the transaction?

For example the code from here https://github.com/LtbLightning/bdk-flutter-quickstart/blob/master/lib/screens/home.dart

      final txBuilder = TxBuilder();
      final address = await Address.create(address: addressStr);
      final script = await address.scriptPubKey();
      final psbt = await txBuilder
          .addRecipient(script, amount)
          .feeRate(1.0)
          .finish(wallet);
      final sbt = await wallet.sign(psbt);

Can we get output tx (UTXO) before broadcasting?

You are supposed to be able get the UTXO before broadcasting. Infact this is the standard way of doing stuff. Libraries like Bitcoinjs get's you methods like : txb.finalizeInput(0).extractTransaction().toHex() this get's you the UTXO in hex format.

BitcoinZavior commented 4 months ago

Hi @Muniru0 I am not sure what you mean by "get UTXO before broadcasting" Because when building a transaction a UTXO is added as input.

The bitcoinjs code you mentioned extracts a transaction in hex format. When using bdk or bdk-flutter you need to do the same, you extract the tx after signing using extractTx() Below is the code for reference: and api reference: https://pub.dev/documentation/bdk_flutter/latest/bdk_flutter/PartiallySignedTransaction-class.html


final txBuilderResult = await txBuilder
          .addRecipient(script, amount)
          .feeRate(1.0)
          .finish(wallet);
      final sbt = await wallet.sign(psbt: txBuilderResult.psbt);
      final tx = await sbt.extractTx();
      await blockchain.broadcast(tx);
Muniru0 commented 4 months ago

Hi @Muniru0 I am not sure what you mean by "get UTXO before broadcasting" Because when building a transaction a UTXO is added as input.

The bitcoinjs code you mentioned extracts a transaction in hex format. When using bdk or bdk-flutter you need to do the same, you extract the tx after signing using extractTx() Below is the code for reference: and api reference: https://pub.dev/documentation/bdk_flutter/latest/bdk_flutter/PartiallySignedTransaction-class.html


final txBuilderResult = await txBuilder
          .addRecipient(script, amount)
          .feeRate(1.0)
          .finish(wallet);
      final sbt = await wallet.sign(psbt: txBuilderResult.psbt);
      final tx = await sbt.extractTx();
      await blockchain.broadcast(tx);

Yh, @BitcoinZavior you are absolutely right. I made a mistake I was refering to the rawtransactionhex. @awaik the UTXO can only be generated if the transaction has being broadcasted. Because it is generally the script that needs to be validated to spend the transaction. But it is not even officially valid until the transaction has being confirmed.