diff --git a/nostr_core/cashu_mint.c b/nostr_core/cashu_mint.c index 393f8419..e8bdf161 100644 --- a/nostr_core/cashu_mint.c +++ b/nostr_core/cashu_mint.c @@ -1632,12 +1632,26 @@ static int cashu_encode_token_b(const cashu_decoded_token_t* token, if (rc != NOSTR_SUCCESS) goto fail; const char* inherited_id = token->proofs[0].id; + size_t inherited_id_hex_len = inherited_id ? strlen(inherited_id) : 0; + if (!inherited_id || inherited_id_hex_len == 0 || (inherited_id_hex_len % 2) != 0 || + !cashu_is_hex_string_len(inherited_id, inherited_id_hex_len) || + inherited_id_hex_len > (NOSTR_CASHU_KEYSET_ID_HEX_SIZE - 1)) { + rc = NOSTR_ERROR_INVALID_INPUT; + goto fail; + } + + unsigned char inherited_id_bytes[(NOSTR_CASHU_KEYSET_ID_HEX_SIZE - 1) / 2] = {0}; + size_t inherited_id_byte_len = inherited_id_hex_len / 2; + if (nostr_hex_to_bytes(inherited_id, inherited_id_bytes, inherited_id_byte_len) != 0) { + rc = NOSTR_ERROR_INVALID_INPUT; + goto fail; + } rc = cashu_cbor_write_type_uint(&b, 5, 2); if (rc != NOSTR_SUCCESS) goto fail; rc = cashu_cbor_write_text(&b, "i"); if (rc != NOSTR_SUCCESS) goto fail; - rc = cashu_cbor_write_text(&b, inherited_id); + rc = cashu_cbor_write_bytes(&b, inherited_id_bytes, inherited_id_byte_len); if (rc != NOSTR_SUCCESS) goto fail; rc = cashu_cbor_write_text(&b, "p"); @@ -1655,9 +1669,24 @@ static int cashu_encode_token_b(const cashu_decoded_token_t* token, if (rc != NOSTR_SUCCESS) goto fail; if (has_local_id) { + size_t local_id_hex_len = p->id ? strlen(p->id) : 0; + if (!p->id || local_id_hex_len == 0 || (local_id_hex_len % 2) != 0 || + !cashu_is_hex_string_len(p->id, local_id_hex_len) || + local_id_hex_len > (NOSTR_CASHU_KEYSET_ID_HEX_SIZE - 1)) { + rc = NOSTR_ERROR_INVALID_INPUT; + goto fail; + } + + unsigned char local_id_bytes[(NOSTR_CASHU_KEYSET_ID_HEX_SIZE - 1) / 2] = {0}; + size_t local_id_byte_len = local_id_hex_len / 2; + if (nostr_hex_to_bytes(p->id, local_id_bytes, local_id_byte_len) != 0) { + rc = NOSTR_ERROR_INVALID_INPUT; + goto fail; + } + rc = cashu_cbor_write_text(&b, "i"); if (rc != NOSTR_SUCCESS) goto fail; - rc = cashu_cbor_write_text(&b, p->id); + rc = cashu_cbor_write_bytes(&b, local_id_bytes, local_id_byte_len); if (rc != NOSTR_SUCCESS) goto fail; } @@ -1671,17 +1700,15 @@ static int cashu_encode_token_b(const cashu_decoded_token_t* token, goto fail; } - unsigned char secret_bytes[32] = {0}; unsigned char C_bytes[33] = {0}; - if (nostr_hex_to_bytes(p->secret, secret_bytes, 32) != 0 || - nostr_hex_to_bytes(p->C, C_bytes, 33) != 0) { + if (nostr_hex_to_bytes(p->C, C_bytes, 33) != 0) { rc = NOSTR_ERROR_INVALID_INPUT; goto fail; } rc = cashu_cbor_write_text(&b, "s"); if (rc != NOSTR_SUCCESS) goto fail; - rc = cashu_cbor_write_bytes(&b, secret_bytes, sizeof(secret_bytes)); + rc = cashu_cbor_write_text(&b, p->secret); if (rc != NOSTR_SUCCESS) goto fail; rc = cashu_cbor_write_text(&b, "c");