added nostr_decode_npub

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

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