Skip to content

Commit 429db33

Browse files
committed
feat(dev): debug UR codes [skip ci]
1 parent a6494a0 commit 429db33

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

cw_bitcoin/lib/bitcoin_wallet.dart

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import 'package:cw_core/encryption_file_utils.dart';
2424
import 'package:cw_core/payjoin_session.dart';
2525
import 'package:cw_core/pending_transaction.dart';
2626
import 'package:cw_core/unspent_coins_info.dart';
27+
import 'package:cw_core/utils/print_verbose.dart';
2728
import 'package:cw_core/wallet_info.dart';
2829
import 'package:cw_core/wallet_keys_file.dart';
2930
import 'package:flutter/foundation.dart';
@@ -32,6 +33,9 @@ import 'package:ledger_bitcoin/ledger_bitcoin.dart';
3233
import 'package:ledger_bitcoin/psbt.dart';
3334
import 'package:ledger_flutter_plus/ledger_flutter_plus.dart';
3435
import 'package:mobx/mobx.dart';
36+
import 'package:ur/cbor_lite.dart';
37+
import 'package:ur/ur.dart';
38+
import 'package:ur/ur_decoder.dart';
3539

3640
part 'bitcoin_wallet.g.dart';
3741

@@ -367,7 +371,7 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
367371
masterFingerprint: Uint8List(0));
368372

369373
if (tx.shouldCommitUR()) {
370-
tx.unsignedPsbt = transaction.serialize();
374+
tx.unsignedPsbt = transaction.asPsbtV0();
371375
return tx;
372376
}
373377

@@ -430,6 +434,24 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
430434
return base64Encode(psbt.asPsbtV0());
431435
}
432436

437+
Future<void> commitPsbtUR(List<String> urCodes) async {
438+
final ur = URDecoder();
439+
for (final inp in urCodes) {
440+
ur.receivePart(inp);
441+
}
442+
final result = (ur.result as UR);
443+
final cbor = result.cbor;
444+
final cborDecoder = CBORDecoder(cbor);
445+
final out = cborDecoder.decodeBytes();
446+
final bytes = out.$1;
447+
final base64psbt = base64Encode(bytes);
448+
final psbt = PsbtV2()..deserializeV0(base64Decode(base64psbt));
449+
450+
// psbt.finalize();
451+
final finalized = base64Encode(psbt.serialize());
452+
await commitPsbt(finalized);
453+
}
454+
433455
@override
434456
Future<String> signMessage(String message, {String? address = null}) async {
435457
if (walletInfo.isHardwareWallet) {

lib/bitcoin/cw_bitcoin.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,12 @@ class CWBitcoin extends Bitcoin {
723723
}
724724
}
725725

726+
@override
727+
Future<void> commitPsbtUR(Object wallet, List<String> urCodes) {
728+
final _wallet = wallet as BitcoinWalletBase;
729+
return _wallet.commitPsbtUR(urCodes);
730+
}
731+
726732
@override
727733
String getPayjoinEndpoint(Object wallet) {
728734
final _wallet = wallet as ElectrumWallet;

lib/src/screens/ur/animated_ur_page.dart

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import 'dart:async';
22

3+
import 'package:cake_wallet/bitcoin/bitcoin.dart';
34
import 'package:cake_wallet/entities/qr_scanner.dart';
45
import 'package:cake_wallet/generated/i18n.dart';
56
import 'package:cake_wallet/monero/monero.dart';
67
import 'package:cake_wallet/src/screens/base_page.dart';
78
import 'package:cake_wallet/src/screens/receive/widgets/qr_image.dart';
89
import 'package:cake_wallet/src/widgets/alert_with_one_action.dart';
910
import 'package:cake_wallet/src/widgets/primary_button.dart';
11+
import 'package:cake_wallet/utils/clipboard_util.dart';
12+
import 'package:cake_wallet/utils/feature_flag.dart';
1013
import 'package:cake_wallet/utils/show_pop_up.dart';
1114
import 'package:cake_wallet/view_model/animated_ur_model.dart';
1215
import 'package:flutter/material.dart';
16+
import 'package:flutter/services.dart';
1317

1418
// ur:xmr-txunsigned - unsigned transaction
1519
// should show a scanner afterwards.
@@ -46,8 +50,8 @@ class AnimatedURPage extends BasePage {
4650
frames: urQr.trim().split("\n"),
4751
),
4852
),
53+
if (["ur:xmr-txunsigned", "ur:xmr-output", "ur:psbt"].contains(urQrType)) ...{
4954
SizedBox(height: 32),
50-
if (urQrType == "ur:xmr-txunsigned" || urQrType == "ur:xmr-output")
5155
Padding(
5256
padding: const EdgeInsets.symmetric(horizontal: 16.0),
5357
child: SizedBox(
@@ -60,8 +64,10 @@ class AnimatedURPage extends BasePage {
6064
),
6165
),
6266
),
63-
SizedBox(height: 32),
64-
if (urQrType == "ur:xmr-output" && !isAll) Padding(
67+
},
68+
if (urQrType == "ur:xmr-output" && !isAll) ...{
69+
SizedBox(height: 32),
70+
Padding(
6571
padding: const EdgeInsets.symmetric(horizontal: 16.0),
6672
child: SizedBox(
6773
width: double.maxFinite,
@@ -73,6 +79,7 @@ class AnimatedURPage extends BasePage {
7379
),
7480
),
7581
),
82+
},
7683
],
7784
);
7885
}
@@ -106,6 +113,10 @@ class AnimatedURPage extends BasePage {
106113
Navigator.of(context).pop(true);
107114
}
108115
break;
116+
case "ur:psbt": // psbt
117+
final ur = await presentQRScanner(context);
118+
if (ur == null) return;
119+
await bitcoin!.commitPsbtUR(animatedURmodel.wallet, ur.trim().split("\n"));
109120
default:
110121
throw UnimplementedError("unable to handle UR: ${urQrType}");
111122
}
@@ -168,10 +179,21 @@ class _URQRState extends State<URQR> {
168179
children: [
169180
Center(
170181
child: QrImage(
171-
data: widget.frames[frame % widget.frames.length], version: -1,
182+
data: widget.frames[frame % widget.frames.length],
183+
version: -1,
172184
size: 400,
173185
),
174186
),
187+
if (FeatureFlag.hasDevOptions) ...{
188+
TextButton(
189+
onPressed: () {
190+
Clipboard.setData(ClipboardData(text: """Current frame (${frame % widget.frames.length}): ${widget.frames[frame % widget.frames.length]},
191+
All frames:
192+
- ${widget.frames.join("\n - ")}"""));
193+
},
194+
child: Text(widget.frames[frame % widget.frames.length]),
195+
),
196+
}
175197
],
176198
);
177199
}

tool/configure.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ abstract class Bitcoin {
245245
bool getMwebEnabled(Object wallet);
246246
String? getUnusedMwebAddress(Object wallet);
247247
String? getUnusedSegwitAddress(Object wallet);
248+
Future<void> commitPsbtUR(Object wallet, List<String> urCodes);
248249
249250
void updatePayjoinState(Object wallet, bool state);
250251
String getPayjoinEndpoint(Object wallet);

0 commit comments

Comments
 (0)