From 4ad880d5a521c548f29df654a8c96d5681f358ce Mon Sep 17 00:00:00 2001 From: pythcoiner Date: Mon, 16 Nov 2026 21:18:13 -0500 Subject: [PATCH] fix(bip322): detect a BIP322 request by its to_sign transaction, not field 0x09 alone --- main/core/bip322.c | 26 ++++++++++----------- main/core/bip322.h | 4 +++- main/core/test/test_bip322.c | 45 ++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 14 deletions(-) diff --git a/main/core/bip322.c b/main/core/bip322.c index fea5709a..48e8e0b2 100644 --- a/main/core/bip322.c +++ b/main/core/bip322.c @@ -29,10 +29,21 @@ static bool get_message_field(const struct wally_psbt *psbt, return true; } +/* The BIP322 to_sign shape: version 0, locktime 0, one input spending + * to_spend:0 with sequence 0, one 0-value OP_RETURN output. */ +static bool is_to_sign_tx(const struct wally_tx *tx) { + return tx && tx->version == 0 && tx->locktime == 0 && tx->num_inputs == 1 && + tx->num_outputs == 1 && tx->inputs[0].index == 0 && + tx->inputs[0].sequence == 0 && tx->outputs[0].satoshi == 0 && + tx->outputs[0].script_len == 1 && + tx->outputs[0].script[0] == OP_RETURN; +} + bool bip322_detect(const struct wally_psbt *psbt) { const unsigned char *msg; size_t msg_len; - return psbt && get_message_field(psbt, &msg, &msg_len); + return psbt && get_message_field(psbt, &msg, &msg_len) && + is_to_sign_tx(psbt->tx); } /* BIP322 tagged hash: SHA256(SHA256(tag) || SHA256(tag) || message). libwally @@ -92,22 +103,11 @@ bool bip322_parse(const struct wally_psbt *psbt, bool is_testnet, return false; } - /* to_sign structure per BIP322 */ const struct wally_tx *tx = psbt->tx; - if (!tx || tx->version != 0 || tx->locktime != 0 || tx->num_inputs != 1 || - tx->num_outputs != 1) { + if (!is_to_sign_tx(tx)) { ESP_LOGW(TAG, "to_sign transaction structure mismatch"); return false; } - if (tx->inputs[0].index != 0 || tx->inputs[0].sequence != 0) { - ESP_LOGW(TAG, "to_sign input must spend to_spend:0 with sequence 0"); - return false; - } - if (tx->outputs[0].satoshi != 0 || tx->outputs[0].script_len != 1 || - tx->outputs[0].script[0] != OP_RETURN) { - ESP_LOGW(TAG, "to_sign output must be 0-value OP_RETURN"); - return false; - } const struct wally_tx_output *utxo = psbt->inputs[0].witness_utxo; if (!utxo || utxo->satoshi != 0 || !utxo->script || utxo->script_len == 0 || diff --git a/main/core/bip322.h b/main/core/bip322.h index 66fc698b..a0c92a4f 100644 --- a/main/core/bip322.h +++ b/main/core/bip322.h @@ -12,7 +12,9 @@ typedef struct { } bip322_request_t; // True if the PSBT carries the PSBT_GLOBAL_GENERIC_SIGNED_MESSAGE (0x09) -// global field, marking it as a BIP322 message-signing request. +// global field over a BIP322 to_sign transaction, marking it as a BIP322 +// message-signing request. A nonce cache session uses the same key on a +// regular transaction. KERN_WARN_UNUSED_RESULT bool bip322_detect(const struct wally_psbt *psbt); // Validates a BIP322 to_sign PSBT: structure (version-0 tx, single input at diff --git a/main/core/test/test_bip322.c b/main/core/test/test_bip322.c index 8eda1fb2..c8913127 100644 --- a/main/core/test/test_bip322.c +++ b/main/core/test/test_bip322.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include "core/bip322.h" @@ -182,6 +183,49 @@ static void test_message_characters(void) { } } +/* A nonce cache session carries global key 0x09 too, on a regular + * transaction: only the to_sign shape makes a request BIP322. Each case breaks + * one field of it. */ +static void test_detect_requires_to_sign(const uint8_t *raw, size_t raw_len) { + static const struct { + const char *name; + uint32_t version, locktime, index, sequence; + uint64_t satoshi; + unsigned char script; + } cases[] = { + {"version 2", 2, 0, 0, 0, 0, OP_RETURN}, + {"locktime 1", 0, 1, 0, 0, 0, OP_RETURN}, + {"input index 1", 0, 0, 1, 0, 0, OP_RETURN}, + {"sequence 0xffffffff", 0, 0, 0, 0xffffffff, 0, OP_RETURN}, + {"output value 1", 0, 0, 0, 0, 1, OP_RETURN}, + {"output OP_TRUE", 0, 0, 0, 0, 0, OP_TRUE}, + }; + for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) { + printf("bip322_detect ignores field 0x09 with %s: ", cases[i].name); + struct wally_psbt *psbt = NULL; + if (wally_psbt_from_bytes(raw, raw_len, 0, &psbt) != WALLY_OK) { + FAIL("PSBT failed to parse"); + continue; + } + struct wally_tx *tx = psbt->tx; + tx->version = cases[i].version; + tx->locktime = cases[i].locktime; + tx->inputs[0].index = cases[i].index; + tx->inputs[0].sequence = cases[i].sequence; + tx->outputs[0].satoshi = cases[i].satoshi; + tx->outputs[0].script[0] = cases[i].script; + bip322_request_t req = {0}; + if (bip322_detect(psbt)) + FAIL("detected on a regular transaction"); + else if (bip322_parse(psbt, true, &req)) + FAIL("parsed a regular transaction"); + else + PASS(); + bip322_request_free(&req); + wally_psbt_free(psbt); + } +} + int main(void) { /* The host libwally hashes through PSA, as the device does, which needs * wally_init. */ @@ -275,6 +319,7 @@ int main(void) { PASS(); /* rejected at parse time (current upstream behavior) */ } + test_detect_requires_to_sign(raw, raw_len); test_message_characters(); printf("\nResults: %d passed, %d failed\n", tests_passed, tests_failed);