Skip to content

Commit 4fc854e

Browse files
committed
psbt: allow pset signing from usb/qrcodes
1 parent b9cc4c2 commit 4fc854e

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

main/process/sign_psbt.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,18 @@ static bool psbt_update_outputs(const network_t network_id, struct wally_psbt* p
650650
return true;
651651
}
652652

653+
// Deduce the network to try to sign with given a psbt/pset.
654+
network_t network_from_psbt_type(struct wally_psbt* psbt)
655+
{
656+
JADE_ASSERT(psbt);
657+
size_t is_elements = 0;
658+
JADE_WALLY_VERIFY(wally_psbt_is_elements(psbt, &is_elements));
659+
if (keychain_get_network_type_restriction() == NETWORK_TYPE_TEST) {
660+
return is_elements ? NETWORK_LIQUID_TESTNET : NETWORK_BITCOIN_TESTNET;
661+
}
662+
return is_elements ? NETWORK_LIQUID : NETWORK_BITCOIN;
663+
}
664+
653665
// Sign a psbt/pset - the passed wally psbt struct is updated with any signatures.
654666
// Returns 0 if no errors occurred - does not necessarily indicate that signatures were added.
655667
// Returns an rpc/message error code on error, and the error string should be populated.

main/qrmode.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ bool select_registered_wallet(const char multisig_names[][NVS_KEY_NAME_MAX_SIZE]
8989

9090
// PSBT struct and functions
9191
struct wally_psbt;
92+
network_t network_from_psbt_type(struct wally_psbt* psbt);
9293
int sign_psbt(
9394
jade_process_t* process, CborValue* params, network_t network_id, struct wally_psbt* psbt, const char** errmsg);
9495
int wally_psbt_free(struct wally_psbt* psbt);
@@ -1092,12 +1093,8 @@ static bool parse_sign_display_bcur_psbt_qr(const uint8_t* cbor, const size_t cb
10921093
// Try to sign extracted PSBT
10931094
bool ret = false;
10941095
const char* errmsg = NULL;
1095-
network_t network_id;
1096-
if (keychain_get_network_type_restriction() == NETWORK_TYPE_TEST) {
1097-
network_id = NETWORK_BITCOIN_TESTNET;
1098-
} else {
1099-
network_id = NETWORK_BITCOIN;
1100-
}
1096+
const network_t network_id = network_from_psbt_type(psbt);
1097+
11011098
// Note we pass NULL process/params as we don't have any additional info
11021099
const int errcode = sign_psbt(NULL, NULL, network_id, psbt, &errmsg);
11031100
if (errcode) {

main/usbhmsc/usbmode.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ void await_qr_help_activity(const char* url);
2929
// PSBT serialisation functions
3030
bool deserialise_psbt(const uint8_t* bytes, size_t bytes_len, struct wally_psbt** psbt_out);
3131
bool serialise_psbt(const struct wally_psbt* psbt, uint8_t** output, size_t* output_len);
32+
network_t network_from_psbt_type(struct wally_psbt* psbt);
3233
int sign_psbt(
3334
jade_process_t* process, CborValue* params, network_t network_id, struct wally_psbt* psbt, const char** errmsg);
3435

@@ -828,12 +829,8 @@ static bool sign_usb_psbt(const usbstorage_action_context_t* ctx)
828829

829830
// Sign PSBT
830831
const char* errmsg = NULL;
831-
network_t network_id;
832-
if (keychain_get_network_type_restriction() == NETWORK_TYPE_TEST) {
833-
network_id = NETWORK_BITCOIN_TESTNET;
834-
} else {
835-
network_id = NETWORK_BITCOIN;
836-
}
832+
const network_t network_id = network_from_psbt_type(psbt);
833+
837834
// Note we pass NULL process/params as we don't have any additional info
838835
const int errcode = sign_psbt(NULL, NULL, network_id, psbt, &errmsg);
839836
if (errcode) {

0 commit comments

Comments
 (0)