dev
dart 39 lines 1.25 KB
Raw
1 import 'dart:convert';
2
3 import 'package:bitcoin_base/bitcoin_base.dart';
4 import 'package:blockchain_utils/blockchain_utils.dart';
5 import 'package:cw_bitcoin/bitcoin_wallet.dart';
6 import 'package:cw_bitcoin/psbt/v0_deserialize.dart';
7 import 'package:cw_core/utils/print_verbose.dart';
8 import 'package:ledger_bitcoin/psbt.dart';
9
10 String getTxIdFromPsbtV0(String psbt) {
11 final psbtV2 = PsbtV2()..deserializeV0(base64.decode(psbt));
12
13 return BtcTransaction.fromRaw(BytesUtils.toHexString(psbtV2.extractUnsignedTX(false))).txId();
14 }
15
16 String getOutputAmountFromPsbt(String psbtV0, BitcoinWalletBase wallet) {
17 printV(psbtV0);
18 final psbt = PsbtV2()..deserializeV0(base64.decode(psbtV0));
19 int amount = 0;
20 for (var i = 0; i < psbt.getGlobalOutputCount(); i++) {
21 final script = psbt.getOutputScript(i);
22 if (wallet.isMine(Script.fromRaw(byteData: script))) {
23 amount += psbt.getOutputAmount(i);
24 }
25 }
26 return amount.toString();
27 }
28
29 String getOutputAmountFromTx(String originalTx, BitcoinWalletBase wallet) {
30 final tx = BtcTransaction.fromRaw(originalTx);
31 BigInt amount = BigInt.zero;
32 for (final output in tx.outputs) {
33 if (wallet.isMine(output.scriptPubKey)) {
34 amount += output.amount;
35 }
36 }
37 printV(amount);
38 return amount.toString();
39 }