diff --git a/Makefile b/Makefile index a4abe685..6b30b6c4 100644 --- a/Makefile +++ b/Makefile @@ -14,10 +14,10 @@ ifneq ($(ENABLE_LOGGING),) endif # Include paths -INCLUDES = -I. -Inostr_core -Icjson -Isecp256k1/include +INCLUDES = -I. -Inostr_core -Icjson -Isecp256k1/include -Inostr_websocket -Imbedtls/include -Imbedtls/tf-psa-crypto/include -Imbedtls/tf-psa-crypto/drivers/builtin/include # Library source files -LIB_SOURCES = nostr_core/core.c nostr_core/core_relays.c nostr_core/nostr_crypto.c nostr_core/nostr_secp256k1.c nostr_core/nostr_aes.c nostr_core/nostr_chacha20.c nostr_core/version.c cjson/cJSON.c +LIB_SOURCES = nostr_core/core.c nostr_core/core_relays.c nostr_core/nostr_crypto.c nostr_core/nostr_secp256k1.c nostr_core/nostr_aes.c nostr_core/nostr_chacha20.c nostr_websocket/nostr_websocket_mbedtls.c cjson/cJSON.c LIB_OBJECTS = $(LIB_SOURCES:.c=.o) ARM64_LIB_OBJECTS = $(LIB_SOURCES:.c=.arm64.o) diff --git a/libnostr_core.a b/libnostr_core.a index 27105e30..9a71c000 100644 Binary files a/libnostr_core.a and b/libnostr_core.a differ diff --git a/nostr_core/core.c b/nostr_core/core.c index 532736ed..4b90a4b2 100644 --- a/nostr_core/core.c +++ b/nostr_core/core.c @@ -421,7 +421,33 @@ int nostr_decode_nsec(const char* input, unsigned char* private_key) { return NOSTR_SUCCESS; } - +int nostr_decode_npub(const char* input, unsigned char* public_key) { + if (!input || !public_key) { + return NOSTR_ERROR_INVALID_INPUT; + } + + nostr_input_type_t type = nostr_detect_input_type(input); + + if (type == NOSTR_INPUT_NSEC_HEX) { // Actually public key hex + if (nostr_hex_to_bytes(input, public_key, 32) != NOSTR_SUCCESS) { + return NOSTR_ERROR_INVALID_INPUT; + } + } else if (strncmp(input, "npub1", 4) == 0) { // Bech32 npub + size_t decoded_len; + if (!bech32_decode(input, "npub", public_key, &decoded_len)) { + return NOSTR_ERROR_INVALID_INPUT; + } + if (decoded_len != 32) { + return NOSTR_ERROR_INVALID_INPUT; + } + } else { + return NOSTR_ERROR_INVALID_INPUT; + } + + // Validate the public key (could add nostr_ec_public_key_verify if it exists) + + return NOSTR_SUCCESS; +} //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// diff --git a/nostr_core/nostr_core.h b/nostr_core/nostr_core.h index f9c4c6aa..366f4856 100644 --- a/nostr_core/nostr_core.h +++ b/nostr_core/nostr_core.h @@ -318,7 +318,14 @@ nostr_input_type_t nostr_detect_input_type(const char* input); */ int nostr_decode_nsec(const char* input, unsigned char* private_key); - +/** + * Validate and decode an npub (hex or bech32) to private key + * + * @param input Input nsec string + * @param private_key Output buffer for private key (32 bytes) + * @return NOSTR_SUCCESS on success, error code on failure + */ +int nostr_decode_npub(const char* input, unsigned char* private_key); ////////////////////////////////////////////////////////////////////////////////