Skip to content

Commit

Permalink
Added isSigned to broadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
i5hi committed Jun 13, 2024
1 parent 44f882f commit bdab41a
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 20 deletions.
13 changes: 12 additions & 1 deletion lib/settings/bloc/broadcasttx_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ class BroadcastTxCubit extends Cubit<BroadcastTxState> {
emit(state.copyWith(loadingFile: false, tx: tx));
}

Future<bool> checkWitnesses(List<bdk.TxIn> inputs) async {
for (final txIn in inputs) {
if (txIn.witness.isEmpty) {
return false;
}
}
return true;
}

void extractTxClicked() async {
try {
await clearErrors();
Expand All @@ -119,10 +128,11 @@ class BroadcastTxCubit extends Cubit<BroadcastTxState> {
bdkTx = await psbt.extractTx();
// maybe this psbt needs to be finalized?
}

final txid = await bdkTx.txid();
final outputs = await bdkTx.output();
final inputs = await bdkTx.input();
final isSigned = await checkWitnesses(inputs);

Transaction? transaction;
WalletBloc? relatedWallet;

Expand Down Expand Up @@ -229,6 +239,7 @@ class BroadcastTxCubit extends Cubit<BroadcastTxState> {
transaction: transaction,
step: BroadcastTxStep.broadcast,
amount: totalAmount,
isSigned: isSigned,
),
);
} on bdk.EncodeException {
Expand Down
1 change: 1 addition & 0 deletions lib/settings/bloc/broadcasttx_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class BroadcastTxState with _$BroadcastTxState {
@Default(false) bool downloadingFile,
@Default('') String errDownloadingFile,
@Default(false) bool downloaded,
@Default(false) bool isSigned,
}) = _BroadcastTxState;
const BroadcastTxState._();

Expand Down
37 changes: 30 additions & 7 deletions lib/settings/bloc/broadcasttx_state.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ mixin _$BroadcastTxState {
bool get downloadingFile => throw _privateConstructorUsedError;
String get errDownloadingFile => throw _privateConstructorUsedError;
bool get downloaded => throw _privateConstructorUsedError;
bool get isSigned => throw _privateConstructorUsedError;

@JsonKey(ignore: true)
$BroadcastTxStateCopyWith<BroadcastTxState> get copyWith =>
Expand Down Expand Up @@ -67,7 +68,8 @@ abstract class $BroadcastTxStateCopyWith<$Res> {
bdk.PartiallySignedTransaction? psbtBDK,
bool downloadingFile,
String errDownloadingFile,
bool downloaded});
bool downloaded,
bool isSigned});

$TransactionCopyWith<$Res>? get transaction;
}
Expand Down Expand Up @@ -104,6 +106,7 @@ class _$BroadcastTxStateCopyWithImpl<$Res, $Val extends BroadcastTxState>
Object? downloadingFile = null,
Object? errDownloadingFile = null,
Object? downloaded = null,
Object? isSigned = null,
}) {
return _then(_value.copyWith(
step: null == step
Expand Down Expand Up @@ -182,6 +185,10 @@ class _$BroadcastTxStateCopyWithImpl<$Res, $Val extends BroadcastTxState>
? _value.downloaded
: downloaded // ignore: cast_nullable_to_non_nullable
as bool,
isSigned: null == isSigned
? _value.isSigned
: isSigned // ignore: cast_nullable_to_non_nullable
as bool,
) as $Val);
}

Expand Down Expand Up @@ -225,7 +232,8 @@ abstract class _$$BroadcastTxStateImplCopyWith<$Res>
bdk.PartiallySignedTransaction? psbtBDK,
bool downloadingFile,
String errDownloadingFile,
bool downloaded});
bool downloaded,
bool isSigned});

@override
$TransactionCopyWith<$Res>? get transaction;
Expand Down Expand Up @@ -261,6 +269,7 @@ class __$$BroadcastTxStateImplCopyWithImpl<$Res>
Object? downloadingFile = null,
Object? errDownloadingFile = null,
Object? downloaded = null,
Object? isSigned = null,
}) {
return _then(_$BroadcastTxStateImpl(
step: null == step
Expand Down Expand Up @@ -334,6 +343,10 @@ class __$$BroadcastTxStateImplCopyWithImpl<$Res>
? _value.downloaded
: downloaded // ignore: cast_nullable_to_non_nullable
as bool,
isSigned: null == isSigned
? _value.isSigned
: isSigned // ignore: cast_nullable_to_non_nullable
as bool,
));
}
}
Expand All @@ -360,7 +373,8 @@ class _$BroadcastTxStateImpl extends _BroadcastTxState {
this.psbtBDK,
this.downloadingFile = false,
this.errDownloadingFile = '',
this.downloaded = false})
this.downloaded = false,
this.isSigned = false})
: super._();

@override
Expand Down Expand Up @@ -416,10 +430,13 @@ class _$BroadcastTxStateImpl extends _BroadcastTxState {
@override
@JsonKey()
final bool downloaded;
@override
@JsonKey()
final bool isSigned;

@override
String toString() {
return 'BroadcastTxState(step: $step, tx: $tx, transaction: $transaction, recognizedTx: $recognizedTx, verified: $verified, amount: $amount, loadingFile: $loadingFile, errLoadingFile: $errLoadingFile, sent: $sent, extractingTx: $extractingTx, errExtractingTx: $errExtractingTx, psbt: $psbt, errPSBT: $errPSBT, broadcastingTx: $broadcastingTx, errBroadcastingTx: $errBroadcastingTx, psbtBDK: $psbtBDK, downloadingFile: $downloadingFile, errDownloadingFile: $errDownloadingFile, downloaded: $downloaded)';
return 'BroadcastTxState(step: $step, tx: $tx, transaction: $transaction, recognizedTx: $recognizedTx, verified: $verified, amount: $amount, loadingFile: $loadingFile, errLoadingFile: $errLoadingFile, sent: $sent, extractingTx: $extractingTx, errExtractingTx: $errExtractingTx, psbt: $psbt, errPSBT: $errPSBT, broadcastingTx: $broadcastingTx, errBroadcastingTx: $errBroadcastingTx, psbtBDK: $psbtBDK, downloadingFile: $downloadingFile, errDownloadingFile: $errDownloadingFile, downloaded: $downloaded, isSigned: $isSigned)';
}

@override
Expand Down Expand Up @@ -456,7 +473,9 @@ class _$BroadcastTxStateImpl extends _BroadcastTxState {
(identical(other.errDownloadingFile, errDownloadingFile) ||
other.errDownloadingFile == errDownloadingFile) &&
(identical(other.downloaded, downloaded) ||
other.downloaded == downloaded));
other.downloaded == downloaded) &&
(identical(other.isSigned, isSigned) ||
other.isSigned == isSigned));
}

@override
Expand All @@ -480,7 +499,8 @@ class _$BroadcastTxStateImpl extends _BroadcastTxState {
psbtBDK,
downloadingFile,
errDownloadingFile,
downloaded
downloaded,
isSigned
]);

@JsonKey(ignore: true)
Expand Down Expand Up @@ -511,7 +531,8 @@ abstract class _BroadcastTxState extends BroadcastTxState {
final bdk.PartiallySignedTransaction? psbtBDK,
final bool downloadingFile,
final String errDownloadingFile,
final bool downloaded}) = _$BroadcastTxStateImpl;
final bool downloaded,
final bool isSigned}) = _$BroadcastTxStateImpl;
const _BroadcastTxState._() : super._();

@override
Expand Down Expand Up @@ -553,6 +574,8 @@ abstract class _BroadcastTxState extends BroadcastTxState {
@override
bool get downloaded;
@override
bool get isSigned;
@override
@JsonKey(ignore: true)
_$$BroadcastTxStateImplCopyWith<_$BroadcastTxStateImpl> get copyWith =>
throw _privateConstructorUsedError;
Expand Down
59 changes: 47 additions & 12 deletions lib/settings/broadcast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,11 @@ class BroadcastPopUp extends StatelessWidget {

class _Screen extends StatelessWidget {
const _Screen();

@override
Widget build(BuildContext context) {
final signed =
context.select((BroadcastTxCubit cubit) => cubit.state.isSigned);

return Padding(
padding: const EdgeInsets.symmetric(
vertical: 16,
Expand All @@ -142,15 +144,7 @@ class _Screen extends StatelessWidget {
return BlocListener<BroadcastTxCubit, BroadcastTxState>(
listenWhen: (previous, current) =>
previous.hasErr() != current.hasErr() && current.hasErr(),
listener: (context, state) async {
// Alert.showErrorAlert(
// context,
// err: state.getErrors(),
// onClose: () {
// context.read<BroadcastTxCubit>().clearErrors();
// },
// );
},
listener: (context, state) async {},
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expand Down Expand Up @@ -212,7 +206,7 @@ class _Screen extends StatelessWidget {
const _TxTextField(),
],
const Gap(80),
const BroadcastSendButton(),
if (!signed) const BroadcastSendButton(),
const Gap(48),
],
),
Expand Down Expand Up @@ -352,6 +346,9 @@ class TxInfo extends StatelessWidget {
final txamt = context.select(
(BroadcastTxCubit cubit) => cubit.state.amount ?? 0,
);
final signed =
context.select((BroadcastTxCubit cubit) => cubit.state.isSigned);

final txfee = context
.select((BroadcastTxCubit cubit) => cubit.state.transaction?.fee ?? 0);
// final txAddress = context.select((BroadcastTxCubit _) => _.state.transaction?.outAddrs ?? []);
Expand Down Expand Up @@ -416,7 +413,45 @@ class TxInfo extends StatelessWidget {
),
],
const Gap(8),

// const Gap(24),
if (signed == true) ...[
Row(
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
children: [
Container(
transformAlignment: Alignment.center,
child: const FaIcon(
FontAwesomeIcons.signature,
size: 12,
),
),
const Gap(8),
const BBText.bodySmall(
'Signed',
),
],
),
] else ...[
Row(
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
children: [
Container(
transformAlignment: Alignment.center,
child: const FaIcon(
FontAwesomeIcons.signature,
size: 12,
),
),
const Gap(8),
const BBText.bodySmall(
'Unsigned',
),
],
),
],
const Gap(8),
const BBText.title('Total output amount'),
const Gap(4),
Row(
Expand Down

0 comments on commit bdab41a

Please sign in to comment.