added nostr_decode_npub

This commit is contained in:
Laan Tungir 2025-08-09 11:40:34 -04:00
parent e137560d64
commit ae4aa7cf80
4 changed files with 37 additions and 4 deletions

View File

@ -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)

Binary file not shown.

View File

@ -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;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

View File

@ -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);
////////////////////////////////////////////////////////////////////////////////