Compare commits

...

24 Commits

Author SHA1 Message Date
f3068f82f3 Rollback of nip44 changes, and added ability to modify timestamps in giftwraps. 2025-10-27 13:16:06 -04:00
Your Name
a8dc2ed046 Add configurable timestamp randomization for NIP-59 gift wraps 2025-10-27 12:57:25 -04:00
9a3965243c Revert NIP-44 to spec compliance (65535 bytes), keep NIP-04 at 1MB 2025-10-27 12:51:50 -04:00
23c2a58c2b Upgrade nip4 and nip44 to be able to handle 1MB payloads. 2025-10-24 18:56:30 -04:00
45fb6d061d Adding async publish to relay pool 2025-10-09 09:19:11 -04:00
c0784fc890 added nip 17 2025-10-04 18:32:28 -04:00
499accf440 Implement NIP-21: nostr: URI scheme with full support for note, nprofile, nevent, and naddr URIs including TLV encoding/decoding and comprehensive test suite 2025-10-03 06:10:56 -04:00
6b95ad37c5 nip 17, and 59 2025-10-03 04:25:10 -04:00
54a6044083 Add enhanced subscription functionality with EOSE result modes 2025-10-02 15:00:50 -04:00
0f897ab1b3 Getting the relay pool up to speed 2025-10-02 11:51:41 -04:00
0d910ca181 Updated synchronous relay queries to handle nip42. 2025-09-30 12:16:23 -04:00
9a63550863 Merge branch 'master' of ssh://git.laantungir.net:222/laantungir/nostr_core_lib
Working on Ginxsom auth and NIP 42 on a different computer at the same
time. My bad.
2025-09-07 13:07:02 -04:00
eb7a9e6098 Add NIP-42 implementation and local updates
- Added NIP-42 authentication implementation (nip042.c, nip042.h)
- Added NIP-42 test suite (nip42_test.c, nip42_test)
- Updated common core files for NIP-42 support
- Updated build script
- Rebuilt test binaries
2025-09-07 13:04:53 -04:00
Your Name
8585e7649c Add unified request validation system with authentication rules
- Add request_validator.h/c with comprehensive authentication system
- Implement pluggable database backend interface (SQLite default)
- Add priority-based rule evaluation engine with caching
- Support pubkey whitelist/blacklist, hash blacklist, MIME restrictions
- Add new authentication error codes to nostr_common.h
- Include request_validator.h in nostr_core.h
- Update build.sh to compile request_validator.c
- Designed for shared use between ginxsom and c-relay projects
2025-09-07 09:44:38 -04:00
55e2a9c68e nip13 validation added 2025-09-05 13:42:03 -04:00
445ab7a8f4 readme.md 2025-09-03 15:16:08 -04:00
33129d82fd remove exposed .h crypto headers 2025-09-02 12:36:52 -04:00
c0d095e57b Streaming sha256 2025-08-19 11:24:48 -04:00
e40f3037d3 version 2025-08-19 07:02:42 -04:00
77d92dbcf9 Nostr note validation added to nip01 2025-08-19 06:59:04 -04:00
1da4f6751e clean up 2025-08-17 14:14:52 -04:00
3ebfdc06c0 Fixed bug in nip44.c, was an error in ecdh_shared_secret. Added comments 2025-08-17 11:29:07 -04:00
d8b342ca3f Fixed error in nip04 implementation. Now working 2025-08-17 10:42:38 -04:00
df23fd618a comparison test for debugging 2025-08-16 17:48:02 -04:00
118 changed files with 14624 additions and 8202 deletions

View File

@@ -1,6 +1,6 @@
This library is fully staticly linked. There should be no external dependencies.
When building, use build.sh, not make.
When building, use build.sh, not make. When building for tests use build.sh -t
When making TUI menus, try to use the first leter of the command and the key to press to execute that command. For example, if the command is "Open file" try to use a keypress of "o" upper or lower case to signal to open the file. Use this instead of number keyed menus when possible. In the command, the letter should be underlined that signifies the command.

4
.gitignore vendored
View File

@@ -7,8 +7,8 @@ nips/
node_modules/
nostr-tools/
tiny-AES-c/
blossom/
ndk/
Trash/debug_tests/
node_modules/

View File

@@ -1,223 +0,0 @@
# NOSTR Core Library - Automatic Versioning System
## Overview
The NOSTR Core Library now features an automatic version increment system that automatically increments the patch version (e.g., v0.2.0 → v0.2.1) with each build. This ensures consistent versioning and traceability across builds.
## How It Works
### Version Format
The library uses semantic versioning with the format: `vMAJOR.MINOR.PATCH`
- **MAJOR**: Incremented for breaking changes (manual)
- **MINOR**: Incremented for new features (manual)
- **PATCH**: Incremented automatically with each build
### Automatic Increment Process
1. **Version Detection**: The build system scans all git tags matching `v*.*.*` pattern
2. **Highest Version**: Uses `sort -V` to find the numerically highest version (not chronologically latest)
3. **Patch Increment**: Increments the patch number by 1
4. **Git Tag Creation**: Creates a new git tag for the incremented version
5. **File Generation**: Generates `nostr_core/version.h` and `nostr_core/version.c` with build metadata
### Generated Files
The system automatically generates two files during each build:
#### `nostr_core/version.h`
```c
#define VERSION_MAJOR 0
#define VERSION_MINOR 2
#define VERSION_PATCH 1
#define VERSION_STRING "0.2.1"
#define VERSION_TAG "v0.2.1"
#define BUILD_DATE "2025-08-09"
#define BUILD_TIME "10:42:45"
#define BUILD_TIMESTAMP "2025-08-09 10:42:45"
#define GIT_HASH "ca6b475"
#define GIT_BRANCH "master"
// API functions
const char* nostr_core_get_version(void);
const char* nostr_core_get_version_full(void);
const char* nostr_core_get_build_info(void);
```
#### `nostr_core/version.c`
Contains the implementation of the version API functions.
## Usage
### Building with Auto-Versioning
All major build targets automatically increment the version:
```bash
# Build static library (increments version)
./build.sh lib
# Build shared library (increments version)
./build.sh shared
# Build all libraries (increments version)
./build.sh all
# Build examples (increments version)
./build.sh examples
# Install to system (increments version)
./build.sh install
```
### Non-Versioned Builds
Some targets do not increment versions:
```bash
# Clean build artifacts (no version increment)
./build.sh clean
# Run tests (no version increment)
./build.sh test
```
### Using Version Information in Code
```c
#include "version.h"
// Get version string
printf("Version: %s\n", nostr_core_get_version());
// Get full version with timestamp and commit
printf("Full: %s\n", nostr_core_get_version_full());
// Get detailed build information
printf("Build: %s\n", nostr_core_get_build_info());
// Use version macros
#if VERSION_MAJOR >= 1
// Use new API
#else
// Use legacy API
#endif
```
### Testing Version System
A version test example is provided:
```bash
# Build and run version test
gcc -I. -Inostr_core examples/version_test.c -o examples/version_test ./libnostr_core.a ./secp256k1/.libs/libsecp256k1.a -lm
./examples/version_test
```
## Version History Tracking
### View All Versions
```bash
# List all version tags
git tag --list
# View version history
git log --oneline --decorate --graph
```
### Current Version
```bash
# Check current version
cat VERSION
# Or check the latest git tag
git describe --tags --abbrev=0
```
## Manual Version Management
### Major/Minor Version Bumps
For major or minor version changes, manually create the appropriate tag:
```bash
# For a minor version bump (new features)
git tag v0.3.0
# For a major version bump (breaking changes)
git tag v1.0.0
```
The next build will automatically increment from the new base version.
### Resetting Version
To reset or fix version numbering:
```bash
# Delete incorrect tags (if needed)
git tag -d v0.2.1
git push origin --delete v0.2.1
# Create correct base version
git tag v0.2.0
# Next build will create v0.2.1
```
## Integration Notes
### Makefile Integration
- The `version.c` file is automatically included in `LIB_SOURCES`
- Version files are compiled and linked with the library
- Clean targets remove generated version object files
### Git Integration
- Version files (`version.h`, `version.c`) are excluded from git via `.gitignore`
- Only version tags and the `VERSION` file are tracked
- Build system works in any git repository with version tags
### Build System Integration
- Version increment is integrated directly into `build.sh`
- No separate scripts or external dependencies required
- Self-contained and portable across systems
## Example Output
When building, you'll see output like:
```
[INFO] Incrementing version...
[INFO] Current version: v0.2.0
[INFO] New version: v0.2.1
[SUCCESS] Created new version tag: v0.2.1
[SUCCESS] Generated version.h and version.c
[SUCCESS] Updated VERSION file to 0.2.1
```
## Troubleshooting
### Version Not Incrementing
- Ensure you're in a git repository
- Check that git tags exist with `git tag --list`
- Verify tag format matches `v*.*.*` pattern
### Tag Already Exists
If a tag already exists, the build will continue with the existing version:
```
[WARNING] Tag v0.2.1 already exists - using existing version
```
### Missing Git Information
If git is not available, version files will show "unknown" for git hash and branch.
## Benefits
1. **Automatic Traceability**: Every build has a unique version
2. **Build Metadata**: Includes timestamp, git commit, and branch information
3. **API Integration**: Version information accessible via C API
4. **Zero Maintenance**: No manual version file editing required
5. **Git Integration**: Automatic git tag creation for version history

View File

@@ -1,309 +0,0 @@
# Exportable C NOSTR Library Design Guide
This document outlines the design principles and structure for making the C NOSTR library easily exportable and reusable across multiple projects.
## Overview
The C NOSTR library is designed as a modular, self-contained implementation that can be easily integrated into other projects while maintaining compatibility with the broader NOSTR ecosystem.
## Design Principles
### 1. Modular Architecture
- **Core Layer**: Essential NOSTR functionality (`nostr_core.c/h`)
- **Crypto Layer**: Self-contained cryptographic primitives (`nostr_crypto.c/h`)
- **WebSocket Layer**: Optional networking functionality (`nostr_websocket/`)
- **Dependencies**: Minimal external dependencies (only cJSON and mbedTLS)
### 2. Clean API Surface
- Consistent function naming with `nostr_` prefix
- Clear return codes using `NOSTR_SUCCESS`/`NOSTR_ERROR_*` constants
- Well-documented function signatures
- No global state where possible
### 3. Self-Contained Crypto
- Custom implementations of SHA-256, HMAC, secp256k1
- BIP39 wordlist embedded in code
- No external crypto library dependencies for core functionality
- Optional mbedTLS integration for enhanced security
### 4. Cross-Platform Compatibility
- Standard C99 code
- Platform-specific code isolated in separate modules
- ARM64 and x86_64 tested builds
- Static library compilation support
## Library Structure
```
c_nostr/
├── nostr_core.h # High-level API
├── nostr_core.c # Implementation
├── nostr_crypto.h # Crypto primitives API
├── nostr_crypto.c # Self-contained crypto
├── libnostr_core.a # Static library (x86_64)
├── libnostr_core.so # Shared library (x86_64)
├── cjson/ # JSON parsing (vendored)
├── mbedtls/ # Optional crypto backend
├── examples/ # Usage examples
├── tests/ # Test suites
└── nostr_websocket/ # Optional WebSocket layer
```
## Integration Methods
### Method 1: Static Library Linking
**Best for**: Applications that want a single binary with all NOSTR functionality embedded.
```bash
# Build the library
make lib
# In your project Makefile:
CFLAGS += -I/path/to/c_nostr
LDFLAGS += -L/path/to/c_nostr -lnostr_core -lm -static
```
**Usage:**
```c
#include "nostr_core.h"
int main() {
if (nostr_init() != NOSTR_SUCCESS) {
return 1;
}
unsigned char private_key[32], public_key[32];
nostr_generate_keypair(private_key, public_key);
cJSON* event = nostr_create_text_event("Hello NOSTR!", private_key);
// ... use event
nostr_cleanup();
return 0;
}
```
### Method 2: Source Code Integration
**Best for**: Applications that want to customize the crypto backend or minimize dependencies.
```bash
# Copy essential files to your project:
cp nostr_core.{c,h} your_project/src/
cp nostr_crypto.{c,h} your_project/src/
cp -r cjson/ your_project/src/
```
**Compile with your project:**
```c
// In your source
#include "nostr_core.h"
// Use NOSTR functions directly
```
### Method 3: Git Submodule
**Best for**: Projects that want to track upstream changes and contribute back.
```bash
# In your project root:
git submodule add https://github.com/yourorg/c_nostr.git deps/c_nostr
git submodule update --init --recursive
# In your Makefile:
CFLAGS += -Ideps/c_nostr
LDFLAGS += -Ldeps/c_nostr -lnostr_core
```
### Method 4: Shared Library
**Best for**: System-wide installation or multiple applications sharing the same NOSTR implementation.
```bash
# Install system-wide
sudo make install
# In your project:
CFLAGS += $(pkg-config --cflags nostr_core)
LDFLAGS += $(pkg-config --libs nostr_core)
```
## API Design for Exportability
### Consistent Error Handling
```c
typedef enum {
NOSTR_SUCCESS = 0,
NOSTR_ERROR_INVALID_INPUT = -1,
NOSTR_ERROR_CRYPTO_FAILED = -2,
NOSTR_ERROR_MEMORY_ALLOCATION = -3,
NOSTR_ERROR_JSON_PARSE = -4,
NOSTR_ERROR_NETWORK = -5
} nostr_error_t;
const char* nostr_strerror(int error_code);
```
### Clean Resource Management
```c
// Always provide cleanup functions
int nostr_init(void);
void nostr_cleanup(void);
// JSON objects returned should be freed by caller
cJSON* nostr_create_text_event(const char* content, const unsigned char* private_key);
// Caller must call cJSON_Delete(event) when done
```
### Optional Features
```c
// Compile-time feature toggles
#ifndef NOSTR_DISABLE_WEBSOCKETS
int nostr_connect_relay(const char* url);
#endif
#ifndef NOSTR_DISABLE_IDENTITY_PERSISTENCE
int nostr_save_identity(const unsigned char* private_key, const char* password, int account);
#endif
```
## Configuration System
### Build-Time Configuration
```c
// nostr_config.h (generated during build)
#define NOSTR_VERSION "1.0.0"
#define NOSTR_HAS_MBEDTLS 1
#define NOSTR_HAS_WEBSOCKETS 1
#define NOSTR_STATIC_BUILD 1
```
### Runtime Configuration
```c
typedef struct {
int log_level;
char* identity_file_path;
int default_account;
int enable_networking;
} nostr_config_t;
int nostr_set_config(const nostr_config_t* config);
const nostr_config_t* nostr_get_config(void);
```
## Testing and Validation
### Ecosystem Compatibility Testing
The library includes comprehensive test suites that validate compatibility with reference implementations like `nak`:
```bash
# Run all tests
cd tests && make test
# Test specific functionality
make test-crypto # Cryptographic primitives
make test-core # High-level NOSTR functions
```
### Test Vectors
Real-world test vectors are generated using `nak` to ensure ecosystem compatibility:
- Key generation and derivation (BIP39/BIP32)
- Event creation and signing
- Bech32 encoding/decoding
- Message serialization
## Documentation for Exporters
### Essential Files Checklist
For projects integrating this library, you need:
**Core Files (Required):**
- `nostr_core.h` - Main API
- `nostr_core.c` - Implementation
- `nostr_crypto.h` - Crypto API
- `nostr_crypto.c` - Self-contained crypto
- `cjson/` directory - JSON parsing
**Optional Files:**
- `nostr_websocket/` - WebSocket relay support
- `mbedtls/` - Enhanced crypto backend
- `examples/` - Usage examples
- `tests/` - Validation tests
### Minimal Integration Example
```c
// minimal_nostr.c - Smallest possible integration
#include "nostr_core.h"
int main() {
// Initialize library
nostr_init();
// Generate keypair
unsigned char priv[32], pub[32];
nostr_generate_keypair(priv, pub);
// Create and sign event
cJSON* event = nostr_create_text_event("Hello from my app!", priv);
char* json_string = cJSON_Print(event);
printf("Event: %s\n", json_string);
// Cleanup
free(json_string);
cJSON_Delete(event);
nostr_cleanup();
return 0;
}
```
**Compile:**
```bash
gcc -I. minimal_nostr.c nostr_core.c nostr_crypto.c cjson/cJSON.c -lm -o minimal_nostr
```
## Future Considerations
### Language Bindings
The C library is designed to be easily wrapped:
- **Python**: Use ctypes or cffi
- **JavaScript**: Use Node.js FFI or WASM compilation
- **Go**: Use cgo
- **Rust**: Use bindgen for FFI bindings
### WebAssembly Support
The library can be compiled to WebAssembly for browser usage:
```bash
emcc -O3 -s WASM=1 -s EXPORTED_FUNCTIONS='["_nostr_init", "_nostr_generate_keypair"]' \
nostr_core.c nostr_crypto.c cjson/cJSON.c -o nostr.wasm
```
### Package Manager Support
Future versions may include:
- pkgconfig files for system installation
- CMake integration for easier builds
- vcpkg/Conan package definitions
## Contributing Back
Projects using this library are encouraged to:
1. Report compatibility issues
2. Submit test vectors from their use cases
3. Contribute performance improvements
4. Add support for additional NIPs
## Version Compatibility
The library follows semantic versioning:
- **Major**: Breaking API changes
- **Minor**: New features, backward compatible
- **Patch**: Bug fixes
API stability guarantees:
- All functions prefixed with `nostr_` are part of the stable API
- Internal functions (static or prefixed with `_`) may change
- Configuration structures may be extended but not modified
---
This design ensures the C NOSTR library can be easily adopted by other projects while maintaining high compatibility with the NOSTR ecosystem standards.

View File

@@ -1,29 +0,0 @@
# c-nostr Export and Implementation Guide
## Overview
This guide provides essential information for developers using the `c-nostr` library, particularly regarding the capabilities and limitations of its self-contained modules, such as the HTTP client.
## HTTP Client (`http_client.c`)
The `http_client` module is a lightweight, self-contained HTTP/HTTPS client designed for simple and direct web requests. It is ideal for tasks like fetching NIP-11 information from Nostr relays or interacting with standard, permissive web APIs.
### Key Features
* **Self-Contained**: It is built with `mbedTLS` and has no external dependencies beyond standard C libraries, making it highly portable.
* **Simplicity**: It provides a straightforward `http_get()` function for making web requests.
* **TLS Support**: It supports HTTPS and basic TLS 1.3/1.2 features, including SNI (Server Name Indication) and ALPN (Application-Layer Protocol Negotiation).
### Limitations
The `http_client` is intentionally simple and is **not a full-featured web browser**. Due to its minimalist design, it will likely be blocked by websites that employ advanced anti-bot protection systems.
* **Incompatibility with Advanced Bot Protection**: Sites like Google, Cloudflare, and others use sophisticated fingerprinting techniques to distinguish between real browsers and automated clients. They analyze the exact combination of TLS cipher suites, TLS extensions, and HTTP headers. Our client's fingerprint does not match a standard browser, so these sites will preemptively drop the connection, typically resulting in a `HTTP_ERROR_NETWORK` error.
* **Intended Use Case**: This client is best suited for interacting with known, friendly APIs and Nostr relays. It is **not** designed for general web scraping or for accessing services that are heavily guarded against automated traffic.
### Best Practices
* **Use for APIs and Relays**: Rely on this client for fetching data from well-defined, public endpoints that do not have aggressive bot-detection measures in place.
* **Avoid Protected Sites**: Do not attempt to use this client to access services like Google Search, as such attempts will fail. For those use cases, a full-featured library like `libcurl` or a dedicated web-scraping framework is required.
* **Check the Test Suite**: The `tests/http_client_test.c` file contains test cases that demonstrate both the successful use cases (e.g., `httpbin.org`) and the expected failures (e.g., `google.com`), providing a clear reference for the client's capabilities.

View File

@@ -1,361 +0,0 @@
# Generic Automatic Version Increment System for Any Repository
Here's a generalized implementation guide for adding automatic versioning to any project:
## Core Concept
**Automatic patch version increment with each build** - Every build automatically increments the patch version: v0.1.0 → v0.1.1 → v0.1.2, etc.
## Implementation Steps
### 1. Add Version Increment Function to Build Script
Add this function to your build script (bash example):
```bash
# Function to automatically increment version
increment_version() {
echo "[INFO] Incrementing version..."
# Check if we're in a git repository
if ! git rev-parse --git-dir > /dev/null 2>&1; then
echo "[WARNING] Not in a git repository - skipping version increment"
return 0
fi
# Get the highest version tag (not chronologically latest)
LATEST_TAG=$(git tag -l 'v*.*.*' | sort -V | tail -n 1 || echo "v0.1.0")
if [[ -z "$LATEST_TAG" ]]; then
LATEST_TAG="v0.1.0"
fi
# Extract version components (remove 'v' prefix)
VERSION=${LATEST_TAG#v}
# Parse major.minor.patch using regex
if [[ $VERSION =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
MAJOR=${BASH_REMATCH[1]}
MINOR=${BASH_REMATCH[2]}
PATCH=${BASH_REMATCH[3]}
else
echo "[ERROR] Invalid version format in tag: $LATEST_TAG"
echo "[ERROR] Expected format: v0.1.0"
return 1
fi
# Increment patch version
NEW_PATCH=$((PATCH + 1))
NEW_VERSION="v${MAJOR}.${MINOR}.${NEW_PATCH}"
echo "[INFO] Current version: $LATEST_TAG"
echo "[INFO] New version: $NEW_VERSION"
# Create new git tag
if git tag "$NEW_VERSION" 2>/dev/null; then
echo "[SUCCESS] Created new version tag: $NEW_VERSION"
else
echo "[WARNING] Tag $NEW_VERSION already exists - using existing version"
NEW_VERSION=$LATEST_TAG
fi
# Update VERSION file for compatibility
echo "${NEW_VERSION#v}" > VERSION
echo "[SUCCESS] Updated VERSION file to ${NEW_VERSION#v}"
}
```
### 2. Generate Version Header Files (For C/C++ Projects)
Add this to the increment_version function:
```bash
# Generate version.h header file (adjust path as needed)
cat > src/version.h << EOF
/*
* Auto-Generated Version Header
* DO NOT EDIT THIS FILE MANUALLY - Generated by build script
*/
#ifndef VERSION_H
#define VERSION_H
#define VERSION_MAJOR ${MAJOR}
#define VERSION_MINOR ${MINOR}
#define VERSION_PATCH ${NEW_PATCH}
#define VERSION_STRING "${MAJOR}.${MINOR}.${NEW_PATCH}"
#define VERSION_TAG "${NEW_VERSION}"
/* Build information */
#define BUILD_DATE "$(date +%Y-%m-%d)"
#define BUILD_TIME "$(date +%H:%M:%S)"
#define BUILD_TIMESTAMP "$(date '+%Y-%m-%d %H:%M:%S')"
/* Git information */
#define GIT_HASH "$(git rev-parse --short HEAD 2>/dev/null || echo 'unknown')"
#define GIT_BRANCH "$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo 'unknown')"
/* Display versions */
#define VERSION_DISPLAY "${NEW_VERSION}"
#define VERSION_FULL_DISPLAY "${NEW_VERSION} ($(date '+%Y-%m-%d %H:%M:%S'), $(git rev-parse --short HEAD 2>/dev/null || echo 'unknown'))"
/* Version API functions */
const char* get_version(void);
const char* get_version_full(void);
const char* get_build_info(void);
#endif /* VERSION_H */
EOF
# Generate version.c implementation file
cat > src/version.c << EOF
/*
* Auto-Generated Version Implementation
* DO NOT EDIT THIS FILE MANUALLY - Generated by build script
*/
#include "version.h"
const char* get_version(void) {
return VERSION_TAG;
}
const char* get_version_full(void) {
return VERSION_FULL_DISPLAY;
}
const char* get_build_info(void) {
return "Built on " BUILD_DATE " at " BUILD_TIME " from commit " GIT_HASH " on branch " GIT_BRANCH;
}
EOF
```
### 3. Generate Version File for Other Languages
**Python (`src/__version__.py`):**
```bash
cat > src/__version__.py << EOF
"""Auto-generated version file"""
__version__ = "${MAJOR}.${MINOR}.${NEW_PATCH}"
__version_tag__ = "${NEW_VERSION}"
__build_date__ = "$(date +%Y-%m-%d)"
__build_time__ = "$(date +%H:%M:%S)"
__git_hash__ = "$(git rev-parse --short HEAD 2>/dev/null || echo 'unknown')"
__git_branch__ = "$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo 'unknown')"
EOF
```
**JavaScript/Node.js (update `package.json`):**
```bash
# Update package.json version field
if [ -f package.json ]; then
sed -i "s/\"version\": \".*\"/\"version\": \"${MAJOR}.${MINOR}.${NEW_PATCH}\"/" package.json
fi
```
**Rust (update `Cargo.toml`):**
```bash
if [ -f Cargo.toml ]; then
sed -i "s/^version = \".*\"/version = \"${MAJOR}.${MINOR}.${NEW_PATCH}\"/" Cargo.toml
fi
```
**Go (generate `version.go`):**
```bash
cat > version.go << EOF
// Auto-generated version file
package main
const (
VersionMajor = ${MAJOR}
VersionMinor = ${MINOR}
VersionPatch = ${NEW_PATCH}
VersionString = "${MAJOR}.${MINOR}.${NEW_PATCH}"
VersionTag = "${NEW_VERSION}"
BuildDate = "$(date +%Y-%m-%d)"
BuildTime = "$(date +%H:%M:%S)"
GitHash = "$(git rev-parse --short HEAD 2>/dev/null || echo 'unknown')"
GitBranch = "$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo 'unknown')"
)
EOF
```
**Java (generate `Version.java`):**
```bash
cat > src/main/java/Version.java << EOF
// Auto-generated version class
public class Version {
public static final int VERSION_MAJOR = ${MAJOR};
public static final int VERSION_MINOR = ${MINOR};
public static final int VERSION_PATCH = ${NEW_PATCH};
public static final String VERSION_STRING = "${MAJOR}.${MINOR}.${NEW_PATCH}";
public static final String VERSION_TAG = "${NEW_VERSION}";
public static final String BUILD_DATE = "$(date +%Y-%m-%d)";
public static final String BUILD_TIME = "$(date +%H:%M:%S)";
public static final String GIT_HASH = "$(git rev-parse --short HEAD 2>/dev/null || echo 'unknown')";
public static final String GIT_BRANCH = "$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo 'unknown')";
}
EOF
```
### 4. Integrate into Build Targets
Call `increment_version` before your main build commands:
```bash
build_library() {
increment_version
echo "[INFO] Building library..."
# Your actual build commands here
make clean && make
}
build_release() {
increment_version
echo "[INFO] Building release..."
# Your release build commands
}
build_package() {
increment_version
echo "[INFO] Building package..."
# Your packaging commands
}
```
### 5. Update .gitignore
Add generated version files to `.gitignore`:
```gitignore
# Auto-generated version files
src/version.h
src/version.c
src/__version__.py
version.go
src/main/java/Version.java
VERSION
```
### 6. Update Build System Files
**For Makefile projects:**
```makefile
# Add version.c to your source files
SOURCES = main.c utils.c version.c
```
**For CMake projects:**
```cmake
# Add version files to your target
target_sources(your_target PRIVATE src/version.c)
```
**For Node.js projects:**
```json
{
"scripts": {
"build": "node build.js && increment_version",
"version": "node -e \"console.log(require('./package.json').version)\""
}
}
```
### 7. Create Initial Version Tag
```bash
# Start with initial version
git tag v0.1.0
```
## Usage Pattern
```bash
./build.sh # v0.1.0 → v0.1.1
./build.sh release # v0.1.1 → v0.1.2
./build.sh package # v0.1.2 → v0.1.3
```
## Manual Version Control
### Major/Minor Version Bumps
```bash
# For feature releases (minor bump)
git tag v0.2.0 # Next build: v0.2.1
# For breaking changes (major bump)
git tag v1.0.0 # Next build: v1.0.1
```
### Version Reset
```bash
# Delete incorrect tags (if needed)
git tag -d v0.2.1
git push origin --delete v0.2.1 # If pushed to remote
# Create correct base version
git tag v0.2.0
# Next build will create v0.2.1
```
## Example Build Script Template
```bash
#!/bin/bash
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
print_status() { echo -e "${BLUE}[INFO]${NC} $1"; }
print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; }
print_error() { echo -e "${RED}[ERROR]${NC} $1"; }
# Insert increment_version function here
case "${1:-build}" in
build)
increment_version
print_status "Building project..."
# Your build commands
;;
clean)
print_status "Cleaning build artifacts..."
# Your clean commands
;;
test)
print_status "Running tests..."
# Your test commands (no version increment)
;;
release)
increment_version
print_status "Building release..."
# Your release commands
;;
*)
echo "Usage: $0 {build|clean|test|release}"
exit 1
;;
esac
```
## Benefits
1. **Zero maintenance** - No manual version editing
2. **Build traceability** - Every build has unique version + metadata
3. **Git integration** - Automatic version tags
4. **Language agnostic** - Adapt generation for any language
5. **CI/CD friendly** - Works in automated environments
6. **Rollback friendly** - Easy to revert to previous versions
## Troubleshooting
### Version Not Incrementing
- Ensure you're in a git repository
- Check that git tags exist: `git tag --list`
- Verify tag format matches `v*.*.*` pattern
### Tag Already Exists
If a tag already exists, the build continues with existing version:
```
[WARNING] Tag v0.2.1 already exists - using existing version
```
### Missing Git Information
If git is unavailable, version files show "unknown" for git hash and branch.

View File

@@ -1,319 +0,0 @@
# NOSTR Core Library - Usage Guide
## Overview
The NOSTR Core Library (`libnostr_core`) is a self-contained, exportable C library for NOSTR protocol implementation. It requires **no external cryptographic dependencies** (no OpenSSL, no libwally) and can be easily integrated into other projects.
## Key Features
- **Self-Contained Crypto**: All cryptographic operations implemented from scratch
- **Zero External Dependencies**: Only requires standard C library, cJSON, and libwebsockets
- **Cross-Platform**: Builds on Linux, macOS, Windows (with appropriate toolchain)
- **NIP-06 Compliant**: Proper BIP39/BIP32 implementation for key derivation
- **Thread-Safe**: Core cryptographic functions are stateless and thread-safe
- **Easy Integration**: Simple C API with clear error handling
## File Structure for Export
### Core Library Files (Required)
```
libnostr_core/
├── nostr_core.h # Main public API header
├── nostr_core.c # Core implementation
├── nostr_crypto.h # Crypto implementation header
├── nostr_crypto.c # Self-contained crypto implementation
├── Makefile # Build configuration
└── cjson/ # JSON library (can be replaced with system cJSON)
├── cJSON.h
└── cJSON.c
```
### Optional Files
```
├── examples/ # Usage examples (helpful for integration)
├── LIBRARY_USAGE.md # This usage guide
├── SELF_CONTAINED_CRYPTO.md # Crypto implementation details
└── CROSS_PLATFORM_GUIDE.md # Platform-specific build notes
```
## Integration Methods
### Method 1: Static Library Integration
1. **Copy Required Files**:
```bash
cp nostr_core.h nostr_core.c nostr_crypto.h nostr_crypto.c /path/to/your/project/
cp -r cjson/ /path/to/your/project/
```
2. **Build Static Library**:
```bash
gcc -c -fPIC nostr_core.c nostr_crypto.c cjson/cJSON.c
ar rcs libnostr_core.a nostr_core.o nostr_crypto.o cJSON.o
```
3. **Link in Your Project**:
```bash
gcc your_project.c -L. -lnostr_core -lm -o your_project
```
### Method 2: Direct Source Integration
Simply include the source files directly in your project:
```c
// In your project
#include "nostr_core.h"
// Compile with:
// gcc your_project.c nostr_core.c nostr_crypto.c cjson/cJSON.c -lm
```
### Method 3: Shared Library Integration
1. **Build Shared Library**:
```bash
make libnostr_core.so
```
2. **Install System-Wide** (optional):
```bash
sudo cp libnostr_core.so /usr/local/lib/
sudo cp nostr_core.h /usr/local/include/
sudo ldconfig
```
3. **Use in Projects**:
```bash
gcc your_project.c -lnostr_core -lm
```
## Building the Library
### Using the Provided Build Script
The library includes an automated build script that handles all dependencies and creates a self-contained static library.
**IMPORTANT**: The build script must be run from within the `nostr_core_lib` directory:
```bash
# Correct usage:
cd nostr_core_lib
./build.sh
# If you need to use it from your project directory:
cd nostr_core_lib
./build.sh
cd ..
gcc your_app.c nostr_core_lib/libnostr_core_x64.a -lz -ldl -lpthread -lm -o your_app
```
**Common Error**: Running `./nostr_core_lib/build.sh` from outside the library directory will fail with:
```
[ERROR] Build script must be run from the nostr_core_lib directory
```
### Build Script Options
```bash
# Auto-detect NIPs from your source code
./build.sh
# Force specific NIPs
./build.sh --nips=1,6,19
# Build all available NIPs
./build.sh --nips=all
# Build for specific architecture
./build.sh arm64
# Build with tests
./build.sh --tests
# Get help
./build.sh --help
```
The build script automatically:
- Scans your `.c` files for `#include "nip*.h"` statements
- Compiles only the needed NIPs
- Creates a self-contained static library (`libnostr_core_x64.a`)
- Includes all dependencies (OpenSSL, curl, secp256k1, cJSON)
## Basic Usage Example
```c
#include "nostr_core.h"
#include <stdio.h>
#include <stdlib.h>
int main() {
// Initialize the library
if (nostr_init() != NOSTR_SUCCESS) {
fprintf(stderr, "Failed to initialize NOSTR library\n");
return 1;
}
// Generate a keypair
unsigned char private_key[32];
unsigned char public_key[32];
if (nostr_generate_keypair(private_key, public_key) != NOSTR_SUCCESS) {
fprintf(stderr, "Failed to generate keypair\n");
nostr_cleanup();
return 1;
}
// Convert to hex and bech32
char private_hex[65], public_hex[65];
char nsec[100], npub[100];
nostr_bytes_to_hex(private_key, 32, private_hex);
nostr_bytes_to_hex(public_key, 32, public_hex);
nostr_key_to_bech32(private_key, "nsec", nsec);
nostr_key_to_bech32(public_key, "npub", npub);
printf("Private Key: %s\n", private_hex);
printf("Public Key: %s\n", public_hex);
printf("nsec: %s\n", nsec);
printf("npub: %s\n", npub);
// Create and sign an event
cJSON* event = nostr_create_text_event("Hello NOSTR!", private_key);
if (event) {
char* event_json = cJSON_Print(event);
printf("Signed Event: %s\n", event_json);
free(event_json);
cJSON_Delete(event);
}
// Cleanup
nostr_cleanup();
return 0;
}
```
## Dependency Management
### Required Dependencies
- **Standard C Library**: malloc, string functions, file I/O
- **Math Library**: `-lm` (for cryptographic calculations)
- **cJSON**: JSON parsing (included, or use system version)
### Optional Dependencies
- **libwebsockets**: Only needed for relay communication functions
- **System cJSON**: Can replace bundled version
### Minimal Integration (Crypto Only)
If you only need key generation and signing:
```bash
# Build with minimal dependencies
gcc -DNOSTR_CRYPTO_ONLY your_project.c nostr_crypto.c -lm
```
## Cross-Platform Considerations
### Linux
- Works out of the box with GCC
- Install build-essential: `sudo apt install build-essential`
### macOS
- Works with Xcode command line tools
- May need: `xcode-select --install`
### Windows
- Use MinGW-w64 or MSYS2
- Or integrate with Visual Studio project
### Embedded Systems
- Library is designed to work on resource-constrained systems
- No heap allocations in core crypto functions
- Stack usage is predictable and bounded
## API Reference
### Initialization
```c
int nostr_init(void); // Initialize library
void nostr_cleanup(void); // Cleanup resources
const char* nostr_strerror(int error); // Get error string
```
### Key Generation
```c
int nostr_generate_keypair(unsigned char* private_key, unsigned char* public_key);
int nostr_generate_mnemonic_and_keys(char* mnemonic, size_t mnemonic_size,
int account, unsigned char* private_key,
unsigned char* public_key);
int nostr_derive_keys_from_mnemonic(const char* mnemonic, int account,
unsigned char* private_key, unsigned char* public_key);
```
### Event Creation
```c
cJSON* nostr_create_text_event(const char* content, const unsigned char* private_key);
cJSON* nostr_create_profile_event(const char* name, const char* about,
const unsigned char* private_key);
int nostr_sign_event(cJSON* event, const unsigned char* private_key);
```
### Utilities
```c
void nostr_bytes_to_hex(const unsigned char* bytes, size_t len, char* hex);
int nostr_hex_to_bytes(const char* hex, unsigned char* bytes, size_t len);
int nostr_key_to_bech32(const unsigned char* key, const char* hrp, char* output);
nostr_input_type_t nostr_detect_input_type(const char* input);
```
## Error Handling
All functions return standardized error codes:
- `NOSTR_SUCCESS` (0): Operation successful
- `NOSTR_ERROR_INVALID_INPUT` (-1): Invalid parameters
- `NOSTR_ERROR_CRYPTO_FAILED` (-2): Cryptographic operation failed
- `NOSTR_ERROR_MEMORY_FAILED` (-3): Memory allocation failed
- `NOSTR_ERROR_IO_FAILED` (-4): File I/O operation failed
- `NOSTR_ERROR_NETWORK_FAILED` (-5): Network operation failed
## Security Considerations
1. **Private Key Handling**: Always clear private keys from memory when done
2. **Random Number Generation**: Uses `/dev/urandom` on Unix systems
3. **Memory Safety**: All buffers are bounds-checked
4. **Constant-Time Operations**: Critical crypto operations are timing-attack resistant
## Testing Your Integration
Use the provided examples to verify your integration:
```bash
# Test key generation
./examples/simple_keygen
# Test mnemonic functionality
./examples/mnemonic_generation
# Test event creation
./examples/text_event
# Test all functionality
./examples/utility_functions
```
## Support and Documentation
- See `examples/` directory for comprehensive usage examples
- Check `SELF_CONTAINED_CRYPTO.md` for cryptographic implementation details
- Review `CROSS_PLATFORM_GUIDE.md` for platform-specific notes
- All functions are documented in `nostr_core.h`
## License
This library is designed to be freely integrable into other projects. Check the individual file headers for specific licensing information.
---
**Quick Start**: Copy `nostr_core.h`, `nostr_core.c`, `nostr_crypto.h`, `nostr_crypto.c`, and the `cjson/` folder to your project, then compile with `-lm`. That's it!

755
POOL_API.md Normal file
View File

@@ -0,0 +1,755 @@
# Relay Pool API Reference
This document describes the public API for the Nostr Relay Pool implementation in [`core_relay_pool.c`](nostr_core/core_relay_pool.c).
## Function Summary
| Function | Description |
|----------|-------------|
| [`nostr_relay_pool_create()`](nostr_core/core_relay_pool.c:219) | Create and initialize a new relay pool |
| [`nostr_relay_pool_destroy()`](nostr_core/core_relay_pool.c:304) | Destroy pool and cleanup all resources |
| [`nostr_relay_pool_add_relay()`](nostr_core/core_relay_pool.c:229) | Add a relay URL to the pool |
| [`nostr_relay_pool_remove_relay()`](nostr_core/core_relay_pool.c:273) | Remove a relay URL from the pool |
| [`nostr_relay_pool_subscribe()`](nostr_core/core_relay_pool.c:399) | Create async subscription with callbacks |
| [`nostr_pool_subscription_close()`](nostr_core/core_relay_pool.c:491) | Close subscription and free resources |
| [`nostr_relay_pool_run()`](nostr_core/core_relay_pool.c:1192) | Run event loop for specified timeout |
| [`nostr_relay_pool_poll()`](nostr_core/core_relay_pool.c:1232) | Single iteration poll and dispatch |
| [`nostr_relay_pool_query_sync()`](nostr_core/core_relay_pool.c:695) | Synchronous query returning event array |
| [`nostr_relay_pool_get_event()`](nostr_core/core_relay_pool.c:825) | Get single most recent event |
| [`nostr_relay_pool_publish_async()`](nostr_core/core_relay_pool.c:866) | Publish event with async callbacks |
| [`nostr_relay_pool_get_relay_status()`](nostr_core/core_relay_pool.c:944) | Get connection status for a relay |
| [`nostr_relay_pool_list_relays()`](nostr_core/core_relay_pool.c:960) | List all relays and their statuses |
| [`nostr_relay_pool_get_relay_stats()`](nostr_core/core_relay_pool.c:992) | Get detailed statistics for a relay |
| [`nostr_relay_pool_reset_relay_stats()`](nostr_core/core_relay_pool.c:1008) | Reset statistics for a relay |
| [`nostr_relay_pool_get_relay_query_latency()`](nostr_core/core_relay_pool.c:1045) | Get average query latency for a relay |
## Pool Lifecycle
### Create Pool
**Function:** [`nostr_relay_pool_create()`](nostr_core/core_relay_pool.c:219)
```c
nostr_relay_pool_t* nostr_relay_pool_create(void);
```
**Example:**
```c
#include "nostr_core.h"
int main() {
// Create a new relay pool
nostr_relay_pool_t* pool = nostr_relay_pool_create();
if (!pool) {
fprintf(stderr, "Failed to create relay pool\n");
return -1;
}
// Use the pool...
// Clean up
nostr_relay_pool_destroy(pool);
return 0;
}
```
### Destroy Pool
**Function:** [`nostr_relay_pool_destroy()`](nostr_core/core_relay_pool.c:304)
```c
void nostr_relay_pool_destroy(nostr_relay_pool_t* pool);
```
**Example:**
```c
// Properly cleanup a relay pool
void cleanup_pool(nostr_relay_pool_t* pool) {
if (pool) {
// This will close all active subscriptions and relay connections
nostr_relay_pool_destroy(pool);
pool = NULL;
}
}
```
## Relay Management
### Add Relay
**Function:** [`nostr_relay_pool_add_relay()`](nostr_core/core_relay_pool.c:229)
```c
int nostr_relay_pool_add_relay(nostr_relay_pool_t* pool, const char* relay_url);
```
**Example:**
```c
int setup_relays(nostr_relay_pool_t* pool) {
const char* relays[] = {
"wss://relay.damus.io",
"wss://nos.lol",
"wss://relay.nostr.band"
};
for (int i = 0; i < 3; i++) {
int result = nostr_relay_pool_add_relay(pool, relays[i]);
if (result != NOSTR_SUCCESS) {
fprintf(stderr, "Failed to add relay %s: %d\n", relays[i], result);
return -1;
}
printf("Added relay: %s\n", relays[i]);
}
return 0;
}
```
### Remove Relay
**Function:** [`nostr_relay_pool_remove_relay()`](nostr_core/core_relay_pool.c:273)
```c
int nostr_relay_pool_remove_relay(nostr_relay_pool_t* pool, const char* relay_url);
```
**Example:**
```c
int remove_slow_relay(nostr_relay_pool_t* pool) {
const char* slow_relay = "wss://slow-relay.example.com";
int result = nostr_relay_pool_remove_relay(pool, slow_relay);
if (result == NOSTR_SUCCESS) {
printf("Successfully removed relay: %s\n", slow_relay);
} else {
printf("Failed to remove relay %s (may not exist)\n", slow_relay);
}
return result;
}
```
## Subscriptions (Asynchronous)
### Subscribe to Events
**Function:** [`nostr_relay_pool_subscribe()`](nostr_core/core_relay_pool.c:399)
```c
nostr_pool_subscription_t* nostr_relay_pool_subscribe(
nostr_relay_pool_t* pool,
const char** relay_urls,
int relay_count,
cJSON* filter,
void (*on_event)(cJSON* event, const char* relay_url, void* user_data),
void (*on_eose)(void* user_data),
void* user_data);
```
**Example:**
```c
#include "cjson/cJSON.h"
// Event callback - called for each received event
void handle_event(cJSON* event, const char* relay_url, void* user_data) {
cJSON* content = cJSON_GetObjectItem(event, "content");
cJSON* pubkey = cJSON_GetObjectItem(event, "pubkey");
if (content && pubkey) {
printf("Event from %s: %s (by %s)\n",
relay_url,
cJSON_GetStringValue(content),
cJSON_GetStringValue(pubkey));
}
}
// EOSE callback - called when all relays finish sending stored events
void handle_eose(void* user_data) {
printf("All relays finished sending stored events\n");
}
int subscribe_to_notes(nostr_relay_pool_t* pool) {
// Create filter for kind 1 (text notes) from last hour
cJSON* filter = cJSON_CreateObject();
cJSON* kinds = cJSON_CreateArray();
cJSON_AddItemToArray(kinds, cJSON_CreateNumber(1));
cJSON_AddItemToObject(filter, "kinds", kinds);
time_t since = time(NULL) - 3600; // Last hour
cJSON_AddItemToObject(filter, "since", cJSON_CreateNumber(since));
cJSON_AddItemToObject(filter, "limit", cJSON_CreateNumber(50));
// Subscribe to specific relays
const char* relay_urls[] = {
"wss://relay.damus.io",
"wss://nos.lol"
};
nostr_pool_subscription_t* sub = nostr_relay_pool_subscribe(
pool,
relay_urls,
2,
filter,
handle_event,
handle_eose,
NULL // user_data
);
cJSON_Delete(filter); // Pool makes its own copy
if (!sub) {
fprintf(stderr, "Failed to create subscription\n");
return -1;
}
// Drive the event loop to receive events
printf("Listening for events for 30 seconds...\n");
nostr_relay_pool_run(pool, 30000); // 30 seconds
// Close subscription
nostr_pool_subscription_close(sub);
return 0;
}
```
### Close Subscription
**Function:** [`nostr_pool_subscription_close()`](nostr_core/core_relay_pool.c:491)
```c
int nostr_pool_subscription_close(nostr_pool_subscription_t* subscription);
```
**Example:**
```c
// Subscription management with cleanup
typedef struct {
nostr_pool_subscription_t* subscription;
int event_count;
int should_stop;
} subscription_context_t;
void event_counter(cJSON* event, const char* relay_url, void* user_data) {
subscription_context_t* ctx = (subscription_context_t*)user_data;
ctx->event_count++;
printf("Received event #%d from %s\n", ctx->event_count, relay_url);
// Stop after 10 events
if (ctx->event_count >= 10) {
ctx->should_stop = 1;
}
}
int limited_subscription(nostr_relay_pool_t* pool) {
subscription_context_t ctx = {0};
// Create filter
cJSON* filter = cJSON_CreateObject();
cJSON* kinds = cJSON_CreateArray();
cJSON_AddItemToArray(kinds, cJSON_CreateNumber(1));
cJSON_AddItemToObject(filter, "kinds", kinds);
const char* relay_urls[] = {"wss://relay.damus.io"};
ctx.subscription = nostr_relay_pool_subscribe(
pool, relay_urls, 1, filter, event_counter, NULL, &ctx);
cJSON_Delete(filter);
if (!ctx.subscription) {
return -1;
}
// Poll until we should stop
while (!ctx.should_stop) {
int events = nostr_relay_pool_poll(pool, 100);
if (events < 0) break;
}
// Clean up
int result = nostr_pool_subscription_close(ctx.subscription);
printf("Subscription closed with result: %d\n", result);
return 0;
}
```
## Event Loop
### Run Timed Loop
**Function:** [`nostr_relay_pool_run()`](nostr_core/core_relay_pool.c:1192)
```c
int nostr_relay_pool_run(nostr_relay_pool_t* pool, int timeout_ms);
```
**Example:**
```c
int run_event_loop(nostr_relay_pool_t* pool) {
printf("Starting event loop for 60 seconds...\n");
// Run for 60 seconds, processing all incoming events
int total_events = nostr_relay_pool_run(pool, 60000);
if (total_events < 0) {
fprintf(stderr, "Event loop error\n");
return -1;
}
printf("Processed %d events total\n", total_events);
return 0;
}
```
### Single Poll Iteration
**Function:** [`nostr_relay_pool_poll()`](nostr_core/core_relay_pool.c:1232)
```c
int nostr_relay_pool_poll(nostr_relay_pool_t* pool, int timeout_ms);
```
**Example:**
```c
// Integration with custom main loop
int custom_main_loop(nostr_relay_pool_t* pool) {
int running = 1;
int total_events = 0;
while (running) {
// Poll for Nostr events (non-blocking with 50ms timeout)
int events = nostr_relay_pool_poll(pool, 50);
if (events > 0) {
total_events += events;
printf("Processed %d events this iteration\n", events);
}
// Do other work in your application
// handle_ui_events();
// process_background_tasks();
// Check exit condition
// running = !should_exit();
// Simple exit after 100 events for demo
if (total_events >= 100) {
running = 0;
}
}
printf("Main loop finished, processed %d total events\n", total_events);
return 0;
}
```
## Synchronous Operations
### Query Multiple Events
**Function:** [`nostr_relay_pool_query_sync()`](nostr_core/core_relay_pool.c:695)
```c
cJSON** nostr_relay_pool_query_sync(
nostr_relay_pool_t* pool,
const char** relay_urls,
int relay_count,
cJSON* filter,
int* event_count,
int timeout_ms);
```
**Example:**
```c
int query_recent_notes(nostr_relay_pool_t* pool) {
// Create filter for recent text notes
cJSON* filter = cJSON_CreateObject();
cJSON* kinds = cJSON_CreateArray();
cJSON_AddItemToArray(kinds, cJSON_CreateNumber(1));
cJSON_AddItemToObject(filter, "kinds", kinds);
cJSON_AddItemToObject(filter, "limit", cJSON_CreateNumber(20));
const char* relay_urls[] = {
"wss://relay.damus.io",
"wss://nos.lol"
};
int event_count = 0;
cJSON** events = nostr_relay_pool_query_sync(
pool, relay_urls, 2, filter, &event_count, 10000); // 10 second timeout
cJSON_Delete(filter);
if (!events) {
printf("No events received or query failed\n");
return -1;
}
printf("Received %d events:\n", event_count);
for (int i = 0; i < event_count; i++) {
cJSON* content = cJSON_GetObjectItem(events[i], "content");
if (content) {
printf(" %d: %s\n", i + 1, cJSON_GetStringValue(content));
}
// Free each event
cJSON_Delete(events[i]);
}
// Free the events array
free(events);
return event_count;
}
```
### Get Single Most Recent Event
**Function:** [`nostr_relay_pool_get_event()`](nostr_core/core_relay_pool.c:825)
```c
cJSON* nostr_relay_pool_get_event(
nostr_relay_pool_t* pool,
const char** relay_urls,
int relay_count,
cJSON* filter,
int timeout_ms);
```
**Example:**
```c
int get_latest_note_from_pubkey(nostr_relay_pool_t* pool, const char* pubkey_hex) {
// Create filter for specific author's notes
cJSON* filter = cJSON_CreateObject();
cJSON* kinds = cJSON_CreateArray();
cJSON_AddItemToArray(kinds, cJSON_CreateNumber(1));
cJSON_AddItemToObject(filter, "kinds", kinds);
cJSON* authors = cJSON_CreateArray();
cJSON_AddItemToArray(authors, cJSON_CreateString(pubkey_hex));
cJSON_AddItemToObject(filter, "authors", authors);
cJSON_AddItemToObject(filter, "limit", cJSON_CreateNumber(1));
const char* relay_urls[] = {"wss://relay.damus.io"};
cJSON* event = nostr_relay_pool_get_event(
pool, relay_urls, 1, filter, 5000); // 5 second timeout
cJSON_Delete(filter);
if (!event) {
printf("No recent event found for pubkey %s\n", pubkey_hex);
return -1;
}
cJSON* content = cJSON_GetObjectItem(event, "content");
cJSON* created_at = cJSON_GetObjectItem(event, "created_at");
if (content && created_at) {
printf("Latest note: %s (created at %ld)\n",
cJSON_GetStringValue(content),
(long)cJSON_GetNumberValue(created_at));
}
cJSON_Delete(event);
return 0;
}
```
### Publish Event
**Function:** [`nostr_relay_pool_publish_async()`](nostr_core/core_relay_pool.c:866)
```c
int nostr_relay_pool_publish_async(
nostr_relay_pool_t* pool,
const char** relay_urls,
int relay_count,
cJSON* event);
```
**Example:**
```c
int publish_text_note(nostr_relay_pool_t* pool, const char* content) {
// Create a basic text note event (this is simplified - real implementation
// would need proper signing with private key)
cJSON* event = cJSON_CreateObject();
cJSON_AddItemToObject(event, "kind", cJSON_CreateNumber(1));
cJSON_AddItemToObject(event, "content", cJSON_CreateString(content));
cJSON_AddItemToObject(event, "created_at", cJSON_CreateNumber(time(NULL)));
// In real usage, you'd add pubkey, id, sig fields here
cJSON_AddItemToObject(event, "pubkey", cJSON_CreateString("your_pubkey_hex"));
cJSON_AddItemToObject(event, "id", cJSON_CreateString("event_id_hash"));
cJSON_AddItemToObject(event, "sig", cJSON_CreateString("event_signature"));
cJSON_AddItemToObject(event, "tags", cJSON_CreateArray());
const char* relay_urls[] = {
"wss://relay.damus.io",
"wss://nos.lol",
"wss://relay.nostr.band"
};
printf("Publishing note: %s\n", content);
int success_count = nostr_relay_pool_publish_async(
pool, relay_urls, 3, event, my_callback, user_data);
cJSON_Delete(event);
printf("Successfully published to %d out of 3 relays\n", success_count);
if (success_count == 0) {
fprintf(stderr, "Failed to publish to any relay\n");
return -1;
}
return success_count;
}
```
## Status and Statistics
### Get Relay Status
**Function:** [`nostr_relay_pool_get_relay_status()`](nostr_core/core_relay_pool.c:944)
```c
nostr_pool_relay_status_t nostr_relay_pool_get_relay_status(
nostr_relay_pool_t* pool,
const char* relay_url);
```
**Example:**
```c
void check_relay_status(nostr_relay_pool_t* pool, const char* relay_url) {
nostr_pool_relay_status_t status = nostr_relay_pool_get_relay_status(pool, relay_url);
const char* status_str;
switch (status) {
case NOSTR_POOL_RELAY_DISCONNECTED:
status_str = "DISCONNECTED";
break;
case NOSTR_POOL_RELAY_CONNECTING:
status_str = "CONNECTING";
break;
case NOSTR_POOL_RELAY_CONNECTED:
status_str = "CONNECTED";
break;
case NOSTR_POOL_RELAY_ERROR:
status_str = "ERROR";
break;
default:
status_str = "UNKNOWN";
break;
}
printf("Relay %s status: %s\n", relay_url, status_str);
}
```
### List All Relays
**Function:** [`nostr_relay_pool_list_relays()`](nostr_core/core_relay_pool.c:960)
```c
int nostr_relay_pool_list_relays(
nostr_relay_pool_t* pool,
char*** relay_urls,
nostr_pool_relay_status_t** statuses);
```
**Example:**
```c
void print_all_relays(nostr_relay_pool_t* pool) {
char** relay_urls = NULL;
nostr_pool_relay_status_t* statuses = NULL;
int count = nostr_relay_pool_list_relays(pool, &relay_urls, &statuses);
if (count < 0) {
printf("Failed to list relays\n");
return;
}
if (count == 0) {
printf("No relays configured\n");
return;
}
printf("Configured relays (%d):\n", count);
for (int i = 0; i < count; i++) {
const char* status_str = (statuses[i] == NOSTR_POOL_RELAY_CONNECTED) ?
"CONNECTED" : "DISCONNECTED";
printf(" %s - %s\n", relay_urls[i], status_str);
// Free the duplicated URL string
free(relay_urls[i]);
}
// Free the arrays
free(relay_urls);
free(statuses);
}
```
### Get Relay Statistics
**Function:** [`nostr_relay_pool_get_relay_stats()`](nostr_core/core_relay_pool.c:992)
```c
const nostr_relay_stats_t* nostr_relay_pool_get_relay_stats(
nostr_relay_pool_t* pool,
const char* relay_url);
```
**Example:**
```c
void print_relay_stats(nostr_relay_pool_t* pool, const char* relay_url) {
const nostr_relay_stats_t* stats = nostr_relay_pool_get_relay_stats(pool, relay_url);
if (!stats) {
printf("No stats available for relay %s\n", relay_url);
return;
}
printf("Statistics for %s:\n", relay_url);
printf(" Connection attempts: %d\n", stats->connection_attempts);
printf(" Connection failures: %d\n", stats->connection_failures);
printf(" Events received: %d\n", stats->events_received);
printf(" Events published: %d\n", stats->events_published);
printf(" Events published OK: %d\n", stats->events_published_ok);
printf(" Events published failed: %d\n", stats->events_published_failed);
printf(" Query latency avg: %.2f ms\n", stats->query_latency_avg);
printf(" Query samples: %d\n", stats->query_samples);
printf(" Publish latency avg: %.2f ms\n", stats->publish_latency_avg);
printf(" Publish samples: %d\n", stats->publish_samples);
if (stats->last_event_time > 0) {
printf(" Last event: %ld seconds ago\n",
time(NULL) - stats->last_event_time);
}
}
```
### Reset Relay Statistics
**Function:** [`nostr_relay_pool_reset_relay_stats()`](nostr_core/core_relay_pool.c:1008)
```c
int nostr_relay_pool_reset_relay_stats(
nostr_relay_pool_t* pool,
const char* relay_url);
```
**Example:**
```c
void reset_stats_for_relay(nostr_relay_pool_t* pool, const char* relay_url) {
int result = nostr_relay_pool_reset_relay_stats(pool, relay_url);
if (result == NOSTR_SUCCESS) {
printf("Successfully reset statistics for %s\n", relay_url);
} else {
printf("Failed to reset statistics for %s\n", relay_url);
}
}
```
### Get Query Latency
**Function:** [`nostr_relay_pool_get_relay_query_latency()`](nostr_core/core_relay_pool.c:1045)
```c
double nostr_relay_pool_get_relay_query_latency(
nostr_relay_pool_t* pool,
const char* relay_url);
```
**Example:**
```c
void check_relay_performance(nostr_relay_pool_t* pool) {
const char* relays[] = {
"wss://relay.damus.io",
"wss://nos.lol",
"wss://relay.nostr.band"
};
printf("Relay performance comparison:\n");
for (int i = 0; i < 3; i++) {
double latency = nostr_relay_pool_get_relay_query_latency(pool, relays[i]);
if (latency >= 0) {
printf(" %s: %.2f ms average query latency\n", relays[i], latency);
} else {
printf(" %s: No latency data available\n", relays[i]);
}
}
}
```
## Complete Example Application
```c
#include "nostr_core.h"
#include "cjson/cJSON.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
// Global context for the example
typedef struct {
int event_count;
int max_events;
} app_context_t;
void on_text_note(cJSON* event, const char* relay_url, void* user_data) {
app_context_t* ctx = (app_context_t*)user_data;
cJSON* content = cJSON_GetObjectItem(event, "content");
if (content && cJSON_IsString(content)) {
printf("[%s] Note #%d: %s\n",
relay_url, ++ctx->event_count, cJSON_GetStringValue(content));
}
}
void on_subscription_complete(void* user_data) {
printf("All relays finished sending stored events\n");
}
int main() {
// Initialize pool
nostr_relay_pool_t* pool = nostr_relay_pool_create();
if (!pool) {
fprintf(stderr, "Failed to create relay pool\n");
return 1;
}
// Add relays
const char* relays[] = {
"wss://relay.damus.io",
"wss://nos.lol"
};
for (int i = 0; i < 2; i++) {
if (nostr_relay_pool_add_relay(pool, relays[i]) != NOSTR_SUCCESS) {
fprintf(stderr, "Failed to add relay: %s\n", relays[i]);
}
}
// Create filter for recent text notes
cJSON* filter = cJSON_CreateObject();
cJSON* kinds = cJSON_CreateArray();
cJSON_AddItemToArray(kinds, cJSON_CreateNumber(1));
cJSON_AddItemToObject(filter, "kinds", kinds);
cJSON_AddItemToObject(filter, "limit", cJSON_CreateNumber(10));
// Set up context
app_context_t ctx = {0, 10};
// Subscribe
nostr_pool_subscription_t* sub = nostr_relay_pool_subscribe(
pool, relays, 2, filter, on_text_note, on_subscription_complete, &ctx);
cJSON_Delete(filter);
if (!sub) {
fprintf(stderr, "Failed to create subscription\n");
nostr_relay_pool_destroy(pool);
return 1;
}
// Run event loop for 30 seconds
printf("Listening for events...\n");
nostr_relay_pool_run(pool, 30000);
// Print final stats
for (int i = 0; i < 2; i++) {
print_relay_stats(pool, relays[i]);
}
// Cleanup
nostr_pool_subscription_close(sub);
nostr_relay_pool_destroy(pool);
printf("Application finished. Received %d events total.\n", ctx.event_count);
return 0;
}
```
## Notes
- All functions are **not thread-safe**. Use from a single thread or add external synchronization.
- **Memory ownership**: The pool duplicates filters and URLs internally. Caller owns returned events and must free them.
- **Event deduplication** is applied pool-wide using a circular buffer of 1000 event IDs.
- **Ping functionality** is currently disabled in this build.
- **Reconnection** happens on-demand when sending, but active subscriptions are not automatically re-sent after reconnect.
- **Polling model**: You must drive the event loop via [`nostr_relay_pool_run()`](nostr_core/core_relay_pool.c:1192) or [`nostr_relay_pool_poll()`](nostr_core/core_relay_pool.c:1232) to receive events.

175
README.md
View File

@@ -1,46 +1,103 @@
# NOSTR Core Library
A comprehensive, production-ready C library for NOSTR protocol implementation with OpenSSL-based cryptography and extensive protocol support.
A C library for NOSTR protocol implementation. Work in progress.
[![Version](https://img.shields.io/badge/version-0.1.20-blue.svg)](VERSION)
[![Version](https://img.shields.io/badge/version-0.2.1-blue.svg)](VERSION)
[![License](https://img.shields.io/badge/license-MIT-green.svg)](#license)
[![Build Status](https://img.shields.io/badge/build-passing-brightgreen.svg)](#building)
## 🚀 Features
### Core Protocol Support
- **NIP-01**: Basic protocol flow - event creation, signing, and validation
- **NIP-04**: Encrypted direct messages (ECDH + AES-CBC + Base64)
- **NIP-05**: DNS-based internet identifier verification
- **NIP-06**: Key derivation from mnemonic (BIP39/BIP32 compliant)
- **NIP-11**: Relay information documents
- **NIP-13**: Proof of Work for events
- **NIP-44**: Versioned encrypted direct messages (ECDH + ChaCha20 + HMAC)
## 📋 NIP Implementation Status
### Cryptographic Features
- **OpenSSL-Based**: Production-grade cryptography with OpenSSL backend
- **Secp256k1**: Complete elliptic curve implementation bundled
- **BIP39**: Mnemonic phrase generation and validation
- **BIP32**: Hierarchical deterministic key derivation
- **ChaCha20**: Stream cipher for NIP-44 encryption
- **AES-CBC**: Block cipher for NIP-04 encryption
- **Schnorr Signatures**: BIP-340 compliant signing and verification
### Core Protocol NIPs
- [x] [NIP-01](nips/01.md) - Basic protocol flow - event creation, signing, and validation
- [ ] [NIP-02](nips/02.md) - Contact List and Petnames
- [ ] [NIP-03](nips/03.md) - OpenTimestamps Attestations for Events
- [x] [NIP-04](nips/04.md) - Encrypted Direct Messages (legacy)
- [x] [NIP-05](nips/05.md) - Mapping Nostr keys to DNS-based internet identifiers
- [x] [NIP-06](nips/06.md) - Basic key derivation from mnemonic seed phrase
- [ ] [NIP-07](nips/07.md) - `window.nostr` capability for web browsers
- [ ] [NIP-08](nips/08.md) - Handling Mentions
- [ ] [NIP-09](nips/09.md) - Event Deletion
- [ ] [NIP-10](nips/10.md) - Conventions for clients' use of `e` and `p` tags in text events
- [x] [NIP-11](nips/11.md) - Relay Information Document
- [ ] [NIP-12](nips/12.md) - Generic Tag Queries
- [x] [NIP-13](nips/13.md) - Proof of Work
- [ ] [NIP-14](nips/14.md) - Subject tag in text events
- [ ] [NIP-15](nips/15.md) - Nostr Marketplace (for resilient marketplaces)
- [ ] [NIP-16](nips/16.md) - Event Treatment
- [x] [NIP-17](nips/17.md) - Private Direct Messages
- [ ] [NIP-18](nips/18.md) - Reposts
- [x] [NIP-19](nips/19.md) - bech32-encoded entities
- [ ] [NIP-20](nips/20.md) - Command Results
- [x] [NIP-21](nips/21.md) - `nostr:` URI scheme
- [ ] [NIP-22](nips/22.md) - Event `created_at` Limits
- [ ] [NIP-23](nips/23.md) - Long-form Content
- [ ] [NIP-24](nips/24.md) - Extra metadata fields and tags
- [ ] [NIP-25](nips/25.md) - Reactions
- [ ] [NIP-26](nips/26.md) - Delegated Event Signing
- [ ] [NIP-27](nips/27.md) - Text Note References
- [ ] [NIP-28](nips/28.md) - Public Chat
- [ ] [NIP-29](nips/29.md) - Relay-based Groups
- [ ] [NIP-30](nips/30.md) - Custom Emoji
- [ ] [NIP-31](nips/31.md) - Dealing with Unknown Events
- [ ] [NIP-32](nips/32.md) - Labeling
- [ ] [NIP-33](nips/33.md) - Parameterized Replaceable Events
- [ ] [NIP-34](nips/34.md) - `git` stuff
- [ ] [NIP-35](nips/35.md) - Torrents
- [ ] [NIP-36](nips/36.md) - Sensitive Content / Content Warning
- [ ] [NIP-37](nips/37.md) - Draft Events
- [ ] [NIP-38](nips/38.md) - User Statuses
- [ ] [NIP-39](nips/39.md) - External Identities in Profiles
- [ ] [NIP-40](nips/40.md) - Expiration Timestamp
- [x] [NIP-42](nips/42.md) - Authentication of clients to relays
- [x] [NIP-44](nips/44.md) - Versioned Encryption
- [ ] [NIP-45](nips/45.md) - Counting results
- [ ] [NIP-46](nips/46.md) - Nostr Connect
- [ ] [NIP-47](nips/47.md) - Wallet Connect
- [ ] [NIP-48](nips/48.md) - Proxy Tags
- [ ] [NIP-49](nips/49.md) - Private Key Encryption
- [ ] [NIP-50](nips/50.md) - Search Capability
- [ ] [NIP-51](nips/51.md) - Lists
- [ ] [NIP-52](nips/52.md) - Calendar Events
- [ ] [NIP-53](nips/53.md) - Live Activities
- [ ] [NIP-54](nips/54.md) - Wiki
- [ ] [NIP-55](nips/55.md) - Android Signer Application
- [ ] [NIP-56](nips/56.md) - Reporting
- [ ] [NIP-57](nips/57.md) - Lightning Zaps
- [ ] [NIP-58](nips/58.md) - Badges
- [x] [NIP-59](nips/59.md) - Gift Wrap
- [ ] [NIP-60](nips/60.md) - Cashu Wallet
- [ ] [NIP-61](nips/61.md) - Nutzaps
- [ ] [NIP-62](nips/62.md) - Log events
- [ ] [NIP-64](nips/64.md) - Chess (PGN)
- [ ] [NIP-65](nips/65.md) - Relay List Metadata
- [ ] [NIP-66](nips/66.md) - Relay Monitor
- [ ] [NIP-68](nips/68.md) - Web badges
- [ ] [NIP-69](nips/69.md) - Peer-to-peer Order events
- [ ] [NIP-70](nips/70.md) - Protected Events
- [ ] [NIP-71](nips/71.md) - Video Events
- [ ] [NIP-72](nips/72.md) - Moderated Communities
- [ ] [NIP-73](nips/73.md) - External Content IDs
- [ ] [NIP-75](nips/75.md) - Zap Goals
- [ ] [NIP-77](nips/77.md) - Arbitrary custom app data
- [ ] [NIP-78](nips/78.md) - Application-specific data
- [ ] [NIP-84](nips/84.md) - Highlights
- [ ] [NIP-86](nips/86.md) - Relay Management API
- [ ] [NIP-87](nips/87.md) - Relay List Recommendations
- [ ] [NIP-88](nips/88.md) - Stella: A Stellar Relay
- [ ] [NIP-89](nips/89.md) - Recommended Application Handlers
- [ ] [NIP-90](nips/90.md) - Data Vending Machines
- [ ] [NIP-92](nips/92.md) - Media Attachments
- [ ] [NIP-94](nips/94.md) - File Metadata
- [ ] [NIP-96](nips/96.md) - HTTP File Storage Integration
- [ ] [NIP-98](nips/98.md) - HTTP Auth
- [ ] [NIP-99](nips/99.md) - Classified Listings
### Networking & Relay Support
- **Multi-Relay Queries**: Synchronous querying with progress callbacks
- **Relay Pools**: Asynchronous connection management with statistics
- **OpenSSL WebSocket Communication**: Full relay protocol support with TLS
- **NIP-05 Identifier Verification**: DNS-based identity resolution
- **NIP-11 Relay Information**: Automatic relay capability discovery
- **Event Deduplication**: Automatic handling of duplicate events across relays
- **Connection Management**: Automatic reconnection and error handling
**Legend:** ✅ Fully Implemented | ⚠️ Partial Implementation | ❌ Not Implemented
**Implementation Summary:** 12 of 96+ NIPs fully implemented (12.5%)
### Developer Experience
- **System Dependencies**: Uses system-installed OpenSSL, curl, and secp256k1 libraries
- **Thread-Safe**: Core cryptographic functions are stateless
- **Cross-Platform**: Builds on Linux, macOS, Windows
- **Comprehensive Examples**: Ready-to-run demonstration programs
- **Automatic Versioning**: Git-tag based version management
## 📦 Quick Start
@@ -245,20 +302,32 @@ publish_result_t* synchronous_publish_event_with_progress(const char** relay_url
### Relay Pools (Asynchronous)
```c
// Create and manage relay pool
nostr_relay_pool_t* nostr_relay_pool_create(void);
// Create and manage relay pool with reconnection
nostr_pool_reconnect_config_t* config = nostr_pool_reconnect_config_default();
nostr_relay_pool_t* nostr_relay_pool_create(nostr_pool_reconnect_config_t* config);
int nostr_relay_pool_add_relay(nostr_relay_pool_t* pool, const char* relay_url);
void nostr_relay_pool_destroy(nostr_relay_pool_t* pool);
// Subscribe to events
// Subscribe to events (with auto-reconnection)
nostr_pool_subscription_t* nostr_relay_pool_subscribe(
nostr_relay_pool_t* pool, const char** relay_urls, int relay_count, cJSON* filter,
void (*on_event)(cJSON* event, const char* relay_url, void* user_data),
void (*on_eose)(void* user_data), void* user_data);
void (*on_eose)(void* user_data), void* user_data, int close_on_eose);
// Run event loop
// Run event loop (handles reconnection automatically)
int nostr_relay_pool_run(nostr_relay_pool_t* pool, int timeout_ms);
int nostr_relay_pool_poll(nostr_relay_pool_t* pool, int timeout_ms);
// Reconnection configuration
typedef struct {
int enable_auto_reconnect; // Enable automatic reconnection
int max_reconnect_attempts; // Maximum retry attempts
int initial_reconnect_delay_ms; // Initial delay between attempts
int max_reconnect_delay_ms; // Maximum delay cap
int reconnect_backoff_multiplier; // Exponential backoff factor
int ping_interval_seconds; // Health check ping interval
int pong_timeout_seconds; // Pong response timeout
} nostr_pool_reconnect_config_t;
```
### NIP-05 Identifier Verification
@@ -313,6 +382,9 @@ The library includes extensive tests:
# Individual test categories
cd tests && make test
# Interactive relay pool testing
cd tests && ./pool_test
```
**Test Categories:**
@@ -326,6 +398,7 @@ cd tests && make test
- **Relay Communication**: `relay_pool_test`, `sync_test`
- **HTTP/WebSocket**: `http_test`, `wss_test`
- **Proof of Work**: `test_pow_loop`
- **Interactive Pool Testing**: `pool_test` (menu-driven interface with reconnection testing)
## 🏗️ Integration
@@ -434,7 +507,7 @@ make arm64
## 📈 Version History
Current version: **0.1.20**
Current version: **0.2.1**
The library uses automatic semantic versioning based on Git tags. Each build increments the patch version automatically.
@@ -442,13 +515,15 @@ The library uses automatic semantic versioning based on Git tags. Each build inc
- **OpenSSL Migration**: Transitioned from mbedTLS to OpenSSL for improved compatibility
- **NIP-05 Support**: DNS-based internet identifier verification
- **NIP-11 Support**: Relay information document fetching and parsing
- **NIP-19 Support**: Bech32-encoded entities (nsec/npub)
- **Enhanced WebSocket**: OpenSSL-based TLS WebSocket communication
- **Production Ready**: Comprehensive test suite and error handling
- **Comprehensive Testing**: Extensive test suite and error handling
**Version Timeline:**
- `v0.2.x` - Current development releases with enhanced NIP support
- `v0.1.x` - Initial development releases
- Focus on core protocol implementation and OpenSSL-based crypto
- Full NIP-01, NIP-04, NIP-05, NIP-06, NIP-11, NIP-13, NIP-44 support
- Full NIP-01, NIP-04, NIP-05, NIP-06, NIP-11, NIP-13, NIP-17, NIP-19, NIP-42, NIP-44, NIP-59 support
## 🐛 Troubleshooting
@@ -456,16 +531,24 @@ The library uses automatic semantic versioning based on Git tags. Each build inc
**Build fails with secp256k1 errors:**
```bash
# Install secp256k1 with Schnorr support
sudo apt install libsecp256k1-dev # Ubuntu/Debian
# or
sudo yum install libsecp256k1-devel # CentOS/RHEL
# or
brew install secp256k1 # macOS
# If still failing, build from source with Schnorr support:
git clone https://github.com/bitcoin-core/secp256k1.git
cd secp256k1
./autogen.sh
./configure --enable-module-schnorrsig --enable-module-ecdh
make
cd ..
./build.sh lib
sudo make install
```
**Library too large:**
The x64 library is intentionally large (~15MB) because it includes all secp256k1 cryptographic functions and OpenSSL for complete self-containment. The ARM64 library is smaller (~2.4MB) as it links against system OpenSSL.
**Library size:**
The library is small (~500KB) as it links against system libraries (secp256k1, OpenSSL, curl) rather than including them statically. This keeps the binary size manageable while maintaining full functionality.
**Linking errors:**
Make sure to include the math library:
@@ -496,4 +579,4 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
**Built with ❤️ for the decentralized web**
*OpenSSL-based • Minimal dependencies • Production ready*
*OpenSSL-based • Minimal dependencies • Work in progress*

View File

@@ -1 +1 @@
0.1.33
0.4.8

View File

@@ -58,6 +58,7 @@ FORCE_NIPS=""
VERBOSE=false
HELP=false
BUILD_TESTS=false
BUILD_EXAMPLES=false
NO_COLOR_FLAG=false
# Parse command line arguments
@@ -83,6 +84,10 @@ while [[ $# -gt 0 ]]; do
BUILD_TESTS=true
shift
;;
--examples|-e)
BUILD_EXAMPLES=true
shift
;;
--no-color)
NO_COLOR_FLAG=true
shift
@@ -119,6 +124,7 @@ if [ "$HELP" = true ]; then
echo " --nips=1,5,6,19 Force specific NIPs (comma-separated)"
echo " --nips=all Include all available NIPs"
echo " --tests, -t Build all test programs in tests/ directory"
echo " --examples, -e Build all example programs in examples/ directory"
echo " --verbose, -v Verbose output"
echo " --no-color Disable colored output"
echo " --help, -h Show this help"
@@ -134,8 +140,12 @@ if [ "$HELP" = true ]; then
echo " 006 - Key derivation from mnemonic"
echo " 011 - Relay information document"
echo " 013 - Proof of Work"
echo " 017 - Private Direct Messages"
echo " 019 - Bech32 encoding (nsec/npub)"
echo " 021 - nostr: URI scheme"
echo " 042 - Authentication of clients to relays"
echo " 044 - Encryption (modern)"
echo " 059 - Gift Wrap"
echo ""
echo "Examples:"
echo " $0 # Auto-detect NIPs, build for current arch"
@@ -166,7 +176,7 @@ if [ "$CURRENT_DIR" != "nostr_core_lib" ]; then
echo " cd nostr_core_lib"
echo " ./build.sh"
echo " cd .."
echo " gcc your_app.c nostr_core_lib/libnostr_core_x64.a -lz -ldl -lpthread -lm -lssl -lcrypto -lcurl -o your_app"
echo " gcc your_app.c nostr_core_lib/libnostr_core_x64.a -lz -ldl -lpthread -lm -lssl -lcrypto -lcurl -lsecp256k1 -o your_app"
echo ""
exit 1
fi
@@ -184,7 +194,7 @@ print_info "Auto-detecting needed NIPs from your source code..."
NEEDED_NIPS=""
if [ -n "$FORCE_NIPS" ]; then
if [ "$FORCE_NIPS" = "all" ]; then
NEEDED_NIPS="001 004 005 006 011 013 019 044"
NEEDED_NIPS="001 004 005 006 011 013 017 019 042 044 059"
print_info "Forced: Building all available NIPs"
else
# Convert comma-separated list to space-separated with 3-digit format
@@ -203,7 +213,7 @@ else
# Check for nostr_core.h (includes everything)
if grep -q '#include[[:space:]]*["\<]nostr_core\.h["\>]' *.c 2>/dev/null; then
print_info "Found #include \"nostr_core.h\" - building all NIPs"
NEEDED_NIPS="001 004 005 006 011 013 019 044"
NEEDED_NIPS="001 004 005 006 011 013 019 042 044"
elif [ -n "$DETECTED" ]; then
NEEDED_NIPS="$DETECTED"
print_success "Auto-detected NIPs: $(echo $NEEDED_NIPS | tr ' ' ',')"
@@ -219,10 +229,10 @@ else
fi
fi
# If building tests, include all NIPs to ensure test compatibility
if [ "$BUILD_TESTS" = true ] && [ -z "$FORCE_NIPS" ]; then
NEEDED_NIPS="001 004 005 006 011 013 019 044"
print_info "Building tests - including all available NIPs for test compatibility"
# If building tests or examples, include all NIPs to ensure compatibility
if ([ "$BUILD_TESTS" = true ] || [ "$BUILD_EXAMPLES" = true ]) && [ -z "$FORCE_NIPS" ]; then
NEEDED_NIPS="001 004 005 006 011 013 017 019 021 042 044 059"
print_info "Building tests/examples - including all available NIPs for compatibility"
fi
# Ensure NIP-001 is always included (required for core functionality)
@@ -484,13 +494,15 @@ detect_system_curl
###########################################################################################
SOURCES="nostr_core/crypto/nostr_secp256k1.c"
SOURCES="$SOURCES nostr_core/crypto/nostr_aes.c"
SOURCES="$SOURCES nostr_core/crypto/nostr_aes.c"
SOURCES="$SOURCES nostr_core/crypto/nostr_chacha20.c"
SOURCES="$SOURCES cjson/cJSON.c"
SOURCES="$SOURCES nostr_core/utils.c"
SOURCES="$SOURCES nostr_core/nostr_common.c"
SOURCES="$SOURCES nostr_core/core_relays.c"
SOURCES="$SOURCES nostr_core/core_relay_pool.c"
SOURCES="$SOURCES nostr_websocket/nostr_websocket_openssl.c"
SOURCES="$SOURCES nostr_core/request_validator.c"
NIP_DESCRIPTIONS=""
@@ -505,8 +517,12 @@ for nip in $NEEDED_NIPS; do
006) NIP_DESCRIPTIONS="$NIP_DESCRIPTIONS NIP-006(Keys)" ;;
011) NIP_DESCRIPTIONS="$NIP_DESCRIPTIONS NIP-011(Relay-Info)" ;;
013) NIP_DESCRIPTIONS="$NIP_DESCRIPTIONS NIP-013(PoW)" ;;
017) NIP_DESCRIPTIONS="$NIP_DESCRIPTIONS NIP-017(DMs)" ;;
019) NIP_DESCRIPTIONS="$NIP_DESCRIPTIONS NIP-019(Bech32)" ;;
021) NIP_DESCRIPTIONS="$NIP_DESCRIPTIONS NIP-021(URI)" ;;
042) NIP_DESCRIPTIONS="$NIP_DESCRIPTIONS NIP-042(Auth)" ;;
044) NIP_DESCRIPTIONS="$NIP_DESCRIPTIONS NIP-044(Encrypt)" ;;
059) NIP_DESCRIPTIONS="$NIP_DESCRIPTIONS NIP-059(Gift-Wrap)" ;;
esac
else
print_warning "NIP file not found: $NIP_FILE - skipping"
@@ -663,7 +679,53 @@ if [ $AR_RESULT -eq 0 ]; then
fi
echo ""
fi
# Build examples if requested
if [ "$BUILD_EXAMPLES" = true ]; then
print_info "Scanning examples/ directory for example programs..."
if [ ! -d "examples" ]; then
print_warning "examples/ directory not found - skipping example builds"
else
EXAMPLE_COUNT=0
SUCCESS_COUNT=0
# Find all .c files in examples/ directory (not subdirectories)
while IFS= read -r -d '' example_file; do
EXAMPLE_COUNT=$((EXAMPLE_COUNT + 1))
example_name=$(basename "$example_file" .c)
example_exe="examples/$example_name"
print_info "Building example: $example_name"
# Example compilation with system libraries
LINK_FLAGS="-lz -ldl -lpthread -lm $SYSTEM_LIBS"
if [ "$VERBOSE" = true ]; then
print_info " Command: $CC $CFLAGS $INCLUDES \"$example_file\" -o \"$example_exe\" ./$OUTPUT $LINK_FLAGS"
fi
if $CC $CFLAGS $INCLUDES "$example_file" -o "$example_exe" "./$OUTPUT" $LINK_FLAGS; then
SUCCESS_COUNT=$((SUCCESS_COUNT + 1))
print_success "Built $example_name"
if [ "$VERBOSE" = true ]; then
print_info " Executable: $example_exe"
fi
else
print_error " Failed to build: $example_name"
fi
done < <(find examples/ -maxdepth 1 -name "*.c" -type f -print0)
if [ $EXAMPLE_COUNT -eq 0 ]; then
print_warning "No .c files found in examples/ directory"
else
print_success "Built $SUCCESS_COUNT/$EXAMPLE_COUNT example programs"
fi
fi
echo ""
fi
echo "Usage in your project:"
echo " gcc your_app.c $OUTPUT -lz -ldl -lpthread -lm $SYSTEM_LIBS -o your_app"
echo ""

View File

@@ -1,670 +0,0 @@
#!/bin/bash
# NOSTR Core Library - Customer Build Script with Auto-Detection
# Automatically detects which NIPs are needed based on #include statements
set -e # Exit on error
# Color constants
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
BOLD='\033[1m'
RESET='\033[0m'
# Detect if we should use colors (terminal output and not piped)
USE_COLORS=true
if [ ! -t 1 ] || [ "$NO_COLOR" = "1" ]; then
USE_COLORS=false
fi
# Function to print output with colors
print_info() {
if [ "$USE_COLORS" = true ]; then
echo -e "${BLUE}[INFO]${RESET} $1"
else
echo "[INFO] $1"
fi
}
print_success() {
if [ "$USE_COLORS" = true ]; then
echo -e "${GREEN}${BOLD}[SUCCESS]${RESET} $1"
else
echo "[SUCCESS] $1"
fi
}
print_warning() {
if [ "$USE_COLORS" = true ]; then
echo -e "${YELLOW}[WARNING]${RESET} $1"
else
echo "[WARNING] $1"
fi
}
print_error() {
if [ "$USE_COLORS" = true ]; then
echo -e "${RED}${BOLD}[ERROR]${RESET} $1"
else
echo "[ERROR] $1"
fi
}
# Default values
ARCHITECTURE=""
FORCE_NIPS=""
VERBOSE=false
HELP=false
BUILD_TESTS=false
NO_COLOR_FLAG=false
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
x64|x86_64|amd64)
ARCHITECTURE="x64"
shift
;;
arm64|aarch64)
ARCHITECTURE="arm64"
shift
;;
--nips=*)
FORCE_NIPS="${1#*=}"
shift
;;
--verbose|-v)
VERBOSE=true
shift
;;
--tests|-t)
BUILD_TESTS=true
shift
;;
--no-color)
NO_COLOR_FLAG=true
shift
;;
--help|-h)
HELP=true
shift
;;
*)
print_error "Unknown argument: $1"
HELP=true
shift
;;
esac
done
# Apply no-color flag
if [ "$NO_COLOR_FLAG" = true ]; then
USE_COLORS=false
fi
# Show help
if [ "$HELP" = true ]; then
echo "NOSTR Core Library - Customer Build Script"
echo ""
echo "Usage: $0 [architecture] [options]"
echo ""
echo "Architectures:"
echo " x64, x86_64, amd64 Build for x86-64 architecture"
echo " arm64, aarch64 Build for ARM64 architecture"
echo " (default) Build for current architecture"
echo ""
echo "Options:"
echo " --nips=1,5,6,19 Force specific NIPs (comma-separated)"
echo " --nips=all Include all available NIPs"
echo " --tests, -t Build all test programs in tests/ directory"
echo " --verbose, -v Verbose output"
echo " --no-color Disable colored output"
echo " --help, -h Show this help"
echo ""
echo "Auto-Detection:"
echo " Script automatically scans your .c files for #include statements"
echo " like #include \"nip001.h\" and compiles only needed NIPs."
echo ""
echo "Available NIPs:"
echo " 001 - Basic Protocol (event creation, signing)"
echo " 004 - Encryption (legacy)"
echo " 005 - DNS-based identifiers"
echo " 006 - Key derivation from mnemonic"
echo " 011 - Relay information document"
echo " 013 - Proof of Work"
echo " 019 - Bech32 encoding (nsec/npub)"
echo " 044 - Encryption (modern)"
echo ""
echo "Examples:"
echo " $0 # Auto-detect NIPs, build for current arch"
echo " $0 x64 # Auto-detect NIPs, build for x64"
echo " $0 --nips=1,6,19 # Force NIPs 1,6,19 only"
echo " $0 arm64 --nips=all # Build all NIPs for ARM64"
echo " $0 -t # Build library and all tests"
exit 0
fi
print_info "NOSTR Core Library - Customer Build Script"
# Check if we're running from the correct directory
CURRENT_DIR=$(basename "$(pwd)")
if [ "$CURRENT_DIR" != "nostr_core_lib" ]; then
print_error "Build script must be run from the nostr_core_lib directory"
echo ""
echo "Current directory: $CURRENT_DIR"
echo "Expected directory: nostr_core_lib"
echo ""
echo "Please change to the nostr_core_lib directory first, then run the build script."
echo ""
echo "Correct usage:"
echo " cd nostr_core_lib"
echo " ./build.sh"
echo ""
echo "Or if nostr_core_lib is in your project directory:"
echo " cd nostr_core_lib"
echo " ./build.sh"
echo " cd .."
echo " gcc your_app.c nostr_core_lib/libnostr_core_x64.a -lz -ldl -lpthread -lm -o your_app"
echo ""
exit 1
fi
print_info "Auto-detecting needed NIPs from your source code..."
###########################################################################################
###########################################################################################
############ AUTODETECT NIPS FROM SOURCE FILES
###########################################################################################
###########################################################################################
NEEDED_NIPS=""
if [ -n "$FORCE_NIPS" ]; then
if [ "$FORCE_NIPS" = "all" ]; then
NEEDED_NIPS="001 004 005 006 011 013 019 044"
print_info "Forced: Building all available NIPs"
else
# Convert comma-separated list to space-separated with 3-digit format
NEEDED_NIPS=$(echo "$FORCE_NIPS" | tr ',' ' ' | sed 's/\b\([0-9]\)\b/00\1/g' | sed 's/\b\([0-9][0-9]\)\b/0\1/g')
print_info "Forced NIPs: $NEEDED_NIPS"
fi
else
# Auto-detect from .c files in current directory
if ls *.c >/dev/null 2>&1; then
# Scan for nip*.h includes (with or without nostr_core/ prefix)
DETECTED=$(grep -h '#include[[:space:]]*["\<]\(nostr_core/\)\?nip[0-9][0-9][0-9]\.h["\>]' *.c 2>/dev/null | \
sed 's/.*nip0*\([0-9]*\)\.h.*/\1/' | \
sort -u | \
sed 's/\b\([0-9]\)\b/00\1/g' | sed 's/\b\([0-9][0-9]\)\b/0\1/g')
# Check for nostr_core.h (includes everything)
if grep -q '#include[[:space:]]*["\<]nostr_core\.h["\>]' *.c 2>/dev/null; then
print_info "Found #include \"nostr_core.h\" - building all NIPs"
NEEDED_NIPS="001 004 005 006 011 013 019 044"
elif [ -n "$DETECTED" ]; then
NEEDED_NIPS="$DETECTED"
print_success "Auto-detected NIPs: $(echo $NEEDED_NIPS | tr ' ' ',')"
else
print_warning "No NIP includes detected in *.c files"
print_info "Defaulting to basic NIPs: 001, 006, 019"
NEEDED_NIPS="001 006 019"
fi
else
print_warning "No .c files found in current directory"
print_info "Defaulting to basic NIPs: 001, 006, 019"
NEEDED_NIPS="001 006 019"
fi
fi
# If building tests, include all NIPs to ensure test compatibility
if [ "$BUILD_TESTS" = true ] && [ -z "$FORCE_NIPS" ]; then
NEEDED_NIPS="001 004 005 006 011 013 019 044"
print_info "Building tests - including all available NIPs for test compatibility"
fi
# Ensure NIP-001 is always included (required for core functionality)
if ! echo "$NEEDED_NIPS" | grep -q "001"; then
NEEDED_NIPS="001 $NEEDED_NIPS"
print_info "Added NIP-001 (required for core functionality)"
fi
###########################################################################################
###########################################################################################
############ AUTODETECT SYSTEM ARCHITECTURE
###########################################################################################
###########################################################################################
# Determine architecture
if [ -z "$ARCHITECTURE" ]; then
ARCH=$(uname -m)
case $ARCH in
x86_64|amd64)
ARCHITECTURE="x64"
;;
aarch64|arm64)
ARCHITECTURE="arm64"
;;
*)
ARCHITECTURE="x64" # Default fallback
print_warning "Unknown architecture '$ARCH', defaulting to x64"
;;
esac
fi
print_info "Target architecture: $ARCHITECTURE"
# Set compiler based on architecture
case $ARCHITECTURE in
x64)
CC="gcc"
ARCH_SUFFIX="x64"
;;
arm64)
CC="aarch64-linux-gnu-gcc"
ARCH_SUFFIX="arm64"
;;
*)
print_error "Unsupported architecture: $ARCHITECTURE"
exit 1
;;
esac
# Check if compiler exists
if ! command -v $CC &> /dev/null; then
print_error "Compiler $CC not found"
if [ "$ARCHITECTURE" = "arm64" ]; then
print_info "Install ARM64 cross-compiler: sudo apt install gcc-aarch64-linux-gnu"
fi
exit 1
fi
###########################################################################################
###########################################################################################
############ CHECK AND BUILD DEPENDENCIES BASED ON NEEDED NIPS
###########################################################################################
###########################################################################################
print_info "Checking dependencies based on needed NIPs..."
# Set secp256k1 library path based on architecture
case $ARCHITECTURE in
x64)
SECP256K1_LIB="secp256k1/.libs/libsecp256k1.a"
;;
arm64)
SECP256K1_LIB="secp256k1/.libs/libsecp256k1_arm64.a"
;;
esac
# Determine which dependencies are needed based on NIPs
NEED_SECP256K1=false
NEED_OPENSSL=false
NEED_CURL=false
# secp256k1 is always needed (core cryptography for NIP-001)
NEED_SECP256K1=true
# Check if network-dependent NIPs are included
NETWORK_NIPS="005 011" # NIP-005 (DNS), NIP-011 (Relay info)
for nip in $NEEDED_NIPS; do
case $nip in
005|011)
NEED_CURL=true
print_info "NIP-$nip requires HTTP functionality - curl needed"
;;
esac
done
# Check if WebSocket functionality is needed (always included currently)
# Since nostr_websocket/nostr_websocket_openssl.c is always compiled
NEED_OPENSSL=true
NEED_CURL=true
print_info "WebSocket functionality enabled - OpenSSL and curl needed"
if [ "$VERBOSE" = true ]; then
print_info "Dependency requirements:"
[ "$NEED_SECP256K1" = true ] && echo " ✓ secp256k1 (core crypto)"
[ "$NEED_OPENSSL" = true ] && echo " ✓ OpenSSL (TLS/WebSocket)"
[ "$NEED_CURL" = true ] && echo " ✓ curl (HTTP requests)"
fi
# Function to build secp256k1 if needed and missing
build_secp256k1() {
if [ "$NEED_SECP256K1" != true ]; then
return 0
fi
if [ -f "$SECP256K1_LIB" ]; then
if [ "$VERBOSE" = true ]; then
print_success "secp256k1 already available"
fi
return 0
fi
print_info "Building secp256k1..."
if [ ! -d "secp256k1" ]; then
print_error "secp256k1 source directory not found"
exit 1
fi
# Force clean build if .libs exists but no library
if [ -d "secp256k1/.libs" ] && [ ! -f "$SECP256K1_LIB" ]; then
print_info "Cleaning previous secp256k1 build..."
(cd secp256k1 && make clean) || true
fi
(cd secp256k1 && \
./autogen.sh && \
./configure --enable-static --disable-shared --enable-module-recovery && \
make clean && \
make -j$(nproc)) || { print_error "Failed to build secp256k1"; exit 1; }
# Verify the library was actually created
if [ ! -f "$SECP256K1_LIB" ]; then
print_error "secp256k1 library not created: $SECP256K1_LIB"
print_error "Check if secp256k1 source files are present"
if [ "$VERBOSE" = true ]; then
print_info "Checking secp256k1 directory contents:"
ls -la secp256k1/.libs/ 2>/dev/null || print_warning "No .libs directory found"
ls -la secp256k1/src/ 2>/dev/null || print_warning "No src directory found"
fi
exit 1
fi
print_success "secp256k1 built successfully"
}
# Function to build OpenSSL if needed and missing
build_openssl() {
if [ "$NEED_OPENSSL" != true ]; then
return 0
fi
if [ -f "openssl-install/lib64/libssl.a" ] && [ -f "openssl-install/lib64/libcrypto.a" ]; then
if [ "$VERBOSE" = true ]; then
print_success "OpenSSL already available"
fi
return 0
fi
print_info "Building OpenSSL (this may take 5-10 minutes)..."
if [ ! -d "openssl-3.4.2" ]; then
print_error "openssl-3.4.2 source directory not found"
exit 1
fi
(cd openssl-3.4.2 && \
./Configure linux-x86_64 no-shared --prefix="$(pwd)/../openssl-install" && \
make -j$(nproc) && \
make install) || { print_error "Failed to build OpenSSL"; exit 1; }
print_success "OpenSSL built successfully"
}
# Function to build curl if needed and missing
build_curl() {
if [ "$NEED_CURL" != true ]; then
return 0
fi
if [ -f "curl-install/lib/libcurl.a" ]; then
if [ "$VERBOSE" = true ]; then
print_success "curl already available"
fi
return 0
fi
print_info "Building curl..."
if [ ! -d "curl-8.15.0" ]; then
print_error "curl-8.15.0 source directory not found"
exit 1
fi
(cd curl-8.15.0 && \
./configure --disable-shared --enable-static \
--with-openssl="$(pwd)/../openssl-install" \
--without-libpsl --without-brotli \
--disable-ldap --disable-ldaps --disable-rtsp --disable-proxy \
--disable-dict --disable-telnet --disable-tftp --disable-pop3 \
--disable-imap --disable-smb --disable-smtp --disable-gopher \
--disable-manual \
--prefix="$(pwd)/../curl-install" && \
make -j$(nproc) && \
make install) || { print_error "Failed to build curl"; exit 1; }
print_success "curl built successfully"
}
# Build only the needed dependencies
build_secp256k1
build_openssl
build_curl
###########################################################################################
###########################################################################################
############ ADD CORE DEPENDENCIES THAT NEED TO BE BUILT TO THE $SOURCES VARIABLE
###########################################################################################
###########################################################################################
SOURCES="nostr_core/crypto/nostr_secp256k1.c"
SOURCES="$SOURCES nostr_core/crypto/nostr_aes.c"
SOURCES="$SOURCES nostr_core/crypto/nostr_chacha20.c"
SOURCES="$SOURCES cjson/cJSON.c"
SOURCES="$SOURCES nostr_core/utils.c"
SOURCES="$SOURCES nostr_core/nostr_common.c"
SOURCES="$SOURCES nostr_core/core_relays.c"
SOURCES="$SOURCES nostr_websocket/nostr_websocket_openssl.c"
NIP_DESCRIPTIONS=""
for nip in $NEEDED_NIPS; do
NIP_FILE="nostr_core/nip${nip}.c"
if [ -f "$NIP_FILE" ]; then
SOURCES="$SOURCES $NIP_FILE"
case $nip in
001) NIP_DESCRIPTIONS="$NIP_DESCRIPTIONS NIP-001(Basic)" ;;
004) NIP_DESCRIPTIONS="$NIP_DESCRIPTIONS NIP-004(Encrypt)" ;;
005) NIP_DESCRIPTIONS="$NIP_DESCRIPTIONS NIP-005(DNS)" ;;
006) NIP_DESCRIPTIONS="$NIP_DESCRIPTIONS NIP-006(Keys)" ;;
011) NIP_DESCRIPTIONS="$NIP_DESCRIPTIONS NIP-011(Relay-Info)" ;;
013) NIP_DESCRIPTIONS="$NIP_DESCRIPTIONS NIP-013(PoW)" ;;
019) NIP_DESCRIPTIONS="$NIP_DESCRIPTIONS NIP-019(Bech32)" ;;
044) NIP_DESCRIPTIONS="$NIP_DESCRIPTIONS NIP-044(Encrypt)" ;;
esac
else
print_warning "NIP file not found: $NIP_FILE - skipping"
fi
done
# Build flags
CFLAGS="-Wall -Wextra -std=c99 -fPIC -O2"
CFLAGS="$CFLAGS -DENABLE_FILE_LOGGING -DENABLE_WEBSOCKET_LOGGING -DENABLE_DEBUG_LOGGING"
INCLUDES="-I. -Inostr_core -Inostr_core/crypto -Icjson -Isecp256k1/include -Inostr_websocket"
INCLUDES="$INCLUDES -I./openssl-install/include -I./curl-install/include"
# Static libraries
STATIC_LIBS="./openssl-install/lib64/libssl.a ./openssl-install/lib64/libcrypto.a"
STATIC_LIBS="$STATIC_LIBS ./curl-install/lib/libcurl.a"
# Output library name
OUTPUT="libnostr_core_${ARCH_SUFFIX}.a"
print_info "Compiling with: $CC"
print_info "Including:$NIP_DESCRIPTIONS"
if [ "$VERBOSE" = true ]; then
print_info "Sources: $SOURCES"
print_info "Flags: $CFLAGS $INCLUDES"
fi
###########################################################################################
###########################################################################################
############ COMPILE EACH SOURCE FROM $SOURCES INTO A .o FILE
###########################################################################################
###########################################################################################
OBJECTS=""
for source in $SOURCES; do
if [ -f "$source" ]; then
obj_name=$(basename "$source" .c).${ARCH_SUFFIX}.o
OBJECTS="$OBJECTS $obj_name"
if [ "$VERBOSE" = true ]; then
print_info "Compiling: $source -> $obj_name"
fi
#################################################
# THE ACTUAL COMMAND TO COMPILE .c FILES
#################################################
$CC $CFLAGS $INCLUDES -c "$source" -o "$obj_name"
if [ $? -ne 0 ]; then
print_error "Failed to compile $source"
exit 1
fi
else
print_error "Source file not found: $source"
exit 1
fi
done
###########################################################################################
###########################################################################################
############ CREATE THE FINAL STATIC LIBRARY FOR THE PROJECT: libnostr_core_XX.a
############ BY LINKING IN ALL OUR .o FILES THAT ARE REQUESTED TO BE ADDED.
###########################################################################################
###########################################################################################
print_info "Creating self-contained static library: $OUTPUT"
# Store the build directory to ensure correct paths when extracting from subdirectories
BUILD_DIR=$(pwd)
# Create temporary directories for extracting objects
TMP_SECP256K1=".tmp_secp256k1_$$"
TMP_OPENSSL=".tmp_openssl_$$"
TMP_CURL=".tmp_curl_$$"
mkdir -p "$TMP_SECP256K1" "$TMP_OPENSSL" "$TMP_CURL"
# Extract secp256k1 objects (if library exists)
SECP256K1_OBJECTS=""
if [ -f "$SECP256K1_LIB" ]; then
if [ "$VERBOSE" = true ]; then
print_info "Extracting secp256k1 objects..."
fi
(cd "$TMP_SECP256K1" && ar x "$BUILD_DIR/$SECP256K1_LIB")
SECP256K1_OBJECTS="$TMP_SECP256K1/*.o"
else
print_warning "secp256k1 library not found: $SECP256K1_LIB - skipping secp256k1 objects"
fi
# Extract OpenSSL objects
if [ "$VERBOSE" = true ]; then
print_info "Extracting OpenSSL objects..."
fi
(cd "$TMP_OPENSSL" && ar x "$BUILD_DIR/openssl-install/lib64/libssl.a")
(cd "$TMP_OPENSSL" && ar x "$BUILD_DIR/openssl-install/lib64/libcrypto.a")
# Extract curl objects
if [ "$VERBOSE" = true ]; then
print_info "Extracting curl objects..."
fi
(cd "$TMP_CURL" && ar x "$BUILD_DIR/curl-install/lib/libcurl.a")
# Combine all objects into final library
if [ "$VERBOSE" = true ]; then
print_info "Combining all objects into self-contained library..."
fi
#########################################################
### THE ACTUAL COMMAND TO LINK .o FILES INTO A .a FILE
#########################################################
if [ -n "$SECP256K1_OBJECTS" ]; then
ar rcs "$OUTPUT" $OBJECTS $SECP256K1_OBJECTS "$TMP_OPENSSL"/*.o "$TMP_CURL"/*.o
else
ar rcs "$OUTPUT" $OBJECTS "$TMP_OPENSSL"/*.o "$TMP_CURL"/*.o
fi
AR_RESULT=$?
# Cleanup temporary directories
rm -rf "$TMP_SECP256K1" "$TMP_OPENSSL" "$TMP_CURL"
###########################################################################################
###########################################################################################
############ IF THE LINKING OCCURED SUCCESSFULLY, BUILD THE TEST FILE EXECUTABLES
############ BY LINKING IN ALL OUR .o FILES THAT ARE REQUESTED TO BE ADDED.
###########################################################################################
###########################################################################################
if [ $AR_RESULT -eq 0 ]; then
# Cleanup object files
rm -f $OBJECTS
# Show library info
size_kb=$(du -k "$OUTPUT" | cut -f1)
print_success "Built $OUTPUT (${size_kb}KB) with:$NIP_DESCRIPTIONS"
# Build tests if requested
if [ "$BUILD_TESTS" = true ]; then
print_info "Scanning tests/ directory for test programs..."
if [ ! -d "tests" ]; then
print_warning "tests/ directory not found - skipping test builds"
else
TEST_COUNT=0
SUCCESS_COUNT=0
# Find all .c files in tests/ directory (not subdirectories)
while IFS= read -r -d '' test_file; do
TEST_COUNT=$((TEST_COUNT + 1))
test_name=$(basename "$test_file" .c)
test_exe="tests/$test_name"
print_info "Building test: $test_name"
# Simple test compilation - everything is in our fat library
LINK_FLAGS="-lz -ldl -lpthread -lm -static"
if [ "$VERBOSE" = true ]; then
print_info " Command: $CC $CFLAGS $INCLUDES \"$test_file\" -o \"$test_exe\" ./$OUTPUT $LINK_FLAGS"
fi
if $CC $CFLAGS $INCLUDES "$test_file" -o "$test_exe" "./$OUTPUT" $LINK_FLAGS; then
SUCCESS_COUNT=$((SUCCESS_COUNT + 1))
print_success "Built $test_name"
if [ "$VERBOSE" = true ]; then
print_info " Executable: $test_exe"
fi
else
print_error " Failed to build: $test_name"
fi
done < <(find tests/ -maxdepth 1 -name "*.c" -type f -print0)
if [ $TEST_COUNT -eq 0 ]; then
print_warning "No .c files found in tests/ directory"
else
print_success "Built $SUCCESS_COUNT/$TEST_COUNT test programs"
fi
fi
echo ""
fi
echo "Usage in your project:"
echo " gcc your_app.c $OUTPUT -lz -ldl -lpthread -lm -o your_app"
echo ""
else
print_error "Failed to create static library"
exit 1
fi

1302
debug.log

File diff suppressed because one or more lines are too long

View File

@@ -1,107 +0,0 @@
// Debug script to extract all intermediate values from nostr-tools NIP-44
const { v2 } = require('./nostr-tools/nip44.js');
const { bytesToHex, hexToBytes } = require('@noble/hashes/utils');
// Test vector 1: single char 'a'
console.log('=== NOSTR-TOOLS DEBUG: Single char "a" ===');
const sec1 = hexToBytes('0000000000000000000000000000000000000000000000000000000000000001');
const sec2 = hexToBytes('0000000000000000000000000000000000000000000000000000000000000002');
const nonce = hexToBytes('0000000000000000000000000000000000000000000000000000000000000001');
const plaintext = 'a';
// Step 1: Get public keys
const { schnorr } = require('@noble/curves/secp256k1');
const pub1 = bytesToHex(schnorr.getPublicKey(sec1));
const pub2 = bytesToHex(schnorr.getPublicKey(sec2));
console.log('pub1:', pub1);
console.log('pub2:', pub2);
// Step 2: Get conversation key
const conversationKey = v2.utils.getConversationKey(sec1, pub2);
console.log('conversation_key:', bytesToHex(conversationKey));
// Step 3: Get shared secret (raw ECDH)
const { secp256k1 } = require('@noble/curves/secp256k1');
const sharedPoint = secp256k1.getSharedSecret(sec1, '02' + pub2);
const sharedSecret = sharedPoint.subarray(1, 33); // X coordinate only
console.log('ecdh_shared_secret:', bytesToHex(sharedSecret));
// Step 4: Get message keys using internal function
const hkdf = require('@noble/hashes/hkdf');
const { sha256 } = require('@noble/hashes/sha256');
// HKDF Extract step
const salt = new TextEncoder().encode('nip44-v2');
const prk = hkdf.extract(sha256, sharedSecret, salt);
console.log('hkdf_extract_result:', bytesToHex(prk));
// HKDF Expand step
const messageKeys = hkdf.expand(sha256, prk, nonce, 76);
const chachaKey = messageKeys.subarray(0, 32);
const chachaNonce = messageKeys.subarray(32, 44);
const hmacKey = messageKeys.subarray(44, 76);
console.log('chacha_key:', bytesToHex(chachaKey));
console.log('chacha_nonce:', bytesToHex(chachaNonce));
console.log('hmac_key:', bytesToHex(hmacKey));
// Step 5: Pad the plaintext
function pad(plaintext) {
const utf8Encoder = new TextEncoder();
const unpadded = utf8Encoder.encode(plaintext);
const unpaddedLen = unpadded.length;
// Length prefix (big-endian u16)
const prefix = new Uint8Array(2);
new DataView(prefix.buffer).setUint16(0, unpaddedLen, false);
// Calculate padded length
function calcPaddedLen(len) {
if (len <= 32) return 32;
const nextPower = 1 << (Math.floor(Math.log2(len - 1)) + 1);
const chunk = nextPower <= 256 ? 32 : nextPower / 8;
return chunk * (Math.floor((len - 1) / chunk) + 1);
}
const paddedLen = calcPaddedLen(unpaddedLen + 2);
const suffix = new Uint8Array(paddedLen - 2 - unpaddedLen);
// Combine: prefix + plaintext + padding
const result = new Uint8Array(paddedLen);
result.set(prefix);
result.set(unpadded, 2);
result.set(suffix, 2 + unpaddedLen);
return result;
}
const paddedPlaintext = pad(plaintext);
console.log('padded_plaintext:', bytesToHex(paddedPlaintext));
console.log('padded_length:', paddedPlaintext.length);
// Step 6: ChaCha20 encrypt
const { chacha20 } = require('@noble/ciphers/chacha');
const ciphertext = chacha20(chachaKey, chachaNonce, paddedPlaintext);
console.log('ciphertext:', bytesToHex(ciphertext));
// Step 7: HMAC with AAD
const { hmac } = require('@noble/hashes/hmac');
const { concatBytes } = require('@noble/hashes/utils');
const aad = concatBytes(nonce, ciphertext);
console.log('aad_data:', bytesToHex(aad));
const mac = hmac(sha256, hmacKey, aad);
console.log('mac:', bytesToHex(mac));
// Step 8: Final payload
const { base64 } = require('@scure/base');
const payload = concatBytes(new Uint8Array([2]), nonce, ciphertext, mac);
console.log('raw_payload:', bytesToHex(payload));
const base64Payload = base64.encode(payload);
console.log('final_payload:', base64Payload);
// Expected from test vectors
console.log('expected_payload:', 'AgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABee0G5VSK0/9YypIObAtDKfYEAjD35uVkHyB0F4DwrcNaCXlCWZKaArsGrY6M9wnuTMxWfp1RTN9Xga8no+kF5Vsb');
// Now let's also test the full encrypt function
const fullEncrypt = v2.encrypt(plaintext, conversationKey, nonce);
console.log('v2.encrypt_result:', fullEncrypt);

View File

@@ -1,394 +0,0 @@
#!/bin/bash
# NOSTR Core Library Build Script
# Provides convenient build targets for the standalone library
# Automatically increments patch version with each build
set -e # Exit on any error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to automatically increment version
increment_version() {
print_status "Incrementing version..."
# Check if we're in a git repository
if ! git rev-parse --git-dir > /dev/null 2>&1; then
print_warning "Not in a git repository - skipping version increment"
return 0
fi
# Get the highest version tag (not necessarily the most recent chronologically)
LATEST_TAG=$(git tag -l 'v*.*.*' | sort -V | tail -n 1 || echo "v0.1.0")
if [[ -z "$LATEST_TAG" ]]; then
LATEST_TAG="v0.1.0"
fi
# Extract version components (remove 'v' prefix if present)
VERSION=${LATEST_TAG#v}
# Parse major.minor.patch
if [[ $VERSION =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
MAJOR=${BASH_REMATCH[1]}
MINOR=${BASH_REMATCH[2]}
PATCH=${BASH_REMATCH[3]}
else
print_error "Invalid version format in tag: $LATEST_TAG"
print_error "Expected format: v0.1.0"
return 1
fi
# Increment patch version
NEW_PATCH=$((PATCH + 1))
NEW_VERSION="v${MAJOR}.${MINOR}.${NEW_PATCH}"
print_status "Current version: $LATEST_TAG"
print_status "New version: $NEW_VERSION"
# Create new git tag
if git tag "$NEW_VERSION" 2>/dev/null; then
print_success "Created new version tag: $NEW_VERSION"
else
print_warning "Tag $NEW_VERSION already exists - using existing version"
NEW_VERSION=$LATEST_TAG
fi
# Update VERSION file for compatibility
echo "${NEW_VERSION#v}" > VERSION
print_success "Updated VERSION file to ${NEW_VERSION#v}"
}
# Function to perform git operations after successful build
perform_git_operations() {
local commit_message="$1"
if [[ -z "$commit_message" ]]; then
return 0 # No commit message provided, skip git operations
fi
print_status "Performing git operations..."
# Check if we're in a git repository
if ! git rev-parse --git-dir > /dev/null 2>&1; then
print_warning "Not in a git repository - skipping git operations"
return 0
fi
# Check if there are changes to commit
if git diff --quiet && git diff --cached --quiet; then
print_warning "No changes to commit"
return 0
fi
# Add all changes
print_status "Adding changes to git..."
if ! git add .; then
print_error "Failed to add changes to git"
return 1
fi
# Commit changes
print_status "Committing changes with message: '$commit_message'"
if ! git commit -m "$commit_message"; then
print_error "Failed to commit changes"
return 1
fi
# Push changes
print_status "Pushing changes to remote repository..."
if ! git push; then
print_error "Failed to push changes to remote repository"
print_warning "Changes have been committed locally but not pushed"
return 1
fi
print_success "Git operations completed successfully!"
return 0
}
# Function to show usage
show_usage() {
echo "NOSTR Core Library Build Script"
echo "==============================="
echo ""
echo "Usage: $0 [target] [-m \"commit message\"]"
echo ""
echo "Available targets:"
echo " clean - Clean all build artifacts"
echo " lib - Build static libraries for both x64 and ARM64 (default)"
echo " x64 - Build x64 static library only"
echo " arm64 - Build ARM64 static library only"
echo " all - Build both architectures and examples"
echo " examples - Build example programs"
echo " test - Run tests"
echo " install - Install library to system"
echo " uninstall - Remove library from system"
echo " help - Show this help message"
echo ""
echo "Options:"
echo " -m \"message\" - Git commit message (triggers automatic git add, commit, push after successful build)"
echo ""
echo "Examples:"
echo " $0 lib -m \"Add new proof-of-work parameters\""
echo " $0 x64 -m \"Fix OpenSSL minimal build configuration\""
echo " $0 lib # Build without git operations"
echo ""
echo "Library outputs (both self-contained with secp256k1):"
echo " libnostr_core.a - x86_64 static library"
echo " libnostr_core_arm64.a - ARM64 static library"
echo " examples/* - Example programs"
echo ""
echo "Both libraries include secp256k1 objects internally."
echo "Users only need to link with the library + -lm."
}
# Parse command line arguments
TARGET=""
COMMIT_MESSAGE=""
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-m)
COMMIT_MESSAGE="$2"
shift 2
;;
-*)
print_error "Unknown option: $1"
show_usage
exit 1
;;
*)
if [[ -z "$TARGET" ]]; then
TARGET="$1"
else
print_error "Multiple targets specified: $TARGET and $1"
show_usage
exit 1
fi
shift
;;
esac
done
# Set default target if none specified
TARGET=${TARGET:-lib}
case "$TARGET" in
clean)
print_status "Cleaning build artifacts..."
make clean
print_success "Clean completed"
;;
lib|library)
increment_version
print_status "Building both x64 and ARM64 static libraries..."
make clean
make
# Check both libraries were built
SUCCESS=0
if [ -f "libnostr_core.a" ]; then
SIZE_X64=$(stat -c%s "libnostr_core.a")
print_success "x64 static library built successfully (${SIZE_X64} bytes)"
SUCCESS=$((SUCCESS + 1))
else
print_error "Failed to build x64 static library"
fi
if [ -f "libnostr_core_arm64.a" ]; then
SIZE_ARM64=$(stat -c%s "libnostr_core_arm64.a")
print_success "ARM64 static library built successfully (${SIZE_ARM64} bytes)"
SUCCESS=$((SUCCESS + 1))
else
print_error "Failed to build ARM64 static library"
fi
if [ $SUCCESS -eq 2 ]; then
print_success "Both architectures built successfully!"
ls -la libnostr_core*.a
perform_git_operations "$COMMIT_MESSAGE"
else
print_error "Failed to build all libraries"
exit 1
fi
;;
x64|x64-only)
increment_version
print_status "Building x64 static library only..."
make clean
make x64
if [ -f "libnostr_core.a" ]; then
SIZE=$(stat -c%s "libnostr_core.a")
print_success "x64 static library built successfully (${SIZE} bytes)"
ls -la libnostr_core.a
perform_git_operations "$COMMIT_MESSAGE"
else
print_error "Failed to build x64 static library"
exit 1
fi
;;
arm64|arm64-only)
increment_version
print_status "Building ARM64 static library only..."
make clean
make arm64
if [ -f "libnostr_core_arm64.a" ]; then
SIZE=$(stat -c%s "libnostr_core_arm64.a")
print_success "ARM64 static library built successfully (${SIZE} bytes)"
ls -la libnostr_core_arm64.a
perform_git_operations "$COMMIT_MESSAGE"
else
print_error "Failed to build ARM64 static library"
exit 1
fi
;;
shared)
increment_version
print_status "Building shared library..."
make clean
make libnostr_core.so
if [ -f "libnostr_core.so" ]; then
SIZE=$(stat -c%s "libnostr_core.so")
print_success "Shared library built successfully (${SIZE} bytes)"
ls -la libnostr_core.so
perform_git_operations "$COMMIT_MESSAGE"
else
print_error "Failed to build shared library"
exit 1
fi
;;
all)
increment_version
print_status "Building all libraries and examples..."
make clean
make all
# Check both libraries and examples were built
SUCCESS=0
if [ -f "libnostr_core.a" ]; then
SIZE_X64=$(stat -c%s "libnostr_core.a")
print_success "x64 static library built successfully (${SIZE_X64} bytes)"
SUCCESS=$((SUCCESS + 1))
else
print_error "Failed to build x64 static library"
fi
if [ -f "libnostr_core_arm64.a" ]; then
SIZE_ARM64=$(stat -c%s "libnostr_core_arm64.a")
print_success "ARM64 static library built successfully (${SIZE_ARM64} bytes)"
SUCCESS=$((SUCCESS + 1))
else
print_error "Failed to build ARM64 static library"
fi
if [ $SUCCESS -eq 2 ]; then
print_success "All libraries and examples built successfully!"
ls -la libnostr_core*.a
ls -la examples/
perform_git_operations "$COMMIT_MESSAGE"
else
print_error "Failed to build all components"
exit 1
fi
;;
examples)
increment_version
print_status "Building both libraries and examples..."
make clean
make
make examples
# Verify libraries were built
if [ -f "libnostr_core.a" ] && [ -f "libnostr_core_arm64.a" ]; then
print_success "Both libraries and examples built successfully"
ls -la libnostr_core*.a
ls -la examples/
perform_git_operations "$COMMIT_MESSAGE"
else
print_error "Failed to build libraries for examples"
exit 1
fi
;;
test)
print_status "Running tests..."
make clean
make
if make test-crypto 2>/dev/null; then
print_success "All tests passed"
else
print_warning "Running simple test instead..."
make test
print_success "Basic test completed"
fi
;;
tests)
print_status "Running tests..."
make clean
make
if make test-crypto 2>/dev/null; then
print_success "All tests passed"
else
print_warning "Running simple test instead..."
make test
print_success "Basic test completed"
fi
;;
install)
increment_version
print_status "Installing library to system..."
make clean
make all
sudo make install
print_success "Library installed to /usr/local"
perform_git_operations "$COMMIT_MESSAGE"
;;
uninstall)
print_status "Uninstalling library from system..."
sudo make uninstall
print_success "Library uninstalled"
;;
help|--help|-h)
show_usage
;;
*)
print_error "Unknown target: $TARGET"
echo ""
show_usage
exit 1
;;
esac

View File

@@ -1,40 +0,0 @@
/*
* Example: Basic NOSTR functionality
* This example shows auto-detection working with selective includes
*/
#include "nostr_core/nip001.h" // Basic protocol
#include "nostr_core/nip006.h" // Key generation (will be created next)
#include "nostr_core/nip019.h" // Bech32 encoding (will be created next)
#include <stdio.h>
int main() {
printf("NOSTR Core Library - Basic Example\n");
// Initialize library
if (nostr_init() != 0) {
printf("Failed to initialize NOSTR library\n");
return 1;
}
printf("Library initialized successfully!\n");
// Generate keypair (from NIP-006)
// unsigned char private_key[32], public_key[32];
// nostr_generate_keypair(private_key, public_key);
// Convert to bech32 (from NIP-019)
// char nsec[100];
// nostr_key_to_bech32(private_key, "nsec", nsec);
// Create basic event (from NIP-001)
// cJSON* event = nostr_create_and_sign_event(1, "Hello Nostr!", NULL, private_key, 0);
printf("Example completed - the build script should detect NIPs: 001, 006, 019\n");
// Cleanup
nostr_cleanup();
return 0;
}

View File

@@ -1,104 +0,0 @@
/*
* Example demonstrating modular NIP usage
* Shows how different NIPs can be used independently
*/
#include "nostr_core/nip001.h" // Basic protocol
#include "nostr_core/nip005.h" // NIP-05 DNS verification
#include "nostr_core/nip006.h" // Key derivation
#include "nostr_core/nip011.h" // Relay information
#include "nostr_core/nip013.h" // Proof of work
#include "nostr_core/nip019.h" // Bech32 encoding
#include "nostr_core/utils.h" // Utility functions
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("=== Modular NOSTR Core Library Demo ===\n\n");
// Initialize the library
if (nostr_init() != NOSTR_SUCCESS) {
printf("Failed to initialize NOSTR library\n");
return 1;
}
// Test NIP-006: Key generation and detection
printf("1. NIP-006: Key Generation and Input Detection\n");
unsigned char private_key[32], public_key[32];
if (nostr_generate_keypair(private_key, public_key) == NOSTR_SUCCESS) {
char private_hex[65], public_hex[65];
nostr_bytes_to_hex(private_key, 32, private_hex);
nostr_bytes_to_hex(public_key, 32, public_hex);
printf(" Generated keypair:\n");
printf(" Private: %s\n", private_hex);
printf(" Public: %s\n", public_hex);
// Test input type detection
nostr_input_type_t type = nostr_detect_input_type(private_hex);
printf(" Input type: %s\n",
type == NOSTR_INPUT_NSEC_HEX ? "NSEC_HEX" :
type == NOSTR_INPUT_NSEC_BECH32 ? "NSEC_BECH32" :
type == NOSTR_INPUT_MNEMONIC ? "MNEMONIC" : "UNKNOWN");
}
// Test NIP-019: Bech32 encoding
printf("\n2. NIP-019: Bech32 Encoding\n");
char bech32_nsec[200], bech32_npub[200];
if (nostr_key_to_bech32(private_key, "nsec", bech32_nsec) == NOSTR_SUCCESS) {
printf(" nsec: %s\n", bech32_nsec);
}
if (nostr_key_to_bech32(public_key, "npub", bech32_npub) == NOSTR_SUCCESS) {
printf(" npub: %s\n", bech32_npub);
}
// Test NIP-001: Event creation
printf("\n3. NIP-001: Event Creation\n");
cJSON* event = nostr_create_and_sign_event(1, "Hello from modular NOSTR!", NULL, private_key, 0);
if (event) {
char* event_json = cJSON_Print(event);
printf(" Created event: %s\n", event_json);
free(event_json);
cJSON_Delete(event);
}
// Test NIP-013: Proof of Work (light test)
printf("\n4. NIP-013: Proof of Work\n");
cJSON* pow_event = nostr_create_and_sign_event(1, "PoW test message", NULL, private_key, 0);
if (pow_event) {
printf(" Adding PoW (target difficulty: 4)...\n");
int result = nostr_add_proof_of_work(pow_event, private_key, 4, 100000, 10000, 5000, NULL, NULL);
if (result == NOSTR_SUCCESS) {
cJSON* id_item = cJSON_GetObjectItem(pow_event, "id");
if (id_item) {
printf(" PoW success! Event ID: %s\n", cJSON_GetStringValue(id_item));
}
} else {
printf(" PoW failed or timeout\n");
}
cJSON_Delete(pow_event);
}
// Test NIP-005: DNS verification (parse only - no network call in example)
printf("\n5. NIP-005: DNS Identifier Parsing\n");
const char* test_json = "{\"names\":{\"test\":\"1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef\"}}";
char found_pubkey[65];
int result = nostr_nip05_parse_well_known(test_json, "test", found_pubkey, NULL, NULL);
if (result == NOSTR_SUCCESS) {
printf(" Parsed pubkey from test JSON: %s\n", found_pubkey);
}
// Test NIP-011: Just show the structure exists
printf("\n6. NIP-011: Relay Information Structure\n");
printf(" Relay info structure available for fetching relay metadata\n");
printf(" (Network calls not performed in this example)\n");
printf("\n=== All modular NIPs working! ===\n");
nostr_cleanup();
return 0;
}

BIN
examples/input_detection Executable file

Binary file not shown.

View File

@@ -1,39 +0,0 @@
# Example CMakeLists.txt for a project using nostr_core library
cmake_minimum_required(VERSION 3.12)
project(my_nostr_app VERSION 1.0.0 LANGUAGES C)
set(CMAKE_C_STANDARD 99)
# Method 1: Find installed package
# Uncomment if nostr_core is installed system-wide
# find_package(nostr_core REQUIRED)
# Method 2: Use as subdirectory
# Uncomment if nostr_core is a subdirectory
# add_subdirectory(nostr_core)
# Method 3: Use pkg-config
# Uncomment if using pkg-config
# find_package(PkgConfig REQUIRED)
# pkg_check_modules(NOSTR_CORE REQUIRED nostr_core)
# Create executable
add_executable(my_nostr_app main.c)
# Link with nostr_core
# Choose one of the following based on your integration method:
# Method 1: Installed package
# target_link_libraries(my_nostr_app nostr_core::static)
# Method 2: Subdirectory
# target_link_libraries(my_nostr_app nostr_core_static)
# Method 3: pkg-config
# target_include_directories(my_nostr_app PRIVATE ${NOSTR_CORE_INCLUDE_DIRS})
# target_link_libraries(my_nostr_app ${NOSTR_CORE_LIBRARIES})
# For this example, we'll assume Method 2 (subdirectory)
# Add the parent nostr_core directory
add_subdirectory(../.. nostr_core)
target_link_libraries(my_nostr_app nostr_core_static)

View File

@@ -1,186 +0,0 @@
# NOSTR Core Integration Example
This directory contains a complete example showing how to integrate the NOSTR Core library into your own projects.
## What This Example Demonstrates
- **Library Initialization**: Proper setup and cleanup of the NOSTR library
- **Identity Management**: Key generation, bech32 encoding, and format detection
- **Event Creation**: Creating and signing different types of NOSTR events
- **Input Handling**: Processing various input formats (mnemonic, hex, bech32)
- **Utility Functions**: Using helper functions for hex conversion and error handling
- **CMake Integration**: How to integrate the library in your CMake-based project
## Building and Running
### Method 1: Using CMake
```bash
# Create build directory
mkdir build && cd build
# Configure with CMake
cmake ..
# Build
make
# Run the example
./my_nostr_app
```
### Method 2: Manual Compilation
```bash
# Compile directly (assuming you're in the c_nostr root directory)
gcc -I. examples/integration_example/main.c nostr_core.c nostr_crypto.c cjson/cJSON.c -lm -o integration_example
# Run
./integration_example
```
## Expected Output
The example will demonstrate:
1. **Identity Management Demo**
- Generate a new keypair
- Display keys in hex and bech32 format
2. **Event Creation Demo**
- Create a text note event
- Create a profile event
- Display the JSON for both events
3. **Input Handling Demo**
- Process different input formats
- Show format detection and decoding
4. **Utility Functions Demo**
- Hex conversion round-trip
- Error message display
## Integration Patterns
### Pattern 1: CMake Find Package
If NOSTR Core is installed system-wide:
```cmake
find_package(nostr_core REQUIRED)
target_link_libraries(your_app nostr_core::static)
```
### Pattern 2: CMake Subdirectory
If NOSTR Core is a subdirectory of your project:
```cmake
add_subdirectory(nostr_core)
target_link_libraries(your_app nostr_core_static)
```
### Pattern 3: pkg-config
If using pkg-config:
```cmake
find_package(PkgConfig REQUIRED)
pkg_check_modules(NOSTR_CORE REQUIRED nostr_core)
target_include_directories(your_app PRIVATE ${NOSTR_CORE_INCLUDE_DIRS})
target_link_libraries(your_app ${NOSTR_CORE_LIBRARIES})
```
### Pattern 4: Direct Source Integration
Copy the essential files to your project:
```bash
cp nostr_core.{c,h} nostr_crypto.{c,h} your_project/src/
cp -r cjson/ your_project/src/
```
Then compile them with your project sources.
## Code Structure
### main.c Structure
The example is organized into clear demonstration functions:
- `demo_identity_management()` - Key generation and encoding
- `demo_event_creation()` - Creating different event types
- `demo_input_handling()` - Processing various input formats
- `demo_utilities()` - Using utility functions
Each function demonstrates specific aspects of the library while maintaining proper error handling and resource cleanup.
### Key Integration Points
1. **Initialization**
```c
int ret = nostr_init();
if (ret != NOSTR_SUCCESS) {
// Handle error
}
```
2. **Resource Cleanup**
```c
// Always clean up JSON objects
cJSON_Delete(event);
// Clean up library on exit
nostr_cleanup();
```
3. **Error Handling**
```c
if (ret != NOSTR_SUCCESS) {
printf("Error: %s\n", nostr_strerror(ret));
return ret;
}
```
## Customization
You can modify this example for your specific needs:
- Change the `app_config_t` structure to match your application's configuration
- Add additional event types or custom event creation logic
- Integrate with your existing error handling and logging systems
- Add networking functionality using the WebSocket layer
## Dependencies
This example requires:
- C99 compiler (gcc, clang)
- CMake 3.12+ (for CMake build)
- NOSTR Core library and its dependencies
## Testing
You can test different input formats by passing them as command line arguments:
```bash
# Test with mnemonic
./my_nostr_app "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
# Test with hex private key
./my_nostr_app "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
# Test with bech32 nsec
./my_nostr_app "nsec1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
```
## Next Steps
After studying this example, you can:
1. Integrate the patterns into your own application
2. Explore the WebSocket functionality for relay communication
3. Add support for additional NOSTR event types
4. Implement your own identity persistence layer
5. Add networking and relay management features
For more examples, see the other files in the `examples/` directory.

View File

@@ -1,271 +0,0 @@
/*
* Example application demonstrating how to integrate nostr_core into other projects
* This shows a complete workflow from key generation to event publishing
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "nostr_core.h"
// Example application configuration
typedef struct {
char* app_name;
char* version;
int debug_mode;
} app_config_t;
static app_config_t g_config = {
.app_name = "My NOSTR App",
.version = "1.0.0",
.debug_mode = 1
};
// Helper function to print hex data
static void print_hex(const char* label, const unsigned char* data, size_t len) {
if (g_config.debug_mode) {
printf("%s: ", label);
for (size_t i = 0; i < len; i++) {
printf("%02x", data[i]);
}
printf("\n");
}
}
// Helper function to print JSON nicely
static void print_event(const char* label, cJSON* event) {
if (!event) {
printf("%s: NULL\n", label);
return;
}
char* json_string = cJSON_Print(event);
if (json_string) {
printf("%s:\n%s\n", label, json_string);
free(json_string);
}
}
// Example: Generate and manage identity
static int demo_identity_management(void) {
printf("\n=== Identity Management Demo ===\n");
unsigned char private_key[32], public_key[32];
char nsec[100], npub[100];
// Generate a new keypair
printf("Generating new keypair...\n");
int ret = nostr_generate_keypair(private_key, public_key);
if (ret != NOSTR_SUCCESS) {
printf("Error generating keypair: %s\n", nostr_strerror(ret));
return ret;
}
print_hex("Private Key", private_key, 32);
print_hex("Public Key", public_key, 32);
// Convert to bech32 format
ret = nostr_key_to_bech32(private_key, "nsec", nsec);
if (ret != NOSTR_SUCCESS) {
printf("Error encoding nsec: %s\n", nostr_strerror(ret));
return ret;
}
ret = nostr_key_to_bech32(public_key, "npub", npub);
if (ret != NOSTR_SUCCESS) {
printf("Error encoding npub: %s\n", nostr_strerror(ret));
return ret;
}
printf("nsec: %s\n", nsec);
printf("npub: %s\n", npub);
return NOSTR_SUCCESS;
}
// Example: Create different types of events
static int demo_event_creation(const unsigned char* private_key) {
printf("\n=== Event Creation Demo ===\n");
// Create a text note
printf("Creating text note...\n");
cJSON* text_event = nostr_create_text_event("Hello from my NOSTR app!", private_key);
if (!text_event) {
printf("Error creating text event\n");
return NOSTR_ERROR_JSON_PARSE;
}
print_event("Text Event", text_event);
// Create a profile event
printf("\nCreating profile event...\n");
cJSON* profile_event = nostr_create_profile_event(
g_config.app_name,
"A sample application demonstrating NOSTR integration",
private_key
);
if (!profile_event) {
printf("Error creating profile event\n");
cJSON_Delete(text_event);
return NOSTR_ERROR_JSON_PARSE;
}
print_event("Profile Event", profile_event);
// Cleanup
cJSON_Delete(text_event);
cJSON_Delete(profile_event);
return NOSTR_SUCCESS;
}
// Example: Handle different input formats
static int demo_input_handling(const char* user_input) {
printf("\n=== Input Handling Demo ===\n");
printf("Processing input: %s\n", user_input);
// Detect input type
int input_type = nostr_detect_input_type(user_input);
switch (input_type) {
case NOSTR_INPUT_MNEMONIC:
printf("Detected: BIP39 Mnemonic\n");
{
unsigned char priv[32], pub[32];
int ret = nostr_derive_keys_from_mnemonic(user_input, 0, priv, pub);
if (ret == NOSTR_SUCCESS) {
print_hex("Derived Private Key", priv, 32);
print_hex("Derived Public Key", pub, 32);
}
}
break;
case NOSTR_INPUT_NSEC_HEX:
printf("Detected: Hex-encoded private key\n");
{
unsigned char decoded[32];
int ret = nostr_decode_nsec(user_input, decoded);
if (ret == NOSTR_SUCCESS) {
print_hex("Decoded Private Key", decoded, 32);
}
}
break;
case NOSTR_INPUT_NSEC_BECH32:
printf("Detected: Bech32-encoded private key (nsec)\n");
{
unsigned char decoded[32];
int ret = nostr_decode_nsec(user_input, decoded);
if (ret == NOSTR_SUCCESS) {
print_hex("Decoded Private Key", decoded, 32);
}
}
break;
default:
printf("Unknown input format\n");
return NOSTR_ERROR_INVALID_INPUT;
}
return NOSTR_SUCCESS;
}
// Example: Demonstrate utility functions
static int demo_utilities(void) {
printf("\n=== Utility Functions Demo ===\n");
// Hex conversion
const char* test_hex = "deadbeef";
unsigned char bytes[4];
char hex_result[9];
printf("Testing hex conversion with: %s\n", test_hex);
int ret = nostr_hex_to_bytes(test_hex, bytes, 4);
if (ret != NOSTR_SUCCESS) {
printf("Error in hex_to_bytes: %s\n", nostr_strerror(ret));
return ret;
}
nostr_bytes_to_hex(bytes, 4, hex_result);
printf("Round-trip result: %s\n", hex_result);
// Error message testing
printf("\nTesting error messages:\n");
for (int i = 0; i >= -10; i--) {
const char* msg = nostr_strerror(i);
if (msg && strlen(msg) > 0) {
printf(" %d: %s\n", i, msg);
}
}
return NOSTR_SUCCESS;
}
int main(int argc, char* argv[]) {
printf("%s v%s\n", g_config.app_name, g_config.version);
printf("NOSTR Core Integration Example\n");
printf("=====================================\n");
// Initialize the NOSTR library
printf("Initializing NOSTR core library...\n");
int ret = nostr_init();
if (ret != NOSTR_SUCCESS) {
printf("Failed to initialize NOSTR library: %s\n", nostr_strerror(ret));
return 1;
}
// Run demonstrations
unsigned char demo_private_key[32];
// 1. Identity management
ret = demo_identity_management();
if (ret != NOSTR_SUCCESS) {
goto cleanup;
}
// Generate a key for other demos
nostr_generate_keypair(demo_private_key, NULL);
// 2. Event creation
ret = demo_event_creation(demo_private_key);
if (ret != NOSTR_SUCCESS) {
goto cleanup;
}
// 3. Input handling (use command line argument if provided)
const char* test_input = (argc > 1) ? argv[1] :
"abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
ret = demo_input_handling(test_input);
if (ret != NOSTR_SUCCESS && ret != NOSTR_ERROR_INVALID_INPUT) {
goto cleanup;
}
// 4. Utility functions
ret = demo_utilities();
if (ret != NOSTR_SUCCESS) {
goto cleanup;
}
printf("\n=====================================\n");
printf("All demonstrations completed successfully!\n");
printf("\nThis example shows how to:\n");
printf(" • Initialize the NOSTR library\n");
printf(" • Generate and manage keypairs\n");
printf(" • Create and sign different event types\n");
printf(" • Handle various input formats\n");
printf(" • Use utility functions\n");
printf(" • Clean up resources properly\n");
ret = NOSTR_SUCCESS;
cleanup:
// Clean up the NOSTR library
printf("\nCleaning up NOSTR library...\n");
nostr_cleanup();
if (ret == NOSTR_SUCCESS) {
printf("Example completed successfully.\n");
return 0;
} else {
printf("Example failed with error: %s\n", nostr_strerror(ret));
return 1;
}
}

BIN
examples/keypair_generation Executable file

Binary file not shown.

BIN
examples/mnemonic_derivation Executable file

Binary file not shown.

BIN
examples/mnemonic_generation Executable file

Binary file not shown.

BIN
examples/relay_pool Executable file

Binary file not shown.

889
examples/relay_pool.c Normal file
View File

@@ -0,0 +1,889 @@
/*
* Interactive Relay Pool Test Program
*
* Interactive command-line interface for testing nostr_relay_pool functionality.
* All output is logged to pool.log while the menu runs in the terminal.
*
* Usage: ./pool_test
*/
#define _POSIX_C_SOURCE 200809L
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "../nostr_core/nostr_core.h"
#include "../cjson/cJSON.h"
// Global variables
volatile sig_atomic_t running = 1;
nostr_relay_pool_t* pool = NULL;
nostr_pool_subscription_t** subscriptions = NULL;
int subscription_count = 0;
int subscription_capacity = 0;
pthread_t poll_thread;
int log_fd = -1;
// Signal handler for clean shutdown
void signal_handler(int signum) {
(void)signum;
running = 0;
}
// Event callback - called when an event is received
void on_event(cJSON* event, const char* relay_url, void* user_data) {
(void)user_data;
// Extract basic event information
cJSON* id = cJSON_GetObjectItem(event, "id");
cJSON* pubkey = cJSON_GetObjectItem(event, "pubkey");
cJSON* created_at = cJSON_GetObjectItem(event, "created_at");
cJSON* kind = cJSON_GetObjectItem(event, "kind");
cJSON* content = cJSON_GetObjectItem(event, "content");
time_t now = time(NULL);
char timestamp[26];
ctime_r(&now, timestamp);
timestamp[24] = '\0'; // Remove newline
dprintf(log_fd, "[%s] 📨 EVENT from %s\n", timestamp, relay_url);
dprintf(log_fd, "├── ID: %.12s...\n", id && cJSON_IsString(id) ? cJSON_GetStringValue(id) : "unknown");
dprintf(log_fd, "├── Pubkey: %.12s...\n", pubkey && cJSON_IsString(pubkey) ? cJSON_GetStringValue(pubkey) : "unknown");
dprintf(log_fd, "├── Kind: %d\n", kind && cJSON_IsNumber(kind) ? (int)cJSON_GetNumberValue(kind) : -1);
dprintf(log_fd, "├── Created: %lld\n", created_at && cJSON_IsNumber(created_at) ? (long long)cJSON_GetNumberValue(created_at) : 0);
// Truncate content if too long
if (content && cJSON_IsString(content)) {
const char* content_str = cJSON_GetStringValue(content);
size_t content_len = strlen(content_str);
if (content_len > 100) {
dprintf(log_fd, "└── Content: %.97s...\n", content_str);
} else {
dprintf(log_fd, "└── Content: %s\n", content_str);
}
} else {
dprintf(log_fd, "└── Content: (empty)\n");
}
dprintf(log_fd, "\n");
}
// EOSE callback - called when End of Stored Events is received
void on_eose(cJSON** events, int event_count, void* user_data) {
(void)user_data;
time_t now = time(NULL);
char timestamp[26];
ctime_r(&now, timestamp);
timestamp[24] = '\0';
dprintf(log_fd, "[%s] 📋 EOSE received - %d events collected\n", timestamp, event_count);
// Log collected events if any
for (int i = 0; i < event_count; i++) {
cJSON* id = cJSON_GetObjectItem(events[i], "id");
if (id && cJSON_IsString(id)) {
dprintf(log_fd, " Event %d: %.12s...\n", i + 1, cJSON_GetStringValue(id));
}
}
dprintf(log_fd, "\n");
}
// Background polling thread
void* poll_thread_func(void* arg) {
(void)arg;
while (running) {
if (pool) {
nostr_relay_pool_poll(pool, 100);
}
struct timespec ts = {0, 10000000}; // 10ms
nanosleep(&ts, NULL);
}
return NULL;
}
// Print menu
void print_menu() {
printf("\n=== NOSTR Relay Pool Test Menu ===\n");
printf("1. Start Pool (ws://localhost:7555)\n");
printf("2. Stop Pool\n");
printf("3. Add relay to pool\n");
printf("4. Remove relay from pool\n");
printf("5. Add subscription\n");
printf("6. Remove subscription\n");
printf("7. Show pool status\n");
printf("8. Test reconnection (simulate disconnect)\n");
printf("9. Publish Event\n");
printf("0. Exit\n");
printf("Choice: ");
}
// Get user input with default
char* get_input(const char* prompt, const char* default_value) {
static char buffer[1024];
printf("%s", prompt);
if (default_value) {
printf(" [%s]", default_value);
}
printf(": ");
if (!fgets(buffer, sizeof(buffer), stdin)) {
return NULL;
}
// Remove newline
size_t len = strlen(buffer);
if (len > 0 && buffer[len-1] == '\n') {
buffer[len-1] = '\0';
}
// Return default if empty
if (strlen(buffer) == 0 && default_value) {
return strdup(default_value);
}
return strdup(buffer);
}
// Parse comma-separated list into cJSON array
cJSON* parse_comma_list(const char* input, int is_number) {
if (!input || strlen(input) == 0) {
return NULL;
}
cJSON* array = cJSON_CreateArray();
if (!array) return NULL;
char* input_copy = strdup(input);
char* token = strtok(input_copy, ",");
while (token) {
// Trim whitespace
while (*token == ' ') token++;
char* end = token + strlen(token) - 1;
while (end > token && *end == ' ') *end-- = '\0';
if (is_number) {
int num = atoi(token);
cJSON_AddItemToArray(array, cJSON_CreateNumber(num));
} else {
cJSON_AddItemToArray(array, cJSON_CreateString(token));
}
token = strtok(NULL, ",");
}
free(input_copy);
return array;
}
// Add subscription interactively
void add_subscription() {
if (!pool) {
printf("❌ Pool not started\n");
return;
}
printf("\n--- Add Subscription ---\n");
printf("Enter filter values (press Enter for no value):\n");
cJSON* filter = cJSON_CreateObject();
// ids
char* ids_input = get_input("ids (comma-separated event ids)", NULL);
if (ids_input && strlen(ids_input) > 0) {
cJSON* ids = parse_comma_list(ids_input, 0);
if (ids) cJSON_AddItemToObject(filter, "ids", ids);
}
free(ids_input);
// authors
char* authors_input = get_input("authors (comma-separated pubkeys)", NULL);
if (authors_input && strlen(authors_input) > 0) {
cJSON* authors = parse_comma_list(authors_input, 0);
if (authors) cJSON_AddItemToObject(filter, "authors", authors);
}
free(authors_input);
// kinds
char* kinds_input = get_input("kinds (comma-separated numbers)", NULL);
if (kinds_input && strlen(kinds_input) > 0) {
cJSON* kinds = parse_comma_list(kinds_input, 1);
if (kinds) cJSON_AddItemToObject(filter, "kinds", kinds);
}
free(kinds_input);
// #e tag
char* e_input = get_input("#e (comma-separated event ids)", NULL);
if (e_input && strlen(e_input) > 0) {
cJSON* e_array = parse_comma_list(e_input, 0);
if (e_array) cJSON_AddItemToObject(filter, "#e", e_array);
}
free(e_input);
// #p tag
char* p_input = get_input("#p (comma-separated pubkeys)", NULL);
if (p_input && strlen(p_input) > 0) {
cJSON* p_array = parse_comma_list(p_input, 0);
if (p_array) cJSON_AddItemToObject(filter, "#p", p_array);
}
free(p_input);
// since
char* since_input = get_input("since (unix timestamp or 'n' for now)", NULL);
if (since_input && strlen(since_input) > 0) {
if (strcmp(since_input, "n") == 0) {
// Use current timestamp
time_t now = time(NULL);
cJSON_AddItemToObject(filter, "since", cJSON_CreateNumber((int)now));
printf("Using current timestamp: %ld\n", now);
} else {
int since = atoi(since_input);
if (since > 0) cJSON_AddItemToObject(filter, "since", cJSON_CreateNumber(since));
}
}
free(since_input);
// until
char* until_input = get_input("until (unix timestamp)", NULL);
if (until_input && strlen(until_input) > 0) {
int until = atoi(until_input);
if (until > 0) cJSON_AddItemToObject(filter, "until", cJSON_CreateNumber(until));
}
free(until_input);
// limit
char* limit_input = get_input("limit (max events)", "10");
if (limit_input && strlen(limit_input) > 0) {
int limit = atoi(limit_input);
if (limit > 0) cJSON_AddItemToObject(filter, "limit", cJSON_CreateNumber(limit));
}
free(limit_input);
// Get relay URLs from pool
char** relay_urls = NULL;
nostr_pool_relay_status_t* statuses = NULL;
int relay_count = nostr_relay_pool_list_relays(pool, &relay_urls, &statuses);
if (relay_count <= 0) {
printf("❌ No relays in pool\n");
cJSON_Delete(filter);
return;
}
// Ask about close_on_eose behavior
char* close_input = get_input("Close subscription on EOSE? (y/n)", "n");
int close_on_eose = (close_input && strcmp(close_input, "y") == 0) ? 1 : 0;
free(close_input);
// Create subscription with new parameters
nostr_pool_subscription_t* sub = nostr_relay_pool_subscribe(
pool,
(const char**)relay_urls,
relay_count,
filter,
on_event,
on_eose,
NULL,
close_on_eose,
1, // enable_deduplication
NOSTR_POOL_EOSE_FULL_SET, // result_mode
30, // relay_timeout_seconds
60 // eose_timeout_seconds
);
// Free relay URLs
for (int i = 0; i < relay_count; i++) {
free(relay_urls[i]);
}
free(relay_urls);
free(statuses);
if (!sub) {
printf("❌ Failed to create subscription\n");
cJSON_Delete(filter);
return;
}
// Store subscription
if (subscription_count >= subscription_capacity) {
subscription_capacity = subscription_capacity == 0 ? 10 : subscription_capacity * 2;
subscriptions = realloc(subscriptions, subscription_capacity * sizeof(nostr_pool_subscription_t*));
}
subscriptions[subscription_count++] = sub;
printf("✅ Subscription created (ID: %d)\n", subscription_count);
// Log the filter
char* filter_json = cJSON_Print(filter);
time_t now = time(NULL);
char timestamp[26];
ctime_r(&now, timestamp);
timestamp[24] = '\0';
dprintf(log_fd, "[%s] 🔍 New subscription created (ID: %d)\n", timestamp, subscription_count);
dprintf(log_fd, "Filter: %s\n\n", filter_json);
free(filter_json);
}
// Remove subscription
void remove_subscription() {
if (subscription_count == 0) {
printf("❌ No subscriptions to remove\n");
return;
}
printf("\n--- Remove Subscription ---\n");
printf("Available subscriptions:\n");
for (int i = 0; i < subscription_count; i++) {
printf("%d. Subscription %d\n", i + 1, i + 1);
}
char* choice_input = get_input("Enter subscription number to remove", NULL);
if (!choice_input || strlen(choice_input) == 0) {
free(choice_input);
return;
}
int choice = atoi(choice_input) - 1;
free(choice_input);
if (choice < 0 || choice >= subscription_count) {
printf("❌ Invalid subscription number\n");
return;
}
nostr_pool_subscription_close(subscriptions[choice]);
// Shift remaining subscriptions
for (int i = choice; i < subscription_count - 1; i++) {
subscriptions[i] = subscriptions[i + 1];
}
subscription_count--;
printf("✅ Subscription removed\n");
time_t now = time(NULL);
char timestamp[26];
ctime_r(&now, timestamp);
timestamp[24] = '\0';
dprintf(log_fd, "[%s] 🗑️ Subscription removed (was ID: %d)\n\n", timestamp, choice + 1);
}
// Show pool status
void show_pool_status() {
if (!pool) {
printf("❌ Pool not started\n");
return;
}
// Give polling thread time to establish connections
printf("⏳ Waiting for connections to establish...\n");
sleep(3);
char** relay_urls = NULL;
nostr_pool_relay_status_t* statuses = NULL;
int relay_count = nostr_relay_pool_list_relays(pool, &relay_urls, &statuses);
printf("\n📊 POOL STATUS\n");
printf("Relays: %d\n", relay_count);
printf("Subscriptions: %d\n", subscription_count);
if (relay_count > 0) {
printf("\nRelay Details:\n");
for (int i = 0; i < relay_count; i++) {
const char* status_str;
switch (statuses[i]) {
case NOSTR_POOL_RELAY_CONNECTED: status_str = "🟢 CONNECTED"; break;
case NOSTR_POOL_RELAY_CONNECTING: status_str = "🟡 CONNECTING"; break;
case NOSTR_POOL_RELAY_DISCONNECTED: status_str = "⚪ DISCONNECTED"; break;
case NOSTR_POOL_RELAY_ERROR: status_str = "🔴 ERROR"; break;
default: status_str = "❓ UNKNOWN"; break;
}
printf("├── %s: %s\n", relay_urls[i], status_str);
// Show connection and publish error details
const char* conn_error = nostr_relay_pool_get_relay_last_connection_error(pool, relay_urls[i]);
const char* pub_error = nostr_relay_pool_get_relay_last_publish_error(pool, relay_urls[i]);
if (conn_error) {
printf("│ ├── Connection error: %s\n", conn_error);
}
if (pub_error) {
printf("│ ├── Last publish error: %s\n", pub_error);
}
const nostr_relay_stats_t* stats = nostr_relay_pool_get_relay_stats(pool, relay_urls[i]);
if (stats) {
printf("│ ├── Events received: %d\n", stats->events_received);
printf("│ ├── Connection attempts: %d\n", stats->connection_attempts);
printf("│ ├── Connection failures: %d\n", stats->connection_failures);
printf("│ ├── Events published: %d (OK: %d, Failed: %d)\n",
stats->events_published, stats->events_published_ok, stats->events_published_failed);
printf("│ ├── Ping latency: %.2f ms\n", stats->ping_latency_current);
printf("│ └── Query latency: %.2f ms\n", stats->query_latency_avg);
}
free(relay_urls[i]);
}
free(relay_urls);
free(statuses);
}
printf("\n");
}
// Async publish callback context
typedef struct {
int total_relays;
int responses_received;
int success_count;
time_t start_time;
} async_publish_context_t;
// Async publish callback - called for each relay response
void async_publish_callback(const char* relay_url, const char* event_id,
int success, const char* message, void* user_data) {
async_publish_context_t* ctx = (async_publish_context_t*)user_data;
ctx->responses_received++;
if (success) {
ctx->success_count++;
}
// Calculate elapsed time
time_t now = time(NULL);
double elapsed = difftime(now, ctx->start_time);
// Log to file with real-time feedback
char timestamp[26];
ctime_r(&now, timestamp);
timestamp[24] = '\0';
if (success) {
printf("✅ %s: Published successfully (%.1fs)\n", relay_url, elapsed);
dprintf(log_fd, "[%s] ✅ ASYNC: %s published successfully (%.1fs)\n",
timestamp, relay_url, elapsed);
} else {
printf("❌ %s: Failed - %s (%.1fs)\n", relay_url, message ? message : "unknown error", elapsed);
dprintf(log_fd, "[%s] ❌ ASYNC: %s failed - %s (%.1fs)\n",
timestamp, relay_url, message ? message : "unknown error", elapsed);
}
// Show progress
printf(" Progress: %d/%d responses received\n", ctx->responses_received, ctx->total_relays);
if (ctx->responses_received >= ctx->total_relays) {
printf("\n🎉 All relays responded! Final result: %d/%d successful\n",
ctx->success_count, ctx->total_relays);
dprintf(log_fd, "[%s] 🎉 ASYNC: All relays responded - %d/%d successful\n\n",
timestamp, ctx->success_count, ctx->total_relays);
}
}
// Publish test event with async callbacks
void publish_event() {
if (!pool) {
printf("❌ Pool not started\n");
return;
}
printf("\n--- Publish Test Event ---\n");
// Generate random keypair
unsigned char private_key[32], public_key[32];
if (nostr_generate_keypair(private_key, public_key) != NOSTR_SUCCESS) {
printf("❌ Failed to generate keypair\n");
return;
}
// Get current timestamp
time_t now = time(NULL);
// Format content with date/time
char content[256];
struct tm* tm_info = localtime(&now);
strftime(content, sizeof(content), "Test post at %Y-%m-%d %H:%M:%S", tm_info);
// Create kind 1 event
cJSON* event = nostr_create_and_sign_event(1, content, NULL, private_key, now);
if (!event) {
printf("❌ Failed to create event\n");
return;
}
// Get relay URLs from pool
char** relay_urls = NULL;
nostr_pool_relay_status_t* statuses = NULL;
int relay_count = nostr_relay_pool_list_relays(pool, &relay_urls, &statuses);
if (relay_count <= 0) {
printf("❌ No relays in pool\n");
cJSON_Delete(event);
return;
}
printf("📤 Publishing event to %d relay(s)...\n", relay_count);
printf("Watch for real-time responses below:\n\n");
// Setup callback context
async_publish_context_t ctx = {0};
ctx.total_relays = relay_count;
ctx.start_time = time(NULL);
// Log the event
char* event_json = cJSON_Print(event);
char timestamp[26];
ctime_r(&now, timestamp);
timestamp[24] = '\0';
dprintf(log_fd, "[%s] 📤 Publishing test event\n", timestamp);
dprintf(log_fd, "Event: %s\n\n", event_json);
free(event_json);
// Publish using async function
int sent_count = nostr_relay_pool_publish_async(pool, (const char**)relay_urls,
relay_count, event,
async_publish_callback, &ctx);
if (sent_count > 0) {
printf("📡 Event sent to %d/%d relays, waiting for responses...\n\n",
sent_count, relay_count);
// Wait for all responses or timeout (10 seconds)
time_t wait_start = time(NULL);
while (ctx.responses_received < ctx.total_relays &&
(time(NULL) - wait_start) < 10) {
// Let the polling thread process messages
usleep(100000); // 100ms
}
if (ctx.responses_received < ctx.total_relays) {
printf("\n⏰ Timeout reached - %d/%d relays responded\n",
ctx.responses_received, ctx.total_relays);
}
} else {
printf("❌ Failed to send event to any relays\n");
}
// Cleanup
for (int i = 0; i < relay_count; i++) {
free(relay_urls[i]);
}
free(relay_urls);
free(statuses);
cJSON_Delete(event);
}
int main() {
// Setup logging to file
log_fd = open("pool.log", O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (log_fd == -1) {
fprintf(stderr, "❌ Failed to open pool.log for writing\n");
return 1;
}
// Initialize NOSTR library
if (nostr_init() != NOSTR_SUCCESS) {
fprintf(stderr, "❌ Failed to initialize NOSTR library\n");
close(log_fd);
return 1;
}
// Setup signal handler
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
// Start polling thread
if (pthread_create(&poll_thread, NULL, poll_thread_func, NULL) != 0) {
fprintf(stderr, "❌ Failed to create polling thread\n");
nostr_cleanup();
close(log_fd);
return 1;
}
printf("🔗 NOSTR Relay Pool Interactive Test\n");
printf("=====================================\n");
printf("All event output is logged to pool.log\n");
printf("Press Ctrl+C to exit\n\n");
time_t now = time(NULL);
char timestamp[26];
ctime_r(&now, timestamp);
timestamp[24] = '\0';
dprintf(log_fd, "[%s] 🚀 Pool test started\n\n", timestamp);
// Main menu loop
while (running) {
print_menu();
char choice;
if (scanf("%c", &choice) != 1) {
break;
}
// Consume newline
int c;
while ((c = getchar()) != '\n' && c != EOF);
switch (choice) {
case '1': { // Start Pool
if (pool) {
printf("❌ Pool already started\n");
break;
}
// Create pool with custom reconnection configuration for faster testing
nostr_pool_reconnect_config_t config = *nostr_pool_reconnect_config_default();
config.ping_interval_seconds = 5; // Ping every 5 seconds for testing
pool = nostr_relay_pool_create(&config);
if (!pool) {
printf("❌ Failed to create pool\n");
break;
}
if (nostr_relay_pool_add_relay(pool, "ws://localhost:7555") != NOSTR_SUCCESS) {
printf("❌ Failed to add default relay\n");
nostr_relay_pool_destroy(pool);
pool = NULL;
break;
}
printf("✅ Pool started with ws://localhost:7555\n");
now = time(NULL);
ctime_r(&now, timestamp);
timestamp[24] = '\0';
dprintf(log_fd, "[%s] 🏊 Pool started with default relay\n\n", timestamp);
break;
}
case '2': { // Stop Pool
if (!pool) {
printf("❌ Pool not started\n");
break;
}
// Close all subscriptions
for (int i = 0; i < subscription_count; i++) {
if (subscriptions[i]) {
nostr_pool_subscription_close(subscriptions[i]);
}
}
free(subscriptions);
subscriptions = NULL;
subscription_count = 0;
subscription_capacity = 0;
nostr_relay_pool_destroy(pool);
pool = NULL;
printf("✅ Pool stopped\n");
now = time(NULL);
ctime_r(&now, timestamp);
timestamp[24] = '\0';
dprintf(log_fd, "[%s] 🛑 Pool stopped\n\n", timestamp);
break;
}
case '3': { // Add relay
if (!pool) {
printf("❌ Pool not started\n");
break;
}
char* url = get_input("Enter relay URL", "wss://relay.example.com");
if (url && strlen(url) > 0) {
if (nostr_relay_pool_add_relay(pool, url) == NOSTR_SUCCESS) {
printf("✅ Relay added: %s\n", url);
printf("⏳ Attempting to connect...\n");
// Give it a moment to attempt connection
sleep(2);
// Check connection status and show any errors
nostr_pool_relay_status_t status = nostr_relay_pool_get_relay_status(pool, url);
const char* error_msg = nostr_relay_pool_get_relay_last_connection_error(pool, url);
switch (status) {
case NOSTR_POOL_RELAY_CONNECTED:
printf("🟢 Successfully connected to %s\n", url);
break;
case NOSTR_POOL_RELAY_CONNECTING:
printf("🟡 Still connecting to %s...\n", url);
break;
case NOSTR_POOL_RELAY_DISCONNECTED:
printf("⚪ Disconnected from %s\n", url);
if (error_msg) {
printf(" Last error: %s\n", error_msg);
}
break;
case NOSTR_POOL_RELAY_ERROR:
printf("🔴 Connection error for %s\n", url);
if (error_msg) {
printf(" Error details: %s\n", error_msg);
}
break;
default:
printf("❓ Unknown status for %s\n", url);
break;
}
now = time(NULL);
ctime_r(&now, timestamp);
timestamp[24] = '\0';
dprintf(log_fd, "[%s] Relay added: %s (status: %d)\n", timestamp, url, status);
if (error_msg) {
dprintf(log_fd, " Connection error: %s\n", error_msg);
}
dprintf(log_fd, "\n");
} else {
printf("❌ Failed to add relay to pool\n");
}
}
free(url);
break;
}
case '4': { // Remove relay
if (!pool) {
printf("❌ Pool not started\n");
break;
}
char* url = get_input("Enter relay URL to remove", NULL);
if (url && strlen(url) > 0) {
if (nostr_relay_pool_remove_relay(pool, url) == NOSTR_SUCCESS) {
printf("✅ Relay removed: %s\n", url);
now = time(NULL);
ctime_r(&now, timestamp);
timestamp[24] = '\0';
dprintf(log_fd, "[%s] Relay removed: %s\n\n", timestamp, url);
} else {
printf("❌ Failed to remove relay\n");
}
}
free(url);
break;
}
case '5': // Add subscription
add_subscription();
break;
case '6': // Remove subscription
remove_subscription();
break;
case '7': // Show status
show_pool_status();
break;
case '8': { // Test reconnection
if (!pool) {
printf("❌ Pool not started\n");
break;
}
char** relay_urls = NULL;
nostr_pool_relay_status_t* statuses = NULL;
int relay_count = nostr_relay_pool_list_relays(pool, &relay_urls, &statuses);
if (relay_count <= 0) {
printf("❌ No relays in pool\n");
break;
}
printf("\n--- Test Reconnection ---\n");
printf("Available relays:\n");
for (int i = 0; i < relay_count; i++) {
printf("%d. %s (%s)\n", i + 1, relay_urls[i],
statuses[i] == NOSTR_POOL_RELAY_CONNECTED ? "CONNECTED" : "NOT CONNECTED");
}
char* choice_input = get_input("Enter relay number to test reconnection with", NULL);
if (!choice_input || strlen(choice_input) == 0) {
for (int i = 0; i < relay_count; i++) free(relay_urls[i]);
free(relay_urls);
free(statuses);
free(choice_input);
break;
}
int choice = atoi(choice_input) - 1;
free(choice_input);
if (choice < 0 || choice >= relay_count) {
printf("❌ Invalid relay number\n");
for (int i = 0; i < relay_count; i++) free(relay_urls[i]);
free(relay_urls);
free(statuses);
break;
}
printf("🔄 Testing reconnection with %s...\n", relay_urls[choice]);
printf(" The pool is configured with automatic reconnection enabled.\n");
printf(" If the connection drops, it will automatically attempt to reconnect\n");
printf(" with exponential backoff (1s → 2s → 4s → 8s → 16s → 30s max).\n");
printf(" Connection health is monitored with ping/pong every 30 seconds.\n");
time_t now = time(NULL);
char timestamp[26];
ctime_r(&now, timestamp);
timestamp[24] = '\0';
dprintf(log_fd, "[%s] 🔄 TEST: Testing reconnection behavior with %s\n", timestamp, relay_urls[choice]);
dprintf(log_fd, " Pool configured with: auto-reconnect=ON, max_attempts=10, ping_interval=30s\n\n");
printf("✅ Reconnection test initiated. Monitor the status and logs for reconnection activity.\n");
for (int i = 0; i < relay_count; i++) free(relay_urls[i]);
free(relay_urls);
free(statuses);
break;
}
case '9': // Publish Event
publish_event();
break;
case '0': // Exit
running = 0;
break;
default:
printf("❌ Invalid choice\n");
break;
}
}
printf("\n🧹 Cleaning up...\n");
// Stop polling thread
running = 0;
pthread_join(poll_thread, NULL);
// Clean up pool and subscriptions
if (pool) {
for (int i = 0; i < subscription_count; i++) {
if (subscriptions[i]) {
nostr_pool_subscription_close(subscriptions[i]);
}
}
free(subscriptions);
nostr_relay_pool_destroy(pool);
printf("✅ Pool destroyed\n");
}
// Cleanup
nostr_cleanup();
close(log_fd);
printf("👋 Test completed\n");
return 0;
}

BIN
examples/send_nip17_dm Executable file

Binary file not shown.

242
examples/send_nip17_dm.c Normal file
View File

@@ -0,0 +1,242 @@
/*
* NIP-17 Private Direct Messages - Command Line Application
*
* This example demonstrates how to send NIP-17 private direct messages
* using the Nostr Core Library.
*
* Usage:
* ./send_nip17_dm <recipient_pubkey> <message> [sender_nsec]
*
* Arguments:
* recipient_pubkey: The npub or hex public key of the recipient
* message: The message to send
* sender_nsec: (optional) The nsec private key to use for sending.
* If not provided, uses a default test key.
*
* Example:
* ./send_nip17_dm npub1example... "Hello from NIP-17!" nsec1test...
*/
#define _GNU_SOURCE
#define _POSIX_C_SOURCE 200809L
#include "../nostr_core/nostr_core.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
// Default test private key (for demonstration - DO NOT USE IN PRODUCTION)
#define DEFAULT_SENDER_NSEC "nsec12kgt0dv2k2safv6s32w8f89z9uw27e68hjaa0d66c5xvk70ezpwqncd045"
// Default relay for sending DMs
#define DEFAULT_RELAY "wss://relay.laantungir.net"
// Progress callback for publishing
void publish_progress_callback(const char* relay_url, const char* status,
const char* message, int success_count,
int total_relays, int completed_relays, void* user_data) {
(void)user_data;
if (relay_url) {
printf("📡 [%s]: %s", relay_url, status);
if (message) {
printf(" - %s", message);
}
printf(" (%d/%d completed, %d successful)\n", completed_relays, total_relays, success_count);
} else {
printf("📡 PUBLISH COMPLETE: %d/%d successful\n", success_count, total_relays);
}
}
/**
* Convert npub to hex if needed
*/
int convert_pubkey_to_hex(const char* input_pubkey, char* output_hex) {
// Check if it's already hex (64 characters)
if (strlen(input_pubkey) == 64) {
// Assume it's already hex
strcpy(output_hex, input_pubkey);
return 0;
}
// Check if it's an npub (starts with "npub1")
if (strncmp(input_pubkey, "npub1", 5) == 0) {
// Convert npub to hex
unsigned char pubkey_bytes[32];
if (nostr_decode_npub(input_pubkey, pubkey_bytes) != 0) {
fprintf(stderr, "Error: Invalid npub format\n");
return -1;
}
nostr_bytes_to_hex(pubkey_bytes, 32, output_hex);
return 0;
}
fprintf(stderr, "Error: Public key must be 64-character hex or valid npub\n");
return -1;
}
/**
* Convert nsec to private key bytes if needed
*/
int convert_nsec_to_private_key(const char* input_nsec, unsigned char* private_key) {
// Check if it's already hex (64 characters)
if (strlen(input_nsec) == 64) {
// Convert hex to bytes
if (nostr_hex_to_bytes(input_nsec, private_key, 32) != 0) {
fprintf(stderr, "Error: Invalid hex private key\n");
return -1;
}
return 0;
}
// Check if it's an nsec (starts with "nsec1")
if (strncmp(input_nsec, "nsec1", 5) == 0) {
// Convert nsec directly to private key bytes
if (nostr_decode_nsec(input_nsec, private_key) != 0) {
fprintf(stderr, "Error: Invalid nsec format\n");
return -1;
}
return 0;
}
fprintf(stderr, "Error: Private key must be 64-character hex or valid nsec\n");
return -1;
}
/**
* Main function
*/
int main(int argc, char* argv[]) {
if (argc < 3 || argc > 4) {
fprintf(stderr, "Usage: %s <recipient_pubkey> <message> [sender_nsec]\n\n", argv[0]);
fprintf(stderr, "Arguments:\n");
fprintf(stderr, " recipient_pubkey: npub or hex public key of recipient\n");
fprintf(stderr, " message: The message to send\n");
fprintf(stderr, " sender_nsec: (optional) nsec private key. Uses test key if not provided.\n\n");
fprintf(stderr, "Example:\n");
fprintf(stderr, " %s npub1example... \"Hello!\" nsec1test...\n", argv[0]);
return 1;
}
const char* recipient_pubkey_input = argv[1];
const char* message = argv[2];
const char* sender_nsec_input = (argc >= 4) ? argv[3] : DEFAULT_SENDER_NSEC;
printf("🧪 NIP-17 Private Direct Message Sender\n");
printf("======================================\n\n");
// Initialize crypto
if (nostr_init() != NOSTR_SUCCESS) {
fprintf(stderr, "Failed to initialize crypto\n");
return 1;
}
// Convert recipient pubkey
char recipient_pubkey_hex[65];
if (convert_pubkey_to_hex(recipient_pubkey_input, recipient_pubkey_hex) != 0) {
return 1;
}
// Convert sender private key
unsigned char sender_privkey[32];
if (convert_nsec_to_private_key(sender_nsec_input, sender_privkey) != 0) {
return 1;
}
// Derive sender public key for display
unsigned char sender_pubkey_bytes[32];
char sender_pubkey_hex[65];
if (nostr_ec_public_key_from_private_key(sender_privkey, sender_pubkey_bytes) != 0) {
fprintf(stderr, "Failed to derive sender public key\n");
return 1;
}
nostr_bytes_to_hex(sender_pubkey_bytes, 32, sender_pubkey_hex);
printf("📤 Sender: %s\n", sender_pubkey_hex);
printf("📥 Recipient: %s\n", recipient_pubkey_hex);
printf("💬 Message: %s\n", message);
printf("🌐 Relay: %s\n\n", DEFAULT_RELAY);
// Create DM event
printf("💬 Creating DM event...\n");
const char* recipient_pubkeys[] = {recipient_pubkey_hex};
cJSON* dm_event = nostr_nip17_create_chat_event(
message,
recipient_pubkeys,
1,
"NIP-17 CLI", // subject
NULL, // no reply
DEFAULT_RELAY, // relay hint
sender_pubkey_hex
);
if (!dm_event) {
fprintf(stderr, "Failed to create DM event\n");
return 1;
}
printf("✅ Created DM event (kind 14)\n");
// Send DM (create gift wraps)
printf("🎁 Creating gift wraps...\n");
cJSON* gift_wraps[10]; // Max 10 gift wraps
int gift_wrap_count = nostr_nip17_send_dm(
dm_event,
recipient_pubkeys,
1,
sender_privkey,
gift_wraps,
10
);
cJSON_Delete(dm_event); // Original DM event no longer needed
if (gift_wrap_count <= 0) {
fprintf(stderr, "Failed to create gift wraps\n");
return 1;
}
printf("✅ Created %d gift wrap(s)\n", gift_wrap_count);
// Publish the gift wrap to relay
printf("\n📤 Publishing gift wrap to relay...\n");
const char* relay_urls[] = {DEFAULT_RELAY};
int success_count = 0;
publish_result_t* publish_results = synchronous_publish_event_with_progress(
relay_urls,
1, // single relay
gift_wraps[0], // Send the first gift wrap
&success_count,
10, // 10 second timeout
publish_progress_callback,
NULL, // no user data
0, // NIP-42 disabled
NULL // no private key for auth
);
if (!publish_results || success_count != 1) {
fprintf(stderr, "\n❌ Failed to publish gift wrap (success_count: %d)\n", success_count);
// Clean up gift wraps
for (int i = 0; i < gift_wrap_count; i++) {
cJSON_Delete(gift_wraps[i]);
}
if (publish_results) free(publish_results);
return 1;
}
printf("\n✅ Successfully published NIP-17 DM!\n");
// Clean up
free(publish_results);
for (int i = 0; i < gift_wrap_count; i++) {
cJSON_Delete(gift_wraps[i]);
}
nostr_cleanup();
printf("\n🎉 DM sent successfully! The recipient can now decrypt it using their private key.\n");
return 0;
}

BIN
examples/simple_keygen Executable file

Binary file not shown.

BIN
examples/utility_functions Executable file

Binary file not shown.

BIN
examples/version_test Executable file

Binary file not shown.

151
increment_and_push.sh Executable file
View File

@@ -0,0 +1,151 @@
#!/bin/bash
# increment_and_push.sh - Version increment and git automation script
# Usage: ./increment_and_push.sh "meaningful git comment"
set -e # Exit on error
# Color constants
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
BOLD='\033[1m'
RESET='\033[0m'
# Function to print output with colors
print_info() {
if [ "$USE_COLORS" = true ]; then
echo -e "${BLUE}[INFO]${RESET} $1"
else
echo "[INFO] $1"
fi
}
print_success() {
if [ "$USE_COLORS" = true ]; then
echo -e "${GREEN}${BOLD}[SUCCESS]${RESET} $1"
else
echo "[SUCCESS] $1"
fi
}
print_warning() {
if [ "$USE_COLORS" = true ]; then
echo -e "${YELLOW}[WARNING]${RESET} $1"
else
echo "[WARNING] $1"
fi
}
print_error() {
if [ "$USE_COLORS" = true ]; then
echo -e "${RED}${BOLD}[ERROR]${RESET} $1"
else
echo "[ERROR] $1"
fi
}
# Check if we're in the correct directory
CURRENT_DIR=$(basename "$(pwd)")
if [ "$CURRENT_DIR" != "nostr_core_lib" ]; then
print_error "Script must be run from the nostr_core_lib directory"
echo ""
echo "Current directory: $CURRENT_DIR"
echo "Expected directory: nostr_core_lib"
echo ""
echo "Please change to the nostr_core_lib directory first."
echo ""
exit 1
fi
# Check if git repository exists
if ! git rev-parse --git-dir > /dev/null 2>&1; then
print_error "Not a git repository. Please initialize git first."
exit 1
fi
# Check if we have a commit message
if [ $# -eq 0 ]; then
print_error "Usage: $0 \"meaningful git comment\""
echo ""
echo "Example: $0 \"Add enhanced subscription functionality\""
echo ""
exit 1
fi
COMMIT_MESSAGE="$1"
# Check if nostr_core.h exists
if [ ! -f "nostr_core/nostr_core.h" ]; then
print_error "nostr_core/nostr_core.h not found"
exit 1
fi
print_info "Starting version increment and push process..."
# Extract current version from nostr_core.h
CURRENT_VERSION=$(grep '#define VERSION ' nostr_core/nostr_core.h | cut -d'"' -f2)
if [ -z "$CURRENT_VERSION" ]; then
print_error "Could not find VERSION define in nostr_core.h"
exit 1
fi
# Extract version components
VERSION_MAJOR=$(grep '#define VERSION_MAJOR ' nostr_core/nostr_core.h | awk '{print $3}')
VERSION_MINOR=$(grep '#define VERSION_MINOR ' nostr_core/nostr_core.h | awk '{print $3}')
VERSION_PATCH=$(grep '#define VERSION_PATCH ' nostr_core/nostr_core.h | awk '{print $3}')
if [ -z "$VERSION_MAJOR" ] || [ -z "$VERSION_MINOR" ] || [ -z "$VERSION_PATCH" ]; then
print_error "Could not extract version components from nostr_core.h"
exit 1
fi
print_info "Current version: $CURRENT_VERSION (Major: $VERSION_MAJOR, Minor: $VERSION_MINOR, Patch: $VERSION_PATCH)"
# Increment patch version
NEW_PATCH=$((VERSION_PATCH + 1))
NEW_VERSION="v$VERSION_MAJOR.$VERSION_MINOR.$NEW_PATCH"
print_info "New version will be: $NEW_VERSION"
# Update version in nostr_core.h
sed -i "s/#define VERSION .*/#define VERSION \"$NEW_VERSION\"/" nostr_core/nostr_core.h
sed -i "s/#define VERSION_PATCH .*/#define VERSION_PATCH $NEW_PATCH/" nostr_core/nostr_core.h
print_success "Updated version in nostr_core.h"
# Check if VERSION file exists and update it
if [ -f "VERSION" ]; then
echo "$VERSION_MAJOR.$VERSION_MINOR.$NEW_PATCH" > VERSION
print_success "Updated VERSION file"
fi
# Check git status
if ! git diff --quiet; then
print_info "Adding changes to git..."
git add .
print_info "Committing changes..."
git commit -m "$COMMIT_MESSAGE"
print_success "Changes committed"
else
print_warning "No changes to commit"
fi
# Create and push git tag
print_info "Creating git tag: $NEW_VERSION"
git tag "$NEW_VERSION"
print_info "Pushing commits and tags..."
CURRENT_BRANCH=$(git branch --show-current)
git push origin "$CURRENT_BRANCH"
git push origin "$NEW_VERSION"
print_success "Version $NEW_VERSION successfully released!"
print_info "Git commit: $COMMIT_MESSAGE"
print_info "Tag: $NEW_VERSION"
echo ""
echo "🎉 Release complete! Version $NEW_VERSION is now live."

File diff suppressed because it is too large Load Diff

View File

@@ -13,7 +13,7 @@
#define _GNU_SOURCE
#define _POSIX_C_SOURCE 200809L
#include "nostr_common.h"
#include "nostr_core.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -26,6 +26,9 @@
// cJSON for JSON handling
#include "../cjson/cJSON.h"
// NIP-42 Authentication
#include "nip042.h"
// =============================================================================
// TYPE DEFINITIONS FOR SYNCHRONOUS RELAY QUERIES
// =============================================================================
@@ -51,6 +54,12 @@ typedef struct {
cJSON** events; // Array of events from this relay
int events_capacity; // Allocated capacity
char subscription_id[32]; // Unique subscription ID
// NIP-42 Authentication fields
nostr_auth_state_t auth_state; // Current authentication state
char auth_challenge[NOSTR_NIP42_MAX_CHALLENGE_LENGTH]; // Stored challenge
time_t auth_challenge_time; // When challenge was received
int nip42_enabled; // Whether NIP-42 is enabled for this relay
} relay_connection_t;
@@ -65,7 +74,9 @@ cJSON** synchronous_query_relays_with_progress(
int* result_count,
int relay_timeout_seconds,
relay_progress_callback_t callback,
void* user_data) {
void* user_data,
int nip42_enabled,
const unsigned char* private_key) {
if (!relay_urls || relay_count <= 0 || !filter || !result_count) {
if (result_count) *result_count = 0;
@@ -95,11 +106,17 @@ cJSON** synchronous_query_relays_with_progress(
relays[i].last_activity = start_time;
relays[i].events_capacity = 10;
relays[i].events = malloc(relays[i].events_capacity * sizeof(cJSON*));
// Initialize NIP-42 authentication fields
relays[i].auth_state = NOSTR_AUTH_STATE_NONE;
memset(relays[i].auth_challenge, 0, sizeof(relays[i].auth_challenge));
relays[i].auth_challenge_time = 0;
relays[i].nip42_enabled = nip42_enabled;
// Generate unique subscription ID
snprintf(relays[i].subscription_id, sizeof(relays[i].subscription_id),
snprintf(relays[i].subscription_id, sizeof(relays[i].subscription_id),
"sync_%d_%ld", i, start_time);
if (callback) {
callback(relays[i].url, "connecting", NULL, 0, relay_count, 0, user_data);
}
@@ -191,19 +208,50 @@ cJSON** synchronous_query_relays_with_progress(
cJSON* parsed = NULL;
if (nostr_parse_relay_message(buffer, &msg_type, &parsed) == 0) {
if (msg_type && strcmp(msg_type, "EVENT") == 0) {
if (msg_type && strcmp(msg_type, "AUTH") == 0) {
// Handle AUTH challenge message: ["AUTH", <challenge-string>]
if (relay->nip42_enabled && private_key && cJSON_IsArray(parsed) && cJSON_GetArraySize(parsed) >= 2) {
cJSON* challenge_json = cJSON_GetArrayItem(parsed, 1);
if (cJSON_IsString(challenge_json)) {
const char* challenge = cJSON_GetStringValue(challenge_json);
// Store challenge and attempt authentication
strncpy(relay->auth_challenge, challenge, sizeof(relay->auth_challenge) - 1);
relay->auth_challenge[sizeof(relay->auth_challenge) - 1] = '\0';
relay->auth_challenge_time = time(NULL);
relay->auth_state = NOSTR_AUTH_STATE_CHALLENGE_RECEIVED;
// Create and send authentication event
cJSON* auth_event = nostr_nip42_create_auth_event(challenge, relay->url, private_key, 0);
if (auth_event) {
char* auth_message = nostr_nip42_create_auth_message(auth_event);
if (auth_message) {
if (nostr_ws_send_text(relay->client, auth_message) >= 0) {
relay->auth_state = NOSTR_AUTH_STATE_AUTHENTICATING;
if (callback) {
callback(relay->url, "authenticating", NULL, 0, relay_count, completed_relays, user_data);
}
}
free(auth_message);
}
cJSON_Delete(auth_event);
}
}
}
} else if (msg_type && strcmp(msg_type, "EVENT") == 0) {
// Handle EVENT message
if (cJSON_IsArray(parsed) && cJSON_GetArraySize(parsed) >= 3) {
cJSON* sub_id_json = cJSON_GetArrayItem(parsed, 1);
cJSON* event = cJSON_GetArrayItem(parsed, 2);
if (cJSON_IsString(sub_id_json) && event &&
strcmp(cJSON_GetStringValue(sub_id_json), relay->subscription_id) == 0) {
cJSON* event_id_json = cJSON_GetObjectItem(event, "id");
if (event_id_json && cJSON_IsString(event_id_json)) {
const char* event_id = cJSON_GetStringValue(event_id_json);
// Check for duplicate
int is_duplicate = 0;
for (int j = 0; j < seen_count; j++) {
@@ -212,31 +260,31 @@ cJSON** synchronous_query_relays_with_progress(
break;
}
}
if (!is_duplicate && seen_count < 1000) {
// New event - add to seen list
strncpy(seen_event_ids[seen_count], event_id, 64);
seen_event_ids[seen_count][64] = '\0';
seen_count++;
total_unique_events++;
// Store event in relay's array
if (relay->events_received >= relay->events_capacity) {
relay->events_capacity *= 2;
relay->events = realloc(relay->events,
relay->events = realloc(relay->events,
relay->events_capacity * sizeof(cJSON*));
}
relay->events[relay->events_received] = cJSON_Duplicate(event, 1);
relay->events_received++;
relay->state = RELAY_STATE_ACTIVE;
if (callback) {
callback(relay->url, "event_found", event_id,
relay->events_received, relay_count,
callback(relay->url, "event_found", event_id,
relay->events_received, relay_count,
completed_relays, user_data);
}
// For FIRST_RESULT mode, return immediately
if (mode == RELAY_QUERY_FIRST_RESULT) {
result_array = malloc(sizeof(cJSON*));
@@ -244,7 +292,7 @@ cJSON** synchronous_query_relays_with_progress(
result_array[0] = cJSON_Duplicate(event, 1);
*result_count = 1;
if (callback) {
callback(NULL, "first_result", event_id,
callback(NULL, "first_result", event_id,
1, relay_count, completed_relays, user_data);
}
}
@@ -254,7 +302,7 @@ cJSON** synchronous_query_relays_with_progress(
}
}
}
} else if (msg_type && strcmp(msg_type, "EOSE") == 0) {
// Handle End of Stored Events
cJSON* sub_id_json = cJSON_GetArrayItem(parsed, 1);
@@ -400,7 +448,9 @@ publish_result_t* synchronous_publish_event_with_progress(
int* success_count,
int relay_timeout_seconds,
publish_progress_callback_t callback,
void* user_data) {
void* user_data,
int nip42_enabled,
const unsigned char* private_key) {
if (!relay_urls || relay_count <= 0 || !event || !success_count) {
if (success_count) *success_count = 0;
@@ -443,7 +493,13 @@ publish_result_t* synchronous_publish_event_with_progress(
relays[i].state = RELAY_STATE_CONNECTING;
relays[i].last_activity = start_time;
results[i] = PUBLISH_ERROR; // Default to error
// Initialize NIP-42 authentication fields
relays[i].auth_state = NOSTR_AUTH_STATE_NONE;
memset(relays[i].auth_challenge, 0, sizeof(relays[i].auth_challenge));
relays[i].auth_challenge_time = 0;
relays[i].nip42_enabled = nip42_enabled;
if (callback) {
callback(relays[i].url, "connecting", NULL, 0, relay_count, 0, user_data);
}
@@ -535,34 +591,65 @@ publish_result_t* synchronous_publish_event_with_progress(
char* msg_type = NULL;
cJSON* parsed = NULL;
if (nostr_parse_relay_message(buffer, &msg_type, &parsed) == 0) {
if (msg_type && strcmp(msg_type, "OK") == 0) {
if (msg_type && strcmp(msg_type, "AUTH") == 0) {
// Handle AUTH challenge message: ["AUTH", <challenge-string>]
if (relay->nip42_enabled && private_key && cJSON_IsArray(parsed) && cJSON_GetArraySize(parsed) >= 2) {
cJSON* challenge_json = cJSON_GetArrayItem(parsed, 1);
if (cJSON_IsString(challenge_json)) {
const char* challenge = cJSON_GetStringValue(challenge_json);
// Store challenge and attempt authentication
strncpy(relay->auth_challenge, challenge, sizeof(relay->auth_challenge) - 1);
relay->auth_challenge[sizeof(relay->auth_challenge) - 1] = '\0';
relay->auth_challenge_time = time(NULL);
relay->auth_state = NOSTR_AUTH_STATE_CHALLENGE_RECEIVED;
// Create and send authentication event
cJSON* auth_event = nostr_nip42_create_auth_event(challenge, relay->url, private_key, 0);
if (auth_event) {
char* auth_message = nostr_nip42_create_auth_message(auth_event);
if (auth_message) {
if (nostr_ws_send_text(relay->client, auth_message) >= 0) {
relay->auth_state = NOSTR_AUTH_STATE_AUTHENTICATING;
if (callback) {
callback(relay->url, "authenticating", NULL, 0, relay_count, completed_relays, user_data);
}
}
free(auth_message);
}
cJSON_Delete(auth_event);
}
}
}
} else if (msg_type && strcmp(msg_type, "OK") == 0) {
// Handle OK message: ["OK", <event_id>, <true|false>, <message>]
if (cJSON_IsArray(parsed) && cJSON_GetArraySize(parsed) >= 3) {
cJSON* ok_event_id = cJSON_GetArrayItem(parsed, 1);
cJSON* accepted = cJSON_GetArrayItem(parsed, 2);
cJSON* message = cJSON_GetArrayItem(parsed, 3);
// Verify this OK is for our event
if (ok_event_id && cJSON_IsString(ok_event_id) && event_id &&
strcmp(cJSON_GetStringValue(ok_event_id), event_id) == 0) {
relay->state = RELAY_STATE_EOSE_RECEIVED; // Reuse for "completed"
active_relays--;
completed_relays++;
const char* ok_message = "";
if (message && cJSON_IsString(message)) {
ok_message = cJSON_GetStringValue(message);
}
if (accepted && cJSON_IsBool(accepted) && cJSON_IsTrue(accepted)) {
// Event accepted
results[i] = PUBLISH_SUCCESS;
(*success_count)++;
if (callback) {
callback(relay->url, "accepted", ok_message,
callback(relay->url, "accepted", ok_message,
*success_count, relay_count, completed_relays, user_data);
}
} else {
@@ -570,11 +657,11 @@ publish_result_t* synchronous_publish_event_with_progress(
results[i] = PUBLISH_REJECTED;
if (callback) {
callback(relay->url, "rejected", ok_message,
callback(relay->url, "rejected", ok_message,
*success_count, relay_count, completed_relays, user_data);
}
}
// Close connection
nostr_ws_close(relay->client);
relay->client = NULL;

View File

@@ -1,15 +1,76 @@
/*
* NOSTR AES Implementation
*
*
* Based on tiny-AES-c by kokke (public domain)
* Configured specifically for NIP-04: AES-256-CBC only
*
*
* This is an implementation of the AES algorithm, specifically CBC mode.
* Configured for AES-256 as required by NIP-04.
*/
#include <stdint.h>
#include <stddef.h>
#include <string.h> // CBC mode, for memset
#include "nostr_aes.h"
// Configure for NIP-04 requirements: AES-256-CBC only
#define CBC 1
#define ECB 0
#define CTR 0
// Configure for AES-256 (required by NIP-04)
#define AES128 0
#define AES192 0
#define AES256 1
#define AES_BLOCKLEN 16 // Block length in bytes - AES is 128b block only
#if defined(AES256) && (AES256 == 1)
#define AES_KEYLEN 32
#define AES_keyExpSize 240
#elif defined(AES192) && (AES192 == 1)
#define AES_KEYLEN 24
#define AES_keyExpSize 208
#else
#define AES_KEYLEN 16 // Key length in bytes
#define AES_keyExpSize 176
#endif
struct AES_ctx
{
uint8_t RoundKey[AES_keyExpSize];
#if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
uint8_t Iv[AES_BLOCKLEN];
#endif
};
// state - array holding the intermediate results during decryption.
typedef uint8_t state_t[4][4];
// Function prototypes (internal use only)
static void KeyExpansion(uint8_t* RoundKey, const uint8_t* Key);
static void AddRoundKey(uint8_t round, state_t* state, const uint8_t* RoundKey);
static void SubBytes(state_t* state);
static void ShiftRows(state_t* state);
static uint8_t xtime(uint8_t x);
static void MixColumns(state_t* state);
static void InvMixColumns(state_t* state);
static void InvSubBytes(state_t* state);
static void InvShiftRows(state_t* state);
static void Cipher(state_t* state, const uint8_t* RoundKey);
static void InvCipher(state_t* state, const uint8_t* RoundKey);
static void XorWithIv(uint8_t* buf, const uint8_t* Iv);
// Public functions (used by NIP-04 implementation)
void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key);
#if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv);
void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv);
#endif
#if defined(CBC) && (CBC == 1)
void AES_CBC_encrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);
void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);
#endif
// The number of columns comprising a state in AES. This is a constant in AES. Value=4
#define Nb 4

View File

@@ -1,53 +0,0 @@
#ifndef _NOSTR_AES_H_
#define _NOSTR_AES_H_
#include <stdint.h>
#include <stddef.h>
// Configure for NIP-04 requirements: AES-256-CBC only
#define CBC 1
#define ECB 0
#define CTR 0
// Configure for AES-256 (required by NIP-04)
#define AES128 0
#define AES192 0
#define AES256 1
#define AES_BLOCKLEN 16 // Block length in bytes - AES is 128b block only
#if defined(AES256) && (AES256 == 1)
#define AES_KEYLEN 32
#define AES_keyExpSize 240
#elif defined(AES192) && (AES192 == 1)
#define AES_KEYLEN 24
#define AES_keyExpSize 208
#else
#define AES_KEYLEN 16 // Key length in bytes
#define AES_keyExpSize 176
#endif
struct AES_ctx
{
uint8_t RoundKey[AES_keyExpSize];
#if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
uint8_t Iv[AES_BLOCKLEN];
#endif
};
void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key);
#if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv);
void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv);
#endif
#if defined(CBC) && (CBC == 1)
// buffer size MUST be multiple of AES_BLOCKLEN;
// Suggest https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme
// NOTES: you need to set IV in ctx via AES_init_ctx_iv() or AES_ctx_set_iv()
// no IV should ever be reused with the same key
void AES_CBC_encrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);
void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);
#endif // #if defined(CBC) && (CBC == 1)
#endif // _NOSTR_AES_H_

View File

@@ -1,15 +1,47 @@
/*
* nostr_chacha20.c - ChaCha20 stream cipher implementation
*
*
* Implementation based on RFC 8439 "ChaCha20 and Poly1305 for IETF Protocols"
*
*
* This implementation is adapted from the RFC 8439 reference specification.
* It prioritizes correctness and clarity over performance optimization.
*/
#include "nostr_chacha20.h"
#include <stdint.h>
#include <stddef.h>
#include <string.h>
/*
* ============================================================================
* CONSTANTS AND DEFINITIONS
* ============================================================================
*/
#define CHACHA20_KEY_SIZE 32 /* 256 bits */
#define CHACHA20_NONCE_SIZE 12 /* 96 bits */
#define CHACHA20_BLOCK_SIZE 64 /* 512 bits */
/*
* ============================================================================
* FUNCTION PROTOTYPES (INTERNAL USE ONLY)
* ============================================================================
*/
// Internal utility functions
static uint32_t bytes_to_u32_le(const uint8_t *bytes);
static void u32_to_bytes_le(uint32_t val, uint8_t *bytes);
// Public functions (used by NIP-44 implementation)
void chacha20_quarter_round(uint32_t state[16], int a, int b, int c, int d);
int chacha20_block(const uint8_t key[32], uint32_t counter,
const uint8_t nonce[12], uint8_t output[64]);
int chacha20_encrypt(const uint8_t key[32], uint32_t counter,
const uint8_t nonce[12], const uint8_t* input,
uint8_t* output, size_t length);
void chacha20_init_state(uint32_t state[16], const uint8_t key[32],
uint32_t counter, const uint8_t nonce[12]);
void chacha20_serialize_state(const uint32_t state[16], uint8_t output[64]);
/*
* ============================================================================
* UTILITY MACROS AND FUNCTIONS

View File

@@ -1,115 +0,0 @@
/*
* nostr_chacha20.h - ChaCha20 stream cipher implementation
*
* Implementation based on RFC 8439 "ChaCha20 and Poly1305 for IETF Protocols"
*
* This is a small, portable implementation for NIP-44 support in the NOSTR library.
* The implementation prioritizes correctness and simplicity over performance.
*/
#ifndef NOSTR_CHACHA20_H
#define NOSTR_CHACHA20_H
#include <stdint.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
* ============================================================================
* CONSTANTS AND DEFINITIONS
* ============================================================================
*/
#define CHACHA20_KEY_SIZE 32 /* 256 bits */
#define CHACHA20_NONCE_SIZE 12 /* 96 bits */
#define CHACHA20_BLOCK_SIZE 64 /* 512 bits */
/*
* ============================================================================
* CORE CHACHA20 FUNCTIONS
* ============================================================================
*/
/**
* ChaCha20 quarter round operation
*
* Operates on four 32-bit words performing the core ChaCha20 quarter round:
* a += b; d ^= a; d <<<= 16;
* c += d; b ^= c; b <<<= 12;
* a += b; d ^= a; d <<<= 8;
* c += d; b ^= c; b <<<= 7;
*
* @param state[in,out] ChaCha state as 16 32-bit words
* @param a, b, c, d Indices into state array for quarter round
*/
void chacha20_quarter_round(uint32_t state[16], int a, int b, int c, int d);
/**
* ChaCha20 block function
*
* Transforms a 64-byte input block using ChaCha20 algorithm with 20 rounds.
*
* @param key[in] 32-byte key
* @param counter[in] 32-bit block counter
* @param nonce[in] 12-byte nonce
* @param output[out] 64-byte output buffer
* @return 0 on success, negative on error
*/
int chacha20_block(const uint8_t key[32], uint32_t counter,
const uint8_t nonce[12], uint8_t output[64]);
/**
* ChaCha20 encryption/decryption
*
* Encrypts or decrypts data using ChaCha20 stream cipher.
* Since ChaCha20 is a stream cipher, encryption and decryption are the same operation.
*
* @param key[in] 32-byte key
* @param counter[in] Initial 32-bit counter value
* @param nonce[in] 12-byte nonce
* @param input[in] Input data to encrypt/decrypt
* @param output[out] Output buffer (can be same as input)
* @param length[in] Length of input data in bytes
* @return 0 on success, negative on error
*/
int chacha20_encrypt(const uint8_t key[32], uint32_t counter,
const uint8_t nonce[12], const uint8_t* input,
uint8_t* output, size_t length);
/*
* ============================================================================
* UTILITY FUNCTIONS
* ============================================================================
*/
/**
* Initialize ChaCha20 state matrix
*
* Sets up the initial 16-word state matrix with constants, key, counter, and nonce.
*
* @param state[out] 16-word state array to initialize
* @param key[in] 32-byte key
* @param counter[in] 32-bit block counter
* @param nonce[in] 12-byte nonce
*/
void chacha20_init_state(uint32_t state[16], const uint8_t key[32],
uint32_t counter, const uint8_t nonce[12]);
/**
* Serialize ChaCha20 state to bytes
*
* Converts 16 32-bit words to 64 bytes in little-endian format.
*
* @param state[in] 16-word state array
* @param output[out] 64-byte output buffer
*/
void chacha20_serialize_state(const uint32_t state[16], uint8_t output[64]);
#ifdef __cplusplus
}
#endif
#endif /* NOSTR_CHACHA20_H */

View File

@@ -1,4 +1,3 @@
#include "nostr_secp256k1.h"
#include <secp256k1.h>
#include <secp256k1_schnorrsig.h>
#include <secp256k1_ecdh.h>
@@ -6,6 +5,33 @@
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <stddef.h>
/*
* PRIVATE INTERNAL FUNCTIONS - NOT EXPORTED
* These functions are for internal library use only.
*/
/** Opaque data structure that holds a parsed and valid public key.
* Guaranteed to be 64 bytes in size, and can be safely copied/moved.
*/
typedef struct nostr_secp256k1_pubkey {
unsigned char data[64];
} nostr_secp256k1_pubkey;
/** Opaque data structure that holds a parsed keypair.
* Guaranteed to be 96 bytes in size, and can be safely copied/moved.
*/
typedef struct nostr_secp256k1_keypair {
unsigned char data[96];
} nostr_secp256k1_keypair;
/** Opaque data structure that holds a parsed x-only public key.
* Guaranteed to be 64 bytes in size, and can be safely copied/moved.
*/
typedef struct nostr_secp256k1_xonly_pubkey {
unsigned char data[64];
} nostr_secp256k1_xonly_pubkey;
// Global context for secp256k1 operations
static secp256k1_context* g_ctx = NULL;

View File

@@ -1,141 +0,0 @@
#ifndef NOSTR_SECP256K1_H
#define NOSTR_SECP256K1_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stddef.h>
/** Opaque data structure that holds a parsed and valid public key.
* Guaranteed to be 64 bytes in size, and can be safely copied/moved.
*/
typedef struct nostr_secp256k1_pubkey {
unsigned char data[64];
} nostr_secp256k1_pubkey;
/** Opaque data structure that holds a parsed keypair.
* Guaranteed to be 96 bytes in size, and can be safely copied/moved.
*/
typedef struct nostr_secp256k1_keypair {
unsigned char data[96];
} nostr_secp256k1_keypair;
/** Opaque data structure that holds a parsed x-only public key.
* Guaranteed to be 64 bytes in size, and can be safely copied/moved.
*/
typedef struct nostr_secp256k1_xonly_pubkey {
unsigned char data[64];
} nostr_secp256k1_xonly_pubkey;
/** Initialize the secp256k1 library. Must be called before any other functions.
* Returns: 1 on success, 0 on failure.
*/
int nostr_secp256k1_context_create(void);
/** Clean up the secp256k1 library resources.
*/
void nostr_secp256k1_context_destroy(void);
/** Verify an elliptic curve secret key.
* Returns: 1: secret key is valid, 0: secret key is invalid
* In: seckey: pointer to a 32-byte secret key.
*/
int nostr_secp256k1_ec_seckey_verify(const unsigned char *seckey);
/** Compute the public key for a secret key.
* Returns: 1: secret was valid, public key stored. 0: secret was invalid.
* Out: pubkey: pointer to the created public key.
* In: seckey: pointer to a 32-byte secret key.
*/
int nostr_secp256k1_ec_pubkey_create(nostr_secp256k1_pubkey *pubkey, const unsigned char *seckey);
/** Create a keypair from a secret key.
* Returns: 1: keypair created, 0: secret key invalid.
* Out: keypair: pointer to the created keypair.
* In: seckey: pointer to a 32-byte secret key.
*/
int nostr_secp256k1_keypair_create(nostr_secp256k1_keypair *keypair, const unsigned char *seckey);
/** Get the x-only public key from a keypair.
* Returns: 1 always.
* Out: pubkey: pointer to storage for the x-only public key.
* In: keypair: pointer to a keypair.
*/
int nostr_secp256k1_keypair_xonly_pub(nostr_secp256k1_xonly_pubkey *pubkey, const nostr_secp256k1_keypair *keypair);
/** Parse an x-only public key from bytes.
* Returns: 1: public key parsed, 0: invalid public key.
* Out: pubkey: pointer to the created x-only public key.
* In: input32: pointer to a 32-byte x-only public key.
*/
int nostr_secp256k1_xonly_pubkey_parse(nostr_secp256k1_xonly_pubkey *pubkey, const unsigned char *input32);
/** Serialize an x-only public key to bytes.
* Returns: 1 always.
* Out: output32: pointer to a 32-byte array to store the serialized key.
* In: pubkey: pointer to an x-only public key.
*/
int nostr_secp256k1_xonly_pubkey_serialize(unsigned char *output32, const nostr_secp256k1_xonly_pubkey *pubkey);
/** Create a Schnorr signature.
* Returns: 1: signature created, 0: nonce generation failed or secret key invalid.
* Out: sig64: pointer to a 64-byte array where the signature will be placed.
* In: msghash32: the 32-byte message hash being signed.
* keypair: pointer to an initialized keypair.
* aux_rand32: pointer to 32 bytes of auxiliary randomness (can be NULL).
*/
int nostr_secp256k1_schnorrsig_sign32(unsigned char *sig64, const unsigned char *msghash32, const nostr_secp256k1_keypair *keypair, const unsigned char *aux_rand32);
/** Verify a Schnorr signature.
* Returns: 1: correct signature, 0: incorrect signature
* In: sig64: pointer to the 64-byte signature being verified.
* msghash32: the 32-byte message hash being verified.
* pubkey: pointer to an x-only public key to verify with.
*/
int nostr_secp256k1_schnorrsig_verify(const unsigned char *sig64, const unsigned char *msghash32, const nostr_secp256k1_xonly_pubkey *pubkey);
/** Serialize a pubkey object into a serialized byte sequence.
* Returns: 1 always.
* Out: output: pointer to a 33-byte array to place the serialized key in.
* In: pubkey: pointer to a secp256k1_pubkey containing an initialized public key.
*
* The output will be a 33-byte compressed public key (0x02 or 0x03 prefix + 32 bytes x coordinate).
*/
int nostr_secp256k1_ec_pubkey_serialize_compressed(unsigned char *output, const nostr_secp256k1_pubkey *pubkey);
/** Tweak a secret key by adding a 32-byte tweak to it.
* Returns: 1: seckey was valid, 0: seckey invalid or resulting key invalid
* In/Out: seckey: pointer to a 32-byte secret key. Will be modified in-place.
* In: tweak: pointer to a 32-byte tweak.
*/
int nostr_secp256k1_ec_seckey_tweak_add(unsigned char *seckey, const unsigned char *tweak);
/** Parse a variable-length public key into the pubkey object.
* Returns: 1: public key parsed, 0: invalid public key.
* Out: pubkey: pointer to the created public key.
* In: input: pointer to a serialized public key
* inputlen: length of the array pointed to by input
*/
int nostr_secp256k1_ec_pubkey_parse(nostr_secp256k1_pubkey *pubkey, const unsigned char *input, size_t inputlen);
/** Compute an EC Diffie-Hellman secret in constant time.
* Returns: 1: exponentiation was successful, 0: scalar was invalid (zero or overflow)
* Out: result: a 32-byte array which will be populated by an ECDH secret computed from point and scalar
* In: pubkey: a pointer to a secp256k1_pubkey containing an initialized public key
* seckey: a 32-byte scalar with which to multiply the point
*/
int nostr_secp256k1_ecdh(unsigned char *result, const nostr_secp256k1_pubkey *pubkey, const unsigned char *seckey, void *hashfp, void *data);
/** Generate cryptographically secure random bytes.
* Returns: 1: success, 0: failure
* Out: buf: buffer to fill with random bytes
* In: len: number of bytes to generate
*/
int nostr_secp256k1_get_random_bytes(unsigned char *buf, size_t len);
#ifdef __cplusplus
}
#endif
#endif /* NOSTR_SECP256K1_H */

View File

@@ -13,9 +13,21 @@
#include <time.h>
#include "../nostr_core/nostr_common.h"
// Forward declarations for crypto functions (private API)
// These functions are implemented in crypto/ but not exposed through public headers
typedef struct {
unsigned char data[64];
} nostr_secp256k1_xonly_pubkey;
int nostr_secp256k1_xonly_pubkey_parse(nostr_secp256k1_xonly_pubkey* pubkey, const unsigned char* input32);
int nostr_secp256k1_schnorrsig_verify(const unsigned char* sig64, const unsigned char* msg32, const nostr_secp256k1_xonly_pubkey* pubkey);
// Declare utility functions
void nostr_bytes_to_hex(const unsigned char* bytes, size_t len, char* hex);
int nostr_hex_to_bytes(const char* hex, unsigned char* bytes, size_t len);
int nostr_sha256(const unsigned char* data, size_t len, unsigned char* hash);
int nostr_ec_public_key_from_private_key(const unsigned char* private_key, unsigned char* public_key);
int nostr_ec_sign(const unsigned char* private_key, const unsigned char* hash, unsigned char* signature);
/**
* Create and sign a NOSTR event
@@ -131,3 +143,203 @@ cJSON* nostr_create_and_sign_event(int kind, const char* content, cJSON* tags, c
return event;
}
/**
* Validate the structure of a NOSTR event
* Checks required fields, types, and basic format validation
*/
int nostr_validate_event_structure(cJSON* event) {
if (!event || !cJSON_IsObject(event)) {
return NOSTR_ERROR_EVENT_INVALID_STRUCTURE;
}
// Check required fields exist
cJSON* id_item = cJSON_GetObjectItem(event, "id");
cJSON* pubkey_item = cJSON_GetObjectItem(event, "pubkey");
cJSON* created_at_item = cJSON_GetObjectItem(event, "created_at");
cJSON* kind_item = cJSON_GetObjectItem(event, "kind");
cJSON* tags_item = cJSON_GetObjectItem(event, "tags");
cJSON* content_item = cJSON_GetObjectItem(event, "content");
cJSON* sig_item = cJSON_GetObjectItem(event, "sig");
if (!id_item || !pubkey_item || !created_at_item || !kind_item ||
!tags_item || !content_item || !sig_item) {
return NOSTR_ERROR_EVENT_INVALID_STRUCTURE;
}
// Validate field types
if (!cJSON_IsString(id_item)) return NOSTR_ERROR_EVENT_INVALID_ID;
if (!cJSON_IsString(pubkey_item)) return NOSTR_ERROR_EVENT_INVALID_PUBKEY;
if (!cJSON_IsNumber(created_at_item)) return NOSTR_ERROR_EVENT_INVALID_CREATED_AT;
if (!cJSON_IsNumber(kind_item)) return NOSTR_ERROR_EVENT_INVALID_KIND;
if (!cJSON_IsArray(tags_item)) return NOSTR_ERROR_EVENT_INVALID_TAGS;
if (!cJSON_IsString(content_item)) return NOSTR_ERROR_EVENT_INVALID_CONTENT;
if (!cJSON_IsString(sig_item)) return NOSTR_ERROR_EVENT_INVALID_SIGNATURE;
// Validate hex string lengths
const char* id_str = cJSON_GetStringValue(id_item);
const char* pubkey_str = cJSON_GetStringValue(pubkey_item);
const char* sig_str = cJSON_GetStringValue(sig_item);
if (!id_str || strlen(id_str) != 64) return NOSTR_ERROR_EVENT_INVALID_ID;
if (!pubkey_str || strlen(pubkey_str) != 64) return NOSTR_ERROR_EVENT_INVALID_PUBKEY;
if (!sig_str || strlen(sig_str) != 128) return NOSTR_ERROR_EVENT_INVALID_SIGNATURE;
// Validate hex characters (lowercase)
for (int i = 0; i < 64; i++) {
char c = id_str[i];
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) {
return NOSTR_ERROR_EVENT_INVALID_ID;
}
c = pubkey_str[i];
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) {
return NOSTR_ERROR_EVENT_INVALID_PUBKEY;
}
}
// Validate signature hex characters (lowercase) - 128 characters
for (int i = 0; i < 128; i++) {
char c = sig_str[i];
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) {
return NOSTR_ERROR_EVENT_INVALID_SIGNATURE;
}
}
// Validate created_at is a valid timestamp (positive number)
double created_at = cJSON_GetNumberValue(created_at_item);
if (created_at < 0) return NOSTR_ERROR_EVENT_INVALID_CREATED_AT;
// Validate kind is valid (0-65535)
double kind = cJSON_GetNumberValue(kind_item);
if (kind < 0 || kind > 65535 || kind != (int)kind) {
return NOSTR_ERROR_EVENT_INVALID_KIND;
}
// Validate tags array structure (array of arrays of strings)
cJSON* tag_item;
cJSON_ArrayForEach(tag_item, tags_item) {
if (!cJSON_IsArray(tag_item)) {
return NOSTR_ERROR_EVENT_INVALID_TAGS;
}
cJSON* tag_element;
cJSON_ArrayForEach(tag_element, tag_item) {
if (!cJSON_IsString(tag_element)) {
return NOSTR_ERROR_EVENT_INVALID_TAGS;
}
}
}
return NOSTR_SUCCESS;
}
/**
* Verify the cryptographic signature of a NOSTR event
* Validates event ID and signature according to NIP-01
*/
int nostr_verify_event_signature(cJSON* event) {
if (!event) {
return NOSTR_ERROR_INVALID_INPUT;
}
// Get event fields
cJSON* id_item = cJSON_GetObjectItem(event, "id");
cJSON* pubkey_item = cJSON_GetObjectItem(event, "pubkey");
cJSON* created_at_item = cJSON_GetObjectItem(event, "created_at");
cJSON* kind_item = cJSON_GetObjectItem(event, "kind");
cJSON* tags_item = cJSON_GetObjectItem(event, "tags");
cJSON* content_item = cJSON_GetObjectItem(event, "content");
cJSON* sig_item = cJSON_GetObjectItem(event, "sig");
if (!id_item || !pubkey_item || !created_at_item || !kind_item ||
!tags_item || !content_item || !sig_item) {
return NOSTR_ERROR_EVENT_INVALID_STRUCTURE;
}
// Create serialization array: [0, pubkey, created_at, kind, tags, content]
cJSON* serialize_array = cJSON_CreateArray();
if (!serialize_array) {
return NOSTR_ERROR_MEMORY_FAILED;
}
cJSON_AddItemToArray(serialize_array, cJSON_CreateNumber(0));
cJSON_AddItemToArray(serialize_array, cJSON_Duplicate(pubkey_item, 1));
cJSON_AddItemToArray(serialize_array, cJSON_Duplicate(created_at_item, 1));
cJSON_AddItemToArray(serialize_array, cJSON_Duplicate(kind_item, 1));
cJSON_AddItemToArray(serialize_array, cJSON_Duplicate(tags_item, 1));
cJSON_AddItemToArray(serialize_array, cJSON_Duplicate(content_item, 1));
char* serialize_string = cJSON_PrintUnformatted(serialize_array);
cJSON_Delete(serialize_array);
if (!serialize_string) {
return NOSTR_ERROR_MEMORY_FAILED;
}
// Hash the serialized event
unsigned char event_hash[32];
if (nostr_sha256((const unsigned char*)serialize_string, strlen(serialize_string), event_hash) != 0) {
free(serialize_string);
return NOSTR_ERROR_CRYPTO_FAILED;
}
// Convert hash to hex for event ID verification
char calculated_id[65];
nostr_bytes_to_hex(event_hash, 32, calculated_id);
// Compare with provided event ID
const char* provided_id = cJSON_GetStringValue(id_item);
if (!provided_id || strcmp(calculated_id, provided_id) != 0) {
free(serialize_string);
return NOSTR_ERROR_EVENT_INVALID_ID;
}
// Verify signature
const char* pubkey_str = cJSON_GetStringValue(pubkey_item);
const char* sig_str = cJSON_GetStringValue(sig_item);
if (!pubkey_str || !sig_str) {
free(serialize_string);
return NOSTR_ERROR_EVENT_INVALID_STRUCTURE;
}
// Convert hex strings to bytes
unsigned char pubkey_bytes[32];
unsigned char sig_bytes[64];
if (nostr_hex_to_bytes(pubkey_str, pubkey_bytes, 32) != 0 ||
nostr_hex_to_bytes(sig_str, sig_bytes, 64) != 0) {
free(serialize_string);
return NOSTR_ERROR_CRYPTO_FAILED;
}
// Parse the public key into secp256k1 format
nostr_secp256k1_xonly_pubkey xonly_pubkey;
if (!nostr_secp256k1_xonly_pubkey_parse(&xonly_pubkey, pubkey_bytes)) {
free(serialize_string);
return NOSTR_ERROR_EVENT_INVALID_PUBKEY;
}
// Verify Schnorr signature
if (!nostr_secp256k1_schnorrsig_verify(sig_bytes, event_hash, &xonly_pubkey)) {
free(serialize_string);
return NOSTR_ERROR_EVENT_INVALID_SIGNATURE;
}
free(serialize_string);
return NOSTR_SUCCESS;
}
/**
* Complete validation of a NOSTR event
* Performs both structure and cryptographic validation
*/
int nostr_validate_event(cJSON* event) {
// First validate structure (fast check)
int structure_result = nostr_validate_event_structure(event);
if (structure_result != NOSTR_SUCCESS) {
return structure_result;
}
// Then verify signature (expensive check)
return nostr_verify_event_signature(event);
}

View File

@@ -15,4 +15,9 @@
// Function declarations
cJSON* nostr_create_and_sign_event(int kind, const char* content, cJSON* tags, const unsigned char* private_key, time_t timestamp);
// Event validation functions
int nostr_validate_event_structure(cJSON* event);
int nostr_verify_event_signature(cJSON* event);
int nostr_validate_event(cJSON* event);
#endif // NIP001_H

View File

@@ -6,13 +6,24 @@
#include "nip004.h"
#include "utils.h"
#include "nostr_common.h"
#include "crypto/nostr_secp256k1.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Include our AES implementation
#include "crypto/nostr_aes.h"
// Forward declarations for crypto functions (private API)
// These functions are implemented in crypto/ but not exposed through public headers
int ecdh_shared_secret(const unsigned char* private_key, const unsigned char* public_key, unsigned char* shared_secret);
int nostr_secp256k1_get_random_bytes(unsigned char* buf, size_t len);
// AES context and functions for NIP-04 encryption
struct AES_ctx {
unsigned char RoundKey[240]; // AES-256 key expansion
unsigned char Iv[16]; // Initialization vector
};
void AES_init_ctx_iv(struct AES_ctx* ctx, const unsigned char* key, const unsigned char* iv);
void AES_CBC_encrypt_buffer(struct AES_ctx* ctx, unsigned char* buf, size_t length);
void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, unsigned char* buf, size_t length);
// Forward declarations for internal functions
static int aes_cbc_encrypt(const unsigned char* key, const unsigned char* iv,

View File

@@ -13,7 +13,7 @@ extern "C" {
#endif
// NIP-04 constants
// #define NOSTR_NIP04_MAX_PLAINTEXT_SIZE 65535
// #define NOSTR_NIP04_MAX_PLAINTEXT_SIZE 1048576 // 1MB
// NIP-04 Constants
// #define NOSTR_NIP04_MAX_PLAINTEXT_SIZE 16777216 // 16MB
// #define NOSTR_NIP04_MAX_ENCRYPTED_SIZE 22369621 // ~21.3MB (accounts for base64 overhead + IV)

View File

@@ -277,3 +277,332 @@ int nostr_add_proof_of_work(cJSON* event, const unsigned char* private_key,
// If we reach here, we've exceeded max attempts
return NOSTR_ERROR_CRYPTO_FAILED;
}
/**
* Calculate PoW difficulty (leading zero bits) for an event ID
*
* @param event_id_hex Hexadecimal event ID string (64 characters)
* @return Number of leading zero bits, or negative error code on failure
*/
int nostr_calculate_pow_difficulty(const char* event_id_hex) {
if (!event_id_hex) {
return NOSTR_ERROR_INVALID_INPUT;
}
// Validate hex string length (should be 64 characters for SHA-256)
size_t hex_len = strlen(event_id_hex);
if (hex_len != 64) {
return NOSTR_ERROR_NIP13_CALCULATION;
}
// Convert hex to bytes
unsigned char hash[32];
if (nostr_hex_to_bytes(event_id_hex, hash, 32) != NOSTR_SUCCESS) {
return NOSTR_ERROR_NIP13_CALCULATION;
}
// Use existing NIP-13 reference implementation
return count_leading_zero_bits(hash);
}
/**
* Extract nonce information from event tags
*
* @param event Complete event JSON object
* @param nonce_out Output pointer for nonce value (can be NULL)
* @param target_difficulty_out Output pointer for target difficulty (can be NULL)
* @return NOSTR_SUCCESS if nonce tag found, NOSTR_ERROR_NIP13_NO_NONCE_TAG if not found, other error codes on failure
*/
int nostr_extract_nonce_info(cJSON* event, uint64_t* nonce_out, int* target_difficulty_out) {
if (!event) {
return NOSTR_ERROR_INVALID_INPUT;
}
// Initialize output values
if (nonce_out) *nonce_out = 0;
if (target_difficulty_out) *target_difficulty_out = -1;
// Get tags array
cJSON* tags = cJSON_GetObjectItem(event, "tags");
if (!tags || !cJSON_IsArray(tags)) {
return NOSTR_ERROR_NIP13_NO_NONCE_TAG;
}
// Search for nonce tag
cJSON* tag = NULL;
cJSON_ArrayForEach(tag, tags) {
if (!cJSON_IsArray(tag) || cJSON_GetArraySize(tag) < 2) {
continue;
}
// Check if this is a nonce tag
cJSON* tag_type = cJSON_GetArrayItem(tag, 0);
if (!tag_type || !cJSON_IsString(tag_type)) {
continue;
}
const char* tag_name = cJSON_GetStringValue(tag_type);
if (!tag_name || strcmp(tag_name, "nonce") != 0) {
continue;
}
// Extract nonce value (second element)
cJSON* nonce_item = cJSON_GetArrayItem(tag, 1);
if (!nonce_item || !cJSON_IsString(nonce_item)) {
return NOSTR_ERROR_NIP13_INVALID_NONCE_TAG;
}
const char* nonce_str = cJSON_GetStringValue(nonce_item);
if (!nonce_str) {
return NOSTR_ERROR_NIP13_INVALID_NONCE_TAG;
}
// Parse nonce value
char* endptr;
uint64_t nonce_val = strtoull(nonce_str, &endptr, 10);
if (*endptr != '\0') {
return NOSTR_ERROR_NIP13_INVALID_NONCE_TAG;
}
if (nonce_out) *nonce_out = nonce_val;
// Extract target difficulty (third element, optional)
if (cJSON_GetArraySize(tag) >= 3) {
cJSON* target_item = cJSON_GetArrayItem(tag, 2);
if (target_item && cJSON_IsString(target_item)) {
const char* target_str = cJSON_GetStringValue(target_item);
if (target_str) {
char* endptr2;
long target_val = strtol(target_str, &endptr2, 10);
if (*endptr2 == '\0' && target_val >= 0) {
if (target_difficulty_out) *target_difficulty_out = (int)target_val;
}
}
}
}
return NOSTR_SUCCESS;
}
// No nonce tag found
return NOSTR_ERROR_NIP13_NO_NONCE_TAG;
}
/**
* Validate just the nonce tag format (without PoW calculation)
*
* @param nonce_tag_array JSON array representing a nonce tag
* @return NOSTR_SUCCESS if valid format, error code otherwise
*/
int nostr_validate_nonce_tag(cJSON* nonce_tag_array) {
if (!nonce_tag_array || !cJSON_IsArray(nonce_tag_array)) {
return NOSTR_ERROR_NIP13_INVALID_NONCE_TAG;
}
int array_size = cJSON_GetArraySize(nonce_tag_array);
if (array_size < 2) {
return NOSTR_ERROR_NIP13_INVALID_NONCE_TAG;
}
// First element should be "nonce"
cJSON* tag_type = cJSON_GetArrayItem(nonce_tag_array, 0);
if (!tag_type || !cJSON_IsString(tag_type)) {
return NOSTR_ERROR_NIP13_INVALID_NONCE_TAG;
}
const char* tag_name = cJSON_GetStringValue(tag_type);
if (!tag_name || strcmp(tag_name, "nonce") != 0) {
return NOSTR_ERROR_NIP13_INVALID_NONCE_TAG;
}
// Second element should be a valid nonce (numeric string)
cJSON* nonce_item = cJSON_GetArrayItem(nonce_tag_array, 1);
if (!nonce_item || !cJSON_IsString(nonce_item)) {
return NOSTR_ERROR_NIP13_INVALID_NONCE_TAG;
}
const char* nonce_str = cJSON_GetStringValue(nonce_item);
if (!nonce_str) {
return NOSTR_ERROR_NIP13_INVALID_NONCE_TAG;
}
// Validate nonce is a valid number
char* endptr;
strtoull(nonce_str, &endptr, 10);
if (*endptr != '\0') {
return NOSTR_ERROR_NIP13_INVALID_NONCE_TAG;
}
// Third element (target difficulty) is optional, but if present should be valid
if (array_size >= 3) {
cJSON* target_item = cJSON_GetArrayItem(nonce_tag_array, 2);
if (target_item && cJSON_IsString(target_item)) {
const char* target_str = cJSON_GetStringValue(target_item);
if (target_str) {
char* endptr2;
strtol(target_str, &endptr2, 10);
if (*endptr2 != '\0') {
return NOSTR_ERROR_NIP13_INVALID_NONCE_TAG;
}
}
}
}
return NOSTR_SUCCESS;
}
/**
* Validate Proof of Work for an event according to NIP-13
*
* @param event Complete event JSON object to validate
* @param min_difficulty Minimum difficulty required by the relay (0 = no requirement)
* @param validation_flags Bitflags for validation options
* @param result_info Optional output struct for detailed results (can be NULL)
* @return NOSTR_SUCCESS if PoW is valid and meets requirements, error code otherwise
*/
int nostr_validate_pow(cJSON* event, int min_difficulty, int validation_flags,
nostr_pow_result_t* result_info) {
if (!event) {
return NOSTR_ERROR_INVALID_INPUT;
}
// Initialize result info if provided
if (result_info) {
result_info->actual_difficulty = 0;
result_info->committed_target = -1;
result_info->nonce_value = 0;
result_info->has_nonce_tag = 0;
result_info->error_detail[0] = '\0';
}
// Get event ID for PoW calculation
cJSON* id_item = cJSON_GetObjectItem(event, "id");
if (!id_item || !cJSON_IsString(id_item)) {
if (result_info) {
snprintf(result_info->error_detail, sizeof(result_info->error_detail),
"Missing or invalid event ID");
}
return NOSTR_ERROR_EVENT_INVALID_ID;
}
const char* event_id = cJSON_GetStringValue(id_item);
if (!event_id) {
if (result_info) {
snprintf(result_info->error_detail, sizeof(result_info->error_detail),
"Event ID is not a string");
}
return NOSTR_ERROR_EVENT_INVALID_ID;
}
// Calculate actual PoW difficulty
int actual_difficulty = nostr_calculate_pow_difficulty(event_id);
if (actual_difficulty < 0) {
if (result_info) {
snprintf(result_info->error_detail, sizeof(result_info->error_detail),
"Failed to calculate PoW difficulty");
}
return actual_difficulty; // Return the specific error from calculation
}
if (result_info) {
result_info->actual_difficulty = actual_difficulty;
}
// Check if minimum difficulty requirement is met
if (min_difficulty > 0 && actual_difficulty < min_difficulty) {
if (result_info) {
snprintf(result_info->error_detail, sizeof(result_info->error_detail),
"Insufficient difficulty: %d < %d", actual_difficulty, min_difficulty);
}
return NOSTR_ERROR_NIP13_INSUFFICIENT;
}
// Extract nonce information if validation flags require it
uint64_t nonce_value = 0;
int committed_target = -1;
int nonce_extract_result = nostr_extract_nonce_info(event, &nonce_value, &committed_target);
if (result_info) {
result_info->nonce_value = nonce_value;
result_info->committed_target = committed_target;
result_info->has_nonce_tag = (nonce_extract_result == NOSTR_SUCCESS) ? 1 : 0;
}
// Validate nonce tag presence if required
if (validation_flags & NOSTR_POW_VALIDATE_NONCE_TAG) {
if (nonce_extract_result == NOSTR_ERROR_NIP13_NO_NONCE_TAG) {
if (result_info) {
snprintf(result_info->error_detail, sizeof(result_info->error_detail),
"Missing required nonce tag");
}
return NOSTR_ERROR_NIP13_NO_NONCE_TAG;
} else if (nonce_extract_result != NOSTR_SUCCESS) {
if (result_info) {
snprintf(result_info->error_detail, sizeof(result_info->error_detail),
"Invalid nonce tag format");
}
return nonce_extract_result;
}
// If strict format validation is enabled, validate the nonce tag structure
if (validation_flags & NOSTR_POW_STRICT_FORMAT) {
cJSON* tags = cJSON_GetObjectItem(event, "tags");
if (tags && cJSON_IsArray(tags)) {
cJSON* tag = NULL;
cJSON_ArrayForEach(tag, tags) {
if (cJSON_IsArray(tag) && cJSON_GetArraySize(tag) >= 2) {
cJSON* tag_type = cJSON_GetArrayItem(tag, 0);
if (tag_type && cJSON_IsString(tag_type)) {
const char* tag_name = cJSON_GetStringValue(tag_type);
if (tag_name && strcmp(tag_name, "nonce") == 0) {
int validation_result = nostr_validate_nonce_tag(tag);
if (validation_result != NOSTR_SUCCESS) {
if (result_info) {
snprintf(result_info->error_detail, sizeof(result_info->error_detail),
"Nonce tag failed strict format validation");
}
return validation_result;
}
break;
}
}
}
}
}
}
}
// Validate committed target difficulty if required
if (validation_flags & NOSTR_POW_VALIDATE_TARGET_COMMIT) {
if (nonce_extract_result == NOSTR_SUCCESS && committed_target == -1) {
if (result_info) {
snprintf(result_info->error_detail, sizeof(result_info->error_detail),
"Missing committed target difficulty in nonce tag");
}
return NOSTR_ERROR_NIP13_INVALID_NONCE_TAG;
}
// Check for target/difficulty mismatch if rejecting lower targets
if (validation_flags & NOSTR_POW_REJECT_LOWER_TARGET) {
// According to NIP-13: "if you require 40 bits to reply to your thread and see
// a committed target of 30, you can safely reject it even if the note has 40 bits difficulty"
// This means we reject if committed_target < min_difficulty, not actual_difficulty
if (committed_target != -1 && min_difficulty > 0 && committed_target < min_difficulty) {
if (result_info) {
snprintf(result_info->error_detail, sizeof(result_info->error_detail),
"Committed target (%d) is lower than required minimum (%d)",
committed_target, min_difficulty);
}
return NOSTR_ERROR_NIP13_TARGET_MISMATCH;
}
}
}
// All validations passed
if (result_info) {
snprintf(result_info->error_detail, sizeof(result_info->error_detail),
"PoW validation successful");
}
return NOSTR_SUCCESS;
}

View File

@@ -8,11 +8,41 @@
#include "nip001.h"
#include <stdint.h>
// PoW validation flags
#define NOSTR_POW_VALIDATE_NONCE_TAG 0x01 // Require and validate nonce tag format
#define NOSTR_POW_VALIDATE_TARGET_COMMIT 0x02 // Validate committed target difficulty
#define NOSTR_POW_REJECT_LOWER_TARGET 0x04 // Reject if committed target < actual difficulty
#define NOSTR_POW_STRICT_FORMAT 0x08 // Strict nonce tag format validation
// Convenience combinations
#define NOSTR_POW_VALIDATE_BASIC (NOSTR_POW_VALIDATE_NONCE_TAG)
#define NOSTR_POW_VALIDATE_FULL (NOSTR_POW_VALIDATE_NONCE_TAG | NOSTR_POW_VALIDATE_TARGET_COMMIT)
#define NOSTR_POW_VALIDATE_ANTI_SPAM (NOSTR_POW_VALIDATE_FULL | NOSTR_POW_REJECT_LOWER_TARGET)
// Result information structure (optional output)
typedef struct {
int actual_difficulty; // Calculated difficulty (leading zero bits)
int committed_target; // Target difficulty from nonce tag (-1 if none)
uint64_t nonce_value; // Nonce value from tag (0 if none)
int has_nonce_tag; // 1 if nonce tag present, 0 otherwise
char error_detail[256]; // Detailed error description
} nostr_pow_result_t;
// Function declarations
int nostr_add_proof_of_work(cJSON* event, const unsigned char* private_key,
int nostr_add_proof_of_work(cJSON* event, const unsigned char* private_key,
int target_difficulty, int max_attempts,
int progress_report_interval, int timestamp_update_interval,
void (*progress_callback)(int current_difficulty, uint64_t nonce, void* user_data),
void* user_data);
// PoW validation functions
int nostr_validate_pow(cJSON* event, int min_difficulty, int validation_flags,
nostr_pow_result_t* result_info);
int nostr_calculate_pow_difficulty(const char* event_id_hex);
int nostr_extract_nonce_info(cJSON* event, uint64_t* nonce_out, int* target_difficulty_out);
int nostr_validate_nonce_tag(cJSON* nonce_tag_array);
#endif // NIP013_H

407
nostr_core/nip017.c Normal file
View File

@@ -0,0 +1,407 @@
/*
* NIP-17: Private Direct Messages Implementation
* https://github.com/nostr-protocol/nips/blob/master/17.md
*/
#define _GNU_SOURCE
#include "nip017.h"
#include "nip059.h"
#include "nip001.h"
#include "utils.h"
#include "nostr_common.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// Forward declarations for crypto functions
int nostr_ec_public_key_from_private_key(const unsigned char* private_key, unsigned char* public_key);
/**
* Create tags array for DM events
*/
static cJSON* create_dm_tags(const char** recipient_pubkeys,
int num_recipients,
const char* subject,
const char* reply_to_event_id,
const char* reply_relay_url) {
cJSON* tags = cJSON_CreateArray();
if (!tags) return NULL;
// Add "p" tags for each recipient
for (int i = 0; i < num_recipients; i++) {
cJSON* p_tag = cJSON_CreateArray();
if (!p_tag) {
cJSON_Delete(tags);
return NULL;
}
cJSON_AddItemToArray(p_tag, cJSON_CreateString("p"));
cJSON_AddItemToArray(p_tag, cJSON_CreateString(recipient_pubkeys[i]));
// Add relay URL if provided (recommended)
if (reply_relay_url) {
cJSON_AddItemToArray(p_tag, cJSON_CreateString(reply_relay_url));
}
cJSON_AddItemToArray(tags, p_tag);
}
// Add subject tag if provided
if (subject && strlen(subject) > 0) {
cJSON* subject_tag = cJSON_CreateArray();
if (!subject_tag) {
cJSON_Delete(tags);
return NULL;
}
cJSON_AddItemToArray(subject_tag, cJSON_CreateString("subject"));
cJSON_AddItemToArray(subject_tag, cJSON_CreateString(subject));
cJSON_AddItemToArray(tags, subject_tag);
}
// Add reply reference if provided
if (reply_to_event_id && strlen(reply_to_event_id) > 0) {
cJSON* e_tag = cJSON_CreateArray();
if (!e_tag) {
cJSON_Delete(tags);
return NULL;
}
cJSON_AddItemToArray(e_tag, cJSON_CreateString("e"));
cJSON_AddItemToArray(e_tag, cJSON_CreateString(reply_to_event_id));
if (reply_relay_url) {
cJSON_AddItemToArray(e_tag, cJSON_CreateString(reply_relay_url));
}
// For replies, add "reply" marker as per NIP-17
cJSON_AddItemToArray(e_tag, cJSON_CreateString("reply"));
cJSON_AddItemToArray(tags, e_tag);
}
return tags;
}
/**
* NIP-17: Create a chat message event (kind 14)
*/
cJSON* nostr_nip17_create_chat_event(const char* message,
const char** recipient_pubkeys,
int num_recipients,
const char* subject,
const char* reply_to_event_id,
const char* reply_relay_url,
const char* sender_pubkey_hex) {
if (!message || !recipient_pubkeys || num_recipients <= 0 || !sender_pubkey_hex) {
return NULL;
}
// Create tags
cJSON* tags = create_dm_tags(recipient_pubkeys, num_recipients, subject,
reply_to_event_id, reply_relay_url);
if (!tags) {
return NULL;
}
// Create the chat rumor (kind 14, unsigned)
cJSON* chat_event = nostr_nip59_create_rumor(14, message, tags, sender_pubkey_hex, 0);
cJSON_Delete(tags); // Tags are duplicated in create_rumor
return chat_event;
}
/**
* NIP-17: Create a file message event (kind 15)
*/
cJSON* nostr_nip17_create_file_event(const char* file_url,
const char* file_type,
const char* encryption_algorithm,
const char* decryption_key,
const char* decryption_nonce,
const char* file_hash,
const char* original_file_hash,
size_t file_size,
const char* dimensions,
const char* blurhash,
const char* thumbnail_url,
const char** recipient_pubkeys,
int num_recipients,
const char* subject,
const char* reply_to_event_id,
const char* reply_relay_url,
const char* sender_pubkey_hex) {
if (!file_url || !file_type || !encryption_algorithm || !decryption_key ||
!decryption_nonce || !file_hash || !recipient_pubkeys ||
num_recipients <= 0 || !sender_pubkey_hex) {
return NULL;
}
// Create base tags
cJSON* tags = create_dm_tags(recipient_pubkeys, num_recipients, subject,
reply_to_event_id, reply_relay_url);
if (!tags) {
return NULL;
}
// Add file-specific tags
cJSON* file_type_tag = cJSON_CreateArray();
cJSON_AddItemToArray(file_type_tag, cJSON_CreateString("file-type"));
cJSON_AddItemToArray(file_type_tag, cJSON_CreateString(file_type));
cJSON_AddItemToArray(tags, file_type_tag);
cJSON* encryption_tag = cJSON_CreateArray();
cJSON_AddItemToArray(encryption_tag, cJSON_CreateString("encryption-algorithm"));
cJSON_AddItemToArray(encryption_tag, cJSON_CreateString(encryption_algorithm));
cJSON_AddItemToArray(tags, encryption_tag);
cJSON* key_tag = cJSON_CreateArray();
cJSON_AddItemToArray(key_tag, cJSON_CreateString("decryption-key"));
cJSON_AddItemToArray(key_tag, cJSON_CreateString(decryption_key));
cJSON_AddItemToArray(tags, key_tag);
cJSON* nonce_tag = cJSON_CreateArray();
cJSON_AddItemToArray(nonce_tag, cJSON_CreateString("decryption-nonce"));
cJSON_AddItemToArray(nonce_tag, cJSON_CreateString(decryption_nonce));
cJSON_AddItemToArray(tags, nonce_tag);
cJSON* x_tag = cJSON_CreateArray();
cJSON_AddItemToArray(x_tag, cJSON_CreateString("x"));
cJSON_AddItemToArray(x_tag, cJSON_CreateString(file_hash));
cJSON_AddItemToArray(tags, x_tag);
// Optional tags
if (original_file_hash && strlen(original_file_hash) > 0) {
cJSON* ox_tag = cJSON_CreateArray();
cJSON_AddItemToArray(ox_tag, cJSON_CreateString("ox"));
cJSON_AddItemToArray(ox_tag, cJSON_CreateString(original_file_hash));
cJSON_AddItemToArray(tags, ox_tag);
}
if (file_size > 0) {
char size_str[32];
snprintf(size_str, sizeof(size_str), "%zu", file_size);
cJSON* size_tag = cJSON_CreateArray();
cJSON_AddItemToArray(size_tag, cJSON_CreateString("size"));
cJSON_AddItemToArray(size_tag, cJSON_CreateString(size_str));
cJSON_AddItemToArray(tags, size_tag);
}
if (dimensions && strlen(dimensions) > 0) {
cJSON* dim_tag = cJSON_CreateArray();
cJSON_AddItemToArray(dim_tag, cJSON_CreateString("dim"));
cJSON_AddItemToArray(dim_tag, cJSON_CreateString(dimensions));
cJSON_AddItemToArray(tags, dim_tag);
}
if (blurhash && strlen(blurhash) > 0) {
cJSON* blurhash_tag = cJSON_CreateArray();
cJSON_AddItemToArray(blurhash_tag, cJSON_CreateString("blurhash"));
cJSON_AddItemToArray(blurhash_tag, cJSON_CreateString(blurhash));
cJSON_AddItemToArray(tags, blurhash_tag);
}
if (thumbnail_url && strlen(thumbnail_url) > 0) {
cJSON* thumb_tag = cJSON_CreateArray();
cJSON_AddItemToArray(thumb_tag, cJSON_CreateString("thumb"));
cJSON_AddItemToArray(thumb_tag, cJSON_CreateString(thumbnail_url));
cJSON_AddItemToArray(tags, thumb_tag);
}
// Create the file rumor (kind 15, unsigned)
cJSON* file_event = nostr_nip59_create_rumor(15, file_url, tags, sender_pubkey_hex, 0);
cJSON_Delete(tags); // Tags are duplicated in create_rumor
return file_event;
}
/**
* NIP-17: Create a relay list event (kind 10050)
*/
cJSON* nostr_nip17_create_relay_list_event(const char** relay_urls,
int num_relays,
const unsigned char* private_key) {
if (!relay_urls || num_relays <= 0 || !private_key) {
return NULL;
}
// Get public key
unsigned char public_key[32];
if (nostr_ec_public_key_from_private_key(private_key, public_key) != 0) {
return NULL;
}
char pubkey_hex[65];
nostr_bytes_to_hex(public_key, 32, pubkey_hex);
// Create tags with relay URLs
cJSON* tags = cJSON_CreateArray();
if (!tags) {
return NULL;
}
for (int i = 0; i < num_relays; i++) {
cJSON* relay_tag = cJSON_CreateArray();
if (!relay_tag) {
cJSON_Delete(tags);
return NULL;
}
cJSON_AddItemToArray(relay_tag, cJSON_CreateString("relay"));
cJSON_AddItemToArray(relay_tag, cJSON_CreateString(relay_urls[i]));
cJSON_AddItemToArray(tags, relay_tag);
}
// Create and sign the event
cJSON* relay_event = nostr_create_and_sign_event(10050, "", tags, private_key, time(NULL));
cJSON_Delete(tags); // Tags are duplicated in create_and_sign_event
return relay_event;
}
/**
* NIP-17: Send a direct message to recipients
*/
int nostr_nip17_send_dm(cJSON* dm_event,
const char** recipient_pubkeys,
int num_recipients,
const unsigned char* sender_private_key,
cJSON** gift_wraps_out,
int max_gift_wraps,
long max_delay_sec) {
if (!dm_event || !recipient_pubkeys || num_recipients <= 0 ||
!sender_private_key || !gift_wraps_out || max_gift_wraps <= 0) {
return -1;
}
int created_wraps = 0;
for (int i = 0; i < num_recipients && created_wraps < max_gift_wraps; i++) {
// Convert recipient pubkey hex to bytes
unsigned char recipient_public_key[32];
if (nostr_hex_to_bytes(recipient_pubkeys[i], recipient_public_key, 32) != 0) {
continue; // Skip invalid pubkeys
}
// Create seal for this recipient
cJSON* seal = nostr_nip59_create_seal(dm_event, sender_private_key, recipient_public_key, max_delay_sec);
if (!seal) {
continue; // Skip if sealing fails
}
// Create gift wrap for this recipient
cJSON* gift_wrap = nostr_nip59_create_gift_wrap(seal, recipient_pubkeys[i], max_delay_sec);
cJSON_Delete(seal); // Seal is now wrapped
if (!gift_wrap) {
continue; // Skip if wrapping fails
}
gift_wraps_out[created_wraps++] = gift_wrap;
}
// Also create a gift wrap for the sender (so they can see their own messages)
if (created_wraps < max_gift_wraps) {
// Get sender's public key
unsigned char sender_public_key[32];
if (nostr_ec_public_key_from_private_key(sender_private_key, sender_public_key) == 0) {
char sender_pubkey_hex[65];
nostr_bytes_to_hex(sender_public_key, 32, sender_pubkey_hex);
// Create seal for sender
cJSON* sender_seal = nostr_nip59_create_seal(dm_event, sender_private_key, sender_public_key, max_delay_sec);
if (sender_seal) {
// Create gift wrap for sender
cJSON* sender_gift_wrap = nostr_nip59_create_gift_wrap(sender_seal, sender_pubkey_hex, max_delay_sec);
cJSON_Delete(sender_seal);
if (sender_gift_wrap) {
gift_wraps_out[created_wraps++] = sender_gift_wrap;
}
}
}
}
return created_wraps;
}
/**
* NIP-17: Receive and decrypt a direct message
*/
cJSON* nostr_nip17_receive_dm(cJSON* gift_wrap,
const unsigned char* recipient_private_key) {
if (!gift_wrap || !recipient_private_key) {
return NULL;
}
// Unwrap the gift wrap to get the seal
cJSON* seal = nostr_nip59_unwrap_gift(gift_wrap, recipient_private_key);
if (!seal) {
return NULL;
}
// Get sender's public key from the seal
cJSON* seal_pubkey_item = cJSON_GetObjectItem(seal, "pubkey");
if (!seal_pubkey_item || !cJSON_IsString(seal_pubkey_item)) {
cJSON_Delete(seal);
return NULL;
}
const char* sender_pubkey_hex = cJSON_GetStringValue(seal_pubkey_item);
// Convert sender pubkey hex to bytes
unsigned char sender_public_key[32];
if (nostr_hex_to_bytes(sender_pubkey_hex, sender_public_key, 32) != 0) {
cJSON_Delete(seal);
return NULL;
}
// Unseal the rumor
cJSON* rumor = nostr_nip59_unseal_rumor(seal, sender_public_key, recipient_private_key);
cJSON_Delete(seal); // Seal is no longer needed
return rumor;
}
/**
* NIP-17: Extract DM relay URLs from a user's kind 10050 event
*/
int nostr_nip17_extract_dm_relays(cJSON* relay_list_event,
char** relay_urls_out,
int max_relays) {
if (!relay_list_event || !relay_urls_out || max_relays <= 0) {
return -1;
}
// Check if this is a kind 10050 event
cJSON* kind_item = cJSON_GetObjectItem(relay_list_event, "kind");
if (!kind_item || !cJSON_IsNumber(kind_item) || cJSON_GetNumberValue(kind_item) != 10050) {
return -1;
}
// Get tags array
cJSON* tags_item = cJSON_GetObjectItem(relay_list_event, "tags");
if (!tags_item || !cJSON_IsArray(tags_item)) {
return -1;
}
int extracted = 0;
cJSON* tag_item;
cJSON_ArrayForEach(tag_item, tags_item) {
if (!cJSON_IsArray(tag_item) || cJSON_GetArraySize(tag_item) < 2) {
continue;
}
// Check if this is a "relay" tag
cJSON* tag_name = cJSON_GetArrayItem(tag_item, 0);
cJSON* relay_url = cJSON_GetArrayItem(tag_item, 1);
if (cJSON_IsString(tag_name) && cJSON_IsString(relay_url) &&
strcmp(cJSON_GetStringValue(tag_name), "relay") == 0) {
if (extracted < max_relays) {
relay_urls_out[extracted] = strdup(cJSON_GetStringValue(relay_url));
if (relay_urls_out[extracted]) {
extracted++;
}
}
}
}
return extracted;
}

139
nostr_core/nip017.h Normal file
View File

@@ -0,0 +1,139 @@
/*
* NIP-17: Private Direct Messages
* https://github.com/nostr-protocol/nips/blob/master/17.md
*/
#ifndef NOSTR_NIP017_H
#define NOSTR_NIP017_H
#include <stddef.h>
#include "../cjson/cJSON.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* NIP-17: Create a chat message event (kind 14)
*
* @param message Plain text message content
* @param recipient_pubkeys Array of recipient public keys (hex strings)
* @param num_recipients Number of recipients
* @param subject Optional conversation subject/title (can be NULL)
* @param reply_to_event_id Optional event ID this message replies to (can be NULL)
* @param reply_relay_url Optional relay URL for reply reference (can be NULL)
* @param sender_pubkey_hex Sender's public key in hex format
* @return cJSON object representing the unsigned chat event, or NULL on error
*/
cJSON* nostr_nip17_create_chat_event(const char* message,
const char** recipient_pubkeys,
int num_recipients,
const char* subject,
const char* reply_to_event_id,
const char* reply_relay_url,
const char* sender_pubkey_hex);
/**
* NIP-17: Create a file message event (kind 15)
*
* @param file_url URL of the encrypted file
* @param file_type MIME type of the original file (e.g., "image/jpeg")
* @param encryption_algorithm Encryption algorithm used ("aes-gcm")
* @param decryption_key Base64-encoded decryption key
* @param decryption_nonce Base64-encoded decryption nonce
* @param file_hash SHA-256 hash of the encrypted file (hex)
* @param original_file_hash SHA-256 hash of the original file before encryption (hex, optional)
* @param file_size Size of encrypted file in bytes (optional, 0 to skip)
* @param dimensions Image dimensions in "WxH" format (optional, NULL to skip)
* @param blurhash Blurhash for preview (optional, NULL to skip)
* @param thumbnail_url URL of encrypted thumbnail (optional, NULL to skip)
* @param recipient_pubkeys Array of recipient public keys (hex strings)
* @param num_recipients Number of recipients
* @param subject Optional conversation subject/title (can be NULL)
* @param reply_to_event_id Optional event ID this message replies to (can be NULL)
* @param reply_relay_url Optional relay URL for reply reference (can be NULL)
* @param sender_pubkey_hex Sender's public key in hex format
* @return cJSON object representing the unsigned file event, or NULL on error
*/
cJSON* nostr_nip17_create_file_event(const char* file_url,
const char* file_type,
const char* encryption_algorithm,
const char* decryption_key,
const char* decryption_nonce,
const char* file_hash,
const char* original_file_hash,
size_t file_size,
const char* dimensions,
const char* blurhash,
const char* thumbnail_url,
const char** recipient_pubkeys,
int num_recipients,
const char* subject,
const char* reply_to_event_id,
const char* reply_relay_url,
const char* sender_pubkey_hex);
/**
* NIP-17: Create a relay list event (kind 10050)
*
* @param relay_urls Array of relay URLs for DM delivery
* @param num_relays Number of relay URLs
* @param private_key Sender's private key for signing
* @return cJSON object representing the signed relay list event, or NULL on error
*/
cJSON* nostr_nip17_create_relay_list_event(const char** relay_urls,
int num_relays,
const unsigned char* private_key);
/**
* NIP-17: Send a direct message to recipients
*
* This function creates the appropriate rumor, seals it, gift wraps it,
* and returns the final gift wrap events ready for publishing.
*
* @param dm_event The unsigned DM event (kind 14 or 15)
* @param recipient_pubkeys Array of recipient public keys (hex strings)
* @param num_recipients Number of recipients
* @param sender_private_key 32-byte sender private key
* @param gift_wraps_out Array to store resulting gift wrap events (caller must free)
* @param max_gift_wraps Maximum number of gift wraps to create
* @param max_delay_sec Maximum random timestamp delay in seconds (0 = no randomization)
* @return Number of gift wrap events created, or -1 on error
*/
int nostr_nip17_send_dm(cJSON* dm_event,
const char** recipient_pubkeys,
int num_recipients,
const unsigned char* sender_private_key,
cJSON** gift_wraps_out,
int max_gift_wraps,
long max_delay_sec);
/**
* NIP-17: Receive and decrypt a direct message
*
* This function unwraps a gift wrap, unseals the rumor, and returns the original DM event.
*
* @param gift_wrap The received gift wrap event (kind 1059)
* @param recipient_private_key 32-byte recipient private key
* @return cJSON object representing the decrypted DM event, or NULL on error
*/
cJSON* nostr_nip17_receive_dm(cJSON* gift_wrap,
const unsigned char* recipient_private_key);
/**
* NIP-17: Extract DM relay URLs from a user's kind 10050 event
*
* @param relay_list_event The kind 10050 event
* @param relay_urls_out Array to store extracted relay URLs (caller must free)
* @param max_relays Maximum number of relays to extract
* @return Number of relay URLs extracted, or -1 on error
*/
int nostr_nip17_extract_dm_relays(cJSON* relay_list_event,
char** relay_urls_out,
int max_relays);
#ifdef __cplusplus
}
#endif
#endif // NOSTR_NIP017_H

855
nostr_core/nip021.c Normal file
View File

@@ -0,0 +1,855 @@
/*
* NOSTR Core Library - NIP-021: nostr: URI scheme
*/
#include "nip021.h"
#include "nip019.h" // For existing bech32 functions
#include "utils.h"
#include "nostr_common.h" // For error codes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "../cjson/cJSON.h"
// Forward declarations for internal parsing functions
static int parse_nprofile_data(const uint8_t* data, size_t data_len, nostr_nprofile_t* nprofile);
static int parse_nevent_data(const uint8_t* data, size_t data_len, nostr_nevent_t* nevent);
static int parse_naddr_data(const uint8_t* data, size_t data_len, nostr_naddr_t* naddr);
// Bech32 constants and functions (copied from nip019.c for internal use)
static const char bech32_charset[] = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
static const int8_t bech32_charset_rev[128] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
15, -1, 10, 17, 21, 20, 26, 30, 7, 5, -1, -1, -1, -1, -1, -1,
-1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1,
1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1,
-1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1,
1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1
};
static uint32_t bech32_polymod_step(uint32_t pre) {
uint8_t b = pre >> 25;
return ((pre & 0x1FFFFFF) << 5) ^
(-((b >> 0) & 1) & 0x3b6a57b2UL) ^
(-((b >> 1) & 1) & 0x26508e6dUL) ^
(-((b >> 2) & 1) & 0x1ea119faUL) ^
(-((b >> 3) & 1) & 0x3d4233ddUL) ^
(-((b >> 4) & 1) & 0x2a1462b3UL);
}
static int convert_bits(uint8_t *out, size_t *outlen, int outbits, const uint8_t *in, size_t inlen, int inbits, int pad) {
uint32_t val = 0;
int bits = 0;
uint32_t maxv = (((uint32_t)1) << outbits) - 1;
*outlen = 0;
while (inlen--) {
val = (val << inbits) | *(in++);
bits += inbits;
while (bits >= outbits) {
bits -= outbits;
out[(*outlen)++] = (val >> bits) & maxv;
}
}
if (pad) {
if (bits) {
out[(*outlen)++] = (val << (outbits - bits)) & maxv;
}
} else if (((val << (outbits - bits)) & maxv) || bits >= inbits) {
return 0;
}
return 1;
}
static int bech32_encode(char *output, const char *hrp, const uint8_t *data, size_t data_len) {
uint32_t chk = 1;
size_t i, hrp_len = strlen(hrp);
for (i = 0; i < hrp_len; ++i) {
int ch = hrp[i];
if (ch < 33 || ch > 126) return 0;
if (ch >= 'A' && ch <= 'Z') return 0;
chk = bech32_polymod_step(chk) ^ (ch >> 5);
}
chk = bech32_polymod_step(chk);
for (i = 0; i < hrp_len; ++i) {
chk = bech32_polymod_step(chk) ^ (hrp[i] & 0x1f);
*(output++) = hrp[i];
}
*(output++) = '1';
for (i = 0; i < data_len; ++i) {
if (*data >> 5) return 0;
chk = bech32_polymod_step(chk) ^ (*data);
*(output++) = bech32_charset[*(data++)];
}
for (i = 0; i < 6; ++i) {
chk = bech32_polymod_step(chk);
}
chk ^= 1;
for (i = 0; i < 6; ++i) {
*(output++) = bech32_charset[(chk >> ((5 - i) * 5)) & 0x1f];
}
*output = 0;
return 1;
}
static int bech32_decode(const char* input, const char* hrp, unsigned char* data, size_t* data_len) {
if (!input || !hrp || !data || !data_len) {
return 0;
}
size_t input_len = strlen(input);
size_t hrp_len = strlen(hrp);
if (input_len < hrp_len + 7) return 0;
if (strncmp(input, hrp, hrp_len) != 0) return 0;
if (input[hrp_len] != '1') return 0;
const char* data_part = input + hrp_len + 1;
size_t data_part_len = input_len - hrp_len - 1;
uint8_t values[256];
for (size_t i = 0; i < data_part_len; i++) {
unsigned char c = (unsigned char)data_part[i];
if (c >= 128) return 0;
int8_t val = bech32_charset_rev[c];
if (val == -1) return 0;
values[i] = (uint8_t)val;
}
if (data_part_len < 6) return 0;
uint32_t chk = 1;
for (size_t i = 0; i < hrp_len; i++) {
chk = bech32_polymod_step(chk) ^ (hrp[i] >> 5);
}
chk = bech32_polymod_step(chk);
for (size_t i = 0; i < hrp_len; i++) {
chk = bech32_polymod_step(chk) ^ (hrp[i] & 0x1f);
}
for (size_t i = 0; i < data_part_len; i++) {
chk = bech32_polymod_step(chk) ^ values[i];
}
if (chk != 1) return 0;
size_t payload_len = data_part_len - 6;
size_t decoded_len;
if (!convert_bits(data, &decoded_len, 8, values, payload_len, 5, 0)) {
return 0;
}
*data_len = decoded_len;
return 1;
}
// TLV (Type-Length-Value) constants for structured data
#define TLV_SPECIAL 0
#define TLV_RELAY 1
#define TLV_AUTHOR 2
#define TLV_KIND 3
#define TLV_CREATED_AT 4
#define TLV_IDENTIFIER 5
// Forward declarations for internal functions
static int tlv_encode(const uint8_t* data, size_t data_len, uint8_t type, uint8_t** output, size_t* output_len);
static int encode_structured_bech32(const char* hrp, const uint8_t* data, size_t data_len, char* output, size_t output_size);
static int decode_structured_bech32(const char* input, const char* expected_hrp, uint8_t** data, size_t* data_len);
// Utility function to duplicate string array (removed - not used)
// Free string array
static void free_string_array(char** array, int count) {
if (!array) return;
for (int i = 0; i < count; i++) {
free(array[i]);
}
free(array);
}
// TLV encoding: Type (1 byte) + Length (1 byte) + Value
static int tlv_encode(const uint8_t* data, size_t data_len, uint8_t type, uint8_t** output, size_t* output_len) {
if (data_len > 255) return 0; // Length must fit in 1 byte
*output_len = 2 + data_len;
*output = malloc(*output_len);
if (!*output) return 0;
(*output)[0] = type;
(*output)[1] = (uint8_t)data_len;
memcpy(*output + 2, data, data_len);
return 1;
}
// TLV decoding (removed - not used)
// Encode structured data to bech32
static int encode_structured_bech32(const char* hrp, const uint8_t* data, size_t data_len, char* output, size_t output_size) {
// For simple cases like note (32 bytes), use the existing key encoding
if (strcmp(hrp, "note") == 0 && data_len == 32) {
return nostr_key_to_bech32(data, "note", output);
}
uint8_t conv[256];
size_t conv_len;
if (!convert_bits(conv, &conv_len, 5, data, data_len, 8, 1)) {
return NOSTR_ERROR_INVALID_INPUT;
}
if (!bech32_encode(output, hrp, conv, conv_len)) {
return NOSTR_ERROR_INVALID_INPUT;
}
if (strlen(output) >= output_size) {
return NOSTR_ERROR_INVALID_INPUT;
}
return NOSTR_SUCCESS;
}
// Decode structured bech32 data
static int decode_structured_bech32(const char* input, const char* expected_hrp, uint8_t** data, size_t* data_len) {
// bech32_decode already converts from 5-bit to 8-bit internally
*data = malloc(256); // Max size
if (!*data) return NOSTR_ERROR_MEMORY_FAILED;
if (!bech32_decode(input, expected_hrp, *data, data_len)) {
free(*data);
return NOSTR_ERROR_INVALID_INPUT;
}
return NOSTR_SUCCESS;
}
// Detect URI type from string
nostr_uri_type_t nostr_detect_uri_type(const char* uri) {
if (!uri) return NOSTR_URI_INVALID;
// Check for nostr: prefix
if (strncmp(uri, "nostr:", 6) != 0) {
return NOSTR_URI_INVALID;
}
const char* bech32_part = uri + 6;
// Check prefixes
if (strncmp(bech32_part, "npub1", 5) == 0) return NOSTR_URI_NPUB;
if (strncmp(bech32_part, "nsec1", 5) == 0) return NOSTR_URI_NSEC;
if (strncmp(bech32_part, "note1", 5) == 0) return NOSTR_URI_NOTE;
if (strncmp(bech32_part, "nprofile1", 9) == 0) return NOSTR_URI_NPROFILE;
if (strncmp(bech32_part, "nevent1", 7) == 0) return NOSTR_URI_NEVENT;
if (strncmp(bech32_part, "naddr1", 6) == 0) return NOSTR_URI_NADDR;
return NOSTR_URI_INVALID;
}
// Free URI result resources
void nostr_uri_result_free(nostr_uri_result_t* result) {
if (!result) return;
switch (result->type) {
case NOSTR_URI_NPROFILE:
free_string_array(result->data.nprofile.relays, result->data.nprofile.relay_count);
break;
case NOSTR_URI_NEVENT:
free_string_array(result->data.nevent.relays, result->data.nevent.relay_count);
free(result->data.nevent.author);
free(result->data.nevent.kind);
free(result->data.nevent.created_at);
break;
case NOSTR_URI_NADDR:
free(result->data.naddr.identifier);
free_string_array(result->data.naddr.relays, result->data.naddr.relay_count);
break;
default:
break;
}
}
// Main URI parsing function
int nostr_parse_uri(const char* uri, nostr_uri_result_t* result) {
if (!uri || !result) {
return NOSTR_ERROR_INVALID_INPUT;
}
memset(result, 0, sizeof(nostr_uri_result_t));
nostr_uri_type_t type = nostr_detect_uri_type(uri);
if (type == NOSTR_URI_INVALID) {
return NOSTR_ERROR_INVALID_INPUT;
}
const char* bech32_part = uri + 6; // Skip "nostr:"
result->type = type;
int ret;
switch (type) {
case NOSTR_URI_NPUB: {
ret = nostr_decode_npub(bech32_part, result->data.pubkey);
break;
}
case NOSTR_URI_NSEC: {
ret = nostr_decode_nsec(bech32_part, result->data.privkey);
break;
}
case NOSTR_URI_NOTE: {
// Note is similar to npub but with "note" prefix
uint8_t* decoded;
size_t decoded_len;
ret = decode_structured_bech32(bech32_part, "note", &decoded, &decoded_len);
if (ret == NOSTR_SUCCESS) {
if (decoded_len == 32) {
memcpy(result->data.event_id, decoded, 32);
} else {
ret = NOSTR_ERROR_INVALID_INPUT;
}
free(decoded);
}
break;
}
case NOSTR_URI_NPROFILE: {
uint8_t* decoded;
size_t decoded_len;
ret = decode_structured_bech32(bech32_part, "nprofile", &decoded, &decoded_len);
if (ret == NOSTR_SUCCESS) {
ret = parse_nprofile_data(decoded, decoded_len, &result->data.nprofile);
free(decoded);
}
break;
}
case NOSTR_URI_NEVENT: {
uint8_t* decoded;
size_t decoded_len;
ret = decode_structured_bech32(bech32_part, "nevent", &decoded, &decoded_len);
if (ret == NOSTR_SUCCESS) {
ret = parse_nevent_data(decoded, decoded_len, &result->data.nevent);
free(decoded);
}
break;
}
case NOSTR_URI_NADDR: {
uint8_t* decoded;
size_t decoded_len;
ret = decode_structured_bech32(bech32_part, "naddr", &decoded, &decoded_len);
if (ret == NOSTR_SUCCESS) {
ret = parse_naddr_data(decoded, decoded_len, &result->data.naddr);
free(decoded);
}
break;
}
default:
ret = NOSTR_ERROR_INVALID_INPUT;
break;
}
if (ret != NOSTR_SUCCESS) {
nostr_uri_result_free(result);
memset(result, 0, sizeof(nostr_uri_result_t));
}
return ret;
}
// Parse nprofile structured data
static int parse_nprofile_data(const uint8_t* data, size_t data_len, nostr_nprofile_t* nprofile) {
size_t offset = 0;
while (offset < data_len) {
if (offset + 2 > data_len) return NOSTR_ERROR_INVALID_INPUT;
uint8_t type = data[offset];
uint8_t length = data[offset + 1];
offset += 2;
if (offset + length > data_len) return NOSTR_ERROR_INVALID_INPUT;
switch (type) {
case TLV_SPECIAL: // pubkey
if (length != 32) return NOSTR_ERROR_INVALID_INPUT;
memcpy(nprofile->pubkey, data + offset, 32);
break;
case TLV_RELAY: // relay URL
{
char* relay = malloc(length + 1);
if (!relay) return NOSTR_ERROR_MEMORY_FAILED;
memcpy(relay, data + offset, length);
relay[length] = '\0';
char** new_relays = realloc(nprofile->relays, (nprofile->relay_count + 1) * sizeof(char*));
if (!new_relays) {
free(relay);
return NOSTR_ERROR_MEMORY_FAILED;
}
nprofile->relays = new_relays;
nprofile->relays[nprofile->relay_count++] = relay;
}
break;
default:
// Ignore unknown types
break;
}
offset += length;
}
return NOSTR_SUCCESS;
}
// Parse nevent structured data
static int parse_nevent_data(const uint8_t* data, size_t data_len, nostr_nevent_t* nevent) {
size_t offset = 0;
while (offset < data_len) {
if (offset + 2 > data_len) return NOSTR_ERROR_INVALID_INPUT;
uint8_t type = data[offset];
uint8_t length = data[offset + 1];
offset += 2;
if (offset + length > data_len) return NOSTR_ERROR_INVALID_INPUT;
switch (type) {
case TLV_SPECIAL: // event ID
if (length != 32) return NOSTR_ERROR_INVALID_INPUT;
memcpy(nevent->event_id, data + offset, 32);
break;
case TLV_RELAY: // relay URL
{
char* relay = malloc(length + 1);
if (!relay) return NOSTR_ERROR_MEMORY_FAILED;
memcpy(relay, data + offset, length);
relay[length] = '\0';
char** new_relays = realloc(nevent->relays, (nevent->relay_count + 1) * sizeof(char*));
if (!new_relays) {
free(relay);
return NOSTR_ERROR_MEMORY_FAILED;
}
nevent->relays = new_relays;
nevent->relays[nevent->relay_count++] = relay;
}
break;
case TLV_AUTHOR: // author pubkey
if (length != 32) return NOSTR_ERROR_INVALID_INPUT;
nevent->author = malloc(32);
if (!nevent->author) return NOSTR_ERROR_MEMORY_FAILED;
memcpy(nevent->author, data + offset, 32);
break;
case TLV_KIND: // kind
if (length != 4) return NOSTR_ERROR_INVALID_INPUT;
nevent->kind = malloc(sizeof(int));
if (!nevent->kind) return NOSTR_ERROR_MEMORY_FAILED;
*nevent->kind = (data[offset] << 24) | (data[offset+1] << 16) | (data[offset+2] << 8) | data[offset+3];
break;
case TLV_CREATED_AT: // created_at
if (length != 8) return NOSTR_ERROR_INVALID_INPUT;
nevent->created_at = malloc(sizeof(time_t));
if (!nevent->created_at) return NOSTR_ERROR_MEMORY_FAILED;
*nevent->created_at = ((time_t)data[offset] << 56) | ((time_t)data[offset+1] << 48) |
((time_t)data[offset+2] << 40) | ((time_t)data[offset+3] << 32) |
((time_t)data[offset+4] << 24) | ((time_t)data[offset+5] << 16) |
((time_t)data[offset+6] << 8) | (time_t)data[offset+7];
break;
default:
// Ignore unknown types
break;
}
offset += length;
}
return NOSTR_SUCCESS;
}
// Parse naddr structured data
static int parse_naddr_data(const uint8_t* data, size_t data_len, nostr_naddr_t* naddr) {
size_t offset = 0;
while (offset < data_len) {
if (offset + 2 > data_len) return NOSTR_ERROR_INVALID_INPUT;
uint8_t type = data[offset];
uint8_t length = data[offset + 1];
offset += 2;
if (offset + length > data_len) return NOSTR_ERROR_INVALID_INPUT;
switch (type) {
case TLV_IDENTIFIER: // identifier
naddr->identifier = malloc(length + 1);
if (!naddr->identifier) return NOSTR_ERROR_MEMORY_FAILED;
memcpy(naddr->identifier, data + offset, length);
naddr->identifier[length] = '\0';
break;
case TLV_SPECIAL: // pubkey
if (length != 32) return NOSTR_ERROR_INVALID_INPUT;
memcpy(naddr->pubkey, data + offset, 32);
break;
case TLV_KIND: // kind
if (length != 4) return NOSTR_ERROR_INVALID_INPUT;
naddr->kind = (data[offset] << 24) | (data[offset+1] << 16) | (data[offset+2] << 8) | data[offset+3];
break;
case TLV_RELAY: // relay URL
{
char* relay = malloc(length + 1);
if (!relay) return NOSTR_ERROR_MEMORY_FAILED;
memcpy(relay, data + offset, length);
relay[length] = '\0';
char** new_relays = realloc(naddr->relays, (naddr->relay_count + 1) * sizeof(char*));
if (!new_relays) {
free(relay);
return NOSTR_ERROR_MEMORY_FAILED;
}
naddr->relays = new_relays;
naddr->relays[naddr->relay_count++] = relay;
}
break;
default:
// Ignore unknown types
break;
}
offset += length;
}
return NOSTR_SUCCESS;
}
// URI construction functions
int nostr_build_uri_npub(const unsigned char* pubkey, char* output, size_t output_size) {
if (!pubkey || !output || output_size < 70) {
return NOSTR_ERROR_INVALID_INPUT;
}
char bech32[100];
int ret = nostr_key_to_bech32(pubkey, "npub", bech32);
if (ret != NOSTR_SUCCESS) return ret;
size_t len = strlen(bech32);
if (len + 7 >= output_size) return NOSTR_ERROR_INVALID_INPUT;
strcpy(output, "nostr:");
strcpy(output + 6, bech32);
return NOSTR_SUCCESS;
}
int nostr_build_uri_nsec(const unsigned char* privkey, char* output, size_t output_size) {
if (!privkey || !output || output_size < 70) {
return NOSTR_ERROR_INVALID_INPUT;
}
char bech32[100];
int ret = nostr_key_to_bech32(privkey, "nsec", bech32);
if (ret != NOSTR_SUCCESS) return ret;
size_t len = strlen(bech32);
if (len + 7 >= output_size) return NOSTR_ERROR_INVALID_INPUT;
strcpy(output, "nostr:");
strcpy(output + 6, bech32);
return NOSTR_SUCCESS;
}
// Helper to build URI with prefix
static int build_uri_with_prefix(const char* bech32, char* output, size_t output_size) {
size_t len = strlen(bech32);
if (len + 7 >= output_size) return NOSTR_ERROR_INVALID_INPUT;
strcpy(output, "nostr:");
strcpy(output + 6, bech32);
return NOSTR_SUCCESS;
}
int nostr_build_uri_note(const unsigned char* event_id, char* output, size_t output_size) {
if (!event_id || !output || output_size < 70) {
return NOSTR_ERROR_INVALID_INPUT;
}
char bech32[100];
int ret = encode_structured_bech32("note", event_id, 32, bech32, sizeof(bech32));
if (ret != NOSTR_SUCCESS) return ret;
return build_uri_with_prefix(bech32, output, output_size);
}
int nostr_build_uri_nprofile(const unsigned char* pubkey, const char** relays, int relay_count,
char* output, size_t output_size) {
if (!pubkey || !output) return NOSTR_ERROR_INVALID_INPUT;
// Build TLV data
uint8_t* data = NULL;
size_t data_len = 0;
// Add pubkey (special)
uint8_t* pubkey_tlv;
size_t pubkey_tlv_len;
if (!tlv_encode(pubkey, 32, TLV_SPECIAL, &pubkey_tlv, &pubkey_tlv_len)) {
return NOSTR_ERROR_INVALID_INPUT;
}
data = realloc(data, data_len + pubkey_tlv_len);
if (!data) {
free(pubkey_tlv);
return NOSTR_ERROR_MEMORY_FAILED;
}
memcpy(data + data_len, pubkey_tlv, pubkey_tlv_len);
data_len += pubkey_tlv_len;
free(pubkey_tlv);
// Add relays
for (int i = 0; i < relay_count; i++) {
size_t relay_len = strlen(relays[i]);
uint8_t* relay_tlv;
size_t relay_tlv_len;
if (!tlv_encode((uint8_t*)relays[i], relay_len, TLV_RELAY, &relay_tlv, &relay_tlv_len)) {
free(data);
return NOSTR_ERROR_INVALID_INPUT;
}
data = realloc(data, data_len + relay_tlv_len);
if (!data) {
free(relay_tlv);
return NOSTR_ERROR_MEMORY_FAILED;
}
memcpy(data + data_len, relay_tlv, relay_tlv_len);
data_len += relay_tlv_len;
free(relay_tlv);
}
// Encode to bech32
char bech32[500];
int ret = encode_structured_bech32("nprofile", data, data_len, bech32, sizeof(bech32));
free(data);
if (ret != NOSTR_SUCCESS) return ret;
return build_uri_with_prefix(bech32, output, output_size);
}
int nostr_build_uri_nevent(const unsigned char* event_id, const char** relays, int relay_count,
const unsigned char* author, int kind, time_t created_at,
char* output, size_t output_size) {
if (!event_id || !output) return NOSTR_ERROR_INVALID_INPUT;
// Build TLV data
uint8_t* data = NULL;
size_t data_len = 0;
// Add event_id (special)
uint8_t* event_tlv;
size_t event_tlv_len;
if (!tlv_encode(event_id, 32, TLV_SPECIAL, &event_tlv, &event_tlv_len)) {
return NOSTR_ERROR_INVALID_INPUT;
}
data = realloc(data, data_len + event_tlv_len);
if (!data) {
free(event_tlv);
return NOSTR_ERROR_MEMORY_FAILED;
}
memcpy(data + data_len, event_tlv, event_tlv_len);
data_len += event_tlv_len;
free(event_tlv);
// Add relays
for (int i = 0; i < relay_count; i++) {
size_t relay_len = strlen(relays[i]);
uint8_t* relay_tlv;
size_t relay_tlv_len;
if (!tlv_encode((uint8_t*)relays[i], relay_len, TLV_RELAY, &relay_tlv, &relay_tlv_len)) {
free(data);
return NOSTR_ERROR_INVALID_INPUT;
}
data = realloc(data, data_len + relay_tlv_len);
if (!data) {
free(relay_tlv);
return NOSTR_ERROR_MEMORY_FAILED;
}
memcpy(data + data_len, relay_tlv, relay_tlv_len);
data_len += relay_tlv_len;
free(relay_tlv);
}
// Add author if provided
if (author) {
uint8_t* author_tlv;
size_t author_tlv_len;
if (!tlv_encode(author, 32, TLV_AUTHOR, &author_tlv, &author_tlv_len)) {
free(data);
return NOSTR_ERROR_INVALID_INPUT;
}
data = realloc(data, data_len + author_tlv_len);
if (!data) {
free(author_tlv);
return NOSTR_ERROR_MEMORY_FAILED;
}
memcpy(data + data_len, author_tlv, author_tlv_len);
data_len += author_tlv_len;
free(author_tlv);
}
// Add kind if provided
if (kind >= 0) {
uint8_t kind_bytes[4];
kind_bytes[0] = (kind >> 24) & 0xFF;
kind_bytes[1] = (kind >> 16) & 0xFF;
kind_bytes[2] = (kind >> 8) & 0xFF;
kind_bytes[3] = kind & 0xFF;
uint8_t* kind_tlv;
size_t kind_tlv_len;
if (!tlv_encode(kind_bytes, 4, TLV_KIND, &kind_tlv, &kind_tlv_len)) {
free(data);
return NOSTR_ERROR_INVALID_INPUT;
}
data = realloc(data, data_len + kind_tlv_len);
if (!data) {
free(kind_tlv);
return NOSTR_ERROR_MEMORY_FAILED;
}
memcpy(data + data_len, kind_tlv, kind_tlv_len);
data_len += kind_tlv_len;
free(kind_tlv);
}
// Add created_at if provided
if (created_at > 0) {
uint8_t time_bytes[8];
time_bytes[0] = (created_at >> 56) & 0xFF;
time_bytes[1] = (created_at >> 48) & 0xFF;
time_bytes[2] = (created_at >> 40) & 0xFF;
time_bytes[3] = (created_at >> 32) & 0xFF;
time_bytes[4] = (created_at >> 24) & 0xFF;
time_bytes[5] = (created_at >> 16) & 0xFF;
time_bytes[6] = (created_at >> 8) & 0xFF;
time_bytes[7] = created_at & 0xFF;
uint8_t* time_tlv;
size_t time_tlv_len;
if (!tlv_encode(time_bytes, 8, TLV_CREATED_AT, &time_tlv, &time_tlv_len)) {
free(data);
return NOSTR_ERROR_INVALID_INPUT;
}
data = realloc(data, data_len + time_tlv_len);
if (!data) {
free(time_tlv);
return NOSTR_ERROR_MEMORY_FAILED;
}
memcpy(data + data_len, time_tlv, time_tlv_len);
data_len += time_tlv_len;
free(time_tlv);
}
// Encode to bech32
char bech32[1000];
int ret = encode_structured_bech32("nevent", data, data_len, bech32, sizeof(bech32));
free(data);
if (ret != NOSTR_SUCCESS) return ret;
return build_uri_with_prefix(bech32, output, output_size);
}
int nostr_build_uri_naddr(const char* identifier, const unsigned char* pubkey, int kind,
const char** relays, int relay_count, char* output, size_t output_size) {
if (!identifier || !pubkey || !output) return NOSTR_ERROR_INVALID_INPUT;
// Build TLV data
uint8_t* data = NULL;
size_t data_len = 0;
// Add identifier
size_t id_len = strlen(identifier);
uint8_t* id_tlv;
size_t id_tlv_len;
if (!tlv_encode((uint8_t*)identifier, id_len, TLV_IDENTIFIER, &id_tlv, &id_tlv_len)) {
return NOSTR_ERROR_INVALID_INPUT;
}
data = realloc(data, data_len + id_tlv_len);
if (!data) {
free(id_tlv);
return NOSTR_ERROR_MEMORY_FAILED;
}
memcpy(data + data_len, id_tlv, id_tlv_len);
data_len += id_tlv_len;
free(id_tlv);
// Add pubkey (special)
uint8_t* pubkey_tlv;
size_t pubkey_tlv_len;
if (!tlv_encode(pubkey, 32, TLV_SPECIAL, &pubkey_tlv, &pubkey_tlv_len)) {
free(data);
return NOSTR_ERROR_INVALID_INPUT;
}
data = realloc(data, data_len + pubkey_tlv_len);
if (!data) {
free(pubkey_tlv);
return NOSTR_ERROR_MEMORY_FAILED;
}
memcpy(data + data_len, pubkey_tlv, pubkey_tlv_len);
data_len += pubkey_tlv_len;
free(pubkey_tlv);
// Add kind
uint8_t kind_bytes[4];
kind_bytes[0] = (kind >> 24) & 0xFF;
kind_bytes[1] = (kind >> 16) & 0xFF;
kind_bytes[2] = (kind >> 8) & 0xFF;
kind_bytes[3] = kind & 0xFF;
uint8_t* kind_tlv;
size_t kind_tlv_len;
if (!tlv_encode(kind_bytes, 4, TLV_KIND, &kind_tlv, &kind_tlv_len)) {
free(data);
return NOSTR_ERROR_INVALID_INPUT;
}
data = realloc(data, data_len + kind_tlv_len);
if (!data) {
free(kind_tlv);
return NOSTR_ERROR_MEMORY_FAILED;
}
memcpy(data + data_len, kind_tlv, kind_tlv_len);
data_len += kind_tlv_len;
free(kind_tlv);
// Add relays
for (int i = 0; i < relay_count; i++) {
size_t relay_len = strlen(relays[i]);
uint8_t* relay_tlv;
size_t relay_tlv_len;
if (!tlv_encode((uint8_t*)relays[i], relay_len, TLV_RELAY, &relay_tlv, &relay_tlv_len)) {
free(data);
return NOSTR_ERROR_INVALID_INPUT;
}
data = realloc(data, data_len + relay_tlv_len);
if (!data) {
free(relay_tlv);
return NOSTR_ERROR_MEMORY_FAILED;
}
memcpy(data + data_len, relay_tlv, relay_tlv_len);
data_len += relay_tlv_len;
free(relay_tlv);
}
// Encode to bech32
char bech32[1000];
int ret = encode_structured_bech32("naddr", data, data_len, bech32, sizeof(bech32));
free(data);
if (ret != NOSTR_SUCCESS) return ret;
return build_uri_with_prefix(bech32, output, output_size);
}

81
nostr_core/nip021.h Normal file
View File

@@ -0,0 +1,81 @@
/*
* NOSTR Core Library - NIP-021: nostr: URI scheme
*/
#ifndef NIP021_H
#define NIP021_H
#include <stdint.h>
#include <time.h>
#include "nip001.h"
// URI type enumeration
typedef enum {
NOSTR_URI_NPUB, // Simple 32-byte pubkey
NOSTR_URI_NSEC, // Simple 32-byte privkey
NOSTR_URI_NOTE, // Simple 32-byte event ID
NOSTR_URI_NPROFILE, // Structured: pubkey + relays
NOSTR_URI_NEVENT, // Structured: event ID + relays + metadata
NOSTR_URI_NADDR, // Structured: address + relays + metadata
NOSTR_URI_INVALID
} nostr_uri_type_t;
// Structured data types for complex URIs
typedef struct {
unsigned char pubkey[32];
char** relays;
int relay_count;
} nostr_nprofile_t;
typedef struct {
unsigned char event_id[32];
char** relays;
int relay_count;
unsigned char* author; // Optional, 32 bytes if present
int* kind; // Optional
time_t* created_at; // Optional
} nostr_nevent_t;
typedef struct {
char* identifier;
unsigned char pubkey[32];
int kind;
char** relays;
int relay_count;
} nostr_naddr_t;
// Unified URI result structure
typedef struct {
nostr_uri_type_t type;
union {
unsigned char pubkey[32]; // For NPUB
unsigned char privkey[32]; // For NSEC
unsigned char event_id[32]; // For NOTE
nostr_nprofile_t nprofile; // For NPROFILE
nostr_nevent_t nevent; // For NEVENT
nostr_naddr_t naddr; // For NADDR
} data;
} nostr_uri_result_t;
// Function declarations
// Main parsing function - unified entry point
int nostr_parse_uri(const char* uri, nostr_uri_result_t* result);
// URI construction functions
int nostr_build_uri_npub(const unsigned char* pubkey, char* output, size_t output_size);
int nostr_build_uri_nsec(const unsigned char* privkey, char* output, size_t output_size);
int nostr_build_uri_note(const unsigned char* event_id, char* output, size_t output_size);
int nostr_build_uri_nprofile(const unsigned char* pubkey, const char** relays, int relay_count,
char* output, size_t output_size);
int nostr_build_uri_nevent(const unsigned char* event_id, const char** relays, int relay_count,
const unsigned char* author, int kind, time_t created_at,
char* output, size_t output_size);
int nostr_build_uri_naddr(const char* identifier, const unsigned char* pubkey, int kind,
const char** relays, int relay_count, char* output, size_t output_size);
// Utility functions
void nostr_uri_result_free(nostr_uri_result_t* result);
nostr_uri_type_t nostr_detect_uri_type(const char* uri);
#endif // NIP021_H

628
nostr_core/nip042.c Normal file
View File

@@ -0,0 +1,628 @@
/*
* NOSTR Core Library - NIP-042: Authentication of clients to relays
*
* Implements client authentication through signed ephemeral events
*/
#include "nip042.h"
#include "nip001.h"
#include "utils.h"
#include "../cjson/cJSON.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// Forward declarations for crypto functions
int nostr_secp256k1_get_random_bytes(unsigned char* buf, size_t len);
// =============================================================================
// CLIENT-SIDE FUNCTIONS
// =============================================================================
/**
* Create NIP-42 authentication event (kind 22242)
*/
cJSON* nostr_nip42_create_auth_event(const char* challenge,
const char* relay_url,
const unsigned char* private_key,
time_t timestamp) {
if (!challenge || !relay_url || !private_key) {
return NULL;
}
// Validate challenge format
size_t challenge_len = strlen(challenge);
if (challenge_len < NOSTR_NIP42_MIN_CHALLENGE_LENGTH ||
challenge_len >= NOSTR_NIP42_MAX_CHALLENGE_LENGTH) {
return NULL;
}
// Create tags array with relay and challenge
cJSON* tags = cJSON_CreateArray();
if (!tags) {
return NULL;
}
// Add relay tag
cJSON* relay_tag = cJSON_CreateArray();
if (!relay_tag) {
cJSON_Delete(tags);
return NULL;
}
cJSON_AddItemToArray(relay_tag, cJSON_CreateString("relay"));
cJSON_AddItemToArray(relay_tag, cJSON_CreateString(relay_url));
cJSON_AddItemToArray(tags, relay_tag);
// Add challenge tag
cJSON* challenge_tag = cJSON_CreateArray();
if (!challenge_tag) {
cJSON_Delete(tags);
return NULL;
}
cJSON_AddItemToArray(challenge_tag, cJSON_CreateString("challenge"));
cJSON_AddItemToArray(challenge_tag, cJSON_CreateString(challenge));
cJSON_AddItemToArray(tags, challenge_tag);
// Create authentication event using existing function
// Note: Empty content as per NIP-42 specification
cJSON* auth_event = nostr_create_and_sign_event(
NOSTR_NIP42_AUTH_EVENT_KIND,
"", // Empty content
tags,
private_key,
timestamp
);
cJSON_Delete(tags);
return auth_event;
}
/**
* Create AUTH message JSON for relay communication
*/
char* nostr_nip42_create_auth_message(cJSON* auth_event) {
if (!auth_event) {
return NULL;
}
// Create AUTH message array: ["AUTH", <event-json>]
cJSON* message_array = cJSON_CreateArray();
if (!message_array) {
return NULL;
}
cJSON_AddItemToArray(message_array, cJSON_CreateString("AUTH"));
cJSON_AddItemToArray(message_array, cJSON_Duplicate(auth_event, 1));
char* message_string = cJSON_PrintUnformatted(message_array);
cJSON_Delete(message_array);
return message_string;
}
/**
* Validate challenge string format and freshness
*/
int nostr_nip42_validate_challenge(const char* challenge,
time_t received_at,
int time_tolerance) {
if (!challenge) {
return NOSTR_ERROR_INVALID_INPUT;
}
size_t challenge_len = strlen(challenge);
// Check challenge length
if (challenge_len < NOSTR_NIP42_MIN_CHALLENGE_LENGTH) {
return NOSTR_ERROR_NIP42_CHALLENGE_TOO_SHORT;
}
if (challenge_len >= NOSTR_NIP42_MAX_CHALLENGE_LENGTH) {
return NOSTR_ERROR_NIP42_CHALLENGE_TOO_LONG;
}
// Check time validity if provided
if (received_at > 0) {
time_t now = time(NULL);
int tolerance = (time_tolerance > 0) ? time_tolerance : NOSTR_NIP42_DEFAULT_TIME_TOLERANCE;
if (now - received_at > tolerance) {
return NOSTR_ERROR_NIP42_CHALLENGE_EXPIRED;
}
}
return NOSTR_SUCCESS;
}
/**
* Parse AUTH challenge message from relay
*/
int nostr_nip42_parse_auth_challenge(const char* message,
char* challenge_out,
size_t challenge_size) {
if (!message || !challenge_out || challenge_size == 0) {
return NOSTR_ERROR_INVALID_INPUT;
}
cJSON* json = cJSON_Parse(message);
if (!json || !cJSON_IsArray(json)) {
if (json) cJSON_Delete(json);
return NOSTR_ERROR_NIP42_INVALID_MESSAGE_FORMAT;
}
// Check array has exactly 2 elements
if (cJSON_GetArraySize(json) != 2) {
cJSON_Delete(json);
return NOSTR_ERROR_NIP42_INVALID_MESSAGE_FORMAT;
}
// Check first element is "AUTH"
cJSON* message_type = cJSON_GetArrayItem(json, 0);
if (!message_type || !cJSON_IsString(message_type) ||
strcmp(cJSON_GetStringValue(message_type), "AUTH") != 0) {
cJSON_Delete(json);
return NOSTR_ERROR_NIP42_INVALID_MESSAGE_FORMAT;
}
// Get challenge string
cJSON* challenge_item = cJSON_GetArrayItem(json, 1);
if (!challenge_item || !cJSON_IsString(challenge_item)) {
cJSON_Delete(json);
return NOSTR_ERROR_NIP42_INVALID_MESSAGE_FORMAT;
}
const char* challenge_str = cJSON_GetStringValue(challenge_item);
if (!challenge_str || strlen(challenge_str) >= challenge_size) {
cJSON_Delete(json);
return NOSTR_ERROR_NIP42_INVALID_CHALLENGE;
}
strcpy(challenge_out, challenge_str);
cJSON_Delete(json);
return NOSTR_SUCCESS;
}
// =============================================================================
// SERVER-SIDE FUNCTIONS
// =============================================================================
/**
* Generate cryptographically secure challenge string
*/
int nostr_nip42_generate_challenge(char* challenge_out, size_t length) {
if (!challenge_out || length < NOSTR_NIP42_MIN_CHALLENGE_LENGTH ||
length > NOSTR_NIP42_MAX_CHALLENGE_LENGTH / 2) {
return NOSTR_ERROR_INVALID_INPUT;
}
// Generate random bytes
unsigned char random_bytes[NOSTR_NIP42_MAX_CHALLENGE_LENGTH / 2];
if (nostr_secp256k1_get_random_bytes(random_bytes, length) != 1) {
return NOSTR_ERROR_CRYPTO_FAILED;
}
// Convert to hex string (reusing existing function)
nostr_bytes_to_hex(random_bytes, length, challenge_out);
return NOSTR_SUCCESS;
}
/**
* Verify NIP-42 authentication event
*/
int nostr_nip42_verify_auth_event(cJSON* auth_event,
const char* expected_challenge,
const char* relay_url,
int time_tolerance) {
if (!auth_event || !expected_challenge || !relay_url) {
return NOSTR_ERROR_INVALID_INPUT;
}
// First validate basic event structure using existing function
int structure_result = nostr_validate_event_structure(auth_event);
if (structure_result != NOSTR_SUCCESS) {
return structure_result;
}
// Validate NIP-42 specific structure
int nip42_structure_result = nostr_nip42_validate_auth_event_structure(
auth_event, relay_url, expected_challenge, time_tolerance);
if (nip42_structure_result != NOSTR_SUCCESS) {
return nip42_structure_result;
}
// Finally verify cryptographic signature using existing function
return nostr_verify_event_signature(auth_event);
}
/**
* Parse AUTH message from client
*/
int nostr_nip42_parse_auth_message(const char* message, cJSON** auth_event_out) {
if (!message || !auth_event_out) {
return NOSTR_ERROR_INVALID_INPUT;
}
cJSON* json = cJSON_Parse(message);
if (!json || !cJSON_IsArray(json)) {
if (json) cJSON_Delete(json);
return NOSTR_ERROR_NIP42_INVALID_MESSAGE_FORMAT;
}
// Check array has exactly 2 elements
if (cJSON_GetArraySize(json) != 2) {
cJSON_Delete(json);
return NOSTR_ERROR_NIP42_INVALID_MESSAGE_FORMAT;
}
// Check first element is "AUTH"
cJSON* message_type = cJSON_GetArrayItem(json, 0);
if (!message_type || !cJSON_IsString(message_type) ||
strcmp(cJSON_GetStringValue(message_type), "AUTH") != 0) {
cJSON_Delete(json);
return NOSTR_ERROR_NIP42_INVALID_MESSAGE_FORMAT;
}
// Get event object
cJSON* event_item = cJSON_GetArrayItem(json, 1);
if (!event_item || !cJSON_IsObject(event_item)) {
cJSON_Delete(json);
return NOSTR_ERROR_NIP42_INVALID_MESSAGE_FORMAT;
}
// Duplicate the event for the caller
*auth_event_out = cJSON_Duplicate(event_item, 1);
cJSON_Delete(json);
if (!*auth_event_out) {
return NOSTR_ERROR_MEMORY_FAILED;
}
return NOSTR_SUCCESS;
}
/**
* Create "auth-required" error response
*/
char* nostr_nip42_create_auth_required_message(const char* subscription_id,
const char* event_id,
const char* reason) {
const char* default_reason = "authentication required";
const char* message_reason = reason ? reason : default_reason;
cJSON* response = cJSON_CreateArray();
if (!response) {
return NULL;
}
if (subscription_id) {
// CLOSED message for subscriptions
cJSON_AddItemToArray(response, cJSON_CreateString("CLOSED"));
cJSON_AddItemToArray(response, cJSON_CreateString(subscription_id));
char prefix_message[512];
snprintf(prefix_message, sizeof(prefix_message), "auth-required: %s", message_reason);
cJSON_AddItemToArray(response, cJSON_CreateString(prefix_message));
} else if (event_id) {
// OK message for events
cJSON_AddItemToArray(response, cJSON_CreateString("OK"));
cJSON_AddItemToArray(response, cJSON_CreateString(event_id));
cJSON_AddItemToArray(response, cJSON_CreateBool(0)); // false
char prefix_message[512];
snprintf(prefix_message, sizeof(prefix_message), "auth-required: %s", message_reason);
cJSON_AddItemToArray(response, cJSON_CreateString(prefix_message));
} else {
cJSON_Delete(response);
return NULL;
}
char* message_string = cJSON_PrintUnformatted(response);
cJSON_Delete(response);
return message_string;
}
/**
* Create "restricted" error response
*/
char* nostr_nip42_create_restricted_message(const char* subscription_id,
const char* event_id,
const char* reason) {
const char* default_reason = "access restricted";
const char* message_reason = reason ? reason : default_reason;
cJSON* response = cJSON_CreateArray();
if (!response) {
return NULL;
}
if (subscription_id) {
// CLOSED message for subscriptions
cJSON_AddItemToArray(response, cJSON_CreateString("CLOSED"));
cJSON_AddItemToArray(response, cJSON_CreateString(subscription_id));
char prefix_message[512];
snprintf(prefix_message, sizeof(prefix_message), "restricted: %s", message_reason);
cJSON_AddItemToArray(response, cJSON_CreateString(prefix_message));
} else if (event_id) {
// OK message for events
cJSON_AddItemToArray(response, cJSON_CreateString("OK"));
cJSON_AddItemToArray(response, cJSON_CreateString(event_id));
cJSON_AddItemToArray(response, cJSON_CreateBool(0)); // false
char prefix_message[512];
snprintf(prefix_message, sizeof(prefix_message), "restricted: %s", message_reason);
cJSON_AddItemToArray(response, cJSON_CreateString(prefix_message));
} else {
cJSON_Delete(response);
return NULL;
}
char* message_string = cJSON_PrintUnformatted(response);
cJSON_Delete(response);
return message_string;
}
// =============================================================================
// URL NORMALIZATION FUNCTIONS
// =============================================================================
/**
* Normalize relay URL for comparison
*/
char* nostr_nip42_normalize_url(const char* url) {
if (!url) {
return NULL;
}
size_t url_len = strlen(url);
char* normalized = malloc(url_len + 1);
if (!normalized) {
return NULL;
}
strcpy(normalized, url);
// Remove trailing slash
if (url_len > 1 && normalized[url_len - 1] == '/') {
normalized[url_len - 1] = '\0';
}
// Convert to lowercase for domain comparison
for (size_t i = 0; normalized[i]; i++) {
if (normalized[i] >= 'A' && normalized[i] <= 'Z') {
normalized[i] = normalized[i] + ('a' - 'A');
}
}
return normalized;
}
/**
* Check if two relay URLs match after normalization
*/
int nostr_nip42_urls_match(const char* url1, const char* url2) {
if (!url1 || !url2) {
return -1;
}
char* norm1 = nostr_nip42_normalize_url(url1);
char* norm2 = nostr_nip42_normalize_url(url2);
if (!norm1 || !norm2) {
free(norm1);
free(norm2);
return -1;
}
int result = (strcmp(norm1, norm2) == 0) ? 1 : 0;
free(norm1);
free(norm2);
return result;
}
// =============================================================================
// UTILITY FUNCTIONS
// =============================================================================
/**
* Get string description of authentication state
*/
const char* nostr_nip42_auth_state_str(nostr_auth_state_t state) {
switch (state) {
case NOSTR_AUTH_STATE_NONE:
return "none";
case NOSTR_AUTH_STATE_CHALLENGE_RECEIVED:
return "challenge_received";
case NOSTR_AUTH_STATE_AUTHENTICATING:
return "authenticating";
case NOSTR_AUTH_STATE_AUTHENTICATED:
return "authenticated";
case NOSTR_AUTH_STATE_REJECTED:
return "rejected";
default:
return "unknown";
}
}
/**
* Initialize authentication context structure
*/
int nostr_nip42_init_auth_context(nostr_auth_context_t* ctx,
const char* relay_url,
const char* challenge,
int time_tolerance) {
if (!ctx || !relay_url || !challenge) {
return NOSTR_ERROR_INVALID_INPUT;
}
memset(ctx, 0, sizeof(nostr_auth_context_t));
ctx->relay_url = malloc(strlen(relay_url) + 1);
if (!ctx->relay_url) {
return NOSTR_ERROR_MEMORY_FAILED;
}
strcpy(ctx->relay_url, relay_url);
ctx->challenge = malloc(strlen(challenge) + 1);
if (!ctx->challenge) {
free(ctx->relay_url);
ctx->relay_url = NULL;
return NOSTR_ERROR_MEMORY_FAILED;
}
strcpy(ctx->challenge, challenge);
ctx->timestamp = time(NULL);
ctx->time_tolerance = (time_tolerance > 0) ? time_tolerance : NOSTR_NIP42_DEFAULT_TIME_TOLERANCE;
return NOSTR_SUCCESS;
}
/**
* Free authentication context structure
*/
void nostr_nip42_free_auth_context(nostr_auth_context_t* ctx) {
if (!ctx) {
return;
}
free(ctx->relay_url);
free(ctx->challenge);
free(ctx->pubkey_hex);
memset(ctx, 0, sizeof(nostr_auth_context_t));
}
/**
* Validate authentication event structure (without signature verification)
*/
int nostr_nip42_validate_auth_event_structure(cJSON* auth_event,
const char* relay_url,
const char* challenge,
int time_tolerance) {
if (!auth_event || !relay_url || !challenge) {
return NOSTR_ERROR_INVALID_INPUT;
}
// Check event kind is 22242
cJSON* kind_item = cJSON_GetObjectItem(auth_event, "kind");
if (!kind_item || !cJSON_IsNumber(kind_item) ||
(int)cJSON_GetNumberValue(kind_item) != NOSTR_NIP42_AUTH_EVENT_KIND) {
return NOSTR_ERROR_NIP42_AUTH_EVENT_INVALID;
}
// Check timestamp is within tolerance
cJSON* created_at_item = cJSON_GetObjectItem(auth_event, "created_at");
if (!created_at_item || !cJSON_IsNumber(created_at_item)) {
return NOSTR_ERROR_EVENT_INVALID_CREATED_AT;
}
time_t event_time = (time_t)cJSON_GetNumberValue(created_at_item);
time_t now = time(NULL);
int tolerance = (time_tolerance > 0) ? time_tolerance : NOSTR_NIP42_DEFAULT_TIME_TOLERANCE;
if (abs((int)(now - event_time)) > tolerance) {
return NOSTR_ERROR_NIP42_TIME_TOLERANCE;
}
// Check tags contain required relay and challenge
cJSON* tags_item = cJSON_GetObjectItem(auth_event, "tags");
if (!tags_item || !cJSON_IsArray(tags_item)) {
return NOSTR_ERROR_EVENT_INVALID_TAGS;
}
int found_relay = 0, found_challenge = 0;
cJSON* tag_item;
cJSON_ArrayForEach(tag_item, tags_item) {
if (!cJSON_IsArray(tag_item) || cJSON_GetArraySize(tag_item) < 2) {
continue;
}
cJSON* tag_name = cJSON_GetArrayItem(tag_item, 0);
cJSON* tag_value = cJSON_GetArrayItem(tag_item, 1);
if (!cJSON_IsString(tag_name) || !cJSON_IsString(tag_value)) {
continue;
}
const char* name = cJSON_GetStringValue(tag_name);
const char* value = cJSON_GetStringValue(tag_value);
if (strcmp(name, "relay") == 0) {
if (nostr_nip42_urls_match(value, relay_url) == 1) {
found_relay = 1;
}
} else if (strcmp(name, "challenge") == 0) {
if (strcmp(value, challenge) == 0) {
found_challenge = 1;
}
}
}
if (!found_relay) {
return NOSTR_ERROR_NIP42_URL_MISMATCH;
}
if (!found_challenge) {
return NOSTR_ERROR_NIP42_INVALID_CHALLENGE;
}
return NOSTR_SUCCESS;
}
// =============================================================================
// WEBSOCKET CLIENT INTEGRATION STUB FUNCTIONS
// =============================================================================
// Note: These will need to be implemented when WebSocket client structure is available
int nostr_ws_authenticate(struct nostr_ws_client* client,
const unsigned char* private_key,
int time_tolerance) {
// TODO: Implement when WebSocket client structure is available
(void)client;
(void)private_key;
(void)time_tolerance;
return NOSTR_ERROR_NETWORK_FAILED; // Placeholder
}
nostr_auth_state_t nostr_ws_get_auth_state(struct nostr_ws_client* client) {
// TODO: Implement when WebSocket client structure is available
(void)client;
return NOSTR_AUTH_STATE_NONE; // Placeholder
}
int nostr_ws_has_valid_challenge(struct nostr_ws_client* client) {
// TODO: Implement when WebSocket client structure is available
(void)client;
return 0; // Placeholder
}
int nostr_ws_get_challenge(struct nostr_ws_client* client,
char* challenge_out,
size_t challenge_size) {
// TODO: Implement when WebSocket client structure is available
(void)client;
(void)challenge_out;
(void)challenge_size;
return NOSTR_ERROR_NETWORK_FAILED; // Placeholder
}
int nostr_ws_store_challenge(struct nostr_ws_client* client,
const char* challenge) {
// TODO: Implement when WebSocket client structure is available
(void)client;
(void)challenge;
return NOSTR_ERROR_NETWORK_FAILED; // Placeholder
}
int nostr_ws_clear_auth_state(struct nostr_ws_client* client) {
// TODO: Implement when WebSocket client structure is available
(void)client;
return NOSTR_ERROR_NETWORK_FAILED; // Placeholder
}

281
nostr_core/nip042.h Normal file
View File

@@ -0,0 +1,281 @@
/*
* NOSTR Core Library - NIP-042: Authentication of clients to relays
*
* Implements client authentication through signed ephemeral events
*/
#ifndef NIP042_H
#define NIP042_H
#include <stddef.h>
#include <stdint.h>
#include <time.h>
#include "../cjson/cJSON.h"
#include "nostr_common.h"
#ifdef __cplusplus
extern "C" {
#endif
// =============================================================================
// NIP-42 CONSTANTS AND DEFINITIONS
// =============================================================================
#define NOSTR_NIP42_AUTH_EVENT_KIND 22242
#define NOSTR_NIP42_DEFAULT_CHALLENGE_LENGTH 32
#define NOSTR_NIP42_DEFAULT_TIME_TOLERANCE 600 // 10 minutes in seconds
#define NOSTR_NIP42_MAX_CHALLENGE_LENGTH 256
#define NOSTR_NIP42_MIN_CHALLENGE_LENGTH 16
// Authentication states for WebSocket client integration
typedef enum {
NOSTR_AUTH_STATE_NONE = 0, // No authentication attempted
NOSTR_AUTH_STATE_CHALLENGE_RECEIVED = 1, // Challenge received from relay
NOSTR_AUTH_STATE_AUTHENTICATING = 2, // AUTH event sent, waiting for OK
NOSTR_AUTH_STATE_AUTHENTICATED = 3, // Successfully authenticated
NOSTR_AUTH_STATE_REJECTED = 4 // Authentication rejected
} nostr_auth_state_t;
// Challenge storage structure
typedef struct {
char challenge[NOSTR_NIP42_MAX_CHALLENGE_LENGTH];
time_t received_at;
int is_valid;
} nostr_auth_challenge_t;
// Authentication context for relay verification
typedef struct {
char* relay_url;
char* challenge;
time_t timestamp;
int time_tolerance;
char* pubkey_hex;
} nostr_auth_context_t;
// =============================================================================
// CLIENT-SIDE FUNCTIONS (for nostr clients)
// =============================================================================
/**
* Create NIP-42 authentication event (kind 22242)
* @param challenge Challenge string received from relay
* @param relay_url Relay URL (normalized)
* @param private_key 32-byte private key for signing
* @param timestamp Event timestamp (0 for current time)
* @return cJSON event object or NULL on error
*/
cJSON* nostr_nip42_create_auth_event(const char* challenge,
const char* relay_url,
const unsigned char* private_key,
time_t timestamp);
/**
* Create AUTH message JSON for relay communication
* @param auth_event Authentication event (kind 22242)
* @return JSON string for AUTH message or NULL on error (caller must free)
*/
char* nostr_nip42_create_auth_message(cJSON* auth_event);
/**
* Validate challenge string format and freshness
* @param challenge Challenge string to validate
* @param received_at Time when challenge was received (0 for no time check)
* @param time_tolerance Maximum age in seconds (0 for default)
* @return NOSTR_SUCCESS or error code
*/
int nostr_nip42_validate_challenge(const char* challenge,
time_t received_at,
int time_tolerance);
/**
* Parse AUTH challenge message from relay
* @param message Raw message from relay
* @param challenge_out Output buffer for challenge string
* @param challenge_size Size of challenge buffer
* @return NOSTR_SUCCESS or error code
*/
int nostr_nip42_parse_auth_challenge(const char* message,
char* challenge_out,
size_t challenge_size);
// =============================================================================
// SERVER-SIDE FUNCTIONS (for relay implementations)
// =============================================================================
/**
* Generate cryptographically secure challenge string
* @param challenge_out Output buffer for challenge (must be at least length*2+1)
* @param length Desired challenge length in bytes (16-128)
* @return NOSTR_SUCCESS or error code
*/
int nostr_nip42_generate_challenge(char* challenge_out, size_t length);
/**
* Verify NIP-42 authentication event
* @param auth_event Authentication event to verify
* @param expected_challenge Challenge that was sent to client
* @param relay_url Expected relay URL
* @param time_tolerance Maximum timestamp deviation in seconds
* @return NOSTR_SUCCESS or error code
*/
int nostr_nip42_verify_auth_event(cJSON* auth_event,
const char* expected_challenge,
const char* relay_url,
int time_tolerance);
/**
* Parse AUTH message from client
* @param message Raw AUTH message from client
* @param auth_event_out Output pointer to parsed event (caller must free)
* @return NOSTR_SUCCESS or error code
*/
int nostr_nip42_parse_auth_message(const char* message, cJSON** auth_event_out);
/**
* Create "auth-required" error response
* @param subscription_id Subscription ID (for CLOSED) or NULL (for OK)
* @param event_id Event ID (for OK) or NULL (for CLOSED)
* @param reason Human-readable reason
* @return JSON string for response or NULL on error (caller must free)
*/
char* nostr_nip42_create_auth_required_message(const char* subscription_id,
const char* event_id,
const char* reason);
/**
* Create "restricted" error response
* @param subscription_id Subscription ID (for CLOSED) or NULL (for OK)
* @param event_id Event ID (for OK) or NULL (for CLOSED)
* @param reason Human-readable reason
* @return JSON string for response or NULL on error (caller must free)
*/
char* nostr_nip42_create_restricted_message(const char* subscription_id,
const char* event_id,
const char* reason);
// =============================================================================
// URL NORMALIZATION FUNCTIONS
// =============================================================================
/**
* Normalize relay URL for comparison (removes trailing slashes, etc.)
* @param url Original URL
* @return Normalized URL string or NULL on error (caller must free)
*/
char* nostr_nip42_normalize_url(const char* url);
/**
* Check if two relay URLs match after normalization
* @param url1 First URL
* @param url2 Second URL
* @return 1 if URLs match, 0 if they don't, -1 on error
*/
int nostr_nip42_urls_match(const char* url1, const char* url2);
// =============================================================================
// UTILITY FUNCTIONS
// =============================================================================
/**
* Get string description of authentication state
* @param state Authentication state
* @return Human-readable string
*/
const char* nostr_nip42_auth_state_str(nostr_auth_state_t state);
/**
* Initialize authentication context structure
* @param ctx Context to initialize
* @param relay_url Relay URL
* @param challenge Challenge string
* @param time_tolerance Time tolerance in seconds
* @return NOSTR_SUCCESS or error code
*/
int nostr_nip42_init_auth_context(nostr_auth_context_t* ctx,
const char* relay_url,
const char* challenge,
int time_tolerance);
/**
* Free authentication context structure
* @param ctx Context to free
*/
void nostr_nip42_free_auth_context(nostr_auth_context_t* ctx);
/**
* Validate authentication event structure (without signature verification)
* @param auth_event Event to validate
* @param relay_url Expected relay URL
* @param challenge Expected challenge
* @param time_tolerance Maximum timestamp deviation in seconds
* @return NOSTR_SUCCESS or error code
*/
int nostr_nip42_validate_auth_event_structure(cJSON* auth_event,
const char* relay_url,
const char* challenge,
int time_tolerance);
// =============================================================================
// WEBSOCKET CLIENT INTEGRATION
// =============================================================================
// Forward declaration for WebSocket client
struct nostr_ws_client;
/**
* Authenticate WebSocket client with relay
* @param client WebSocket client handle
* @param private_key 32-byte private key for authentication
* @param time_tolerance Maximum timestamp deviation in seconds (0 for default)
* @return NOSTR_SUCCESS or error code
*/
int nostr_ws_authenticate(struct nostr_ws_client* client,
const unsigned char* private_key,
int time_tolerance);
/**
* Get current authentication state of WebSocket client
* @param client WebSocket client handle
* @return Current authentication state
*/
nostr_auth_state_t nostr_ws_get_auth_state(struct nostr_ws_client* client);
/**
* Check if WebSocket client has stored valid challenge
* @param client WebSocket client handle
* @return 1 if valid challenge exists, 0 otherwise
*/
int nostr_ws_has_valid_challenge(struct nostr_ws_client* client);
/**
* Get stored challenge from WebSocket client
* @param client WebSocket client handle
* @param challenge_out Output buffer for challenge
* @param challenge_size Size of output buffer
* @return NOSTR_SUCCESS or error code
*/
int nostr_ws_get_challenge(struct nostr_ws_client* client,
char* challenge_out,
size_t challenge_size);
/**
* Store challenge in WebSocket client (internal function)
* @param client WebSocket client handle
* @param challenge Challenge string to store
* @return NOSTR_SUCCESS or error code
*/
int nostr_ws_store_challenge(struct nostr_ws_client* client,
const char* challenge);
/**
* Clear authentication state in WebSocket client
* @param client WebSocket client handle
* @return NOSTR_SUCCESS or error code
*/
int nostr_ws_clear_auth_state(struct nostr_ws_client* client);
#ifdef __cplusplus
}
#endif
#endif // NIP042_H

View File

@@ -9,10 +9,29 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "./crypto/nostr_secp256k1.h"
// Include our ChaCha20 implementation
#include "crypto/nostr_chacha20.h"
// Forward declarations for crypto functions (private API)
// These functions are implemented in crypto/ but not exposed through public headers
int ecdh_shared_secret(const unsigned char* private_key, const unsigned char* public_key, unsigned char* shared_secret);
int nostr_secp256k1_get_random_bytes(unsigned char* buf, size_t len);
// ChaCha20 functions for NIP-44 encryption
int chacha20_encrypt(const unsigned char key[32], unsigned int counter,
const unsigned char nonce[12], const unsigned char* input,
unsigned char* output, size_t length);
// HKDF functions for NIP-44 key derivation
int nostr_hkdf_extract(const unsigned char* salt, size_t salt_len,
const unsigned char* ikm, size_t ikm_len,
unsigned char* prk);
int nostr_hkdf_expand(const unsigned char* prk, size_t prk_len,
const unsigned char* info, size_t info_len,
unsigned char* okm, size_t okm_len);
// HMAC-SHA256 function for NIP-44 authentication
int nostr_hmac_sha256(const unsigned char* key, size_t key_len,
const unsigned char* data, size_t data_len,
unsigned char* hmac);
// Forward declarations for internal functions
static size_t calc_padded_len(size_t unpadded_len);
@@ -27,6 +46,7 @@ static void memory_clear(const void *p, size_t len) {
}
}
// =============================================================================
// NIP-44 UTILITY FUNCTIONS
// =============================================================================
@@ -62,8 +82,8 @@ static unsigned char* pad_plaintext(const char* plaintext, size_t* padded_len) {
return NULL;
}
// NIP-44 allows empty messages (unpadded_len can be 0)
*padded_len = calc_padded_len(unpadded_len + 2); // +2 for length prefix
size_t padded_content_len = calc_padded_len(unpadded_len);
*padded_len = padded_content_len + 2; // Add 2 bytes for the length prefix
unsigned char* padded = malloc(*padded_len);
if (!padded) return NULL;
@@ -71,33 +91,29 @@ static unsigned char* pad_plaintext(const char* plaintext, size_t* padded_len) {
padded[0] = (unpadded_len >> 8) & 0xFF;
padded[1] = unpadded_len & 0xFF;
// Copy plaintext (if any)
if (unpadded_len > 0) {
memcpy(padded + 2, plaintext, unpadded_len);
}
// Zero-fill padding
memset(padded + 2 + unpadded_len, 0, *padded_len - 2 - unpadded_len);
// Copy plaintext and add zero-padding
memcpy(padded + 2, plaintext, unpadded_len);
memset(padded + 2 + unpadded_len, 0, padded_content_len - unpadded_len);
return padded;
}
// NIP-44 unpadding (per spec)
// NIP-44 unpadding (per spec)
static char* unpad_plaintext(const unsigned char* padded, size_t padded_len) {
if (padded_len < 2) return NULL;
// Read length prefix (big-endian u16)
if (padded_len < 4) return NULL;
size_t unpadded_len = (padded[0] << 8) | padded[1];
size_t expected_padded_len = calc_padded_len(unpadded_len);
if (padded_len != expected_padded_len + 2) {
return NULL;
}
if (unpadded_len > padded_len - 2) {
return NULL;
}
// Verify padding length matches expected
size_t expected_padded_len = calc_padded_len(unpadded_len + 2);
if (padded_len != expected_padded_len) {
return NULL;
}
char* plaintext = malloc(unpadded_len + 1);
if (!plaintext) return NULL;
@@ -129,6 +145,7 @@ int nostr_nip44_encrypt_with_nonce(const unsigned char* sender_private_key,
return NOSTR_ERROR_NIP44_BUFFER_TOO_SMALL;
}
// Step 1: Compute ECDH shared secret
unsigned char shared_secret[32];
if (ecdh_shared_secret(sender_private_key, recipient_public_key, shared_secret) != 0) {
@@ -149,6 +166,7 @@ int nostr_nip44_encrypt_with_nonce(const unsigned char* sender_private_key,
unsigned char nonce_copy[32];
memcpy(nonce_copy, nonce, 32);
// Step 4: Derive message keys (HKDF-expand with nonce as info)
unsigned char message_keys[76]; // 32 chacha_key + 12 chacha_nonce + 32 hmac_key
if (nostr_hkdf_expand(conversation_key, 32, nonce_copy, 32, message_keys, 76) != 0) {
@@ -162,6 +180,7 @@ int nostr_nip44_encrypt_with_nonce(const unsigned char* sender_private_key,
unsigned char* chacha_nonce = message_keys + 32;
unsigned char* hmac_key = message_keys + 44;
// Step 5: Pad plaintext according to NIP-44 spec
size_t padded_len;
unsigned char* padded_plaintext = pad_plaintext(plaintext, &padded_len);
@@ -339,9 +358,9 @@ int nostr_nip44_decrypt(const unsigned char* recipient_private_key,
}
unsigned char* nonce = payload + 1;
size_t ciphertext_len = payload_len - 65; // payload - version - nonce - mac
unsigned char* ciphertext = payload + 33;
unsigned char* received_mac = payload + payload_len - 32;
size_t ciphertext_len = (payload + payload_len - 32) - (payload + 33); // mac_start - ciphertext_start
// Step 3: Compute ECDH shared secret
unsigned char shared_secret[32];
@@ -402,6 +421,7 @@ int nostr_nip44_decrypt(const unsigned char* recipient_private_key,
return NOSTR_ERROR_CRYPTO_FAILED;
}
// Constant-time MAC verification
// Constant-time MAC verification
if (!constant_time_compare(received_mac, computed_mac, 32)) {
memory_clear(shared_secret, 32);
@@ -439,6 +459,7 @@ int nostr_nip44_decrypt(const unsigned char* recipient_private_key,
return NOSTR_ERROR_CRYPTO_FAILED;
}
// Step 8: Remove padding according to NIP-44 spec
// Step 8: Remove padding according to NIP-44 spec
char* plaintext = unpad_plaintext(padded_plaintext, ciphertext_len);
if (!plaintext) {

View File

@@ -13,7 +13,7 @@ extern "C" {
#endif
// NIP-44 constants
// #define NOSTR_NIP44_MAX_PLAINTEXT_SIZE 65535
// #define NOSTR_NIP44_MAX_PLAINTEXT_SIZE 1048576
/**
* NIP-44: Encrypt a message using ECDH + ChaCha20 + HMAC

412
nostr_core/nip059.c Normal file
View File

@@ -0,0 +1,412 @@
/*
* NIP-59: Gift Wrap Implementation
* https://github.com/nostr-protocol/nips/blob/master/59.md
*/
#include "nip059.h"
#include "nip044.h"
#include "nip001.h"
#include "utils.h"
#include "nostr_common.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// Forward declarations for crypto functions
int nostr_secp256k1_get_random_bytes(unsigned char* buf, size_t len);
int nostr_ec_public_key_from_private_key(const unsigned char* private_key, unsigned char* public_key);
int nostr_ec_sign(const unsigned char* private_key, const unsigned char* hash, unsigned char* signature);
// Memory clearing utility
static void memory_clear(const void *p, size_t len) {
if (p && len) {
memset((void *)p, 0, len);
}
}
/**
* Create a random timestamp within max_delay_sec in the past (configurable)
*/
static time_t random_past_timestamp(long max_delay_sec) {
time_t now = time(NULL);
// If max_delay_sec is 0, return current timestamp (no randomization)
if (max_delay_sec == 0) {
return now;
}
// Random time up to max_delay_sec in the past
long random_offset = (long)(rand() % max_delay_sec);
return now - random_offset;
}
/**
* Generate a random private key for gift wrap
*/
static int generate_random_private_key(unsigned char* private_key) {
return nostr_secp256k1_get_random_bytes(private_key, 32);
}
/**
* Create event ID from event data (without signature)
*/
static int create_event_id(cJSON* event, char* event_id_hex) {
if (!event || !event_id_hex) {
return -1;
}
// Get event fields for serialization
cJSON* pubkey_item = cJSON_GetObjectItem(event, "pubkey");
cJSON* created_at_item = cJSON_GetObjectItem(event, "created_at");
cJSON* kind_item = cJSON_GetObjectItem(event, "kind");
cJSON* tags_item = cJSON_GetObjectItem(event, "tags");
cJSON* content_item = cJSON_GetObjectItem(event, "content");
if (!pubkey_item || !created_at_item || !kind_item || !tags_item || !content_item) {
return -1;
}
// Create serialization array: [0, pubkey, created_at, kind, tags, content]
cJSON* serialize_array = cJSON_CreateArray();
if (!serialize_array) {
return -1;
}
cJSON_AddItemToArray(serialize_array, cJSON_CreateNumber(0));
cJSON_AddItemToArray(serialize_array, cJSON_Duplicate(pubkey_item, 1));
cJSON_AddItemToArray(serialize_array, cJSON_Duplicate(created_at_item, 1));
cJSON_AddItemToArray(serialize_array, cJSON_Duplicate(kind_item, 1));
cJSON_AddItemToArray(serialize_array, cJSON_Duplicate(tags_item, 1));
cJSON_AddItemToArray(serialize_array, cJSON_Duplicate(content_item, 1));
char* serialize_string = cJSON_PrintUnformatted(serialize_array);
cJSON_Delete(serialize_array);
if (!serialize_string) {
return -1;
}
// Hash the serialized event
unsigned char event_hash[32];
if (nostr_sha256((const unsigned char*)serialize_string, strlen(serialize_string), event_hash) != 0) {
free(serialize_string);
return -1;
}
// Convert hash to hex
nostr_bytes_to_hex(event_hash, 32, event_id_hex);
free(serialize_string);
return 0;
}
/**
* NIP-59: Create a rumor (unsigned event)
*/
cJSON* nostr_nip59_create_rumor(int kind, const char* content, cJSON* tags,
const char* pubkey_hex, time_t created_at) {
if (!pubkey_hex || !content) {
return NULL;
}
// Use provided timestamp or random past timestamp (default to 0 for compatibility)
time_t event_time = (created_at == 0) ? random_past_timestamp(0) : created_at;
// Create event structure (without id and sig - that's what makes it a rumor)
cJSON* rumor = cJSON_CreateObject();
if (!rumor) {
return NULL;
}
cJSON_AddStringToObject(rumor, "pubkey", pubkey_hex);
cJSON_AddNumberToObject(rumor, "created_at", (double)event_time);
cJSON_AddNumberToObject(rumor, "kind", kind);
// Add tags (copy provided tags or create empty array)
if (tags) {
cJSON_AddItemToObject(rumor, "tags", cJSON_Duplicate(tags, 1));
} else {
cJSON_AddItemToObject(rumor, "tags", cJSON_CreateArray());
}
cJSON_AddStringToObject(rumor, "content", content);
// Calculate and add event ID
char event_id[65];
if (create_event_id(rumor, event_id) != 0) {
cJSON_Delete(rumor);
return NULL;
}
cJSON_AddStringToObject(rumor, "id", event_id);
return rumor;
}
/**
* NIP-59: Create a seal (kind 13) wrapping a rumor
*/
cJSON* nostr_nip59_create_seal(cJSON* rumor, const unsigned char* sender_private_key,
const unsigned char* recipient_public_key, long max_delay_sec) {
if (!rumor || !sender_private_key || !recipient_public_key) {
return NULL;
}
// Serialize the rumor to JSON
char* rumor_json = cJSON_PrintUnformatted(rumor);
if (!rumor_json) {
return NULL;
}
// Encrypt the rumor using NIP-44
char encrypted_content[4096]; // Should be large enough for most events
int encrypt_result = nostr_nip44_encrypt(sender_private_key, recipient_public_key,
rumor_json, encrypted_content, sizeof(encrypted_content));
free(rumor_json);
if (encrypt_result != NOSTR_SUCCESS) {
return NULL;
}
// Get sender's public key
unsigned char sender_public_key[32];
if (nostr_ec_public_key_from_private_key(sender_private_key, sender_public_key) != 0) {
return NULL;
}
char sender_pubkey_hex[65];
nostr_bytes_to_hex(sender_public_key, 32, sender_pubkey_hex);
// Create seal event (kind 13)
cJSON* seal = cJSON_CreateObject();
if (!seal) {
return NULL;
}
time_t seal_time = random_past_timestamp(max_delay_sec);
cJSON_AddStringToObject(seal, "pubkey", sender_pubkey_hex);
cJSON_AddNumberToObject(seal, "created_at", (double)seal_time);
cJSON_AddNumberToObject(seal, "kind", 13);
cJSON_AddItemToObject(seal, "tags", cJSON_CreateArray()); // Empty tags array
cJSON_AddStringToObject(seal, "content", encrypted_content);
// Calculate event ID
char event_id[65];
if (create_event_id(seal, event_id) != 0) {
cJSON_Delete(seal);
return NULL;
}
cJSON_AddStringToObject(seal, "id", event_id);
// Sign the seal
unsigned char event_hash[32];
if (nostr_hex_to_bytes(event_id, event_hash, 32) != 0) {
cJSON_Delete(seal);
return NULL;
}
unsigned char signature[64];
if (nostr_ec_sign(sender_private_key, event_hash, signature) != 0) {
cJSON_Delete(seal);
return NULL;
}
char sig_hex[129];
nostr_bytes_to_hex(signature, 64, sig_hex);
cJSON_AddStringToObject(seal, "sig", sig_hex);
return seal;
}
/**
* NIP-59: Create a gift wrap (kind 1059) wrapping a seal
*/
cJSON* nostr_nip59_create_gift_wrap(cJSON* seal, const char* recipient_public_key_hex, long max_delay_sec) {
if (!seal || !recipient_public_key_hex) {
return NULL;
}
// Serialize the seal to JSON
char* seal_json = cJSON_PrintUnformatted(seal);
if (!seal_json) {
return NULL;
}
// Generate random private key for gift wrap
unsigned char random_private_key[32];
if (generate_random_private_key(random_private_key) != 1) {
free(seal_json);
return NULL;
}
// Get random public key
unsigned char random_public_key[32];
if (nostr_ec_public_key_from_private_key(random_private_key, random_public_key) != 0) {
memory_clear(random_private_key, 32);
free(seal_json);
return NULL;
}
char random_pubkey_hex[65];
nostr_bytes_to_hex(random_public_key, 32, random_pubkey_hex);
// Convert recipient pubkey hex to bytes
unsigned char recipient_public_key[32];
if (nostr_hex_to_bytes(recipient_public_key_hex, recipient_public_key, 32) != 0) {
memory_clear(random_private_key, 32);
free(seal_json);
return NULL;
}
// Encrypt the seal using NIP-44
char encrypted_content[8192]; // Larger buffer for nested encryption
int encrypt_result = nostr_nip44_encrypt(random_private_key, recipient_public_key,
seal_json, encrypted_content, sizeof(encrypted_content));
free(seal_json);
if (encrypt_result != NOSTR_SUCCESS) {
memory_clear(random_private_key, 32);
return NULL;
}
// Create gift wrap event (kind 1059)
cJSON* gift_wrap = cJSON_CreateObject();
if (!gift_wrap) {
memory_clear(random_private_key, 32);
return NULL;
}
time_t wrap_time = random_past_timestamp(max_delay_sec);
cJSON_AddStringToObject(gift_wrap, "pubkey", random_pubkey_hex);
cJSON_AddNumberToObject(gift_wrap, "created_at", (double)wrap_time);
cJSON_AddNumberToObject(gift_wrap, "kind", 1059);
// Add p tag for recipient
cJSON* tags = cJSON_CreateArray();
cJSON* p_tag = cJSON_CreateArray();
cJSON_AddItemToArray(p_tag, cJSON_CreateString("p"));
cJSON_AddItemToArray(p_tag, cJSON_CreateString(recipient_public_key_hex));
cJSON_AddItemToArray(tags, p_tag);
cJSON_AddItemToObject(gift_wrap, "tags", tags);
cJSON_AddStringToObject(gift_wrap, "content", encrypted_content);
// Calculate event ID
char event_id[65];
if (create_event_id(gift_wrap, event_id) != 0) {
memory_clear(random_private_key, 32);
cJSON_Delete(gift_wrap);
return NULL;
}
cJSON_AddStringToObject(gift_wrap, "id", event_id);
// Sign the gift wrap
unsigned char event_hash[32];
if (nostr_hex_to_bytes(event_id, event_hash, 32) != 0) {
memory_clear(random_private_key, 32);
cJSON_Delete(gift_wrap);
return NULL;
}
unsigned char signature[64];
if (nostr_ec_sign(random_private_key, event_hash, signature) != 0) {
memory_clear(random_private_key, 32);
cJSON_Delete(gift_wrap);
return NULL;
}
char sig_hex[129];
nostr_bytes_to_hex(signature, 64, sig_hex);
cJSON_AddStringToObject(gift_wrap, "sig", sig_hex);
// Clear the random private key from memory
memory_clear(random_private_key, 32);
return gift_wrap;
}
/**
* NIP-59: Unwrap a gift wrap to get the seal
*/
cJSON* nostr_nip59_unwrap_gift(cJSON* gift_wrap, const unsigned char* recipient_private_key) {
if (!gift_wrap || !recipient_private_key) {
return NULL;
}
// Get the encrypted content
cJSON* content_item = cJSON_GetObjectItem(gift_wrap, "content");
if (!content_item || !cJSON_IsString(content_item)) {
return NULL;
}
const char* encrypted_content = cJSON_GetStringValue(content_item);
// Get the sender's public key (gift wrap pubkey)
cJSON* pubkey_item = cJSON_GetObjectItem(gift_wrap, "pubkey");
if (!pubkey_item || !cJSON_IsString(pubkey_item)) {
return NULL;
}
const char* sender_pubkey_hex = cJSON_GetStringValue(pubkey_item);
// Convert sender pubkey hex to bytes
unsigned char sender_public_key[32];
if (nostr_hex_to_bytes(sender_pubkey_hex, sender_public_key, 32) != 0) {
return NULL;
}
// Decrypt the content using NIP-44
char decrypted_json[8192];
int decrypt_result = nostr_nip44_decrypt(recipient_private_key, sender_public_key,
encrypted_content, decrypted_json, sizeof(decrypted_json));
if (decrypt_result != NOSTR_SUCCESS) {
return NULL;
}
// Parse the decrypted JSON as the seal event
cJSON* seal = cJSON_Parse(decrypted_json);
if (!seal) {
return NULL;
}
return seal;
}
/**
* NIP-59: Unseal a seal to get the rumor
*/
cJSON* nostr_nip59_unseal_rumor(cJSON* seal, const unsigned char* sender_public_key,
const unsigned char* recipient_private_key) {
if (!seal || !sender_public_key || !recipient_private_key) {
return NULL;
}
// Get the encrypted content
cJSON* content_item = cJSON_GetObjectItem(seal, "content");
if (!content_item || !cJSON_IsString(content_item)) {
return NULL;
}
const char* encrypted_content = cJSON_GetStringValue(content_item);
// Decrypt the content using NIP-44
char decrypted_json[4096];
int decrypt_result = nostr_nip44_decrypt(recipient_private_key, sender_public_key,
encrypted_content, decrypted_json, sizeof(decrypted_json));
if (decrypt_result != NOSTR_SUCCESS) {
return NULL;
}
// Parse the decrypted JSON as the rumor event
cJSON* rumor = cJSON_Parse(decrypted_json);
if (!rumor) {
return NULL;
}
return rumor;
}

76
nostr_core/nip059.h Normal file
View File

@@ -0,0 +1,76 @@
/*
* NIP-59: Gift Wrap
* https://github.com/nostr-protocol/nips/blob/master/59.md
*/
#ifndef NOSTR_NIP059_H
#define NOSTR_NIP059_H
#include <stddef.h>
#include <time.h>
#include "../cjson/cJSON.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* NIP-59: Create a rumor (unsigned event)
*
* @param kind Event kind
* @param content Event content
* @param tags Event tags (cJSON array, can be NULL)
* @param pubkey_hex Sender's public key in hex format
* @param created_at Event timestamp (0 for current time)
* @return cJSON object representing the rumor, or NULL on error
*/
cJSON* nostr_nip59_create_rumor(int kind, const char* content, cJSON* tags,
const char* pubkey_hex, time_t created_at);
/**
* NIP-59: Create a seal (kind 13) wrapping a rumor
*
* @param rumor The rumor event to seal (cJSON object)
* @param sender_private_key 32-byte sender private key
* @param recipient_public_key 32-byte recipient public key (x-only)
* @param max_delay_sec Maximum random timestamp delay in seconds (0 = no randomization)
* @return cJSON object representing the seal event, or NULL on error
*/
cJSON* nostr_nip59_create_seal(cJSON* rumor, const unsigned char* sender_private_key,
const unsigned char* recipient_public_key, long max_delay_sec);
/**
* NIP-59: Create a gift wrap (kind 1059) wrapping a seal
*
* @param seal The seal event to wrap (cJSON object)
* @param recipient_public_key_hex Recipient's public key in hex format
* @param max_delay_sec Maximum random timestamp delay in seconds (0 = no randomization)
* @return cJSON object representing the gift wrap event, or NULL on error
*/
cJSON* nostr_nip59_create_gift_wrap(cJSON* seal, const char* recipient_public_key_hex, long max_delay_sec);
/**
* NIP-59: Unwrap a gift wrap to get the seal
*
* @param gift_wrap The gift wrap event (cJSON object)
* @param recipient_private_key 32-byte recipient private key
* @return cJSON object representing the seal event, or NULL on error
*/
cJSON* nostr_nip59_unwrap_gift(cJSON* gift_wrap, const unsigned char* recipient_private_key);
/**
* NIP-59: Unseal a seal to get the rumor
*
* @param seal The seal event (cJSON object)
* @param sender_public_key 32-byte sender public key (x-only)
* @param recipient_private_key 32-byte recipient private key
* @return cJSON object representing the rumor event, or NULL on error
*/
cJSON* nostr_nip59_unseal_rumor(cJSON* seal, const unsigned char* sender_public_key,
const unsigned char* recipient_private_key);
#ifdef __cplusplus
}
#endif
#endif // NOSTR_NIP059_H

View File

@@ -30,6 +30,28 @@ const char* nostr_strerror(int error_code) {
case NOSTR_ERROR_NIP05_JSON_PARSE_FAILED: return "NIP-05: JSON parsing failed";
case NOSTR_ERROR_NIP05_NAME_NOT_FOUND: return "NIP-05: Name not found in .well-known";
case NOSTR_ERROR_NIP05_PUBKEY_MISMATCH: return "NIP-05: Public key mismatch";
case NOSTR_ERROR_EVENT_INVALID_STRUCTURE: return "Event has invalid structure";
case NOSTR_ERROR_EVENT_INVALID_ID: return "Event has invalid ID";
case NOSTR_ERROR_EVENT_INVALID_PUBKEY: return "Event has invalid public key";
case NOSTR_ERROR_EVENT_INVALID_SIGNATURE: return "Event has invalid signature";
case NOSTR_ERROR_EVENT_INVALID_CREATED_AT: return "Event has invalid timestamp";
case NOSTR_ERROR_EVENT_INVALID_KIND: return "Event has invalid kind";
case NOSTR_ERROR_EVENT_INVALID_TAGS: return "Event has invalid tags";
case NOSTR_ERROR_EVENT_INVALID_CONTENT: return "Event has invalid content";
case NOSTR_ERROR_NIP13_INSUFFICIENT: return "NIP-13: Insufficient PoW difficulty";
case NOSTR_ERROR_NIP13_NO_NONCE_TAG: return "NIP-13: Missing nonce tag";
case NOSTR_ERROR_NIP13_INVALID_NONCE_TAG: return "NIP-13: Invalid nonce tag format";
case NOSTR_ERROR_NIP13_TARGET_MISMATCH: return "NIP-13: Target difficulty mismatch";
case NOSTR_ERROR_NIP13_CALCULATION: return "NIP-13: PoW calculation error";
case NOSTR_ERROR_NIP42_INVALID_CHALLENGE: return "NIP-42: Invalid challenge";
case NOSTR_ERROR_NIP42_CHALLENGE_EXPIRED: return "NIP-42: Challenge expired";
case NOSTR_ERROR_NIP42_AUTH_EVENT_INVALID: return "NIP-42: Authentication event invalid";
case NOSTR_ERROR_NIP42_URL_MISMATCH: return "NIP-42: Relay URL mismatch";
case NOSTR_ERROR_NIP42_TIME_TOLERANCE: return "NIP-42: Timestamp outside tolerance";
case NOSTR_ERROR_NIP42_NOT_AUTHENTICATED: return "NIP-42: Client not authenticated";
case NOSTR_ERROR_NIP42_INVALID_MESSAGE_FORMAT: return "NIP-42: Invalid message format";
case NOSTR_ERROR_NIP42_CHALLENGE_TOO_SHORT: return "NIP-42: Challenge too short";
case NOSTR_ERROR_NIP42_CHALLENGE_TOO_LONG: return "NIP-42: Challenge too long";
default: return "Unknown error";
}
}

View File

@@ -27,7 +27,40 @@
#define NOSTR_ERROR_NIP05_JSON_PARSE_FAILED -18
#define NOSTR_ERROR_NIP05_NAME_NOT_FOUND -19
#define NOSTR_ERROR_NIP05_PUBKEY_MISMATCH -20
#define NOSTR_ERROR_EVENT_INVALID_STRUCTURE -30
#define NOSTR_ERROR_EVENT_INVALID_ID -31
#define NOSTR_ERROR_EVENT_INVALID_PUBKEY -32
#define NOSTR_ERROR_EVENT_INVALID_SIGNATURE -33
#define NOSTR_ERROR_EVENT_INVALID_CREATED_AT -34
#define NOSTR_ERROR_EVENT_INVALID_KIND -35
#define NOSTR_ERROR_EVENT_INVALID_TAGS -36
#define NOSTR_ERROR_EVENT_INVALID_CONTENT -37
// Authentication Rules System Error Codes
#define NOSTR_ERROR_AUTH_RULES_DISABLED -50
#define NOSTR_ERROR_AUTH_RULES_DENIED -51
#define NOSTR_ERROR_AUTH_RULES_DB_FAILED -52
#define NOSTR_ERROR_AUTH_RULES_INVALID_RULE -53
#define NOSTR_ERROR_AUTH_RULES_CACHE_FAILED -54
#define NOSTR_ERROR_AUTH_RULES_BACKEND_NOT_FOUND -55
// NIP-13 PoW-specific error codes
#define NOSTR_ERROR_NIP13_INSUFFICIENT -100
#define NOSTR_ERROR_NIP13_NO_NONCE_TAG -101
#define NOSTR_ERROR_NIP13_INVALID_NONCE_TAG -102
#define NOSTR_ERROR_NIP13_TARGET_MISMATCH -103
#define NOSTR_ERROR_NIP13_CALCULATION -104
// NIP-42 Authentication-specific error codes
#define NOSTR_ERROR_NIP42_INVALID_CHALLENGE -200
#define NOSTR_ERROR_NIP42_CHALLENGE_EXPIRED -201
#define NOSTR_ERROR_NIP42_AUTH_EVENT_INVALID -202
#define NOSTR_ERROR_NIP42_URL_MISMATCH -203
#define NOSTR_ERROR_NIP42_TIME_TOLERANCE -204
#define NOSTR_ERROR_NIP42_NOT_AUTHENTICATED -205
#define NOSTR_ERROR_NIP42_INVALID_MESSAGE_FORMAT -206
#define NOSTR_ERROR_NIP42_CHALLENGE_TOO_SHORT -207
#define NOSTR_ERROR_NIP42_CHALLENGE_TOO_LONG -208
// Constants
#define NOSTR_PRIVATE_KEY_SIZE 32
@@ -39,75 +72,20 @@
#define NIP05_DEFAULT_TIMEOUT 10
// NIP-04 Constants
#define NOSTR_NIP04_MAX_PLAINTEXT_SIZE 16777216 // 16MB
#define NOSTR_NIP04_MAX_PLAINTEXT_SIZE 1048576 // 1MB
#define NOSTR_NIP04_MAX_ENCRYPTED_SIZE 22369621 // ~21.3MB (accounts for base64 overhead + IV)
// NIP-44 Constants
#define NOSTR_NIP44_MAX_PLAINTEXT_SIZE 65536 // 64KB max plaintext (matches crypto header)
// NIP-44 Constants
#define NOSTR_NIP44_MAX_PLAINTEXT_SIZE 65535 // 64KB - 1 (NIP-44 spec compliant)
// Forward declaration for cJSON (to avoid requiring cJSON.h in header)
struct cJSON;
// Relay query modes
typedef enum {
RELAY_QUERY_FIRST_RESULT, // Return as soon as first event is received
RELAY_QUERY_MOST_RECENT, // Return the most recent event from all relays
RELAY_QUERY_ALL_RESULTS // Return all unique events from all relays
} relay_query_mode_t;
// Publish result types
typedef enum {
PUBLISH_SUCCESS, // Event was accepted by relay
PUBLISH_REJECTED, // Event was rejected by relay
PUBLISH_TIMEOUT, // No response within timeout
PUBLISH_ERROR // Connection or other error
} publish_result_t;
// Progress callback function types
typedef void (*relay_progress_callback_t)(
const char* relay_url,
const char* status,
const char* event_id,
int events_received,
int total_relays,
int completed_relays,
void* user_data);
typedef void (*publish_progress_callback_t)(
const char* relay_url,
const char* status,
const char* message,
int success_count,
int total_relays,
int completed_relays,
void* user_data);
// Function declarations
const char* nostr_strerror(int error_code);
// Library initialization functions
// Library initialization functions
int nostr_init(void);
void nostr_cleanup(void);
// Relay query functions
struct cJSON** synchronous_query_relays_with_progress(
const char** relay_urls,
int relay_count,
struct cJSON* filter,
relay_query_mode_t mode,
int* result_count,
int relay_timeout_seconds,
relay_progress_callback_t callback,
void* user_data);
// Relay publish functions
publish_result_t* synchronous_publish_event_with_progress(
const char** relay_urls,
int relay_count,
struct cJSON* event,
int* success_count,
int relay_timeout_seconds,
publish_progress_callback_t callback,
void* user_data);
#endif // NOSTR_COMMON_H

400
nostr_core/nostr_core.h Normal file
View File

@@ -0,0 +1,400 @@
#ifndef NOSTR_CORE_H
#define NOSTR_CORE_H
// Version information (auto-updated by increment_and_push.sh)
#define VERSION "v0.4.8"
#define VERSION_MAJOR 0
#define VERSION_MINOR 4
#define VERSION_PATCH 8
/*
* NOSTR Core Library - Complete API Reference
*
* This header includes ALL library functionality. For modular includes,
* use individual headers instead.
*
* ============================================================================
* QUICK FUNCTION REFERENCE - Find what you need fast!
* ============================================================================
*
* EVENT OPERATIONS (NIP-01):
* - nostr_create_and_sign_event() -> Create and sign new Nostr events
* - nostr_validate_event() -> Validate complete Nostr event
* - nostr_validate_event_structure() -> Check event structure only
* - nostr_verify_event_signature() -> Verify cryptographic signature
*
* CRYPTOGRAPHIC HASHING:
* - nostr_sha256() -> Single-call SHA-256 hash
* - nostr_sha256_init() -> Initialize streaming SHA-256 context
* - nostr_sha256_update() -> Process data chunks incrementally
* - nostr_sha256_final() -> Finalize hash and clear context
* - nostr_sha256_file_stream() -> Hash large files efficiently (NEW!)
* - nostr_sha512() -> SHA-512 hash function
* - nostr_hmac_sha256() -> HMAC with SHA-256
* - nostr_hmac_sha512() -> HMAC with SHA-512
*
* DIGITAL SIGNATURES & KEYS:
* - nostr_schnorr_sign() -> Create Schnorr signatures
* - nostr_ec_public_key_from_private_key() -> Generate public keys
* - nostr_ec_private_key_verify() -> Validate private key format
* - nostr_ec_sign() -> ECDSA signature creation
* - nostr_rfc6979_generate_k() -> Deterministic nonce generation
*
* NIP-04 ENCRYPTION (Legacy):
* - nostr_nip04_encrypt() -> Encrypt messages (AES-256-CBC)
* - nostr_nip04_decrypt() -> Decrypt messages (AES-256-CBC)
*
* NIP-44 ENCRYPTION (Modern - Recommended):
* - nostr_nip44_encrypt() -> Encrypt with ChaCha20 + HMAC
* - nostr_nip44_encrypt_with_nonce() -> Encrypt with specific nonce (testing)
* - nostr_nip44_decrypt() -> Decrypt ChaCha20 + HMAC messages
*
* NIP-59 GIFT WRAP:
* - nostr_nip59_create_rumor() -> Create unsigned event (rumor)
* - nostr_nip59_create_seal() -> Seal rumor with sender's key (kind 13)
* - nostr_nip59_create_gift_wrap() -> Wrap seal with random key (kind 1059)
* - nostr_nip59_unwrap_gift() -> Unwrap gift wrap to get seal
* - nostr_nip59_unseal_rumor() -> Unseal to get original rumor
*
* NIP-17 PRIVATE DIRECT MESSAGES:
* - nostr_nip17_create_chat_event() -> Create chat message (kind 14)
* - nostr_nip17_create_file_event() -> Create file message (kind 15)
* - nostr_nip17_create_relay_list_event() -> Create DM relay list (kind 10050)
* - nostr_nip17_send_dm() -> Send DM to multiple recipients
* - nostr_nip17_receive_dm() -> Receive and decrypt DM
* - nostr_nip17_extract_dm_relays() -> Extract relay URLs from kind 10050
*
* NIP-42 AUTHENTICATION:
* - nostr_nip42_create_auth_event() -> Create authentication event (kind 22242)
* - nostr_nip42_verify_auth_event() -> Verify authentication event (relay-side)
* - nostr_nip42_generate_challenge() -> Generate challenge string (relay-side)
* - nostr_ws_authenticate() -> Authenticate WebSocket client
* - nostr_ws_get_auth_state() -> Get client authentication state
*
* BIP39 MNEMONICS:
* - nostr_bip39_mnemonic_from_bytes() -> Generate mnemonic from entropy
* - nostr_bip39_mnemonic_validate() -> Validate mnemonic phrase
* - nostr_bip39_mnemonic_to_seed() -> Convert mnemonic to seed
*
* BIP32 HD WALLETS:
* - nostr_bip32_key_from_seed() -> Create master key from seed
* - nostr_bip32_derive_child() -> Derive child key from parent
* - nostr_bip32_derive_path() -> Derive keys from derivation path
*
* KEY DERIVATION:
* - nostr_hkdf() -> HKDF key derivation (full)
* - nostr_hkdf_extract() -> HKDF extract step only
* - nostr_hkdf_expand() -> HKDF expand step only
* - nostr_pbkdf2_hmac_sha512() -> PBKDF2 with HMAC-SHA512
* - ecdh_shared_secret() -> ECDH shared secret computation
*
* UTILITIES & ENCODING:
* - nostr_bytes_to_hex() -> Convert bytes to hex string
* - nostr_hex_to_bytes() -> Convert hex string to bytes
* - base64_encode() -> Base64 encoding
* - base64_decode() -> Base64 decoding
*
* REQUEST VALIDATION & AUTHENTICATION:
* - nostr_validate_request() -> Unified request validation (events + auth rules)
* - nostr_request_validator_init() -> Initialize authentication system
* - nostr_auth_check_upload() -> Validate file upload requests
* - nostr_auth_check_delete() -> Validate file delete requests
* - nostr_auth_check_publish() -> Validate event publish requests
* - nostr_auth_rule_add() -> Add authentication rule
* - nostr_auth_rule_remove() -> Remove authentication rule
*
* RELAY OPERATIONS:
* - synchronous_query_relays_with_progress() -> One-off query from multiple relays
* - synchronous_publish_event_with_progress() -> One-off publish to multiple relays
* *
* RELAY POOL OPERATIONS:
* - nostr_relay_pool_create() -> Create relay pool for persistent connections
* - nostr_relay_pool_subscribe() -> Subscribe to events with callbacks
* - nostr_relay_pool_run() -> Run event loop for receiving events
* SYSTEM FUNCTIONS:
* - nostr_crypto_init() -> Initialize crypto subsystem
* - nostr_crypto_cleanup() -> Cleanup crypto subsystem
*
* ============================================================================
* USAGE EXAMPLES:
* ============================================================================
*
* Basic Event Creation:
* cJSON* event = nostr_create_and_sign_event(1, "Hello Nostr!", NULL, private_key, time(NULL));
* if (nostr_validate_event(event) == NOSTR_SUCCESS) { ... }
*
* Streaming SHA-256 (for large files):
* nostr_sha256_ctx_t ctx;
* nostr_sha256_init(&ctx);
* // Process data in chunks...
* nostr_sha256_update(&ctx, data, data_size);
* nostr_sha256_final(&ctx, hash_output);
*
* File Hashing:
* unsigned char file_hash[32];
* nostr_sha256_file_stream("large_video.mp4", file_hash);
*
* Modern Encryption (NIP-44):
* nostr_nip44_encrypt(sender_key, recipient_pubkey, "secret message", output, sizeof(output));
*
* HD Wallet Derivation:
* nostr_bip32_key_from_seed(seed, 64, &master_key);
* uint32_t path[] = {44, 1237, 0, 0, 0}; // m/44'/1237'/0'/0/0
* nostr_bip32_derive_path(&master_key, path, 5, &derived_key);
*
* Client Authentication (NIP-42):
* cJSON* auth_event = nostr_nip42_create_auth_event(challenge, relay_url, private_key, 0);
* nostr_ws_authenticate(client, private_key, 600); // Auto-authenticate WebSocket
*
* Private Direct Messages (NIP-17):
* // Create and send a DM
* cJSON* dm_event = nostr_nip17_create_chat_event("Hello!", &recipient_pubkey, 1, NULL, NULL, NULL, sender_pubkey);
* cJSON* gift_wraps[10];
* int count = nostr_nip17_send_dm(dm_event, &recipient_pubkey, 1, sender_privkey, gift_wraps, 10);
* // Publish gift_wraps[0] to recipient's relays
*
* // Receive a DM
* cJSON* decrypted_dm = nostr_nip17_receive_dm(received_gift_wrap, recipient_privkey);
*
* ============================================================================
*/
#ifdef __cplusplus
extern "C" {
#endif
// Core common definitions and utilities
#include "nostr_common.h"
#include "utils.h"
// NIP implementations
#include "nip001.h" // Basic Protocol
#include "nip004.h" // Encryption (legacy)
#include "nip005.h" // DNS-based identifiers
#include "nip006.h" // Key derivation from mnemonic
#include "nip011.h" // Relay information document
#include "nip013.h" // Proof of Work
#include "nip017.h" // Private Direct Messages
#include "nip019.h" // Bech32 encoding (nsec/npub)
#include "nip021.h" // nostr: URI scheme
#include "nip042.h" // Authentication of clients to relays
#include "nip044.h" // Encryption (modern)
#include "nip059.h" // Gift Wrap
// Authentication and request validation system
#include "request_validator.h" // Request validation and authentication rules
// Relay pool types and functions
typedef enum {
NOSTR_POOL_RELAY_DISCONNECTED = 0,
NOSTR_POOL_RELAY_CONNECTING = 1,
NOSTR_POOL_RELAY_CONNECTED = 2,
NOSTR_POOL_RELAY_ERROR = -1
} nostr_pool_relay_status_t;
// EOSE result mode for subscriptions
typedef enum {
NOSTR_POOL_EOSE_FULL_SET, // Wait for all relays, return all events
NOSTR_POOL_EOSE_MOST_RECENT, // Wait for all relays, return most recent event
NOSTR_POOL_EOSE_FIRST // Return results on first EOSE (fastest response)
} nostr_pool_eose_result_mode_t;
typedef struct {
int connection_attempts;
int connection_failures;
int events_received;
int events_published;
int events_published_ok;
int events_published_failed;
time_t last_event_time;
time_t connection_uptime_start;
double ping_latency_avg;
double ping_latency_min;
double ping_latency_max;
double ping_latency_current;
int ping_samples;
double query_latency_avg;
double query_latency_min;
double query_latency_max;
int query_samples;
double publish_latency_avg;
int publish_samples;
} nostr_relay_stats_t;
typedef struct nostr_relay_pool nostr_relay_pool_t;
typedef struct nostr_pool_subscription nostr_pool_subscription_t;
// Reconnection configuration
typedef struct {
int enable_auto_reconnect; // 1 = enable, 0 = disable
int max_reconnect_attempts; // Max attempts per relay
int initial_reconnect_delay_ms; // Initial delay between attempts
int max_reconnect_delay_ms; // Max delay (cap exponential backoff)
int reconnect_backoff_multiplier; // Delay multiplier
int ping_interval_seconds; // How often to ping (0 = disable)
int pong_timeout_seconds; // How long to wait for pong before reconnecting
} nostr_pool_reconnect_config_t;
// Relay pool management functions
nostr_relay_pool_t* nostr_relay_pool_create(nostr_pool_reconnect_config_t* config);
nostr_pool_reconnect_config_t* nostr_pool_reconnect_config_default(void);
int nostr_relay_pool_add_relay(nostr_relay_pool_t* pool, const char* relay_url);
int nostr_relay_pool_remove_relay(nostr_relay_pool_t* pool, const char* relay_url);
void nostr_relay_pool_destroy(nostr_relay_pool_t* pool);
// Subscription management
nostr_pool_subscription_t* nostr_relay_pool_subscribe(
nostr_relay_pool_t* pool,
const char** relay_urls,
int relay_count,
cJSON* filter,
void (*on_event)(cJSON* event, const char* relay_url, void* user_data),
void (*on_eose)(cJSON** events, int event_count, void* user_data),
void* user_data,
int close_on_eose,
int enable_deduplication,
nostr_pool_eose_result_mode_t result_mode,
int relay_timeout_seconds,
int eose_timeout_seconds);
int nostr_pool_subscription_close(nostr_pool_subscription_t* subscription);
// Backward compatibility wrapper
nostr_pool_subscription_t* nostr_relay_pool_subscribe_compat(
nostr_relay_pool_t* pool,
const char** relay_urls,
int relay_count,
cJSON* filter,
void (*on_event)(cJSON* event, const char* relay_url, void* user_data),
void (*on_eose)(void* user_data),
void* user_data,
int close_on_eose);
// Event loop functions
int nostr_relay_pool_run(nostr_relay_pool_t* pool, int timeout_ms);
int nostr_relay_pool_poll(nostr_relay_pool_t* pool, int timeout_ms);
// Synchronous query/publish functions
cJSON** nostr_relay_pool_query_sync(
nostr_relay_pool_t* pool,
const char** relay_urls,
int relay_count,
cJSON* filter,
int* event_count,
int timeout_ms);
cJSON* nostr_relay_pool_get_event(
nostr_relay_pool_t* pool,
const char** relay_urls,
int relay_count,
cJSON* filter,
int timeout_ms);
// Async publish callback typedef
typedef void (*publish_response_callback_t)(
const char* relay_url,
const char* event_id,
int success, // 1 for OK, 0 for rejection
const char* message, // Error message if rejected, NULL if success
void* user_data
);
// Async publish function (only async version available)
int nostr_relay_pool_publish_async(
nostr_relay_pool_t* pool,
const char** relay_urls,
int relay_count,
cJSON* event,
publish_response_callback_t callback,
void* user_data);
// Status and statistics functions
nostr_pool_relay_status_t nostr_relay_pool_get_relay_status(
nostr_relay_pool_t* pool,
const char* relay_url);
int nostr_relay_pool_list_relays(
nostr_relay_pool_t* pool,
char*** relay_urls,
nostr_pool_relay_status_t** statuses);
const nostr_relay_stats_t* nostr_relay_pool_get_relay_stats(
nostr_relay_pool_t* pool,
const char* relay_url);
int nostr_relay_pool_reset_relay_stats(
nostr_relay_pool_t* pool,
const char* relay_url);
double nostr_relay_pool_get_relay_query_latency(
nostr_relay_pool_t* pool,
const char* relay_url);
const char* nostr_relay_pool_get_relay_last_publish_error(
nostr_relay_pool_t* pool,
const char* relay_url);
const char* nostr_relay_pool_get_relay_last_connection_error(
nostr_relay_pool_t* pool,
const char* relay_url);
double nostr_relay_pool_get_relay_ping_latency(
nostr_relay_pool_t* pool,
const char* relay_url);
// Synchronous relay operations (one-off queries/publishes)
typedef enum {
RELAY_QUERY_FIRST_RESULT, // Return as soon as first event is received
RELAY_QUERY_MOST_RECENT, // Return the most recent event from all relays
RELAY_QUERY_ALL_RESULTS // Return all unique events from all relays
} relay_query_mode_t;
typedef enum {
PUBLISH_SUCCESS, // Event was accepted by relay
PUBLISH_REJECTED, // Event was rejected by relay
PUBLISH_TIMEOUT, // No response within timeout
PUBLISH_ERROR // Connection or other error
} publish_result_t;
typedef void (*relay_progress_callback_t)(
const char* relay_url,
const char* status,
const char* event_id,
int events_received,
int total_relays,
int completed_relays,
void* user_data);
typedef void (*publish_progress_callback_t)(
const char* relay_url,
const char* status,
const char* message,
int success_count,
int total_relays,
int completed_relays,
void* user_data);
// Synchronous relay query functions
struct cJSON** synchronous_query_relays_with_progress(
const char** relay_urls,
int relay_count,
struct cJSON* filter,
relay_query_mode_t mode,
int* result_count,
int relay_timeout_seconds,
relay_progress_callback_t callback,
void* user_data,
int nip42_enabled,
const unsigned char* private_key);
// Synchronous relay publish functions
publish_result_t* synchronous_publish_event_with_progress(
const char** relay_urls,
int relay_count,
struct cJSON* event,
int* success_count,
int relay_timeout_seconds,
publish_progress_callback_t callback,
void* user_data,
int nip42_enabled,
const unsigned char* private_key);
// Relay communication functions are defined in nostr_common.h
// WebSocket functions are defined in nostr_common.h
#ifdef __cplusplus
}
#endif
#endif /* NOSTR_CORE_H */

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,274 @@
/*
* NOSTR Core Library - Request Validator
*
* Unified authentication and authorization system for NOSTR applications.
* Provides rule-based validation for requests with pluggable database backends.
*
* This module combines basic NOSTR event validation with sophisticated
* authentication rules to provide a single entry point for request validation
* across different NOSTR applications (ginxsom, c-relay, etc.).
*/
#ifndef NOSTR_REQUEST_VALIDATOR_H
#define NOSTR_REQUEST_VALIDATOR_H
#include "nostr_common.h"
#include <time.h>
#include <sqlite3.h>
#ifdef __cplusplus
extern "C" {
#endif
// Forward declaration for cJSON
struct cJSON;
// Authentication rule types
typedef enum {
NOSTR_AUTH_RULE_PUBKEY_WHITELIST,
NOSTR_AUTH_RULE_PUBKEY_BLACKLIST,
NOSTR_AUTH_RULE_HASH_BLACKLIST,
NOSTR_AUTH_RULE_MIME_WHITELIST,
NOSTR_AUTH_RULE_MIME_BLACKLIST,
NOSTR_AUTH_RULE_SIZE_LIMIT,
NOSTR_AUTH_RULE_RATE_LIMIT,
NOSTR_AUTH_RULE_CUSTOM
} nostr_auth_rule_type_t;
// Authentication request context
typedef struct {
const char* operation; // Operation type ("upload", "delete", "list", "publish")
const char* auth_header; // Raw authorization header (optional)
struct cJSON* event; // NOSTR event for validation (optional)
// Resource context (for file/blob operations)
const char* resource_hash; // Resource hash (SHA-256, optional)
const char* mime_type; // MIME type (optional)
long file_size; // File size (optional)
// Client context
const char* client_ip; // Client IP for rate limiting (optional)
void* app_context; // Application-specific context (optional)
} nostr_request_t;
// Authentication result
typedef struct {
int valid; // 0 = invalid/denied, 1 = valid/allowed
int error_code; // NOSTR_SUCCESS or specific error code
char reason[256]; // Human-readable reason for denial
char pubkey[65]; // Extracted pubkey from validated event (if available)
int rule_id; // Rule ID that made the decision (0 if no rule)
int priority; // Priority of the rule that matched
time_t cached_until; // Cache expiration time
} nostr_request_result_t;
// Authentication rule definition
typedef struct {
int rule_id; // Unique rule identifier
nostr_auth_rule_type_t type; // Rule type
char operation[32]; // Target operation ("*", "upload", "delete", "publish", etc.)
char target[256]; // Rule target (pubkey, hash, mime pattern, etc.)
char value[256]; // Rule value (size limit, rate limit, custom data)
int priority; // Rule priority (lower number = higher priority)
int enabled; // 1 = enabled, 0 = disabled
time_t expires_at; // Expiration timestamp (0 = never expires)
char description[512]; // Human-readable description
time_t created_at; // Creation timestamp
} nostr_auth_rule_t;
// Database backend interface (pluggable)
typedef struct nostr_auth_db_interface {
const char* name; // Backend name ("sqlite", "redis", etc.)
// Database lifecycle
int (*init)(const char* db_path, const char* app_name);
void (*cleanup)(void);
// Configuration management
int (*get_config)(const char* key, char* value, size_t value_size);
int (*set_config)(const char* key, const char* value);
// Rule querying and management
int (*query_rules)(const nostr_request_t* request, nostr_auth_rule_t** rules, int* count);
int (*rule_add)(const nostr_auth_rule_t* rule);
int (*rule_remove)(int rule_id);
int (*rule_update)(const nostr_auth_rule_t* rule);
int (*rule_list)(const char* operation, nostr_auth_rule_t** rules, int* count);
// Caching operations
int (*cache_get)(const char* cache_key, nostr_request_result_t* result);
int (*cache_set)(const char* cache_key, const nostr_request_result_t* result, int ttl);
int (*cache_clear)(void);
} nostr_auth_db_interface_t;
//=============================================================================
// CORE API FUNCTIONS
//=============================================================================
/**
* Initialize the request validator system with application database
*
* @param app_db_path Path to application's SQLite database
* @param app_name Application name for logging/identification
* @return NOSTR_SUCCESS on success, error code on failure
*/
int nostr_request_validator_init(const char* app_db_path, const char* app_name);
/**
* Initialize with shared database (future use)
*
* @param shared_db_path Path to shared authentication database
* @param app_name Application name for logging/identification
* @return NOSTR_SUCCESS on success, error code on failure
*/
int nostr_request_validator_init_shared(const char* shared_db_path, const char* app_name);
/**
* Main request validation function - validates both NOSTR events and authentication rules
*
* @param request Request context with operation, auth header, and resource details
* @param result Result structure with validation outcome and details
* @return NOSTR_SUCCESS on successful validation processing, error code on system failure
*/
int nostr_validate_request(const nostr_request_t* request, nostr_request_result_t* result);
/**
* Check if authentication rules system is enabled
*
* @return 1 if enabled, 0 if disabled
*/
int nostr_auth_rules_enabled(void);
/**
* Cleanup request validator resources
*/
void nostr_request_validator_cleanup(void);
//=============================================================================
// CONVENIENCE FUNCTIONS
//=============================================================================
/**
* Convenience function for upload validation (ginxsom integration)
*
* @param pubkey Uploader public key (optional, extracted from auth if NULL)
* @param auth_header Authorization header with NOSTR event
* @param hash File hash (SHA-256)
* @param mime_type File MIME type
* @param file_size File size in bytes
* @return NOSTR_SUCCESS if allowed, error code if denied
*/
int nostr_auth_check_upload(const char* pubkey, const char* auth_header,
const char* hash, const char* mime_type, long file_size);
/**
* Convenience function for delete validation (ginxsom integration)
*
* @param pubkey Requester public key
* @param auth_header Authorization header with NOSTR event
* @param hash File hash to delete
* @return NOSTR_SUCCESS if allowed, error code if denied
*/
int nostr_auth_check_delete(const char* pubkey, const char* auth_header, const char* hash);
/**
* Convenience function for publish validation (c-relay integration)
*
* @param pubkey Publisher public key
* @param event NOSTR event to publish
* @return NOSTR_SUCCESS if allowed, error code if denied
*/
int nostr_auth_check_publish(const char* pubkey, struct cJSON* event);
//=============================================================================
// RULE MANAGEMENT FUNCTIONS
//=============================================================================
/**
* Add a new authentication rule
*
* @param rule Rule definition to add
* @return NOSTR_SUCCESS on success, error code on failure
*/
int nostr_auth_rule_add(const nostr_auth_rule_t* rule);
/**
* Remove an authentication rule by ID
*
* @param rule_id Rule ID to remove
* @return NOSTR_SUCCESS on success, error code on failure
*/
int nostr_auth_rule_remove(int rule_id);
/**
* Update an existing authentication rule
*
* @param rule Updated rule definition
* @return NOSTR_SUCCESS on success, error code on failure
*/
int nostr_auth_rule_update(const nostr_auth_rule_t* rule);
/**
* List authentication rules for a specific operation
*
* @param operation Target operation ("*" for all operations)
* @param rules Pointer to receive allocated array of rules
* @param count Pointer to receive number of rules returned
* @return NOSTR_SUCCESS on success, error code on failure
*/
int nostr_auth_rule_list(const char* operation, nostr_auth_rule_t** rules, int* count);
/**
* Free rule array allocated by nostr_auth_rule_list
*
* @param rules Rule array to free
* @param count Number of rules in array
*/
void nostr_auth_rules_free(nostr_auth_rule_t* rules, int count);
//=============================================================================
// DATABASE BACKEND MANAGEMENT
//=============================================================================
/**
* Register a database backend implementation
*
* @param backend Backend interface implementation
* @return NOSTR_SUCCESS on success, error code on failure
*/
int nostr_auth_register_db_backend(const nostr_auth_db_interface_t* backend);
/**
* Set active database backend by name
*
* @param backend_name Name of backend to activate
* @return NOSTR_SUCCESS on success, error code on failure
*/
int nostr_auth_set_db_backend(const char* backend_name);
//=============================================================================
// CACHE MANAGEMENT
//=============================================================================
/**
* Clear authentication decision cache
*
* @return NOSTR_SUCCESS on success, error code on failure
*/
int nostr_auth_cache_clear(void);
/**
* Get cache statistics
*
* @param hit_count Pointer to receive cache hit count
* @param miss_count Pointer to receive cache miss count
* @param entries Pointer to receive current number of cache entries
* @return NOSTR_SUCCESS on success, error code on failure
*/
int nostr_auth_cache_stats(int* hit_count, int* miss_count, int* entries);
#ifdef __cplusplus
}
#endif
#endif /* NOSTR_REQUEST_VALIDATOR_H */

View File

@@ -9,14 +9,28 @@
#include <stdlib.h>
#include <string.h>
// Include our secp256k1 wrapper for elliptic curve operations
#include "crypto/nostr_secp256k1.h"
// Forward declarations for crypto functions (private API)
// These functions are implemented in crypto/ but not exposed through public headers
// Include our self-contained AES implementation for NIP-04
#include "crypto/nostr_aes.h"
// secp256k1 functions
typedef struct {
unsigned char data[64];
} nostr_secp256k1_pubkey;
// Include our ChaCha20 implementation for NIP-44
#include "crypto/nostr_chacha20.h"
typedef struct {
unsigned char data[96];
} nostr_secp256k1_keypair;
int nostr_secp256k1_context_create(void);
void nostr_secp256k1_context_destroy(void);
int nostr_secp256k1_ec_pubkey_parse(nostr_secp256k1_pubkey* pubkey, const unsigned char* input, size_t inputlen);
int nostr_secp256k1_ecdh(unsigned char* output, const nostr_secp256k1_pubkey* pubkey, const unsigned char* privkey, void* hashfp, void* data);
int nostr_secp256k1_ec_seckey_verify(const unsigned char* seckey);
int nostr_secp256k1_ec_pubkey_create(nostr_secp256k1_pubkey* pubkey, const unsigned char* privkey);
int nostr_secp256k1_ec_pubkey_serialize_compressed(unsigned char* output, const nostr_secp256k1_pubkey* pubkey);
int nostr_secp256k1_keypair_create(nostr_secp256k1_keypair* keypair, const unsigned char* privkey);
int nostr_secp256k1_schnorrsig_sign32(unsigned char* sig, const unsigned char* msg32, const nostr_secp256k1_keypair* keypair, const unsigned char* aux_rand32);
int nostr_secp256k1_ec_seckey_tweak_add(unsigned char* seckey, const unsigned char* tweak);
// =============================================================================
@@ -155,36 +169,64 @@ static int ecdh_hash_function_copy_x(unsigned char* output, const unsigned char*
int ecdh_shared_secret(const unsigned char* private_key,
const unsigned char* public_key_x,
unsigned char* shared_secret) {
if (!private_key || !public_key_x || !shared_secret) return -1;
/*
* WARNING: This function requires proper library initialization!
*
* Before calling this function, you must call nostr_init() to initialize
* the secp256k1 global context. All secp256k1 wrapper functions will fail
* with error code 0 if the global context g_ctx is not initialized.
*
* Example usage:
* if (nostr_init() != NOSTR_SUCCESS) {
* // Handle initialization error
* return -1;
* }
* // Now you can safely call ecdh_shared_secret() and other crypto functions
* int result = ecdh_shared_secret(private_key, public_key_x, shared_secret);
*
* // Don't forget to cleanup when done:
* nostr_cleanup();
*/
if (!private_key || !public_key_x || !shared_secret) {
return -1;
}
// NIP-04 ECDH: The key insight from the specification is that NOSTR requires
// "only the X coordinate of the shared point is used as the secret and it is NOT hashed"
//
// libsecp256k1's default ECDH hashes the shared point with SHA256.
// We need to use a custom hash function that just copies the X coordinate.
//
// This matches exactly what @noble/curves getSharedSecret() does:
// 1. Always use 0x02 prefix (compressed format)
// 2. Perform ECDH scalar multiplication
// 3. Extract only the X coordinate (bytes 1-33 from the compressed point)
// The issue was that we can't just assume the y-coordinate parity.
// We need to try both possible y-coordinate parities (0x02 and 0x03).
unsigned char compressed_pubkey[33];
compressed_pubkey[0] = 0x02; // Always use 0x02 prefix like nostr-tools
nostr_secp256k1_pubkey pubkey;
// Try with 0x02 prefix first (even y-coordinate)
compressed_pubkey[0] = 0x02;
memcpy(compressed_pubkey + 1, public_key_x, 32);
// Parse the public key
nostr_secp256k1_pubkey pubkey;
if (nostr_secp256k1_ec_pubkey_parse(&pubkey, compressed_pubkey, 33) != 1) {
return -1;
if (nostr_secp256k1_ec_pubkey_parse(&pubkey, compressed_pubkey, 33) == 1) {
// Perform ECDH with our custom hash function that copies the X coordinate
if (nostr_secp256k1_ecdh(shared_secret, &pubkey, private_key, ecdh_hash_function_copy_x, NULL) == 1) {
return 0;
}
}
// Perform ECDH with our custom hash function that copies the X coordinate
// This is the crucial fix: we pass ecdh_hash_function_copy_x instead of NULL
if (nostr_secp256k1_ecdh(shared_secret, &pubkey, private_key, ecdh_hash_function_copy_x, NULL) != 1) {
return -1;
}
// Try with 0x03 prefix (odd y-coordinate)
compressed_pubkey[0] = 0x03;
// public_key_x is already copied above
return 0;
if (nostr_secp256k1_ec_pubkey_parse(&pubkey, compressed_pubkey, 33) == 1) {
// Perform ECDH with our custom hash function that copies the X coordinate
if (nostr_secp256k1_ecdh(shared_secret, &pubkey, private_key, ecdh_hash_function_copy_x, NULL) == 1) {
return 0;
}
}
return -1;
}
// =============================================================================
@@ -300,6 +342,125 @@ int nostr_sha256(const unsigned char* data, size_t len, unsigned char* hash) {
return 0;
}
// =============================================================================
// STREAMING SHA-256 IMPLEMENTATION
// =============================================================================
int nostr_sha256_init(nostr_sha256_ctx_t* ctx) {
if (!ctx) return -1;
// Initialize SHA-256 state
ctx->state[0] = 0x6a09e667;
ctx->state[1] = 0xbb67ae85;
ctx->state[2] = 0x3c6ef372;
ctx->state[3] = 0xa54ff53a;
ctx->state[4] = 0x510e527f;
ctx->state[5] = 0x9b05688c;
ctx->state[6] = 0x1f83d9ab;
ctx->state[7] = 0x5be0cd19;
// Initialize counters and buffer
ctx->bitlen = 0;
ctx->buflen = 0;
return 0;
}
int nostr_sha256_update(nostr_sha256_ctx_t* ctx, const unsigned char* data, size_t len) {
if (!ctx || !data) return -1;
for (size_t i = 0; i < len; i++) {
ctx->buffer[ctx->buflen] = data[i];
ctx->buflen++;
// Process complete blocks
if (ctx->buflen == 64) {
sha256_transform(ctx->state, ctx->buffer);
ctx->bitlen += 512; // 64 bytes * 8 bits
ctx->buflen = 0;
}
}
return 0;
}
int nostr_sha256_final(nostr_sha256_ctx_t* ctx, unsigned char* hash) {
if (!ctx || !hash) return -1;
// Calculate final bit length
uint64_t final_bitlen = ctx->bitlen + (ctx->buflen * 8);
// Pad the message
ctx->buffer[ctx->buflen] = 0x80;
ctx->buflen++;
// If not enough space for length, pad and process another block
if (ctx->buflen > 56) {
while (ctx->buflen < 64) {
ctx->buffer[ctx->buflen] = 0x00;
ctx->buflen++;
}
sha256_transform(ctx->state, ctx->buffer);
ctx->buflen = 0;
}
// Pad with zeros up to 56 bytes
while (ctx->buflen < 56) {
ctx->buffer[ctx->buflen] = 0x00;
ctx->buflen++;
}
// Append length as big-endian 64-bit integer
for (int i = 0; i < 8; i++) {
ctx->buffer[56 + i] = (final_bitlen >> (56 - i * 8)) & 0xff;
}
// Process final block
sha256_transform(ctx->state, ctx->buffer);
// Convert state to output bytes
for (int i = 0; i < 8; i++) {
hash[i * 4] = (ctx->state[i] >> 24) & 0xff;
hash[i * 4 + 1] = (ctx->state[i] >> 16) & 0xff;
hash[i * 4 + 2] = (ctx->state[i] >> 8) & 0xff;
hash[i * 4 + 3] = ctx->state[i] & 0xff;
}
// Clear sensitive data
memory_clear(ctx, sizeof(nostr_sha256_ctx_t));
return 0;
}
int nostr_sha256_file_stream(const char* filename, unsigned char* hash) {
if (!filename || !hash) return -1;
FILE* file = fopen(filename, "rb");
if (!file) return -1;
nostr_sha256_ctx_t ctx;
if (nostr_sha256_init(&ctx) != 0) {
fclose(file);
return -1;
}
// Process file in 4KB chunks for memory efficiency
unsigned char buffer[4096];
size_t bytes_read;
while ((bytes_read = fread(buffer, 1, sizeof(buffer), file)) > 0) {
if (nostr_sha256_update(&ctx, buffer, bytes_read) != 0) {
fclose(file);
return -1;
}
}
fclose(file);
// Finalize and return result
return nostr_sha256_final(&ctx, hash);
}
// =============================================================================
// HMAC IMPLEMENTATION
// =============================================================================

View File

@@ -45,6 +45,30 @@ void nostr_crypto_cleanup(void);
// SHA-256 hash function
int nostr_sha256(const unsigned char *data, size_t len, unsigned char *hash);
// =============================================================================
// STREAMING SHA-256 FUNCTIONS
// =============================================================================
// SHA-256 streaming context
typedef struct {
uint32_t state[8]; // Current hash state
unsigned char buffer[64]; // Input buffer for incomplete blocks
uint64_t bitlen; // Total bits processed
size_t buflen; // Current buffer length
} nostr_sha256_ctx_t;
// Initialize SHA-256 streaming context
int nostr_sha256_init(nostr_sha256_ctx_t* ctx);
// Update SHA-256 context with new data
int nostr_sha256_update(nostr_sha256_ctx_t* ctx, const unsigned char* data, size_t len);
// Finalize SHA-256 and output hash
int nostr_sha256_final(nostr_sha256_ctx_t* ctx, unsigned char* hash);
// Stream SHA-256 hash of a file
int nostr_sha256_file_stream(const char* filename, unsigned char* hash);
// HMAC-SHA256
int nostr_hmac_sha256(const unsigned char *key, size_t key_len,
const unsigned char *data, size_t data_len,

View File

@@ -0,0 +1,10 @@
{
"folders": [
{
"path": "."
}
],
"settings": {
"git.ignoreLimitWarning": true
}
}

View File

@@ -131,7 +131,7 @@ static void init_openssl(void) {
nostr_ws_client_t* nostr_ws_connect(const char* url) {
if (!url) return NULL;
// Initialize OpenSSL
init_openssl();
@@ -781,32 +781,35 @@ static int ws_perform_handshake(nostr_ws_client_t* client, const char* key) {
// Read response
char response[MAX_HEADER_SIZE];
int total_received = 0;
while ((size_t)total_received < sizeof(response) - 1) {
int received = client->transport->recv(&client->transport_ctx,
response + total_received,
sizeof(response) - total_received - 1,
client->timeout_ms);
int received = client->transport->recv(&client->transport_ctx,
response + total_received,
sizeof(response) - total_received - 1,
client->timeout_ms);
if (received <= 0) {
return -1;
}
total_received += received;
response[total_received] = '\0';
// Check if we have complete headers
if (strstr(response, "\r\n\r\n")) break;
if (strstr(response, "\r\n\r\n")) {
break;
}
}
// Check if response starts with the correct HTTP status line
if (strncmp(response, "HTTP/1.1 101 Switching Protocols\r\n", 34) != 0) {
return -1;
}
if (!strstr(response, "Upgrade: websocket") && !strstr(response, "upgrade: websocket")) {
if (!strstr(response, "Upgrade: websocket") && !strstr(response, "upgrade: websocket") &&
!strstr(response, "Upgrade: WebSocket") && !strstr(response, "upgrade: WebSocket")) {
return -1;
}
return 0;
}

View File

@@ -13,7 +13,7 @@
"keywords": [],
"author": "",
"license": "ISC",
"type": "commonjs",
"type": "module",
"dependencies": {
"nostr-tools": "^2.16.1"
}

47
pool.log Normal file
View File

@@ -0,0 +1,47 @@
[Tue Oct 7 05:51:04 2025] 🚀 Pool test started
[Tue Oct 7 05:51:07 2025] 🏊 Pool started with default relay
[Tue Oct 7 05:52:03 2025] 🔍 New subscription created (ID: 1)
Filter: {
"limit": 10
}
[Tue Oct 7 05:52:03 2025] 📨 EVENT from ws://localhost:7555
├── ID: 8433206a6e00...
├── Pubkey: 17323141f3a9...
├── Kind: 1
├── Created: 1759687410
└── Content: Test post at 2025-10-05 14:03:30
[Tue Oct 7 05:52:03 2025] 📨 EVENT from ws://localhost:7555
├── ID: ec98292f5700...
├── Pubkey: aa3b44608a9e...
├── Kind: 1
├── Created: 1759687283
└── Content: Test post at 2025-10-05 14:01:23
[Tue Oct 7 05:52:03 2025] 📨 EVENT from ws://localhost:7555
├── ID: c70d6c5c8745...
├── Pubkey: 2a0c81450868...
├── Kind: 1
├── Created: 1759687249
└── Content: Test post at 2025-10-05 14:00:49
[Tue Oct 7 05:52:03 2025] 📨 EVENT from ws://localhost:7555
├── ID: 15dbe2cfe923...
├── Pubkey: 7c2065299249...
├── Kind: 1
├── Created: 1759687219
└── Content: Test post at 2025-10-05 14:00:19
[Tue Oct 7 05:52:03 2025] 📋 EOSE received - 0 events collected
[Tue Oct 7 05:52:31 2025] 🔍 New subscription created (ID: 2)
Filter: {
"since": 1759830747,
"limit": 10
}
[Tue Oct 7 05:52:31 2025] 📋 EOSE received - 0 events collected

File diff suppressed because it is too large Load Diff

View File

@@ -1,33 +0,0 @@
#include "nip004.h"
#include "utils.h"
#include <stdio.h>
int main() {
printf("Testing crypto initialization...\n");
if (nostr_crypto_init() != 0) {
printf("❌ Crypto init failed\n");
return 1;
}
printf("✅ Crypto init successful\n");
// Test random bytes generation
unsigned char random_bytes[16];
if (nostr_secp256k1_get_random_bytes(random_bytes, 16) != 1) {
printf("❌ Random bytes generation failed\n");
return 1;
}
printf("✅ Random bytes generation successful\n");
// Test base64 encoding
char output[64];
size_t encoded_len = base64_encode(random_bytes, 16, output, sizeof(output));
if (encoded_len == 0) {
printf("❌ Base64 encoding failed\n");
return 1;
}
printf("✅ Base64 encoding successful: %s\n", output);
nostr_crypto_cleanup();
return 0;
}

View File

@@ -1,69 +0,0 @@
const { generateSecretKey, getPublicKey, nip04 } = require('nostr-tools');
const { bytesToHex } = require('@noble/hashes/utils');
function generateTestVector(testName, plaintext) {
// Generate random keys
const sk1 = generateSecretKey();
const pk1 = getPublicKey(sk1);
const sk2 = generateSecretKey();
const pk2 = getPublicKey(sk2);
// Encrypt from Alice (sk1) to Bob (pk2)
const encrypted = nip04.encrypt(sk1, pk2, plaintext);
// Verify decryption works (Bob using sk2 to decrypt from Alice pk1)
const decrypted = nip04.decrypt(sk2, pk1, encrypted);
if (decrypted !== plaintext) {
throw new Error(`Decryption failed for ${testName}`);
}
return {
testName,
sk1: bytesToHex(sk1),
pk1: pk1,
sk2: bytesToHex(sk2),
pk2: pk2,
plaintext: plaintext,
encrypted: encrypted
};
}
console.log('=== NIP-04 Test Vector Generation ===\n');
// Generate 3 test vectors with different plaintexts
const testVectors = [
generateTestVector('TEST_VECTOR_2', 'Hello, NOSTR!'),
generateTestVector('TEST_VECTOR_3', 'This is a longer message to test encryption with more content. 🚀'),
generateTestVector('TEST_VECTOR_4', 'Short')
];
// Output in C format for easy integration
testVectors.forEach((vector, index) => {
console.log(`=== ${vector.testName}: ${vector.plaintext.replace(/\n/g, '\\n')} ===`);
console.log(`SK1 (Alice): ${vector.sk1}`);
console.log(`PK1 (Alice): ${vector.pk1}`);
console.log(`SK2 (Bob): ${vector.sk2}`);
console.log(`PK2 (Bob): ${vector.pk2}`);
console.log(`Plaintext: "${vector.plaintext}"`);
console.log(`Expected: ${vector.encrypted}`);
console.log('');
});
// Also output as C code that can be copied directly
console.log('=== C CODE FOR INTEGRATION ===\n');
testVectors.forEach((vector, index) => {
const testNum = index + 2; // Start from 2 since we already have TEST_VECTOR_1
console.log(` // ${vector.testName}: ${vector.plaintext.replace(/"/g, '\\"')}`);
console.log(` {`);
console.log(` "${vector.sk1}",`);
console.log(` "${vector.pk1}",`);
console.log(` "${vector.sk2}",`);
console.log(` "${vector.pk2}",`);
console.log(` "${vector.plaintext.replace(/"/g, '\\"')}",`);
console.log(` "${vector.encrypted}"`);
console.log(` }${index < testVectors.length - 1 ? ',' : ''}`);
});
console.log('\n=== Generation Complete ===');

BIN
tests/async_publish_test Executable file

Binary file not shown.

View File

@@ -0,0 +1,93 @@
#define _DEFAULT_SOURCE
#include "../nostr_core/nostr_core.h"
#include "../cjson/cJSON.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
// Test callback function
static int callback_count = 0;
static int success_count = 0;
void test_callback(const char* relay_url, const char* event_id,
int success, const char* message, void* user_data) {
callback_count++;
if (success) {
success_count++;
}
printf("📡 Callback %d: Relay %s, Event %s, Success: %s\n",
callback_count, relay_url, event_id, success ? "YES" : "NO");
if (message) {
printf(" Message: %s\n", message);
}
// Mark test as complete when we get the expected number of callbacks
int* expected_callbacks = (int*)user_data;
if (callback_count >= *expected_callbacks) {
printf("✅ All callbacks received!\n");
}
}
int main() {
printf("🧪 Testing Async Publish Functionality\n");
printf("=====================================\n");
// Create pool
nostr_relay_pool_t* pool = nostr_relay_pool_create(NULL);
if (!pool) {
printf("❌ Failed to create pool\n");
return 1;
}
// Create a test event
cJSON* event = cJSON_CreateObject();
cJSON_AddStringToObject(event, "id", "test_event_12345");
cJSON_AddNumberToObject(event, "kind", 1);
cJSON_AddStringToObject(event, "content", "Test async publish");
cJSON_AddNumberToObject(event, "created_at", time(NULL));
cJSON_AddStringToObject(event, "pubkey", "test_pubkey");
cJSON_AddStringToObject(event, "sig", "test_signature");
// Test with non-existent relays (should trigger connection failure callbacks)
const char* test_relays[] = {
"ws://nonexistent1.example.com",
"ws://nonexistent2.example.com"
};
int expected_callbacks = 2;
printf("🚀 Testing async publish with connection failure callbacks...\n");
// Call async publish
int sent_count = nostr_relay_pool_publish_async(
pool, test_relays, 2, event, test_callback, &expected_callbacks);
printf("📊 Sent to %d relays\n", sent_count);
// Wait a bit for callbacks (connection failures should be immediate)
printf("⏳ Waiting for callbacks...\n");
for (int i = 0; i < 10 && callback_count < expected_callbacks; i++) {
nostr_relay_pool_poll(pool, 100);
usleep(100000); // 100ms
}
printf("\n📈 Results:\n");
printf(" Callbacks received: %d/%d\n", callback_count, expected_callbacks);
printf(" Successful publishes: %d\n", success_count);
// Test backward compatibility with synchronous version
printf("\n🔄 Testing backward compatibility (sync version)...\n");
int sync_result = nostr_relay_pool_publish_async(pool, test_relays, 2, event, NULL, NULL);
printf(" Sync publish result: %d successful publishes\n", sync_result);
// Cleanup
cJSON_Delete(event);
nostr_relay_pool_destroy(pool);
printf("\n✅ Async publish test completed!\n");
printf(" - Async callbacks: %s\n", callback_count >= expected_callbacks ? "PASS" : "FAIL");
printf(" - Backward compatibility: %s\n", sync_result >= 0 ? "PASS" : "FAIL");
return (callback_count >= expected_callbacks && sync_result >= 0) ? 0 : 1;
}

BIN
tests/backward_compat_test Executable file

Binary file not shown.

View File

@@ -0,0 +1,49 @@
#define _DEFAULT_SOURCE
#include "../nostr_core/nostr_core.h"
#include "../cjson/cJSON.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main() {
printf("🧪 Backward Compatibility Test\n");
printf("===============================\n");
// Create pool
nostr_relay_pool_t* pool = nostr_relay_pool_create(NULL);
if (!pool) {
printf("❌ Failed to create pool\n");
return 1;
}
// Create a test event
cJSON* event = cJSON_CreateObject();
cJSON_AddStringToObject(event, "id", "test_event_sync");
cJSON_AddNumberToObject(event, "kind", 1);
cJSON_AddStringToObject(event, "content", "Test sync publish");
cJSON_AddNumberToObject(event, "created_at", time(NULL));
cJSON_AddStringToObject(event, "pubkey", "test_pubkey");
cJSON_AddStringToObject(event, "sig", "test_signature");
// Test with non-existent relay (should return 0 successful publishes)
const char* test_relays[] = {"ws://nonexistent.example.com"};
printf("🚀 Testing synchronous publish (backward compatibility)...\n");
// Call synchronous publish (old API)
int result = nostr_relay_pool_publish_async(pool, test_relays, 1, event, NULL, NULL);
printf("📊 Synchronous publish result: %d successful publishes\n", result);
// Cleanup
cJSON_Delete(event);
nostr_relay_pool_destroy(pool);
printf("\n✅ Backward compatibility test completed!\n");
printf(" Expected: 0 successful publishes (connection failure)\n");
printf(" Actual: %d successful publishes\n", result);
printf(" Result: %s\n", result == 0 ? "PASS" : "FAIL");
return result == 0 ? 0 : 1;
}

Binary file not shown.

View File

@@ -9,7 +9,15 @@
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include "../nostr_core/crypto/nostr_chacha20.h"
// Forward declarations for ChaCha20 functions (private API)
// These functions are implemented in crypto/ but not exposed through public headers
void chacha20_quarter_round(uint32_t state[16], int a, int b, int c, int d);
int chacha20_block(const uint8_t key[32], uint32_t counter,
const uint8_t nonce[12], uint8_t output[64]);
int chacha20_encrypt(const uint8_t key[32], uint32_t counter,
const uint8_t nonce[12], const uint8_t* input,
uint8_t* output, size_t length);
// Helper function to convert hex string to bytes
static int hex_to_bytes(const char* hex, uint8_t* bytes, size_t len) {

Binary file not shown.

View File

@@ -6,3 +6,11 @@
}]
[10:24:53.944] RECV nostr.mom:443: ["EVENT","sync_0_1755354293",{"content":"GM🫡","created_at":1755354265,"id":"3e7c67349dd3e1ccaaf4dcd6f5987451d63561b14cdff6c6e68cc5448ec5acaf","kind":1,"pubkey":"ff2f4cd786e42b4323749c91517ec7baf22dfd035b7a101bea83b6e2bcbacd15","sig":"2278afa7b7f42b68f8b3f393bb72b6af4b2a34b77008e109232e24bcfe8a3d1ce917187ef1ca68f4a69e52c2067c14da03ed63e31e4137b1175f8ee1a08b7c21","tags":[["e","45983e18b7c0f28933ecd1c4ead88eb5561caa465a5bc939914b64fa455aefc3","wss://relay.primal.net","root"],["p","db625e7637543ca7d7be65025834db318a0c7b75b0e23d4fb9e39229f5ba6fa7","","mention"]]}]
[10:24:53.944] SEND nostr.mom:443: ["CLOSE", "sync_0_1755354293"]
=== NOSTR WebSocket Debug Log Started ===
[12:34:34.841] SEND nostr.mom:443: ["REQ", "sync_0_1756830874", {
"kinds": [1],
"limit": 1
}]
[12:34:34.997] RECV nostr.mom:443: ["EVENT","sync_0_1756830874",{"content":"It's a lot of work. 🫂🫂🫂","created_at":1756830871,"id":"dd1db1b8e25c1278b6dc47b2a05633854a1afb936ea035a06182f4e0683a732e","kind":1,"pubkey":"3f770d65d3a764a9c5cb503ae123e62ec7598ad035d836e2a810f3877a745b24","sig":"33ef0e09477fc978fccfe57d40856952af01b7e413a6174cb99e1d39e0639ba75d5c300f74bd2945c48275debb53f80e7f6a5cc91a4511323b756f5e09707969","tags":[["alt","A short note: It's a lot of work. 🫂🫂🫂"],["e","33597a2e46cffec3da6500c5ddc3cfcf8248e065377e9a5abea23cf633a7c134","wss://nos.lol/","root","91c9a5e1a9744114c6fe2d61ae4de82629eaaa0fb52f48288093c7e7e036f832"],["p","91c9a5e1a9744114c6fe2d61ae4de82629eaaa0fb52f48288093c7e7e036f832","wss://nos.lol/"]]}]
[12:34:34.998] SEND nostr.mom:443: ["CLOSE", "sync_0_1756830874"]

BIN
tests/enhanced_header_test Executable file

Binary file not shown.

View File

@@ -0,0 +1,142 @@
/*
* Enhanced Header Integration Test
*
* Tests that the enhanced nostr_core.h master header provides
* easy access to all documented functionality
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// Test the enhanced master header - single include for everything
#include "../nostr_core/nostr_core.h"
int main(void) {
printf("NOSTR Core Library - Enhanced Header Integration Test\n");
printf("====================================================\n\n");
// Initialize crypto subsystem
if (nostr_crypto_init() != 0) {
printf("❌ Failed to initialize crypto subsystem\n");
return 1;
}
printf("✅ Successfully included nostr_core.h master header\n");
printf("✅ Crypto subsystem initialized\n\n");
// Test 1: Basic cryptographic functions are available
printf("=== Test 1: Basic Crypto Functions ===\n");
unsigned char test_data[] = "Hello, NOSTR!";
unsigned char hash[32];
if (nostr_sha256(test_data, strlen((char*)test_data), hash) == 0) {
printf("✅ nostr_sha256() - Single-call SHA-256 works\n");
} else {
printf("❌ nostr_sha256() failed\n");
goto cleanup;
}
// Test 2: Streaming SHA-256 functions are available
printf("\n=== Test 2: Streaming SHA-256 Functions ===\n");
nostr_sha256_ctx_t ctx;
unsigned char streaming_hash[32];
if (nostr_sha256_init(&ctx) == 0 &&
nostr_sha256_update(&ctx, test_data, strlen((char*)test_data)) == 0 &&
nostr_sha256_final(&ctx, streaming_hash) == 0) {
printf("✅ nostr_sha256_init/update/final() - Streaming SHA-256 works\n");
// Verify streaming matches single-call
if (memcmp(hash, streaming_hash, 32) == 0) {
printf("✅ Streaming result matches single-call result\n");
} else {
printf("❌ Streaming result differs from single-call\n");
}
} else {
printf("❌ Streaming SHA-256 functions failed\n");
goto cleanup;
}
// Test 3: Key generation functions are available
printf("\n=== Test 3: Key Generation Functions ===\n");
unsigned char private_key[32] = {
0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00,
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef
};
unsigned char public_key[32];
if (nostr_ec_private_key_verify(private_key) == 0) {
printf("✅ nostr_ec_private_key_verify() - Private key validation works\n");
} else {
printf("❌ Private key validation failed\n");
goto cleanup;
}
if (nostr_ec_public_key_from_private_key(private_key, public_key) == 0) {
printf("✅ nostr_ec_public_key_from_private_key() - Public key generation works\n");
} else {
printf("❌ Public key generation failed\n");
goto cleanup;
}
// Test 4: Event functions are available
printf("\n=== Test 4: Event Functions ===\n");
cJSON* event = nostr_create_and_sign_event(1, "Test message", NULL, private_key, time(NULL));
if (event) {
printf("✅ nostr_create_and_sign_event() - Event creation works\n");
if (nostr_validate_event(event) == 0) {
printf("✅ nostr_validate_event() - Event validation works\n");
} else {
printf("❌ Event validation failed\n");
}
cJSON_Delete(event);
} else {
printf("❌ Event creation failed\n");
goto cleanup;
}
// Test 5: Utility functions are available
printf("\n=== Test 5: Utility Functions ===\n");
char hex_output[65];
nostr_bytes_to_hex(hash, 32, hex_output);
printf("✅ nostr_bytes_to_hex() - Hash as hex: %.16s...\n", hex_output);
unsigned char hex_back[32];
if (nostr_hex_to_bytes(hex_output, hex_back, 32) == 0) {
printf("✅ nostr_hex_to_bytes() - Hex conversion works\n");
if (memcmp(hash, hex_back, 32) == 0) {
printf("✅ Round-trip hex conversion successful\n");
} else {
printf("❌ Round-trip hex conversion failed\n");
}
} else {
printf("❌ Hex to bytes conversion failed\n");
}
printf("\n====================================================\n");
printf("Enhanced Header Integration Test Results:\n");
printf("✅ Master header includes all functionality\n");
printf("✅ All documented function categories accessible\n");
printf("✅ Single #include provides complete API\n");
printf("✅ Functions work correctly through master header\n");
printf("\nDevelopers can now easily discover and use all\n");
printf("NOSTR Core Library functions with just:\n");
printf(" #include \"nostr_core.h\"\n");
printf("====================================================\n");
cleanup:
nostr_crypto_cleanup();
return 0;
}

Binary file not shown.

BIN
tests/nip01_test Executable file

Binary file not shown.

494
tests/nip01_test.c Normal file
View File

@@ -0,0 +1,494 @@
/*
* NIP-01 Event Validation Test Suite
* Tests event structure validation and cryptographic verification
* Following TESTS POLICY: Shows expected vs actual values, prints entire JSON events
*/
#define _GNU_SOURCE // For strdup on Linux
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "../nostr_core/nip001.h"
#include "../nostr_core/nostr_common.h"
#include "../nostr_core/utils.h"
#include "../cjson/cJSON.h"
// Ensure strdup is declared
#ifndef strdup
extern char *strdup(const char *s);
#endif
// Test counter for tracking progress
static int test_count = 0;
static int passed_tests = 0;
void print_test_header(const char* test_name) {
test_count++;
printf("\n=== TEST %d: %s ===\n", test_count, test_name);
}
void print_test_result(int passed, const char* test_name) {
if (passed) {
passed_tests++;
printf("✅ PASS: %s\n", test_name);
} else {
printf("❌ FAIL: %s\n", test_name);
}
}
void print_json_comparison(const char* label, cJSON* expected, cJSON* actual) {
char* expected_str = cJSON_Print(expected);
char* actual_str;
if (actual) {
actual_str = cJSON_Print(actual);
} else {
actual_str = strdup("NULL");
}
printf("%s Expected JSON:\n%s\n", label, expected_str ? expected_str : "NULL");
printf("%s Actual JSON:\n%s\n", label, actual_str ? actual_str : "NULL");
if (expected_str) free(expected_str);
if (actual_str) free(actual_str);
}
// Test vector 1: Valid event from nak (should pass all validation)
int test_valid_event_1(void) {
print_test_header("Valid Event from nak - Basic");
// Generated with: nak event --sec nsec1j4c6269y9w0q2er2xjw8sv2ehyrtfxq3jwgdlxj6qfn8z4gjsq5qfvfk99 -c "Test event 1" -k 1
const char* event_json = "{"
"\"kind\":1,"
"\"id\":\"f1e582c90f071c0110cc5bcac2dcc6d8c32250e3cc26fcbe93470d918f2ffaf0\","
"\"pubkey\":\"aa4fc8665f5696e33db7e1a572e3b0f5b3d615837b0f362dcb1c8068b098c7b4\","
"\"created_at\":1755599338,"
"\"tags\":[],"
"\"content\":\"Test event 1\","
"\"sig\":\"b9c7148e5a3e4ae1985a4b83a6317df72e2ef78dc4389063b7b83d5642aeccb5fb42f7e820ccfaf38c8077f9f6fef1fa1fd7d05c27c7fbc797cf53ee249ea640\""
"}";
printf("Input Event JSON:\n%s\n\n", event_json);
cJSON* event = cJSON_Parse(event_json);
if (!event) {
printf("❌ JSON Parse Error\n");
return 0;
}
printf("Testing Structure Validation...\n");
int structure_result = nostr_validate_event_structure(event);
printf("Expected: NOSTR_SUCCESS (0)\n");
printf("Actual: %d (%s)\n", structure_result, nostr_strerror(structure_result));
if (structure_result != NOSTR_SUCCESS) {
printf("❌ Structure validation failed\n");
cJSON_Delete(event);
return 0;
}
printf("✅ Structure validation passed\n\n");
printf("Testing Cryptographic Verification...\n");
int crypto_result = nostr_verify_event_signature(event);
printf("Expected: NOSTR_SUCCESS (0)\n");
printf("Actual: %d (%s)\n", crypto_result, nostr_strerror(crypto_result));
if (crypto_result != NOSTR_SUCCESS) {
printf("❌ Cryptographic verification failed\n");
cJSON_Delete(event);
return 0;
}
printf("✅ Cryptographic verification passed\n\n");
printf("Testing Complete Validation...\n");
int complete_result = nostr_validate_event(event);
printf("Expected: NOSTR_SUCCESS (0)\n");
printf("Actual: %d (%s)\n", complete_result, nostr_strerror(complete_result));
cJSON_Delete(event);
return (complete_result == NOSTR_SUCCESS);
}
// Test vector 2: Valid event with tags (should pass all validation)
int test_valid_event_with_tags(void) {
print_test_header("Valid Event with Tags");
// Generated with: nak event --sec nsec1j4c6269y9w0q2er2xjw8sv2ehyrtfxq3jwgdlxj6qfn8z4gjsq5qfvfk99 -c "Test event with tags" -k 1 -t "hello=world" -t "test=value"
const char* event_json = "{"
"\"kind\":1,"
"\"id\":\"781bf3185479315350c4039f719c3a8859a1ebb21a4b04e0e649addf0ae4665b\","
"\"pubkey\":\"aa4fc8665f5696e33db7e1a572e3b0f5b3d615837b0f362dcb1c8068b098c7b4\","
"\"created_at\":1755599345,"
"\"tags\":[[\"hello\",\"world\"],[\"test\",\"value\"]],"
"\"content\":\"Test event with tags\","
"\"sig\":\"3a390ece9d746bd576cb8fbba6f9ed59730099781d0d414d1194f832ca072f23e7764935451a025399202e02f28a59fde2d4c25399e241f3de2ad0b3a4830d6a\""
"}";
printf("Input Event JSON:\n%s\n\n", event_json);
cJSON* event = cJSON_Parse(event_json);
if (!event) {
printf("❌ JSON Parse Error\n");
return 0;
}
int result = nostr_validate_event(event);
printf("Expected: NOSTR_SUCCESS (0)\n");
printf("Actual: %d (%s)\n", result, nostr_strerror(result));
cJSON_Delete(event);
return (result == NOSTR_SUCCESS);
}
// Test vector 3: Valid metadata event (kind 0)
int test_valid_metadata_event(void) {
print_test_header("Valid Metadata Event (Kind 0)");
// Generated with: nak event --sec nsec1j4c6269y9w0q2er2xjw8sv2ehyrtfxq3jwgdlxj6qfn8z4gjsq5qfvfk99 -c "" -k 0 --ts 1640995200
const char* event_json = "{"
"\"kind\":0,"
"\"id\":\"a2ed5175224f6597bc3a553e91d6fcea2e13f199e56afc90ccecca1c890b308a\","
"\"pubkey\":\"aa4fc8665f5696e33db7e1a572e3b0f5b3d615837b0f362dcb1c8068b098c7b4\","
"\"created_at\":1640995200,"
"\"tags\":[],"
"\"content\":\"\","
"\"sig\":\"b9b2859ded73bbc010331145e2d297b30205d2b94d80ef0619077b687d53756b8d9eb3954ef1483b814059cd879c992153ca9d7b50dcf7053a3d530cd82fb884\""
"}";
printf("Input Event JSON:\n%s\n\n", event_json);
cJSON* event = cJSON_Parse(event_json);
if (!event) {
printf("❌ JSON Parse Error\n");
return 0;
}
int result = nostr_validate_event(event);
printf("Expected: NOSTR_SUCCESS (0)\n");
printf("Actual: %d (%s)\n", result, nostr_strerror(result));
cJSON_Delete(event);
return (result == NOSTR_SUCCESS);
}
// Test 4: Missing required field (id)
int test_missing_id_field(void) {
print_test_header("Missing Required Field - ID");
const char* event_json = "{"
"\"kind\":1,"
"\"pubkey\":\"aa4fc8665f5696e33db7e1a572e3b0f5b3d615837b0f362dcb1c8068b098c7b4\","
"\"created_at\":1755599338,"
"\"tags\":[],"
"\"content\":\"Test event 1\","
"\"sig\":\"b9c7148e5a3e4ae1985a4b83a6317df72e2ef78dc4389063b7b83d5642aeccb5fb42f7e820ccfaf38c8077f9f6fef1fa1fd7d05c27c7fbc797cf53ee249ea640\""
"}";
printf("Input Event JSON (missing 'id' field):\n%s\n\n", event_json);
cJSON* event = cJSON_Parse(event_json);
if (!event) {
printf("❌ JSON Parse Error\n");
return 0;
}
int result = nostr_validate_event_structure(event);
printf("Expected: NOSTR_ERROR_EVENT_INVALID_STRUCTURE (-30)\n");
printf("Actual: %d (%s)\n", result, nostr_strerror(result));
cJSON_Delete(event);
return (result == NOSTR_ERROR_EVENT_INVALID_STRUCTURE);
}
// Test 5: Invalid hex string length (pubkey too short)
int test_invalid_pubkey_length(void) {
print_test_header("Invalid Pubkey Length");
const char* event_json = "{"
"\"kind\":1,"
"\"id\":\"f1e582c90f071c0110cc5bcac2dcc6d8c32250e3cc26fcbe93470d918f2ffaf0\","
"\"pubkey\":\"aa4fc8665f5696e33db7e1a572e3b0f5b3d615837b0f362dcb1c8068b098c7\"," // Too short (63 chars instead of 64)
"\"created_at\":1755599338,"
"\"tags\":[],"
"\"content\":\"Test event 1\","
"\"sig\":\"b9c7148e5a3e4ae1985a4b83a6317df72e2ef78dc4389063b7b83d5642aeccb5fb42f7e820ccfaf38c8077f9f6fef1fa1fd7d05c27c7fbc797cf53ee249ea640\""
"}";
printf("Input Event JSON (pubkey too short - 63 chars instead of 64):\n%s\n\n", event_json);
cJSON* event = cJSON_Parse(event_json);
if (!event) {
printf("❌ JSON Parse Error\n");
return 0;
}
int result = nostr_validate_event_structure(event);
printf("Expected: NOSTR_ERROR_EVENT_INVALID_PUBKEY (-32)\n");
printf("Actual: %d (%s)\n", result, nostr_strerror(result));
cJSON_Delete(event);
return (result == NOSTR_ERROR_EVENT_INVALID_PUBKEY);
}
// Test 6: Invalid kind (negative)
int test_invalid_kind_negative(void) {
print_test_header("Invalid Kind - Negative Value");
const char* event_json = "{"
"\"kind\":-1," // Invalid negative kind
"\"id\":\"f1e582c90f071c0110cc5bcac2dcc6d8c32250e3cc26fcbe93470d918f2ffaf0\","
"\"pubkey\":\"aa4fc8665f5696e33db7e1a572e3b0f5b3d615837b0f362dcb1c8068b098c7b4\","
"\"created_at\":1755599338,"
"\"tags\":[],"
"\"content\":\"Test event 1\","
"\"sig\":\"b9c7148e5a3e4ae1985a4b83a6317df72e2ef78dc4389063b7b83d5642aeccb5fb42f7e820ccfaf38c8077f9f6fef1fa1fd7d05c27c7fbc797cf53ee249ea640\""
"}";
printf("Input Event JSON (kind = -1):\n%s\n\n", event_json);
cJSON* event = cJSON_Parse(event_json);
if (!event) {
printf("❌ JSON Parse Error\n");
return 0;
}
int result = nostr_validate_event_structure(event);
printf("Expected: NOSTR_ERROR_EVENT_INVALID_KIND (-35)\n");
printf("Actual: %d (%s)\n", result, nostr_strerror(result));
cJSON_Delete(event);
return (result == NOSTR_ERROR_EVENT_INVALID_KIND);
}
// Test 7: Invalid tags (not an array)
int test_invalid_tags_not_array(void) {
print_test_header("Invalid Tags - Not Array");
const char* event_json = "{"
"\"kind\":1,"
"\"id\":\"f1e582c90f071c0110cc5bcac2dcc6d8c32250e3cc26fcbe93470d918f2ffaf0\","
"\"pubkey\":\"aa4fc8665f5696e33db7e1a572e3b0f5b3d615837b0f362dcb1c8068b098c7b4\","
"\"created_at\":1755599338,"
"\"tags\":\"not an array\"," // Should be array
"\"content\":\"Test event 1\","
"\"sig\":\"b9c7148e5a3e4ae1985a4b83a6317df72e2ef78dc4389063b7b83d5642aeccb5fb42f7e820ccfaf38c8077f9f6fef1fa1fd7d05c27c7fbc797cf53ee249ea640\""
"}";
printf("Input Event JSON (tags is string instead of array):\n%s\n\n", event_json);
cJSON* event = cJSON_Parse(event_json);
if (!event) {
printf("❌ JSON Parse Error\n");
return 0;
}
int result = nostr_validate_event_structure(event);
printf("Expected: NOSTR_ERROR_EVENT_INVALID_TAGS (-36)\n");
printf("Actual: %d (%s)\n", result, nostr_strerror(result));
cJSON_Delete(event);
return (result == NOSTR_ERROR_EVENT_INVALID_TAGS);
}
// Test 8: Wrong event ID (cryptographic test)
int test_wrong_event_id(void) {
print_test_header("Wrong Event ID - Cryptographic Test");
// Take valid event but change the ID to something incorrect
const char* event_json = "{"
"\"kind\":1,"
"\"id\":\"0000000000000000000000000000000000000000000000000000000000000000\"," // Wrong ID
"\"pubkey\":\"aa4fc8665f5696e33db7e1a572e3b0f5b3d615837b0f362dcb1c8068b098c7b4\","
"\"created_at\":1755599338,"
"\"tags\":[],"
"\"content\":\"Test event 1\","
"\"sig\":\"b9c7148e5a3e4ae1985a4b83a6317df72e2ef78dc4389063b7b83d5642aeccb5fb42f7e820ccfaf38c8077f9f6fef1fa1fd7d05c27c7fbc797cf53ee249ea640\""
"}";
printf("Input Event JSON (ID changed to all zeros):\n%s\n\n", event_json);
cJSON* event = cJSON_Parse(event_json);
if (!event) {
printf("❌ JSON Parse Error\n");
return 0;
}
// Structure should pass
int structure_result = nostr_validate_event_structure(event);
printf("Structure validation - Expected: NOSTR_SUCCESS (0)\n");
printf("Structure validation - Actual: %d (%s)\n", structure_result, nostr_strerror(structure_result));
if (structure_result != NOSTR_SUCCESS) {
printf("❌ Unexpected structure validation failure\n");
cJSON_Delete(event);
return 0;
}
// Crypto should fail
int crypto_result = nostr_verify_event_signature(event);
printf("Crypto verification - Expected: NOSTR_ERROR_EVENT_INVALID_ID (-31)\n");
printf("Crypto verification - Actual: %d (%s)\n", crypto_result, nostr_strerror(crypto_result));
cJSON_Delete(event);
return (crypto_result == NOSTR_ERROR_EVENT_INVALID_ID);
}
// Test 9: Invalid signature (cryptographic test)
int test_invalid_signature(void) {
print_test_header("Invalid Signature - Cryptographic Test");
// Take valid event but change the signature to a structurally valid but cryptographically wrong signature
const char* event_json = "{"
"\"kind\":1,"
"\"id\":\"f1e582c90f071c0110cc5bcac2dcc6d8c32250e3cc26fcbe93470d918f2ffaf0\","
"\"pubkey\":\"aa4fc8665f5696e33db7e1a572e3b0f5b3d615837b0f362dcb1c8068b098c7b4\","
"\"created_at\":1755599338,"
"\"tags\":[],"
"\"content\":\"Test event 1\","
"\"sig\":\"deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef\"" // Wrong but valid hex
"}";
printf("Input Event JSON (signature changed to invalid hex):\n%s\n\n", event_json);
cJSON* event = cJSON_Parse(event_json);
if (!event) {
printf("❌ JSON Parse Error\n");
return 0;
}
// Structure should pass
int structure_result = nostr_validate_event_structure(event);
printf("Structure validation - Expected: NOSTR_SUCCESS (0)\n");
printf("Structure validation - Actual: %d (%s)\n", structure_result, nostr_strerror(structure_result));
if (structure_result != NOSTR_SUCCESS) {
printf("❌ Unexpected structure validation failure\n");
cJSON_Delete(event);
return 0;
}
// Crypto should fail
int crypto_result = nostr_verify_event_signature(event);
printf("Crypto verification - Expected: NOSTR_ERROR_EVENT_INVALID_SIGNATURE (-33)\n");
printf("Crypto verification - Actual: %d (%s)\n", crypto_result, nostr_strerror(crypto_result));
cJSON_Delete(event);
return (crypto_result == NOSTR_ERROR_EVENT_INVALID_SIGNATURE);
}
// Test 10: Integration test with our own event creation
int test_integration_with_event_creation(void) {
print_test_header("Integration Test - Validate Our Own Created Event");
// Create a test event using our existing function
const char* private_key_hex = "91ba716fa9e7ea2fcbad360cf4f8e0d312f73984da63d90f524ad61a6a1e7dbe";
unsigned char private_key[32];
nostr_hex_to_bytes(private_key_hex, private_key, 32);
cJSON* tags = cJSON_CreateArray();
cJSON* tag = cJSON_CreateArray();
cJSON_AddItemToArray(tag, cJSON_CreateString("test"));
cJSON_AddItemToArray(tag, cJSON_CreateString("integration"));
cJSON_AddItemToArray(tags, tag);
printf("Creating event with our library...\n");
cJSON* created_event = nostr_create_and_sign_event(1, "Integration test message", tags, private_key, time(NULL));
if (!created_event) {
printf("❌ Event creation failed\n");
cJSON_Delete(tags);
return 0;
}
char* event_str = cJSON_Print(created_event);
printf("Created Event JSON:\n%s\n\n", event_str);
free(event_str);
printf("Validating our created event...\n");
int result = nostr_validate_event(created_event);
printf("Expected: NOSTR_SUCCESS (0)\n");
printf("Actual: %d (%s)\n", result, nostr_strerror(result));
if (result == NOSTR_SUCCESS) {
printf("✅ Our library creates valid events that pass validation\n");
} else {
printf("❌ Our own created event failed validation\n");
}
cJSON_Delete(created_event);
return (result == NOSTR_SUCCESS);
}
int main(void) {
printf("=== NIP-01 Event Validation Test Suite ===\n");
printf("Following TESTS POLICY: Shows expected vs actual values, prints entire JSON events\n");
// Initialize crypto library
if (nostr_init() != NOSTR_SUCCESS) {
printf("❌ Failed to initialize nostr library\n");
return 1;
}
int all_passed = 1;
int test_result;
// Valid event tests
test_result = test_valid_event_1();
print_test_result(test_result, "Valid Event from nak - Basic");
if (!test_result) all_passed = 0;
test_result = test_valid_event_with_tags();
print_test_result(test_result, "Valid Event with Tags");
if (!test_result) all_passed = 0;
test_result = test_valid_metadata_event();
print_test_result(test_result, "Valid Metadata Event (Kind 0)");
if (!test_result) all_passed = 0;
// Invalid structure tests
test_result = test_missing_id_field();
print_test_result(test_result, "Missing Required Field - ID");
if (!test_result) all_passed = 0;
test_result = test_invalid_pubkey_length();
print_test_result(test_result, "Invalid Pubkey Length");
if (!test_result) all_passed = 0;
test_result = test_invalid_kind_negative();
print_test_result(test_result, "Invalid Kind - Negative Value");
if (!test_result) all_passed = 0;
test_result = test_invalid_tags_not_array();
print_test_result(test_result, "Invalid Tags - Not Array");
if (!test_result) all_passed = 0;
// Invalid cryptography tests
test_result = test_wrong_event_id();
print_test_result(test_result, "Wrong Event ID - Cryptographic Test");
if (!test_result) all_passed = 0;
test_result = test_invalid_signature();
print_test_result(test_result, "Invalid Signature - Cryptographic Test");
if (!test_result) all_passed = 0;
// Integration test
test_result = test_integration_with_event_creation();
print_test_result(test_result, "Integration Test - Validate Our Own Created Event");
if (!test_result) all_passed = 0;
// Summary
printf("\n=== TEST SUMMARY ===\n");
printf("Total tests: %d\n", test_count);
printf("Passed: %d\n", passed_tests);
printf("Failed: %d\n", test_count - passed_tests);
if (all_passed) {
printf("🎉 ALL TESTS PASSED! Event validation implementation is working correctly.\n");
} else {
printf("❌ SOME TESTS FAILED. Please review the output above.\n");
}
nostr_cleanup();
return all_passed ? 0 : 1;
}

Binary file not shown.

View File

@@ -8,22 +8,7 @@
#include <string.h>
#include "../nostr_core/nip004.h"
#include "../nostr_core/nostr_common.h"
void print_hex(const char* label, const unsigned char* data, size_t len) {
printf("%s: ", label);
for (size_t i = 0; i < len; i++) {
printf("%02x", data[i]);
}
printf("\n");
}
void hex_to_bytes(const char* hex_str, unsigned char* bytes) {
size_t len = strlen(hex_str);
for (size_t i = 0; i < len; i += 2) {
sscanf(hex_str + i, "%2hhx", &bytes[i / 2]);
}
}
#include "../nostr_core/utils.h"
// Simple replacement for strndup which isn't available in C99
char* safe_strndup(const char* s, size_t n) {
@@ -50,10 +35,10 @@ int test_vector_1(void) {
// Convert hex keys to bytes
unsigned char sk1[32], sk2[32], pk1[32], pk2[32];
hex_to_bytes(sk1_hex, sk1);
hex_to_bytes(sk2_hex, sk2);
hex_to_bytes(pk1_hex, pk1);
hex_to_bytes(pk2_hex, pk2);
nostr_hex_to_bytes(sk1_hex, sk1, 32);
nostr_hex_to_bytes(sk2_hex, sk2, 32);
nostr_hex_to_bytes(pk1_hex, pk1, 32);
nostr_hex_to_bytes(pk2_hex, pk2, 32);
printf("Input Test Vector:\n");
printf("SK1 (Alice): %s\n", sk1_hex);
@@ -157,10 +142,10 @@ int test_vector_2(void) {
// Convert hex keys to bytes
unsigned char sk1[32], sk2[32], pk1[32], pk2[32];
hex_to_bytes(sk1_hex, sk1);
hex_to_bytes(sk2_hex, sk2);
hex_to_bytes(pk1_hex, pk1);
hex_to_bytes(pk2_hex, pk2);
nostr_hex_to_bytes(sk1_hex, sk1, 32);
nostr_hex_to_bytes(sk2_hex, sk2, 32);
nostr_hex_to_bytes(pk1_hex, pk1, 32);
nostr_hex_to_bytes(pk2_hex, pk2, 32);
printf("Input Test Vector:\n");
printf("SK1 (Alice): %s\n", sk1_hex);
@@ -255,10 +240,10 @@ int test_vector_3_bidirectional(void) {
// Convert hex keys to bytes
unsigned char sk1[32], sk2[32], pk1[32], pk2[32];
hex_to_bytes(sk1_hex, sk1);
hex_to_bytes(sk2_hex, sk2);
hex_to_bytes(pk1_hex, pk1);
hex_to_bytes(pk2_hex, pk2);
nostr_hex_to_bytes(sk1_hex, sk1, 32);
nostr_hex_to_bytes(sk2_hex, sk2, 32);
nostr_hex_to_bytes(pk1_hex, pk1, 32);
nostr_hex_to_bytes(pk2_hex, pk2, 32);
printf("Input Test Vector:\n");
printf("SK1 (Alice): %s\n", sk1_hex);
@@ -364,10 +349,10 @@ int test_vector_4_random_keys(void) {
// Convert hex keys to bytes
unsigned char sk1[32], sk2[32], pk1[32], pk2[32];
hex_to_bytes(sk1_hex, sk1);
hex_to_bytes(sk2_hex, sk2);
hex_to_bytes(pk1_hex, pk1);
hex_to_bytes(pk2_hex, pk2);
nostr_hex_to_bytes(sk1_hex, sk1, 32);
nostr_hex_to_bytes(sk2_hex, sk2, 32);
nostr_hex_to_bytes(pk1_hex, pk1, 32);
nostr_hex_to_bytes(pk2_hex, pk2, 32);
printf("Input Test Vector:\n");
printf("SK1 (Alice): %s\n", sk1_hex);
@@ -452,10 +437,10 @@ int test_vector_5_long_message(void) {
// Convert hex keys to bytes
unsigned char sk1[32], sk2[32], pk1[32], pk2[32];
hex_to_bytes(sk1_hex, sk1);
hex_to_bytes(sk2_hex, sk2);
hex_to_bytes(pk1_hex, pk1);
hex_to_bytes(pk2_hex, pk2);
nostr_hex_to_bytes(sk1_hex, sk1, 32);
nostr_hex_to_bytes(sk2_hex, sk2, 32);
nostr_hex_to_bytes(pk1_hex, pk1, 32);
nostr_hex_to_bytes(pk2_hex, pk2, 32);
printf("Input Test Vector:\n");
printf("SK1 (Alice): %s\n", sk1_hex);
@@ -540,10 +525,10 @@ int test_vector_6_short_message(void) {
// Convert hex keys to bytes
unsigned char sk1[32], sk2[32], pk1[32], pk2[32];
hex_to_bytes(sk1_hex, sk1);
hex_to_bytes(sk2_hex, sk2);
hex_to_bytes(pk1_hex, pk1);
hex_to_bytes(pk2_hex, pk2);
nostr_hex_to_bytes(sk1_hex, sk1, 32);
nostr_hex_to_bytes(sk2_hex, sk2, 32);
nostr_hex_to_bytes(pk1_hex, pk1, 32);
nostr_hex_to_bytes(pk2_hex, pk2, 32);
printf("Input Test Vector:\n");
printf("SK1 (Alice): %s\n", sk1_hex);
@@ -644,10 +629,10 @@ int test_vector_7_10kb_payload(void) {
// Convert hex keys to bytes
unsigned char sk1[32], sk2[32], pk1[32], pk2[32];
hex_to_bytes(sk1_hex, sk1);
hex_to_bytes(sk2_hex, sk2);
hex_to_bytes(pk1_hex, pk1);
hex_to_bytes(pk2_hex, pk2);
nostr_hex_to_bytes(sk1_hex, sk1, 32);
nostr_hex_to_bytes(sk2_hex, sk2, 32);
nostr_hex_to_bytes(pk1_hex, pk1, 32);
nostr_hex_to_bytes(pk2_hex, pk2, 32);
printf("Input Test Vector:\n");
printf("SK1 (Alice): %s\n", sk1_hex);
@@ -686,8 +671,8 @@ int test_vector_7_10kb_payload(void) {
printf("Last 80 chars: \"...%.80s\"\n", encrypted + encrypted_len - 80);
printf("\n");
// Test decryption with our ciphertext
char* decrypted = malloc(NOSTR_NIP04_MAX_PLAINTEXT_SIZE);
// Test decryption with our ciphertext - allocate larger buffer for safety
char* decrypted = malloc(NOSTR_NIP04_MAX_PLAINTEXT_SIZE + 1024); // 1MB + 1KB extra
if (!decrypted) {
printf("❌ MEMORY ALLOCATION FAILED for decrypted buffer\n");
free(large_plaintext);
@@ -695,7 +680,7 @@ int test_vector_7_10kb_payload(void) {
return 0;
}
printf("Testing decryption of 1MB ciphertext (Bob decrypts from Alice)...\n");
result = nostr_nip04_decrypt(sk2, pk1, encrypted, decrypted, NOSTR_NIP04_MAX_PLAINTEXT_SIZE);
result = nostr_nip04_decrypt(sk2, pk1, encrypted, decrypted, NOSTR_NIP04_MAX_PLAINTEXT_SIZE + 1024);
if (result != NOSTR_SUCCESS) {
printf("❌ 1MB DECRYPTION FAILED: %s\n", nostr_strerror(result));
@@ -757,11 +742,11 @@ int test_vector_7_10kb_payload(void) {
int main(void) {
printf("=== NIP-04 Encryption Test with Reference Test Vectors ===\n\n");
// Initialize the library
// if (nostr_init() != NOSTR_SUCCESS) {
// printf("ERROR: Failed to initialize NOSTR library\n");
// return 1;
// }
// Initialize the library - REQUIRED for secp256k1 operations
if (nostr_crypto_init() != 0) {
printf("ERROR: Failed to initialize NOSTR crypto library\n");
return 1;
}
int all_passed = 1;
@@ -813,6 +798,7 @@ int main(void) {
printf("❌ SOME TESTS FAILED. Please review the output above.\n");
}
// nostr_cleanup();
// Cleanup crypto resources
nostr_crypto_cleanup();
return all_passed ? 0 : 1;
}

Binary file not shown.

Binary file not shown.

BIN
tests/nip13_test Executable file

Binary file not shown.

491
tests/nip13_test.c Normal file
View File

@@ -0,0 +1,491 @@
/*
* NIP-13 Proof of Work Test Suite
* Tests PoW generation, difficulty calculation, nonce extraction, and validation
* Following TESTS POLICY: Shows expected vs actual values, prints entire JSON events
*/
#define _GNU_SOURCE // For strdup on Linux
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "../nostr_core/nip013.h"
#include "../nostr_core/nip001.h"
#include "../nostr_core/nostr_common.h"
#include "../nostr_core/utils.h"
#include "../cjson/cJSON.h"
// Ensure strdup is declared
#ifndef strdup
extern char *strdup(const char *s);
#endif
// Test counter for tracking progress
static int test_count = 0;
static int passed_tests = 0;
void print_test_header(const char* test_name) {
test_count++;
printf("\n=== TEST %d: %s ===\n", test_count, test_name);
}
void print_test_result(int passed, const char* test_name) {
if (passed) {
passed_tests++;
printf("✅ PASS: %s\n", test_name);
} else {
printf("❌ FAIL: %s\n", test_name);
}
}
// Test 1: Difficulty calculation with NIP-13 example
int test_difficulty_calculation_reference(void) {
print_test_header("Difficulty Calculation - NIP-13 Reference");
// Event ID from NIP-13 spec: 000006d8c378af1779d2feebc7603a125d99eca0ccf1085959b307f64e5dd358
// This should have ~20 leading zero bits as stated in the spec
const char* event_id = "000006d8c378af1779d2feebc7603a125d99eca0ccf1085959b307f64e5dd358";
printf("Testing event ID: %s\n", event_id);
printf("Expected: ~20 leading zero bits\n");
int difficulty = nostr_calculate_pow_difficulty(event_id);
printf("Actual: %d leading zero bits\n", difficulty);
if (difficulty < 0) {
printf("❌ Error calculating difficulty: %d (%s)\n", difficulty, nostr_strerror(difficulty));
return 0;
}
// The NIP-13 spec example should have around 20 bits
if (difficulty >= 19 && difficulty <= 21) {
printf("✅ Difficulty calculation matches expected range (19-21 bits)\n");
return 1;
} else {
printf("❌ Difficulty %d is outside expected range (19-21 bits)\n", difficulty);
return 0;
}
}
// Test 2: Extract nonce info from NIP-13 reference event
int test_nonce_extraction_reference(void) {
print_test_header("Nonce Extraction - NIP-13 Reference Event");
// Complete event from NIP-13 spec
const char* event_json = "{"
"\"id\":\"000006d8c378af1779d2feebc7603a125d99eca0ccf1085959b307f64e5dd358\","
"\"pubkey\":\"a48380f4cfcc1ad5378294fcac36439770f9c878dd880ffa94bb74ea54a6f243\","
"\"created_at\":1651794653,"
"\"kind\":1,"
"\"tags\":[[\"nonce\",\"776797\",\"20\"]],"
"\"content\":\"It's just me mining my own business\","
"\"sig\":\"284622fc0a3f4f1303455d5175f7ba962a3300d136085b9566801bc2e0699de0c7e31e44c81fb40ad9049173742e904713c3594a1da0fc5d2382a25c11aba977\""
"}";
printf("Input Event JSON:\n%s\n\n", event_json);
cJSON* event = cJSON_Parse(event_json);
if (!event) {
printf("❌ JSON Parse Error\n");
return 0;
}
uint64_t nonce_value = 0;
int target_difficulty = -1;
int result = nostr_extract_nonce_info(event, &nonce_value, &target_difficulty);
printf("Extraction result: %d (%s)\n", result, nostr_strerror(result));
printf("Expected nonce: 776797\n");
printf("Actual nonce: %llu\n", (unsigned long long)nonce_value);
printf("Expected target: 20\n");
printf("Actual target: %d\n", target_difficulty);
cJSON_Delete(event);
if (result != NOSTR_SUCCESS) {
printf("❌ Failed to extract nonce info\n");
return 0;
}
if (nonce_value == 776797 && target_difficulty == 20) {
printf("✅ Nonce extraction successful\n");
return 1;
} else {
printf("❌ Extracted values don't match expected\n");
return 0;
}
}
// Test 3: PoW validation with reference event
int test_pow_validation_reference(void) {
print_test_header("PoW Validation - NIP-13 Reference Event");
const char* event_json = "{"
"\"id\":\"000006d8c378af1779d2feebc7603a125d99eca0ccf1085959b307f64e5dd358\","
"\"pubkey\":\"a48380f4cfcc1ad5378294fcac36439770f9c878dd880ffa94bb74ea54a6f243\","
"\"created_at\":1651794653,"
"\"kind\":1,"
"\"tags\":[[\"nonce\",\"776797\",\"20\"]],"
"\"content\":\"It's just me mining my own business\","
"\"sig\":\"284622fc0a3f4f1303455d5175f7ba962a3300d136085b9566801bc2e0699de0c7e31e44c81fb40ad9049173742e904713c3594a1da0fc5d2382a25c11aba977\""
"}";
printf("Input Event JSON:\n%s\n\n", event_json);
cJSON* event = cJSON_Parse(event_json);
if (!event) {
printf("❌ JSON Parse Error\n");
return 0;
}
// Test basic validation (no minimum difficulty)
nostr_pow_result_t result_info;
int validation_result = nostr_validate_pow(event, 0, NOSTR_POW_VALIDATE_BASIC, &result_info);
printf("Validation result: %d (%s)\n", validation_result, nostr_strerror(validation_result));
printf("Actual difficulty: %d\n", result_info.actual_difficulty);
printf("Committed target: %d\n", result_info.committed_target);
printf("Nonce value: %llu\n", (unsigned long long)result_info.nonce_value);
printf("Has nonce tag: %d\n", result_info.has_nonce_tag);
printf("Error detail: %s\n", result_info.error_detail);
cJSON_Delete(event);
return (validation_result == NOSTR_SUCCESS && result_info.has_nonce_tag == 1);
}
// Test 4: Generate PoW with our library and validate it
int test_pow_generation_and_validation(void) {
print_test_header("PoW Generation and Validation - Our Library");
// Create a test event for mining
const char* private_key_hex = "91ba716fa9e7ea2fcbad360cf4f8e0d312f73984da63d90f524ad61a6a1e7dbe";
unsigned char private_key[32];
nostr_hex_to_bytes(private_key_hex, private_key, 32);
cJSON* tags = cJSON_CreateArray();
printf("Creating base event...\n");
cJSON* event = nostr_create_and_sign_event(1, "Testing PoW generation", tags, private_key, time(NULL));
if (!event) {
printf("❌ Event creation failed\n");
cJSON_Delete(tags);
return 0;
}
char* original_event_str = cJSON_Print(event);
printf("Original event (no PoW):\n%s\n\n", original_event_str);
free(original_event_str);
// Add PoW with difficulty 8 (reasonable for testing)
printf("Adding PoW with difficulty 8...\n");
int pow_result = nostr_add_proof_of_work(event, private_key, 8, 100000, 10000, 10000, NULL, NULL);
if (pow_result != NOSTR_SUCCESS) {
printf("❌ PoW generation failed: %d (%s)\n", pow_result, nostr_strerror(pow_result));
cJSON_Delete(event);
return 0;
}
char* pow_event_str = cJSON_Print(event);
printf("Event with PoW:\n%s\n\n", pow_event_str);
free(pow_event_str);
// Now validate the PoW
printf("Validating generated PoW...\n");
nostr_pow_result_t result_info;
int validation_result = nostr_validate_pow(event, 8, NOSTR_POW_VALIDATE_FULL, &result_info);
printf("Validation result: %d (%s)\n", validation_result, nostr_strerror(validation_result));
printf("Actual difficulty: %d\n", result_info.actual_difficulty);
printf("Committed target: %d\n", result_info.committed_target);
printf("Has nonce tag: %d\n", result_info.has_nonce_tag);
printf("Error detail: %s\n", result_info.error_detail);
cJSON_Delete(event);
if (validation_result == NOSTR_SUCCESS && result_info.actual_difficulty >= 8) {
printf("✅ PoW generation and validation successful\n");
return 1;
} else {
printf("❌ PoW validation failed\n");
return 0;
}
}
// Test 5: Test various validation flag combinations
int test_validation_flags(void) {
print_test_header("Validation Flags - Various Combinations");
// Use NIP-13 reference event
const char* event_json = "{"
"\"id\":\"000006d8c378af1779d2feebc7603a125d99eca0ccf1085959b307f64e5dd358\","
"\"pubkey\":\"a48380f4cfcc1ad5378294fcac36439770f9c878dd880ffa94bb74ea54a6f243\","
"\"created_at\":1651794653,"
"\"kind\":1,"
"\"tags\":[[\"nonce\",\"776797\",\"20\"]],"
"\"content\":\"It's just me mining my own business\","
"\"sig\":\"284622fc0a3f4f1303455d5175f7ba962a3300d136085b9566801bc2e0699de0c7e31e44c81fb40ad9049173742e904713c3594a1da0fc5d2382a25c11aba977\""
"}";
cJSON* event = cJSON_Parse(event_json);
if (!event) {
printf("❌ JSON Parse Error\n");
return 0;
}
int all_passed = 1;
nostr_pow_result_t result_info;
// Test 1: Basic validation
printf("\nSubtest 1: NOSTR_POW_VALIDATE_BASIC\n");
int result = nostr_validate_pow(event, 0, NOSTR_POW_VALIDATE_BASIC, &result_info);
printf("Result: %d (%s)\n", result, nostr_strerror(result));
if (result != NOSTR_SUCCESS) all_passed = 0;
// Test 2: Full validation
printf("\nSubtest 2: NOSTR_POW_VALIDATE_FULL\n");
result = nostr_validate_pow(event, 0, NOSTR_POW_VALIDATE_FULL, &result_info);
printf("Result: %d (%s)\n", result, nostr_strerror(result));
if (result != NOSTR_SUCCESS) all_passed = 0;
// Test 3: Anti-spam validation (should pass since committed target matches actual)
printf("\nSubtest 3: NOSTR_POW_VALIDATE_ANTI_SPAM\n");
result = nostr_validate_pow(event, 0, NOSTR_POW_VALIDATE_ANTI_SPAM, &result_info);
printf("Result: %d (%s)\n", result, nostr_strerror(result));
if (result != NOSTR_SUCCESS) all_passed = 0;
// Test 4: Minimum difficulty requirement (15 bits - should pass)
printf("\nSubtest 4: Minimum difficulty 15 bits\n");
result = nostr_validate_pow(event, 15, NOSTR_POW_VALIDATE_BASIC, &result_info);
printf("Result: %d (%s)\n", result, nostr_strerror(result));
if (result != NOSTR_SUCCESS) all_passed = 0;
// Test 5: Too high minimum difficulty (30 bits - should fail)
printf("\nSubtest 5: Minimum difficulty 30 bits (should fail)\n");
result = nostr_validate_pow(event, 30, NOSTR_POW_VALIDATE_BASIC, &result_info);
printf("Result: %d (%s)\n", result, nostr_strerror(result));
if (result == NOSTR_SUCCESS) {
printf("❌ Should have failed but didn't\n");
all_passed = 0;
} else if (result == NOSTR_ERROR_NIP13_INSUFFICIENT) {
printf("✅ Correctly failed with insufficient difficulty\n");
} else {
printf("❌ Failed with unexpected error\n");
all_passed = 0;
}
cJSON_Delete(event);
return all_passed;
}
// Test 6: Error conditions and edge cases
int test_error_conditions(void) {
print_test_header("Error Conditions and Edge Cases");
int all_passed = 1;
// Test 1: NULL event
printf("\nSubtest 1: NULL event\n");
int result = nostr_validate_pow(NULL, 0, NOSTR_POW_VALIDATE_BASIC, NULL);
printf("Expected: NOSTR_ERROR_INVALID_INPUT (-1)\n");
printf("Actual: %d (%s)\n", result, nostr_strerror(result));
if (result != NOSTR_ERROR_INVALID_INPUT) all_passed = 0;
// Test 2: Event without ID
printf("\nSubtest 2: Event without ID\n");
const char* no_id_json = "{\"kind\":1,\"content\":\"test\",\"tags\":[]}";
cJSON* no_id_event = cJSON_Parse(no_id_json);
result = nostr_validate_pow(no_id_event, 0, NOSTR_POW_VALIDATE_BASIC, NULL);
printf("Expected: NOSTR_ERROR_EVENT_INVALID_ID (-31)\n");
printf("Actual: %d (%s)\n", result, nostr_strerror(result));
if (result != NOSTR_ERROR_EVENT_INVALID_ID) all_passed = 0;
cJSON_Delete(no_id_event);
// Test 3: Event without nonce tag when required
printf("\nSubtest 3: Event without nonce tag when required\n");
const char* no_nonce_json = "{"
"\"id\":\"f1e582c90f071c0110cc5bcac2dcc6d8c32250e3cc26fcbe93470d918f2ffaf0\","
"\"kind\":1,\"content\":\"test\",\"tags\":[]"
"}";
cJSON* no_nonce_event = cJSON_Parse(no_nonce_json);
result = nostr_validate_pow(no_nonce_event, 0, NOSTR_POW_VALIDATE_NONCE_TAG, NULL);
printf("Expected: NOSTR_ERROR_NIP13_NO_NONCE_TAG (-101)\n");
printf("Actual: %d (%s)\n", result, nostr_strerror(result));
if (result != NOSTR_ERROR_NIP13_NO_NONCE_TAG) all_passed = 0;
cJSON_Delete(no_nonce_event);
// Test 4: Invalid hex ID
printf("\nSubtest 4: Invalid hex ID\n");
result = nostr_calculate_pow_difficulty("invalid_hex_string");
printf("Expected: NOSTR_ERROR_NIP13_CALCULATION (-104)\n");
printf("Actual: %d (%s)\n", result, nostr_strerror(result));
if (result != NOSTR_ERROR_NIP13_CALCULATION) all_passed = 0;
// Test 5: Wrong length hex ID
printf("\nSubtest 5: Wrong length hex ID\n");
result = nostr_calculate_pow_difficulty("f1e582c90f071c0110cc5bcac2dcc6d8c32250e3cc26fcbe93470d918f2ffa"); // 63 chars instead of 64
printf("Expected: NOSTR_ERROR_NIP13_CALCULATION (-104)\n");
printf("Actual: %d (%s)\n", result, nostr_strerror(result));
if (result != NOSTR_ERROR_NIP13_CALCULATION) all_passed = 0;
return all_passed;
}
// Test 7: Integration with nak-generated PoW events
int test_nak_integration(void) {
print_test_header("Integration with nak-generated PoW events");
// Generate a test event with nak and PoW difficulty 8
printf("Generating PoW event with nak (difficulty 8)...\n");
// Create a temporary key for this test
char temp_key[] = "/tmp/nip13_test_key_XXXXXX";
int fd = mkstemp(temp_key);
if (fd == -1) {
printf("❌ Failed to create temporary key file\n");
return 0;
}
// Write test key to file (same as used in other tests)
const char* test_key = "91ba716fa9e7ea2fcbad360cf4f8e0d312f73984da63d90f524ad61a6a1e7dbe";
ssize_t bytes_written = write(fd, test_key, strlen(test_key));
close(fd);
if (bytes_written != (ssize_t)strlen(test_key)) {
printf("❌ Failed to write test key to temporary file\n");
unlink(temp_key);
return 0;
}
// Generate event with PoW using nak
char cmd[1024];
snprintf(cmd, sizeof(cmd), "nak event --sec %s -c \"PoW test from nak\" --pow 8 --ts %ld",
test_key, (long)time(NULL));
printf("Executing: %s\n", cmd);
FILE* pipe = popen(cmd, "r");
if (!pipe) {
printf("❌ Failed to execute nak command\n");
unlink(temp_key);
return 0;
}
char event_json[4096] = {0};
if (!fgets(event_json, sizeof(event_json), pipe)) {
printf("❌ Failed to read nak output\n");
pclose(pipe);
unlink(temp_key);
return 0;
}
pclose(pipe);
unlink(temp_key);
// Remove trailing newline
event_json[strcspn(event_json, "\n")] = 0;
printf("nak-generated event:\n%s\n\n", event_json);
// Parse and validate the event
cJSON* event = cJSON_Parse(event_json);
if (!event) {
printf("❌ Failed to parse nak-generated JSON\n");
return 0;
}
// Validate the PoW
nostr_pow_result_t result_info;
int validation_result = nostr_validate_pow(event, 8, NOSTR_POW_VALIDATE_FULL, &result_info);
printf("Validation result: %d (%s)\n", validation_result, nostr_strerror(validation_result));
printf("Actual difficulty: %d\n", result_info.actual_difficulty);
printf("Committed target: %d\n", result_info.committed_target);
printf("Has nonce tag: %d\n", result_info.has_nonce_tag);
printf("Error detail: %s\n", result_info.error_detail);
cJSON_Delete(event);
if (validation_result == NOSTR_SUCCESS && result_info.actual_difficulty >= 8) {
printf("✅ nak-generated PoW event validates successfully\n");
return 1;
} else {
printf("❌ nak-generated PoW event validation failed\n");
return 0;
}
}
int main(void) {
printf("=== NIP-13 Proof of Work Test Suite ===\n");
printf("Following TESTS POLICY: Shows expected vs actual values, prints entire JSON events\n");
printf("Tests both PoW generation and validation functionality\n");
// Initialize crypto library
if (nostr_init() != NOSTR_SUCCESS) {
printf("❌ Failed to initialize nostr library\n");
return 1;
}
int all_passed = 1;
int test_result;
// Test 1: Basic difficulty calculation
test_result = test_difficulty_calculation_reference();
print_test_result(test_result, "Difficulty Calculation - NIP-13 Reference");
if (!test_result) all_passed = 0;
// Test 2: Nonce extraction
test_result = test_nonce_extraction_reference();
print_test_result(test_result, "Nonce Extraction - NIP-13 Reference Event");
if (!test_result) all_passed = 0;
// Test 3: Basic PoW validation
test_result = test_pow_validation_reference();
print_test_result(test_result, "PoW Validation - NIP-13 Reference Event");
if (!test_result) all_passed = 0;
// Test 4: PoW generation and validation
test_result = test_pow_generation_and_validation();
print_test_result(test_result, "PoW Generation and Validation - Our Library");
if (!test_result) all_passed = 0;
// Test 5: Validation flags
test_result = test_validation_flags();
print_test_result(test_result, "Validation Flags - Various Combinations");
if (!test_result) all_passed = 0;
// Test 6: Error conditions
test_result = test_error_conditions();
print_test_result(test_result, "Error Conditions and Edge Cases");
if (!test_result) all_passed = 0;
// Test 7: nak integration (optional - may fail if nak not available)
printf("\n=== Optional nak Integration Test ===\n");
test_result = test_nak_integration();
if (test_result) {
print_test_result(test_result, "Integration with nak-generated PoW events");
} else {
printf("⚠️ SKIP: Integration with nak-generated PoW events (nak may not be available)\n");
// Don't fail the entire test suite for this optional test
}
// Summary
printf("\n=== TEST SUMMARY ===\n");
printf("Total tests: %d\n", test_count);
printf("Passed: %d\n", passed_tests);
printf("Failed: %d\n", test_count - passed_tests);
if (all_passed) {
printf("🎉 ALL TESTS PASSED! NIP-13 PoW implementation is working correctly.\n");
printf("✅ PoW generation works\n");
printf("✅ PoW difficulty calculation works\n");
printf("✅ Nonce extraction works\n");
printf("✅ PoW validation works\n");
printf("✅ Error handling works\n");
} else {
printf("❌ SOME TESTS FAILED. Please review the output above.\n");
}
nostr_cleanup();
return all_passed ? 0 : 1;
}

BIN
tests/nip17_test Executable file

Binary file not shown.

341
tests/nip17_test.c Normal file
View File

@@ -0,0 +1,341 @@
/*
* NIP-17 Private Direct Messages Test Program
*
* Tests the complete NIP-17 DM flow using synchronous relay operations:
* 1. Generate sender and recipient keypairs
* 2. Create a DM from sender to recipient
* 3. Publish the gift-wrapped DM to relay
* 4. Subscribe to receive the DM back
* 5. Decrypt and verify the received DM
*/
#define _GNU_SOURCE
#define _POSIX_C_SOURCE 200809L
// Enable debug output to see all relay messages
#define NOSTR_DEBUG_ENABLED
#include "../nostr_core/nostr_core.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
// Forward declarations for crypto functions
int nostr_secp256k1_get_random_bytes(unsigned char* buf, size_t len);
// Test configuration
#define RELAY_URL "wss://relay.laantungir.net"
#define TEST_TIMEOUT_MS 5000
// Progress callback for publishing
void publish_progress_callback(const char* relay_url, const char* status,
const char* message, int success_count,
int total_relays, int completed_relays, void* user_data) {
(void)user_data;
if (relay_url) {
printf("📡 PUBLISH [%s]: %s", relay_url, status);
if (message) {
printf(" - %s", message);
}
printf(" (%d/%d completed, %d successful)\n", completed_relays, total_relays, success_count);
} else {
printf("📡 PUBLISH COMPLETE: %d/%d successful\n", success_count, total_relays);
}
}
// Progress callback for querying/subscribing
void query_progress_callback(const char* relay_url, const char* status,
const char* event_id, int event_count,
int total_relays, int completed_relays, void* user_data) {
(void)user_data;
if (relay_url) {
printf("🔍 QUERY [%s]: %s", relay_url, status);
if (event_id) {
printf(" - Event: %.12s...", event_id);
}
if (event_count > 0) {
printf(" (%d events)", event_count);
}
printf(" (%d/%d completed)\n", completed_relays, total_relays);
} else {
printf("🔍 QUERY COMPLETE: %d events found\n", event_count);
}
}
/**
* Generate a random keypair for testing
*/
void generate_test_keypair(unsigned char* private_key, char* pubkey_hex) {
// Generate random private key
if (nostr_secp256k1_get_random_bytes(private_key, 32) != 1) {
fprintf(stderr, "Failed to generate random private key\n");
exit(1);
}
// Derive public key
unsigned char public_key[32];
if (nostr_ec_public_key_from_private_key(private_key, public_key) != 0) {
fprintf(stderr, "Failed to derive public key\n");
exit(1);
}
// Convert to hex
nostr_bytes_to_hex(public_key, 32, pubkey_hex);
}
/**
* Main test function
*/
int main(int argc, char* argv[]) {
(void)argc; // Suppress unused parameter warning
(void)argv; // Suppress unused parameter warning
printf("🧪 NIP-17 Private Direct Messages Test (Synchronous)\n");
printf("=================================================\n\n");
// Initialize crypto
if (nostr_init() != NOSTR_SUCCESS) {
fprintf(stderr, "Failed to initialize crypto\n");
return 1;
}
// Generate keypairs
unsigned char sender_privkey[32];
unsigned char recipient_privkey[32];
char sender_pubkey_hex[65];
char recipient_pubkey_hex[65];
printf("🔑 Generating keypairs...\n");
generate_test_keypair(sender_privkey, sender_pubkey_hex);
generate_test_keypair(recipient_privkey, recipient_pubkey_hex);
printf("📤 Sender pubkey: %s\n", sender_pubkey_hex);
printf("📥 Recipient pubkey: %s\n", recipient_pubkey_hex);
printf("\n");
// Create DM event with timestamp
printf("💬 Creating DM event...\n");
time_t now = time(NULL);
char test_message[256];
snprintf(test_message, sizeof(test_message),
"Hello from NIP-17! This is a private direct message sent at %ld", now);
const char* recipient_pubkeys[] = {recipient_pubkey_hex};
cJSON* dm_event = nostr_nip17_create_chat_event(
test_message,
recipient_pubkeys,
1,
"NIP-17 Test",
NULL, // no reply
RELAY_URL,
sender_pubkey_hex
);
if (!dm_event) {
fprintf(stderr, "Failed to create DM event\n");
return 1;
}
printf("📝 Created DM event (kind 14)\n");
// Send DM (create gift wraps)
printf("🎁 Creating gift wraps...\n");
cJSON* gift_wraps[10]; // Max 10 gift wraps
int gift_wrap_count = nostr_nip17_send_dm(
dm_event,
recipient_pubkeys,
1,
sender_privkey,
gift_wraps,
10
);
cJSON_Delete(dm_event); // Original DM event no longer needed
if (gift_wrap_count <= 0) {
fprintf(stderr, "Failed to create gift wraps\n");
return 1;
}
printf("✅ Created %d gift wrap(s)\n", gift_wrap_count);
// Print the gift wrap JSON
printf("\n📄 Gift wrap event JSON:\n");
printf("========================\n");
char* gift_wrap_json = cJSON_Print(gift_wraps[0]);
printf("%s\n", gift_wrap_json);
free(gift_wrap_json);
// PHASE 1: Publish the gift wrap to relay
printf("\n📤 PHASE 1: Publishing gift wrap to relay\n");
printf("==========================================\n");
const char* relay_urls[] = {RELAY_URL};
int success_count = 0;
publish_result_t* publish_results = synchronous_publish_event_with_progress(
relay_urls,
1, // single relay
gift_wraps[0], // Send the first gift wrap
&success_count,
10, // 10 second timeout
publish_progress_callback,
NULL, // no user data
0, // NIP-42 disabled
NULL // no private key for auth
);
if (!publish_results || success_count != 1) {
fprintf(stderr, "❌ Failed to publish gift wrap (success_count: %d)\n", success_count);
// Clean up gift wraps
for (int i = 0; i < gift_wrap_count; i++) {
cJSON_Delete(gift_wraps[i]);
}
if (publish_results) free(publish_results);
return 1;
}
printf("✅ Successfully published gift wrap!\n");
// Clean up publish results and gift wraps
free(publish_results);
for (int i = 0; i < gift_wrap_count; i++) {
cJSON_Delete(gift_wraps[i]);
}
// Small delay to let the relay process the event
printf("⏳ Waiting 2 seconds for relay to process...\n");
sleep(2);
// PHASE 2: Subscribe to receive the DM back
printf("\n📥 PHASE 2: Subscribing to receive DM back\n");
printf("===========================================\n");
// Create filter for gift wraps addressed to recipient
cJSON* filter = cJSON_CreateObject();
cJSON* kinds = cJSON_CreateArray();
cJSON_AddItemToArray(kinds, cJSON_CreateNumber(1059)); // Gift wrap kind
cJSON_AddItemToObject(filter, "kinds", kinds);
// Filter for gift wraps with p tag matching recipient
cJSON* p_tags = cJSON_CreateArray();
cJSON_AddItemToArray(p_tags, cJSON_CreateString(recipient_pubkey_hex));
cJSON_AddItemToObject(filter, "#p", p_tags);
// Print the subscription filter JSON
printf("📄 Subscription filter JSON:\n");
printf("============================\n");
char* filter_json = cJSON_Print(filter);
printf("%s\n", filter_json);
free(filter_json);
int query_result_count = 0;
cJSON** query_results = synchronous_query_relays_with_progress(
relay_urls,
1, // single relay
filter,
RELAY_QUERY_ALL_RESULTS, // Get all matching events
&query_result_count,
10, // 10 second timeout per relay
query_progress_callback,
NULL, // no user data
0, // NIP-42 disabled
NULL // no private key for auth
);
cJSON_Delete(filter); // Clean up filter
if (!query_results || query_result_count == 0) {
fprintf(stderr, "❌ No DM events received back from relay\n");
if (query_results) free(query_results);
return 1;
}
printf("✅ Received %d event(s) from relay!\n", query_result_count);
// Process the received events
printf("\n🔍 PHASE 3: Processing received events\n");
printf("=====================================\n");
int dm_found = 0;
cJSON* received_dm = NULL;
for (int i = 0; i < query_result_count; i++) {
cJSON* event = query_results[i];
cJSON* kind_item = cJSON_GetObjectItem(event, "kind");
cJSON* id_item = cJSON_GetObjectItem(event, "id");
if (kind_item && cJSON_IsNumber(kind_item) && cJSON_GetNumberValue(kind_item) == 1059) {
printf("🎁 Found gift wrap event: %.12s...\n",
id_item && cJSON_IsString(id_item) ? cJSON_GetStringValue(id_item) : "unknown");
// Try to decrypt this gift wrap
cJSON* decrypted_dm = nostr_nip17_receive_dm(event, recipient_privkey);
if (decrypted_dm) {
printf("✅ Successfully decrypted gift wrap!\n");
// Verify the decrypted DM
cJSON* content_item = cJSON_GetObjectItem(decrypted_dm, "content");
cJSON* dm_kind_item = cJSON_GetObjectItem(decrypted_dm, "kind");
cJSON* pubkey_item = cJSON_GetObjectItem(decrypted_dm, "pubkey");
if (content_item && dm_kind_item && pubkey_item) {
const char* decrypted_content = cJSON_GetStringValue(content_item);
int decrypted_kind = (int)cJSON_GetNumberValue(dm_kind_item);
const char* decrypted_pubkey = cJSON_GetStringValue(pubkey_item);
printf("📧 Decrypted DM:\n");
printf(" Kind: %d\n", decrypted_kind);
printf(" From: %s\n", decrypted_pubkey);
printf(" Content: %s\n", decrypted_content);
// Verify the DM content (check that it contains our message with timestamp)
int content_ok = strstr(decrypted_content, "Hello from NIP-17! This is a private direct message sent at ") != NULL;
if (decrypted_kind == 14 &&
content_ok &&
strlen(decrypted_content) >= 64 && // Should be at least as long as the prefix
strcmp(decrypted_pubkey, sender_pubkey_hex) == 0) {
printf("✅ DM verification successful!\n");
dm_found = 1;
received_dm = decrypted_dm; // Keep reference for cleanup
break; // Found our DM, no need to check more
} else {
printf("❌ DM verification failed\n");
cJSON_Delete(decrypted_dm);
}
} else {
printf("❌ Invalid decrypted DM structure\n");
cJSON_Delete(decrypted_dm);
}
} else {
printf("❌ Failed to decrypt gift wrap\n");
}
}
}
// Clean up query results
for (int i = 0; i < query_result_count; i++) {
cJSON_Delete(query_results[i]);
}
free(query_results);
if (!dm_found) {
fprintf(stderr, "❌ Could not find or decrypt the expected DM\n");
return 1;
}
printf("\n🎉 NIP-17 synchronous test completed successfully!\n");
// Cleanup
if (received_dm) {
cJSON_Delete(received_dm);
}
nostr_cleanup();
return 0;
}

BIN
tests/nip21_test Executable file

Binary file not shown.

373
tests/nip21_test.c Normal file
View File

@@ -0,0 +1,373 @@
/*
* NIP-21 URI Scheme Test Suite
* Tests nostr: URI parsing and construction functionality
* Following TESTS POLICY: Shows expected vs actual values
*/
#define _GNU_SOURCE // For strdup on Linux
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "../nostr_core/nip021.h"
#include "../nostr_core/nostr_common.h"
#include "../nostr_core/utils.h"
// Ensure strdup is declared
#ifndef strdup
extern char *strdup(const char *s);
#endif
// Test counter for tracking progress
static int test_count = 0;
static int passed_tests = 0;
void print_test_header(const char* test_name) {
test_count++;
printf("\n=== TEST %d: %s ===\n", test_count, test_name);
}
void print_test_result(int passed, const char* test_name) {
if (passed) {
passed_tests++;
printf("✅ PASS: %s\n", test_name);
} else {
printf("❌ FAIL: %s\n", test_name);
}
}
// Test 1: Parse note URI
int test_parse_note_uri(void) {
print_test_header("Parse note: URI");
// First build a valid note URI, then parse it
unsigned char event_id[32];
nostr_hex_to_bytes("f1e582c90f071c0110cc5bcac2dcc6d8c32250e3cc26fcbe93470d918f2ffaf0", event_id, 32);
char built_uri[200];
int build_result = nostr_build_uri_note(event_id, built_uri, sizeof(built_uri));
if (build_result != NOSTR_SUCCESS) {
printf("Failed to build URI for testing: %d (%s)\n", build_result, nostr_strerror(build_result));
return 0;
}
printf("Input URI: %s\n", built_uri);
nostr_uri_result_t result;
int parse_result = nostr_parse_uri(built_uri, &result);
printf("Expected: NOSTR_SUCCESS (0)\n");
printf("Actual: %d (%s)\n", parse_result, nostr_strerror(parse_result));
if (parse_result != NOSTR_SUCCESS) {
return 0;
}
printf("Expected type: NOSTR_URI_NOTE\n");
printf("Actual type: %d\n", result.type);
if (result.type != NOSTR_URI_NOTE) {
return 0;
}
printf("Expected event ID to be set\n");
printf("Event ID present: %s\n", result.data.event_id[0] ? "yes" : "no");
// Verify the parsed event ID matches the original
int event_id_match = (memcmp(result.data.event_id, event_id, 32) == 0);
printf("Event ID matches original: %s\n", event_id_match ? "yes" : "no");
return (result.type == NOSTR_URI_NOTE && event_id_match);
}
// Test 2: Parse nprofile URI
int test_parse_nprofile_uri(void) {
print_test_header("Parse nprofile: URI");
// First build a valid nprofile URI, then parse it
unsigned char pubkey[32];
nostr_hex_to_bytes("aa4fc8665f5696e33db7e1a572e3b0f5b3d615837b0f362dcb1c8068b098c7b4", pubkey, 32);
const char* relays[] = {"wss://relay.example.com"};
char built_uri[300];
int build_result = nostr_build_uri_nprofile(pubkey, relays, 1, built_uri, sizeof(built_uri));
if (build_result != NOSTR_SUCCESS) {
printf("Failed to build URI for testing: %d (%s)\n", build_result, nostr_strerror(build_result));
return 0;
}
printf("Input URI: %s\n", built_uri);
nostr_uri_result_t result;
int parse_result = nostr_parse_uri(built_uri, &result);
printf("Expected: NOSTR_SUCCESS (0)\n");
printf("Actual: %d (%s)\n", parse_result, nostr_strerror(parse_result));
if (parse_result != NOSTR_SUCCESS) {
return 0;
}
printf("Expected type: NOSTR_URI_NPROFILE\n");
printf("Actual type: %d\n", result.type);
if (result.type != NOSTR_URI_NPROFILE) {
return 0;
}
// Verify the parsed pubkey matches the original
int pubkey_match = (memcmp(result.data.nprofile.pubkey, pubkey, 32) == 0);
printf("Pubkey matches original: %s\n", pubkey_match ? "yes" : "no");
// Verify relay count
printf("Expected relay count: 1\n");
printf("Actual relay count: %d\n", result.data.nprofile.relay_count);
return (result.type == NOSTR_URI_NPROFILE && pubkey_match && result.data.nprofile.relay_count == 1);
}
// Test 3: Parse nevent URI
int test_parse_nevent_uri(void) {
print_test_header("Parse nevent: URI");
// First build a valid nevent URI, then parse it
unsigned char event_id[32];
nostr_hex_to_bytes("f1e582c90f071c0110cc5bcac2dcc6d8c32250e3cc26fcbe93470d918f2ffaf0", event_id, 32);
const char* relays[] = {"wss://relay.example.com"};
char built_uri[400];
int build_result = nostr_build_uri_nevent(event_id, relays, 1, NULL, 1, 1234567890, built_uri, sizeof(built_uri));
if (build_result != NOSTR_SUCCESS) {
printf("Failed to build URI for testing: %d (%s)\n", build_result, nostr_strerror(build_result));
return 0;
}
printf("Input URI: %s\n", built_uri);
nostr_uri_result_t result;
int parse_result = nostr_parse_uri(built_uri, &result);
printf("Expected: NOSTR_SUCCESS (0)\n");
printf("Actual: %d (%s)\n", parse_result, nostr_strerror(parse_result));
if (parse_result != NOSTR_SUCCESS) {
return 0;
}
printf("Expected type: NOSTR_URI_NEVENT\n");
printf("Actual type: %d\n", result.type);
if (result.type != NOSTR_URI_NEVENT) {
return 0;
}
// Verify the parsed event ID matches the original
int event_id_match = (memcmp(result.data.nevent.event_id, event_id, 32) == 0);
printf("Event ID matches original: %s\n", event_id_match ? "yes" : "no");
// Verify kind
printf("Expected kind: 1\n");
printf("Actual kind: %d\n", result.data.nevent.kind ? *result.data.nevent.kind : -1);
// Verify relay count
printf("Expected relay count: 1\n");
printf("Actual relay count: %d\n", result.data.nevent.relay_count);
return (result.type == NOSTR_URI_NEVENT && event_id_match && result.data.nevent.relay_count == 1);
}
// Test 4: Parse naddr URI
int test_parse_naddr_uri(void) {
print_test_header("Parse naddr: URI");
// First build a valid naddr URI, then parse it
const char* identifier = "draft";
unsigned char pubkey[32];
nostr_hex_to_bytes("aa4fc8665f5696e33db7e1a572e3b0f5b3d615837b0f362dcb1c8068b098c7b4", pubkey, 32);
const char* relays[] = {"wss://relay.example.com"};
char built_uri[400];
int build_result = nostr_build_uri_naddr(identifier, pubkey, 30023, relays, 1, built_uri, sizeof(built_uri));
if (build_result != NOSTR_SUCCESS) {
printf("Failed to build URI for testing: %d (%s)\n", build_result, nostr_strerror(build_result));
return 0;
}
printf("Input URI: %s\n", built_uri);
nostr_uri_result_t result;
int parse_result = nostr_parse_uri(built_uri, &result);
printf("Expected: NOSTR_SUCCESS (0)\n");
printf("Actual: %d (%s)\n", parse_result, nostr_strerror(parse_result));
if (parse_result != NOSTR_SUCCESS) {
return 0;
}
printf("Expected type: NOSTR_URI_NADDR\n");
printf("Actual type: %d\n", result.type);
if (result.type != NOSTR_URI_NADDR) {
return 0;
}
// Verify the parsed identifier matches the original
int identifier_match = (strcmp(result.data.naddr.identifier, identifier) == 0);
printf("Identifier matches original: %s\n", identifier_match ? "yes" : "no");
// Verify kind
printf("Expected kind: 30023\n");
printf("Actual kind: %d\n", result.data.naddr.kind);
// Verify relay count
printf("Expected relay count: 1\n");
printf("Actual relay count: %d\n", result.data.naddr.relay_count);
return (result.type == NOSTR_URI_NADDR && identifier_match && result.data.naddr.relay_count == 1);
}
// Test 5: Invalid URI (wrong prefix)
int test_invalid_uri_prefix(void) {
print_test_header("Invalid URI - Wrong Prefix");
const char* uri = "bitcoin:note1example";
printf("Input URI: %s\n", uri);
nostr_uri_result_t result;
int parse_result = nostr_parse_uri(uri, &result);
printf("Expected: NOSTR_ERROR_INVALID_INPUT (-1)\n");
printf("Actual: %d (%s)\n", parse_result, nostr_strerror(parse_result));
return (parse_result == NOSTR_ERROR_INVALID_INPUT);
}
// Test 6: Invalid URI (missing colon)
int test_invalid_uri_no_colon(void) {
print_test_header("Invalid URI - No Colon");
const char* uri = "nostrnote1example";
printf("Input URI: %s\n", uri);
nostr_uri_result_t result;
int parse_result = nostr_parse_uri(uri, &result);
printf("Expected: NOSTR_ERROR_INVALID_INPUT (-1)\n");
printf("Actual: %d (%s)\n", parse_result, nostr_strerror(parse_result));
return (parse_result == NOSTR_ERROR_INVALID_INPUT);
}
// Test 7: Build note URI
int test_build_note_uri(void) {
print_test_header("Build note: URI");
unsigned char event_id[32];
nostr_hex_to_bytes("f1e582c90f071c0110cc5bcac2dcc6d8c32250e3cc26fcbe93470d918f2ffaf0", event_id, 32);
printf("Input event ID: f1e582c90f071c0110cc5bcac2dcc6d8c32250e3cc26fcbe93470d918f2ffaf0\n");
char uri[200];
int result = nostr_build_uri_note(event_id, uri, sizeof(uri));
printf("Build result: %d (%s)\n", result, nostr_strerror(result));
if (result != NOSTR_SUCCESS) {
return 0;
}
printf("Built URI: %s\n", uri);
int success = (strncmp(uri, "nostr:note1", 11) == 0);
printf("Expected: URI starts with 'nostr:note1'\n");
printf("Actual: %s\n", success ? "yes" : "no");
return success;
}
// Test 8: Build nprofile URI
int test_build_nprofile_uri(void) {
print_test_header("Build nprofile: URI");
unsigned char pubkey[32];
nostr_hex_to_bytes("aa4fc8665f5696e33db7e1a572e3b0f5b3d615837b0f362dcb1c8068b098c7b4", pubkey, 32);
const char* relays[] = {"wss://relay.example.com", "wss://relay2.example.com"};
printf("Input pubkey: aa4fc8665f5696e33db7e1a572e3b0f5b3d615837b0f362dcb1c8068b098c7b4\n");
char uri[300];
int result = nostr_build_uri_nprofile(pubkey, relays, 2, uri, sizeof(uri));
printf("Build result: %d (%s)\n", result, nostr_strerror(result));
if (result != NOSTR_SUCCESS) {
return 0;
}
printf("Built URI: %s\n", uri);
int success = (strncmp(uri, "nostr:nprofile1", 14) == 0);
printf("Expected: URI starts with 'nostr:nprofile1'\n");
printf("Actual: %s\n", success ? "yes" : "no");
return success;
}
int main(void) {
printf("=== NIP-21 URI Scheme Test Suite ===\n");
printf("Following TESTS POLICY: Shows expected vs actual values\n");
// Initialize crypto library
if (nostr_init() != NOSTR_SUCCESS) {
printf("❌ Failed to initialize nostr library\n");
return 1;
}
int all_passed = 1;
int test_result;
// Valid URI parsing tests
test_result = test_parse_note_uri();
print_test_result(test_result, "Parse note: URI");
if (!test_result) all_passed = 0;
test_result = test_parse_nprofile_uri();
print_test_result(test_result, "Parse nprofile: URI");
if (!test_result) all_passed = 0;
test_result = test_parse_nevent_uri();
print_test_result(test_result, "Parse nevent: URI");
if (!test_result) all_passed = 0;
test_result = test_parse_naddr_uri();
print_test_result(test_result, "Parse naddr: URI");
if (!test_result) all_passed = 0;
// Invalid URI tests
test_result = test_invalid_uri_prefix();
print_test_result(test_result, "Invalid URI - Wrong Prefix");
if (!test_result) all_passed = 0;
test_result = test_invalid_uri_no_colon();
print_test_result(test_result, "Invalid URI - No Colon");
if (!test_result) all_passed = 0;
// URI building tests
test_result = test_build_note_uri();
print_test_result(test_result, "Build note: URI");
if (!test_result) all_passed = 0;
test_result = test_build_nprofile_uri();
print_test_result(test_result, "Build nprofile: URI");
if (!test_result) all_passed = 0;
// Summary
printf("\n=== TEST SUMMARY ===\n");
printf("Total tests: %d\n", test_count);
printf("Passed: %d\n", passed_tests);
printf("Failed: %d\n", test_count - passed_tests);
if (all_passed) {
printf("🎉 ALL TESTS PASSED! NIP-21 URI scheme implementation is working correctly.\n");
} else {
printf("❌ SOME TESTS FAILED. Please review the output above.\n");
}
nostr_cleanup();
return all_passed ? 0 : 1;
}

BIN
tests/nip42_test Executable file

Binary file not shown.

690
tests/nip42_test.c Normal file
View File

@@ -0,0 +1,690 @@
/*
* NIP-42 Authentication of Clients to Relays Test Suite
* Tests auth challenge generation, event creation, validation, and message parsing
* Following TESTS POLICY: Shows expected vs actual values, prints entire JSON events
*/
#define _GNU_SOURCE // For strdup on Linux
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "../nostr_core/nip042.h"
#include "../nostr_core/nip001.h"
#include "../nostr_core/nostr_common.h"
#include "../nostr_core/utils.h"
#include "../cjson/cJSON.h"
// Ensure strdup is declared
#ifndef strdup
extern char *strdup(const char *s);
#endif
// Test counter for tracking progress
static int test_count = 0;
static int passed_tests = 0;
void print_test_header(const char* test_name) {
test_count++;
printf("\n=== TEST %d: %s ===\n", test_count, test_name);
}
void print_test_result(int passed, const char* test_name) {
if (passed) {
passed_tests++;
printf("✅ PASS: %s\n", test_name);
} else {
printf("❌ FAIL: %s\n", test_name);
}
}
void print_json_comparison(const char* label, cJSON* expected, cJSON* actual) {
char* expected_str = cJSON_Print(expected);
char* actual_str;
if (actual) {
actual_str = cJSON_Print(actual);
} else {
actual_str = strdup("NULL");
}
printf("%s Expected JSON:\n%s\n", label, expected_str ? expected_str : "NULL");
printf("%s Actual JSON:\n%s\n", label, actual_str ? actual_str : "NULL");
if (expected_str) free(expected_str);
if (actual_str) free(actual_str);
}
// Test 1: Challenge generation
int test_challenge_generation(void) {
print_test_header("Challenge Generation");
nostr_auth_challenge_t challenge1, challenge2;
printf("Generating first challenge...\n");
int result1 = nostr_nip42_generate_challenge(challenge1.challenge, NOSTR_NIP42_DEFAULT_CHALLENGE_LENGTH);
printf("Result: %d (%s)\n", result1, nostr_strerror(result1));
if (result1 != NOSTR_SUCCESS) {
printf("❌ Failed to generate first challenge\n");
return 0;
}
printf("First challenge: %s\n", challenge1.challenge);
printf("Challenge length: %lu\n", strlen(challenge1.challenge));
printf("Expected length: %d\n", NOSTR_NIP42_DEFAULT_CHALLENGE_LENGTH * 2);
if (strlen(challenge1.challenge) != NOSTR_NIP42_DEFAULT_CHALLENGE_LENGTH * 2) {
printf("❌ Challenge length incorrect\n");
return 0;
}
printf("Generating second challenge...\n");
int result2 = nostr_nip42_generate_challenge(challenge2.challenge, NOSTR_NIP42_DEFAULT_CHALLENGE_LENGTH);
printf("Result: %d (%s)\n", result2, nostr_strerror(result2));
if (result2 != NOSTR_SUCCESS) {
printf("❌ Failed to generate second challenge\n");
return 0;
}
printf("Second challenge: %s\n", challenge2.challenge);
// Challenges should be different (extremely high probability)
if (strcmp(challenge1.challenge, challenge2.challenge) == 0) {
printf("❌ Two challenges are identical (highly unlikely)\n");
return 0;
}
printf("✅ Challenges are different (good entropy)\n");
return 1;
}
// Test 2: AUTH message creation (server-side challenge)
int test_auth_message_creation(void) {
print_test_header("AUTH Message Creation - Server Challenge");
nostr_auth_challenge_t challenge;
int gen_result = nostr_nip42_generate_challenge(challenge.challenge, NOSTR_NIP42_DEFAULT_CHALLENGE_LENGTH);
if (gen_result != NOSTR_SUCCESS) {
printf("❌ Failed to generate challenge for message test\n");
return 0;
}
printf("Generated challenge: %s\n", challenge.challenge);
// Create AUTH challenge message: ["AUTH", "challenge_string"]
cJSON* message_array = cJSON_CreateArray();
if (!message_array) {
printf("❌ Failed to create message array\n");
return 0;
}
cJSON_AddItemToArray(message_array, cJSON_CreateString("AUTH"));
cJSON_AddItemToArray(message_array, cJSON_CreateString(challenge.challenge));
char* auth_message = cJSON_PrintUnformatted(message_array);
cJSON_Delete(message_array);
if (!auth_message) {
printf("❌ Failed to create AUTH message\n");
return 0;
}
int result = NOSTR_SUCCESS;
printf("Message creation result: %d (%s)\n", result, nostr_strerror(result));
printf("AUTH message: %s\n", auth_message);
// Parse the message to verify format
cJSON* parsed = cJSON_Parse(auth_message);
if (!parsed) {
printf("❌ AUTH message is not valid JSON\n");
return 0;
}
// Check if it's an array with 2 elements
if (!cJSON_IsArray(parsed) || cJSON_GetArraySize(parsed) != 2) {
printf("❌ AUTH message is not a 2-element array\n");
cJSON_Delete(parsed);
return 0;
}
// Check first element is "AUTH"
cJSON* first = cJSON_GetArrayItem(parsed, 0);
if (!cJSON_IsString(first) || strcmp(cJSON_GetStringValue(first), "AUTH") != 0) {
printf("❌ First element is not 'AUTH'\n");
cJSON_Delete(parsed);
return 0;
}
// Check second element is our challenge string
cJSON* second = cJSON_GetArrayItem(parsed, 1);
if (!cJSON_IsString(second) || strcmp(cJSON_GetStringValue(second), challenge.challenge) != 0) {
printf("❌ Second element is not our challenge string\n");
printf("Expected: %s\n", challenge.challenge);
printf("Actual: %s\n", cJSON_GetStringValue(second));
cJSON_Delete(parsed);
free(auth_message);
return 0;
}
printf("✅ AUTH challenge message format is correct\n");
cJSON_Delete(parsed);
free(auth_message);
return 1;
}
// Test 3: Authentication event creation (client-side)
int test_auth_event_creation(void) {
print_test_header("Authentication Event Creation - Client Side");
const char* private_key_hex = "91ba716fa9e7ea2fcbad360cf4f8e0d312f73984da63d90f524ad61a6a1e7dbe";
const char* relay_url = "wss://relay.example.com";
const char* challenge_string = "test_challenge_12345678901234567890123456789012";
unsigned char private_key[32];
nostr_hex_to_bytes(private_key_hex, private_key, 32);
printf("Private key (hex): %s\n", private_key_hex);
printf("Relay URL: %s\n", relay_url);
printf("Challenge: %s\n", challenge_string);
cJSON* auth_event = nostr_nip42_create_auth_event(challenge_string, relay_url, private_key, 0);
if (!auth_event) {
printf("❌ Failed to create authentication event\n");
return 0;
}
char* event_str = cJSON_Print(auth_event);
printf("Created Auth Event JSON:\n%s\n", event_str);
free(event_str);
// Validate the event structure
int structure_result = nostr_validate_event_structure(auth_event);
printf("Structure validation result: %d (%s)\n", structure_result, nostr_strerror(structure_result));
if (structure_result != NOSTR_SUCCESS) {
printf("❌ Auth event failed structure validation\n");
cJSON_Delete(auth_event);
return 0;
}
// Validate the event signature
int crypto_result = nostr_verify_event_signature(auth_event);
printf("Signature validation result: %d (%s)\n", crypto_result, nostr_strerror(crypto_result));
if (crypto_result != NOSTR_SUCCESS) {
printf("❌ Auth event failed signature validation\n");
cJSON_Delete(auth_event);
return 0;
}
// Check kind is 22242
cJSON* kind = cJSON_GetObjectItem(auth_event, "kind");
if (!cJSON_IsNumber(kind) || cJSON_GetNumberValue(kind) != 22242) {
printf("❌ Auth event kind is not 22242\n");
cJSON_Delete(auth_event);
return 0;
}
// Check for relay tag
cJSON* tags = cJSON_GetObjectItem(auth_event, "tags");
int found_relay = 0, found_challenge = 0;
if (cJSON_IsArray(tags)) {
cJSON* tag = NULL;
cJSON_ArrayForEach(tag, tags) {
if (cJSON_IsArray(tag) && cJSON_GetArraySize(tag) >= 2) {
cJSON* tag_name = cJSON_GetArrayItem(tag, 0);
cJSON* tag_value = cJSON_GetArrayItem(tag, 1);
if (cJSON_IsString(tag_name) && cJSON_IsString(tag_value)) {
if (strcmp(cJSON_GetStringValue(tag_name), "relay") == 0) {
found_relay = 1;
if (strcmp(cJSON_GetStringValue(tag_value), relay_url) != 0) {
printf("❌ Relay tag value incorrect\n");
printf("Expected: %s\n", relay_url);
printf("Actual: %s\n", cJSON_GetStringValue(tag_value));
cJSON_Delete(auth_event);
return 0;
}
} else if (strcmp(cJSON_GetStringValue(tag_name), "challenge") == 0) {
found_challenge = 1;
if (strcmp(cJSON_GetStringValue(tag_value), challenge_string) != 0) {
printf("❌ Challenge tag value incorrect\n");
printf("Expected: %s\n", challenge_string);
printf("Actual: %s\n", cJSON_GetStringValue(tag_value));
cJSON_Delete(auth_event);
return 0;
}
}
}
}
}
}
if (!found_relay) {
printf("❌ Missing relay tag\n");
cJSON_Delete(auth_event);
return 0;
}
if (!found_challenge) {
printf("❌ Missing challenge tag\n");
cJSON_Delete(auth_event);
return 0;
}
printf("✅ Authentication event created successfully with correct tags\n");
cJSON_Delete(auth_event);
return 1;
}
// Test 4: Authentication event validation (server-side)
int test_auth_event_validation(void) {
print_test_header("Authentication Event Validation - Server Side");
// Create a valid auth event first
const char* private_key_hex = "91ba716fa9e7ea2fcbad360cf4f8e0d312f73984da63d90f524ad61a6a1e7dbe";
const char* relay_url = "wss://relay.example.com";
const char* challenge_string = "validation_challenge_1234567890123456789012";
unsigned char private_key[32];
nostr_hex_to_bytes(private_key_hex, private_key, 32);
cJSON* auth_event = nostr_nip42_create_auth_event(challenge_string, relay_url, private_key, 0);
if (!auth_event) {
printf("❌ Failed to create auth event for validation test\n");
return 0;
}
char* event_str = cJSON_Print(auth_event);
printf("Auth Event to validate:\n%s\n", event_str);
free(event_str);
// Test successful validation
printf("Testing successful validation...\n");
int result = nostr_nip42_verify_auth_event(auth_event, challenge_string, relay_url, 0);
printf("Validation result: %d (%s)\n", result, nostr_strerror(result));
if (result != NOSTR_SUCCESS) {
printf("❌ Valid auth event failed validation\n");
cJSON_Delete(auth_event);
return 0;
}
printf("✅ Valid auth event passed validation\n");
// Test wrong relay URL
printf("\nTesting wrong relay URL...\n");
result = nostr_nip42_verify_auth_event(auth_event, challenge_string, "wss://wrong.relay.com", 0);
printf("Expected: NOSTR_ERROR_NIP42_URL_MISMATCH (-206)\n");
printf("Actual: %d (%s)\n", result, nostr_strerror(result));
if (result != NOSTR_ERROR_NIP42_URL_MISMATCH) {
printf("❌ Wrong relay validation didn't fail correctly\n");
cJSON_Delete(auth_event);
return 0;
}
printf("✅ Wrong relay URL correctly rejected\n");
// Test wrong challenge
printf("\nTesting wrong challenge...\n");
result = nostr_nip42_verify_auth_event(auth_event, "wrong_challenge_string_here", relay_url, 0);
printf("Expected: NOSTR_ERROR_NIP42_INVALID_CHALLENGE (-203)\n");
printf("Actual: %d (%s)\n", result, nostr_strerror(result));
if (result != NOSTR_ERROR_NIP42_INVALID_CHALLENGE) {
printf("❌ Wrong challenge validation didn't fail correctly\n");
cJSON_Delete(auth_event);
return 0;
}
printf("✅ Wrong challenge correctly rejected\n");
cJSON_Delete(auth_event);
return 1;
}
// Test 5: AUTH message parsing (client-side)
int test_auth_message_parsing(void) {
print_test_header("AUTH Message Parsing - Client Side");
// Test parsing challenge message
const char* challenge_msg = "[\"AUTH\", \"test_challenge_from_server_123456789012\"]";
printf("Parsing AUTH challenge message: %s\n", challenge_msg);
char extracted_challenge[NOSTR_NIP42_MAX_CHALLENGE_LENGTH];
int result = nostr_nip42_parse_auth_challenge(challenge_msg, extracted_challenge, sizeof(extracted_challenge));
printf("Parse result: %d (%s)\n", result, nostr_strerror(result));
printf("Expected challenge: test_challenge_from_server_123456789012\n");
printf("Extracted challenge: %s\n", extracted_challenge);
if (result != NOSTR_SUCCESS) {
printf("❌ Failed to parse valid AUTH message\n");
return 0;
}
if (strcmp(extracted_challenge, "test_challenge_from_server_123456789012") != 0) {
printf("❌ Extracted challenge doesn't match expected\n");
return 0;
}
printf("✅ AUTH challenge message parsed correctly\n");
// Test invalid message format
printf("\nTesting invalid message format...\n");
const char* invalid_msg = "[\"WRONG\", \"challenge\"]";
result = nostr_nip42_parse_auth_challenge(invalid_msg, extracted_challenge, sizeof(extracted_challenge));
printf("Parse result: %d (%s)\n", result, nostr_strerror(result));
printf("Expected: NOSTR_ERROR_NIP42_INVALID_MESSAGE_FORMAT (-205)\n");
if (result != NOSTR_ERROR_NIP42_INVALID_MESSAGE_FORMAT) {
printf("❌ Invalid message format should have failed\n");
return 0;
}
printf("✅ Invalid message format correctly rejected\n");
return 1;
}
// Test 6: AUTH response message creation (client-side)
int test_auth_response_creation(void) {
print_test_header("AUTH Response Message Creation - Client Side");
// Create an auth event first
const char* private_key_hex = "91ba716fa9e7ea2fcbad360cf4f8e0d312f73984da63d90f524ad61a6a1e7dbe";
const char* relay_url = "wss://relay.example.com";
const char* challenge_string = "response_test_challenge_1234567890123456";
unsigned char private_key[32];
nostr_hex_to_bytes(private_key_hex, private_key, 32);
cJSON* auth_event = nostr_nip42_create_auth_event(challenge_string, relay_url, private_key, 0);
if (!auth_event) {
printf("❌ Failed to create auth event for response test\n");
return 0;
}
char* auth_response = nostr_nip42_create_auth_message(auth_event);
int result = auth_response ? NOSTR_SUCCESS : NOSTR_ERROR_MEMORY_FAILED;
printf("Response creation result: %d (%s)\n", result, nostr_strerror(result));
printf("AUTH response message: %s\n", auth_response ? auth_response : "NULL");
if (result != NOSTR_SUCCESS) {
printf("❌ Failed to create AUTH response message\n");
cJSON_Delete(auth_event);
return 0;
}
// Parse and validate the response format
cJSON* parsed = cJSON_Parse(auth_response);
if (!parsed) {
printf("❌ AUTH response is not valid JSON\n");
cJSON_Delete(auth_event);
return 0;
}
// Should be ["AUTH", <event-json>]
if (!cJSON_IsArray(parsed) || cJSON_GetArraySize(parsed) != 2) {
printf("❌ AUTH response is not a 2-element array\n");
cJSON_Delete(parsed);
cJSON_Delete(auth_event);
return 0;
}
cJSON* first = cJSON_GetArrayItem(parsed, 0);
if (!cJSON_IsString(first) || strcmp(cJSON_GetStringValue(first), "AUTH") != 0) {
printf("❌ First element is not 'AUTH'\n");
cJSON_Delete(parsed);
cJSON_Delete(auth_event);
return 0;
}
cJSON* second = cJSON_GetArrayItem(parsed, 1);
if (!cJSON_IsObject(second)) {
printf("❌ Second element is not an object (event)\n");
cJSON_Delete(parsed);
cJSON_Delete(auth_event);
return 0;
}
// Check if the event in the response matches our created event
cJSON* response_kind = cJSON_GetObjectItem(second, "kind");
if (!cJSON_IsNumber(response_kind) || cJSON_GetNumberValue(response_kind) != 22242) {
printf("❌ Response event kind is not 22242\n");
cJSON_Delete(parsed);
cJSON_Delete(auth_event);
return 0;
}
printf("✅ AUTH response message format is correct\n");
cJSON_Delete(parsed);
cJSON_Delete(auth_event);
free(auth_response);
return 1;
}
// Test 7: Error conditions and edge cases
int test_error_conditions(void) {
print_test_header("Error Conditions and Edge Cases");
int all_passed = 1;
// Test 1: NULL parameters
printf("\nSubtest 1: NULL parameters\n");
int result = nostr_nip42_generate_challenge(NULL, 32);
printf("Expected: NOSTR_ERROR_INVALID_INPUT (-1)\n");
printf("Actual: %d (%s)\n", result, nostr_strerror(result));
if (result != NOSTR_ERROR_INVALID_INPUT) all_passed = 0;
// Test 2: Invalid challenge length
printf("\nSubtest 2: Invalid challenge in validation\n");
const char* private_key_hex = "91ba716fa9e7ea2fcbad360cf4f8e0d312f73984da63d90f524ad61a6a1e7dbe";
const char* relay_url = "wss://relay.example.com";
const char* valid_challenge = "valid_challenge_1234567890123456789012345";
unsigned char private_key[32];
nostr_hex_to_bytes(private_key_hex, private_key, 32);
cJSON* auth_event = nostr_nip42_create_auth_event(valid_challenge, relay_url, private_key, 0);
if (auth_event) {
result = nostr_nip42_verify_auth_event(auth_event, "short", relay_url, 0); // Too short
printf("Expected: NOSTR_ERROR_NIP42_INVALID_CHALLENGE (-203)\n");
printf("Actual: %d (%s)\n", result, nostr_strerror(result));
if (result != NOSTR_ERROR_NIP42_INVALID_CHALLENGE) all_passed = 0;
cJSON_Delete(auth_event);
} else {
printf("❌ Failed to create auth event for validation test\n");
all_passed = 0;
}
// Test 3: Invalid JSON parsing
printf("\nSubtest 3: Invalid JSON in message parsing\n");
char challenge_buffer[NOSTR_NIP42_MAX_CHALLENGE_LENGTH];
result = nostr_nip42_parse_auth_challenge("invalid json", challenge_buffer, sizeof(challenge_buffer));
printf("Expected: NOSTR_ERROR_NIP42_INVALID_MESSAGE_FORMAT (-205)\n");
printf("Actual: %d (%s)\n", result, nostr_strerror(result));
if (result != NOSTR_ERROR_NIP42_INVALID_MESSAGE_FORMAT) all_passed = 0;
// Test 4: Wrong array size
printf("\nSubtest 4: Wrong array size in message parsing\n");
result = nostr_nip42_parse_auth_challenge("[\"AUTH\"]", challenge_buffer, sizeof(challenge_buffer)); // Only 1 element
printf("Expected: NOSTR_ERROR_NIP42_INVALID_MESSAGE_FORMAT (-205)\n");
printf("Actual: %d (%s)\n", result, nostr_strerror(result));
if (result != NOSTR_ERROR_NIP42_INVALID_MESSAGE_FORMAT) all_passed = 0;
return all_passed;
}
// Test 8: Full authentication flow simulation
int test_full_auth_flow(void) {
print_test_header("Full Authentication Flow Simulation");
const char* private_key_hex = "91ba716fa9e7ea2fcbad360cf4f8e0d312f73984da63d90f524ad61a6a1e7dbe";
const char* relay_url = "wss://test-relay.nostr.com";
unsigned char private_key[32];
nostr_hex_to_bytes(private_key_hex, private_key, 32);
printf("=== STEP 1: Server generates challenge ===\n");
nostr_auth_challenge_t challenge;
int result = nostr_nip42_generate_challenge(challenge.challenge, NOSTR_NIP42_DEFAULT_CHALLENGE_LENGTH);
if (result != NOSTR_SUCCESS) {
printf("❌ Failed to generate challenge\n");
return 0;
}
printf("Generated challenge: %s\n", challenge.challenge);
printf("\n=== STEP 2: Server sends AUTH message ===\n");
cJSON* message_array = cJSON_CreateArray();
cJSON_AddItemToArray(message_array, cJSON_CreateString("AUTH"));
cJSON_AddItemToArray(message_array, cJSON_CreateString(challenge.challenge));
char* auth_message = cJSON_PrintUnformatted(message_array);
cJSON_Delete(message_array);
if (!auth_message) {
printf("❌ Failed to create AUTH message\n");
return 0;
}
printf("Server sends: %s\n", auth_message);
printf("\n=== STEP 3: Client parses AUTH message ===\n");
char parsed_challenge[NOSTR_NIP42_MAX_CHALLENGE_LENGTH];
result = nostr_nip42_parse_auth_challenge(auth_message, parsed_challenge, sizeof(parsed_challenge));
if (result != NOSTR_SUCCESS) {
printf("❌ Failed to parse AUTH message\n");
free(auth_message);
return 0;
}
printf("Client extracted challenge: %s\n", parsed_challenge);
if (strcmp(challenge.challenge, parsed_challenge) != 0) {
printf("❌ Parsed challenge doesn't match original\n");
free(auth_message);
return 0;
}
printf("\n=== STEP 4: Client creates auth event ===\n");
cJSON* auth_event = nostr_nip42_create_auth_event(parsed_challenge, relay_url, private_key, 0);
if (!auth_event) {
printf("❌ Failed to create auth event\n");
return 0;
}
char* event_str = cJSON_Print(auth_event);
printf("Client created auth event:\n%s\n", event_str);
free(event_str);
printf("\n=== STEP 5: Client sends AUTH response ===\n");
char* auth_response = nostr_nip42_create_auth_message(auth_event);
if (!auth_response) {
printf("❌ Failed to create AUTH response\n");
cJSON_Delete(auth_event);
free(auth_message);
return 0;
}
printf("Client sends: %s\n", auth_response);
printf("\n=== STEP 6: Server validates auth event ===\n");
result = nostr_nip42_verify_auth_event(auth_event, challenge.challenge, relay_url, 0);
printf("Server validation result: %d (%s)\n", result, nostr_strerror(result));
if (result != NOSTR_SUCCESS) {
printf("❌ Server validation failed\n");
cJSON_Delete(auth_event);
free(auth_message);
free(auth_response);
return 0;
}
printf("✅ FULL AUTHENTICATION FLOW COMPLETED SUCCESSFULLY!\n");
printf("✅ Challenge generated -> Message sent -> Challenge parsed -> Event created -> Response sent -> Event validated\n");
cJSON_Delete(auth_event);
free(auth_message);
free(auth_response);
return 1;
}
int main(void) {
printf("=== NIP-42 Authentication of Clients to Relays Test Suite ===\n");
printf("Following TESTS POLICY: Shows expected vs actual values, prints entire JSON events\n");
printf("Tests both client-side and server-side authentication functionality\n");
// Initialize crypto library
if (nostr_init() != NOSTR_SUCCESS) {
printf("❌ Failed to initialize nostr library\n");
return 1;
}
int all_passed = 1;
int test_result;
// Test 1: Challenge generation
test_result = test_challenge_generation();
print_test_result(test_result, "Challenge Generation");
if (!test_result) all_passed = 0;
// Test 2: AUTH message creation
test_result = test_auth_message_creation();
print_test_result(test_result, "AUTH Message Creation - Server Challenge");
if (!test_result) all_passed = 0;
// Test 3: Auth event creation
test_result = test_auth_event_creation();
print_test_result(test_result, "Authentication Event Creation - Client Side");
if (!test_result) all_passed = 0;
// Test 4: Auth event validation
test_result = test_auth_event_validation();
print_test_result(test_result, "Authentication Event Validation - Server Side");
if (!test_result) all_passed = 0;
// Test 5: AUTH message parsing
test_result = test_auth_message_parsing();
print_test_result(test_result, "AUTH Message Parsing - Client Side");
if (!test_result) all_passed = 0;
// Test 6: AUTH response creation
test_result = test_auth_response_creation();
print_test_result(test_result, "AUTH Response Message Creation - Client Side");
if (!test_result) all_passed = 0;
// Test 7: Error conditions
test_result = test_error_conditions();
print_test_result(test_result, "Error Conditions and Edge Cases");
if (!test_result) all_passed = 0;
// Test 8: Full authentication flow
test_result = test_full_auth_flow();
print_test_result(test_result, "Full Authentication Flow Simulation");
if (!test_result) all_passed = 0;
// Summary
printf("\n=== TEST SUMMARY ===\n");
printf("Total tests: %d\n", test_count);
printf("Passed: %d\n", passed_tests);
printf("Failed: %d\n", test_count - passed_tests);
if (all_passed) {
printf("🎉 ALL TESTS PASSED! NIP-42 Authentication implementation is working correctly.\n");
printf("✅ Challenge generation works\n");
printf("✅ AUTH message creation/parsing works\n");
printf("✅ Authentication event creation works\n");
printf("✅ Authentication event validation works\n");
printf("✅ Full authentication flow works\n");
printf("✅ Error handling works\n");
} else {
printf("❌ SOME TESTS FAILED. Please review the output above.\n");
}
nostr_cleanup();
return all_passed ? 0 : 1;
}

BIN
tests/nip44_test Executable file

Binary file not shown.

466
tests/nip44_test.c Normal file
View File

@@ -0,0 +1,466 @@
/*
* NIP-44 Encryption/Decryption Test
*
* Test suite for NIP-44 versioned encryption functionality
* Uses known test vectors and cross-implementation compatibility tests
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "../nostr_core/nostr_core.h"
// Test vectors for NIP-44 with proper key pairs
typedef struct {
const char* name;
const char* sender_private_key_hex;
const char* recipient_private_key_hex; // FIX: Need proper private key, not public key
const char* plaintext;
const char* expected_encrypted; // Optional - for known test vectors
} nip44_test_vector_t;
// Additional test vectors for edge cases (converted to round-trip tests with new 32-bit padding)
// Round-trip test vectors with proper key pairs
static nip44_test_vector_t test_vectors[] = {
{
"Basic short message",
"91ba716fa9e7ea2fcbad360cf4f8e0d312f73984da63d90f524ad61a6a1e7dbe", // Working keys from simple_nip44_test
"96f6fa197aa07477ab88f6981118466ae3a982faab8ad5db9d5426870c73d220",
"Hello, NIP-44!",
NULL
},
{
"Unicode message",
"1111111111111111111111111111111111111111111111111111111111111111",
"2222222222222222222222222222222222222222222222222222222222222222",
"Hello 🌍 World! 🚀",
NULL
},
{
"Empty message",
"3333333333333333333333333333333333333333333333333333333333333333",
"4444444444444444444444444444444444444444444444444444444444444444",
"",
NULL
},
{
"64KB payload test",
"91ba716fa9e7ea2fcbad360cf4f8e0d312f73984da63d90f524ad61a6a1e7dbe", // Same keys as basic test
"96f6fa197aa07477ab88f6981118466ae3a982faab8ad5db9d5426870c73d220",
NULL, // Will be generated dynamically
NULL
}
};
static int hex_to_bytes(const char* hex, unsigned char* bytes, size_t len) {
if (strlen(hex) != len * 2) return -1;
for (size_t i = 0; i < len; i++) {
if (sscanf(hex + i * 2, "%2hhx", &bytes[i]) != 1) {
return -1;
}
}
return 0;
}
static int test_nip44_round_trip(const nip44_test_vector_t* tv) {
printf("Test: %s\n", tv->name);
// Parse keys - both private keys
unsigned char sender_private_key[32];
unsigned char recipient_private_key[32];
if (hex_to_bytes(tv->sender_private_key_hex, sender_private_key, 32) != 0) {
printf(" FAIL: Failed to parse sender private key\n");
return -1;
}
if (hex_to_bytes(tv->recipient_private_key_hex, recipient_private_key, 32) != 0) {
printf(" FAIL: Failed to parse recipient private key\n");
return -1;
}
// Generate the public keys from the private keys
unsigned char sender_public_key[32];
unsigned char recipient_public_key[32];
if (nostr_ec_public_key_from_private_key(sender_private_key, sender_public_key) != 0) {
printf(" FAIL: Failed to derive sender public key\n");
return -1;
}
if (nostr_ec_public_key_from_private_key(recipient_private_key, recipient_public_key) != 0) {
printf(" FAIL: Failed to derive recipient public key\n");
return -1;
}
// Special handling for large payload tests
char* test_plaintext;
if (strcmp(tv->name, "64KB payload test") == 0) {
// Generate exactly 64KB (65,535 bytes) of predictable content - max NIP-44 size
const size_t payload_size = 65535;
test_plaintext = malloc(payload_size + 1);
if (!test_plaintext) {
printf(" FAIL: Memory allocation failed for 64KB test payload\n");
return -1;
}
// Fill with a predictable pattern: "ABCDEFGH01234567" repeated
const char* pattern = "ABCDEFGH01234567"; // 16 bytes
const size_t pattern_len = 16;
for (size_t i = 0; i < payload_size; i += pattern_len) {
size_t copy_len = (i + pattern_len <= payload_size) ? pattern_len : payload_size - i;
memcpy(test_plaintext + i, pattern, copy_len);
}
test_plaintext[payload_size] = '\0';
printf(" Generated 64KB test payload (%zu bytes)\n", payload_size);
printf(" Pattern: \"%s\" repeated\n", pattern);
printf(" First 64 chars: \"%.64s...\"\n", test_plaintext);
printf(" Last 64 chars: \"...%.64s\"\n", test_plaintext + payload_size - 64);
} else {
test_plaintext = (char*)tv->plaintext;
}
// Debug: Check plaintext length
size_t plaintext_len = strlen(test_plaintext);
printf(" Plaintext length: %zu bytes\n", plaintext_len);
printf(" Output buffer size: %zu bytes\n", (size_t)10485760);
// Test encryption - use larger buffer for 1MB+ payloads (10MB for NIP-44 overhead)
char* encrypted = malloc(10485760); // 10MB buffer for large payloads
if (!encrypted) {
printf(" FAIL: Memory allocation failed for encrypted buffer\n");
if (strcmp(tv->name, "0.5MB payload test") == 0) free(test_plaintext);
return -1;
}
// For large payloads, use _with_nonce to avoid random generation issues
unsigned char fixed_nonce[32] = {0};
int encrypt_result = nostr_nip44_encrypt_with_nonce(
sender_private_key,
recipient_public_key,
test_plaintext,
fixed_nonce,
encrypted,
10485760
);
if (encrypt_result != NOSTR_SUCCESS) {
printf(" FAIL: Encryption - Expected: %d, Actual: %d\n", NOSTR_SUCCESS, encrypt_result);
if (strcmp(tv->name, "1MB payload test") == 0) free(test_plaintext);
free(encrypted);
return -1;
}
// Test decryption - use recipient private key + sender public key
char* decrypted = malloc(65536 + 1); // 64KB + 1 for null terminator
if (!decrypted) {
printf(" FAIL: Memory allocation failed for decrypted buffer\n");
if (strcmp(tv->name, "64KB payload test") == 0) free(test_plaintext);
free(encrypted);
return -1;
}
int decrypt_result = nostr_nip44_decrypt(
recipient_private_key,
sender_public_key,
encrypted,
decrypted,
65536 + 1
);
if (decrypt_result != NOSTR_SUCCESS) {
printf(" FAIL: Decryption - Expected: %d, Actual: %d\n", NOSTR_SUCCESS, decrypt_result);
if (strcmp(tv->name, "1MB payload test") == 0) free(test_plaintext);
free(encrypted);
free(decrypted);
return -1;
}
// Verify round-trip
if (strcmp(test_plaintext, decrypted) != 0) {
printf(" FAIL: Round-trip mismatch\n");
printf(" Expected: \"%s\"\n", test_plaintext);
printf(" Actual: \"%s\"\n", decrypted);
if (strcmp(tv->name, "1MB payload test") == 0) free(test_plaintext);
free(encrypted);
free(decrypted);
return -1;
}
if (strcmp(tv->name, "64KB payload test") == 0) {
printf(" ✅ 64KB payload round-trip: PASS\n");
printf(" ✅ Content verification: All %zu bytes match perfectly!\n", strlen(test_plaintext));
printf(" Encrypted length: %zu bytes\n", strlen(encrypted));
printf(" 🎉 64KB NIP-44 STRESS TEST COMPLETED SUCCESSFULLY! 🎉\n");
} else {
printf(" PASS: Expected: \"%s\", Actual: \"%s\"\n", test_plaintext, decrypted);
printf(" Encrypted output: %s\n", encrypted);
}
if (strcmp(tv->name, "64KB payload test") == 0) free(test_plaintext);
free(encrypted);
free(decrypted);
return 0;
}
static int test_nip44_error_conditions() {
printf("Test: NIP-44 error conditions\n");
// Use proper valid secp256k1 private keys
unsigned char valid_sender_key[32];
unsigned char valid_recipient_key[32];
unsigned char valid_recipient_pubkey[32];
hex_to_bytes("0000000000000000000000000000000000000000000000000000000000000001", valid_sender_key, 32);
hex_to_bytes("0000000000000000000000000000000000000000000000000000000000000002", valid_recipient_key, 32);
// Generate the recipient's public key
if (nostr_ec_public_key_from_private_key(valid_recipient_key, valid_recipient_pubkey) != 0) {
printf(" FAIL: Failed to generate recipient public key\n");
return -1;
}
char output[1024];
// Test NULL parameters
int result = nostr_nip44_encrypt(NULL, valid_recipient_pubkey, "test", output, sizeof(output));
if (result != NOSTR_ERROR_INVALID_INPUT) {
printf(" FAIL: NULL sender key - Expected: %d, Actual: %d\n", NOSTR_ERROR_INVALID_INPUT, result);
return -1;
}
result = nostr_nip44_encrypt(valid_sender_key, NULL, "test", output, sizeof(output));
if (result != NOSTR_ERROR_INVALID_INPUT) {
printf(" FAIL: NULL recipient key - Expected: %d, Actual: %d\n", NOSTR_ERROR_INVALID_INPUT, result);
return -1;
}
result = nostr_nip44_encrypt(valid_sender_key, valid_recipient_pubkey, NULL, output, sizeof(output));
if (result != NOSTR_ERROR_INVALID_INPUT) {
printf(" FAIL: NULL plaintext - Expected: %d, Actual: %d\n", NOSTR_ERROR_INVALID_INPUT, result);
return -1;
}
result = nostr_nip44_encrypt(valid_sender_key, valid_recipient_pubkey, "test", NULL, sizeof(output));
if (result != NOSTR_ERROR_INVALID_INPUT) {
printf(" FAIL: NULL output buffer - Expected: %d, Actual: %d\n", NOSTR_ERROR_INVALID_INPUT, result);
return -1;
}
// Test buffer too small
char small_buffer[10];
result = nostr_nip44_encrypt(valid_sender_key, valid_recipient_pubkey, "test message", small_buffer, sizeof(small_buffer));
if (result != NOSTR_ERROR_NIP44_BUFFER_TOO_SMALL) {
printf(" FAIL: Buffer too small - Expected: %d, Actual: %d\n", NOSTR_ERROR_NIP44_BUFFER_TOO_SMALL, result);
return -1;
}
printf(" PASS: All error conditions handled correctly\n");
return 0;
}
static int test_nip44_encryption_variability() {
printf("Test: NIP-44 encryption variability (non-deterministic)\n");
const char* test_message = "Test message for variability";
unsigned char sender_key[32], recipient_key[32];
// Use fixed test keys
hex_to_bytes("1111111111111111111111111111111111111111111111111111111111111111", sender_key, 32);
hex_to_bytes("2222222222222222222222222222222222222222222222222222222222222222", recipient_key, 32);
// Generate recipient public key
unsigned char recipient_pubkey[32];
if (nostr_ec_public_key_from_private_key(recipient_key, recipient_pubkey) != 0) {
printf(" FAIL: Failed to generate recipient public key\n");
return -1;
}
// Encrypt the same message multiple times
char* encrypted1 = malloc(2097152); // 2MB buffer
char* encrypted2 = malloc(2097152);
char* encrypted3 = malloc(2097152);
if (!encrypted1 || !encrypted2 || !encrypted3) {
printf(" FAIL: Memory allocation failed for encrypted buffers\n");
free(encrypted1);
free(encrypted2);
free(encrypted3);
return -1;
}
int result1 = nostr_nip44_encrypt(sender_key, recipient_pubkey, test_message, encrypted1, 2097152);
int result2 = nostr_nip44_encrypt(sender_key, recipient_pubkey, test_message, encrypted2, 2097152);
int result3 = nostr_nip44_encrypt(sender_key, recipient_pubkey, test_message, encrypted3, 2097152);
if (result1 != NOSTR_SUCCESS || result2 != NOSTR_SUCCESS || result3 != NOSTR_SUCCESS) {
printf(" FAIL: Encryption failed - Results: %d, %d, %d\n", result1, result2, result3);
return -1;
}
// Verify all ciphertexts are different (non-deterministic)
if (strcmp(encrypted1, encrypted2) == 0 || strcmp(encrypted1, encrypted3) == 0 || strcmp(encrypted2, encrypted3) == 0) {
printf(" FAIL: NIP-44 encryption should produce different ciphertext each time\n");
printf(" Encryption 1: %.50s...\n", encrypted1);
printf(" Encryption 2: %.50s...\n", encrypted2);
printf(" Encryption 3: %.50s...\n", encrypted3);
free(encrypted1);
free(encrypted2);
free(encrypted3);
return -1;
}
// Verify all decrypt to the same plaintext
unsigned char sender_pubkey[32];
if (nostr_ec_public_key_from_private_key(sender_key, sender_pubkey) != 0) {
printf(" FAIL: Failed to generate sender public key\n");
return -1;
}
char* decrypted1 = malloc(1048576 + 1);
char* decrypted2 = malloc(1048576 + 1);
char* decrypted3 = malloc(1048576 + 1);
if (!decrypted1 || !decrypted2 || !decrypted3) {
printf(" FAIL: Memory allocation failed for decrypted buffers\n");
free(encrypted1);
free(encrypted2);
free(encrypted3);
free(decrypted1);
free(decrypted2);
free(decrypted3);
return -1;
}
int decrypt1 = nostr_nip44_decrypt(recipient_key, sender_pubkey, encrypted1, decrypted1, 1048576 + 1);
int decrypt2 = nostr_nip44_decrypt(recipient_key, sender_pubkey, encrypted2, decrypted2, 1048576 + 1);
int decrypt3 = nostr_nip44_decrypt(recipient_key, sender_pubkey, encrypted3, decrypted3, 1048576 + 1);
if (decrypt1 != NOSTR_SUCCESS || decrypt2 != NOSTR_SUCCESS || decrypt3 != NOSTR_SUCCESS) {
printf(" FAIL: Decryption failed - Results: %d, %d, %d\n", decrypt1, decrypt2, decrypt3);
return -1;
}
if (strcmp(decrypted1, test_message) != 0 || strcmp(decrypted2, test_message) != 0 || strcmp(decrypted3, test_message) != 0) {
printf(" FAIL: Decryption mismatch\n");
printf(" Expected: \"%s\"\n", test_message);
printf(" Decrypted1: \"%s\"\n", decrypted1);
printf(" Decrypted2: \"%s\"\n", decrypted2);
printf(" Decrypted3: \"%s\"\n", decrypted3);
free(encrypted1);
free(encrypted2);
free(encrypted3);
free(decrypted1);
free(decrypted2);
free(decrypted3);
return -1;
}
printf(" PASS: All encryptions different, all decrypt to: \"%s\"\n", test_message);
printf(" Sample ciphertext lengths: %zu, %zu, %zu bytes\n", strlen(encrypted1), strlen(encrypted2), strlen(encrypted3));
free(encrypted1);
free(encrypted2);
free(encrypted3);
free(decrypted1);
free(decrypted2);
free(decrypted3);
return 0;
}
int main() {
printf("NIP-44 Encryption Test Suite\n");
printf("=============================\n");
// Initialize the library
if (nostr_init() != NOSTR_SUCCESS) {
printf("FAIL: Failed to initialize NOSTR library\n");
return 1;
}
int total_tests = 0;
int passed_tests = 0;
// Test all vectors
size_t num_vectors = sizeof(test_vectors) / sizeof(test_vectors[0]);
for (size_t i = 0; i < num_vectors; i++) {
total_tests++;
printf("Test #%d\n", total_tests);
if (test_nip44_round_trip(&test_vectors[i]) == 0) {
passed_tests++;
}
printf("\n");
}
// Additional edge case tests (converted to round-trip tests with new 32-bit padding)
// These test the same plaintexts as the old decryption vectors but with our new format
static nip44_test_vector_t edge_case_test_vectors[] = {
{
"Edge case: single char 'a'",
"0000000000000000000000000000000000000000000000000000000000000001", // sec1
"0000000000000000000000000000000000000000000000000000000000000002", // sec2
"a",
NULL
},
{
"Edge case: emoji",
"0000000000000000000000000000000000000000000000000000000000000002", // sec1
"0000000000000000000000000000000000000000000000000000000000000001", // sec2
"🍕🫃",
NULL
},
{
"Edge case: wide unicode",
"5c0c523f52a5b6fad39ed2403092df8cebc36318b39383bca6c00808626fab3a", // sec1
"4b22aa260e4acb7021e32f38a6cdf4b673c6a277755bfce287e370c924dc936d", // sec2
"表ポあA鷗Œé逍Üߪąñ丂㐀𠀀",
NULL
}
};
size_t num_edge_case_vectors = sizeof(edge_case_test_vectors) / sizeof(edge_case_test_vectors[0]);
for (size_t i = 0; i < num_edge_case_vectors; i++) {
total_tests++;
printf("Test #%d\n", total_tests);
if (test_nip44_round_trip(&edge_case_test_vectors[i]) == 0) {
passed_tests++;
}
printf("\n");
}
// Test encryption variability (NIP-44 non-deterministic behavior)
total_tests++;
printf("Test #%d\n", total_tests);
if (test_nip44_encryption_variability() == 0) {
passed_tests++;
}
printf("\n");
// Test error conditions
total_tests++;
printf("Test #%d\n", total_tests);
if (test_nip44_error_conditions() == 0) {
passed_tests++;
}
printf("\n");
// Final results
printf("\nTest Results: %d/%d passed\n", passed_tests, total_tests);
if (passed_tests == total_tests) {
printf("All NIP-44 tests PASSED\n");
nostr_cleanup();
return 0;
} else {
printf("Some tests FAILED\n");
nostr_cleanup();
return 1;
}
}

View File

@@ -1,249 +0,0 @@
/*
* NIP-44 Debug Test - Step-by-step comparison with nostr-tools vectors
*
* This test prints intermediate values for comparison with nostr-tools
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "../nostr_core/nostr_core.h"
static int hex_to_bytes(const char* hex, unsigned char* bytes, size_t len) {
if (strlen(hex) != len * 2) return -1;
for (size_t i = 0; i < len; i++) {
if (sscanf(hex + i * 2, "%2hhx", &bytes[i]) != 1) {
return -1;
}
}
return 0;
}
static void bytes_to_hex(const unsigned char* bytes, size_t len, char* hex) {
for (size_t i = 0; i < len; i++) {
sprintf(hex + i * 2, "%02x", bytes[i]);
}
hex[len * 2] = '\0';
}
// Test a specific vector from nostr-tools
static int test_vector_step_by_step(const char* name,
const char* sec1_hex,
const char* sec2_hex,
const char* expected_conversation_key_hex,
const char* nonce_hex,
const char* plaintext,
const char* expected_payload) {
printf("\n🔍 Testing Vector: %s\n", name);
printf("=====================================\n");
// Step 1: Parse keys
unsigned char sec1[32], sec2[32];
if (hex_to_bytes(sec1_hex, sec1, 32) != 0 || hex_to_bytes(sec2_hex, sec2, 32) != 0) {
printf("❌ Failed to parse private keys\n");
return -1;
}
printf("📝 sec1: %s\n", sec1_hex);
printf("📝 sec2: %s\n", sec2_hex);
// Step 2: Generate public keys
unsigned char pub1[32], pub2[32];
if (nostr_ec_public_key_from_private_key(sec1, pub1) != 0 ||
nostr_ec_public_key_from_private_key(sec2, pub2) != 0) {
printf("❌ Failed to derive public keys\n");
return -1;
}
char pub1_hex[65], pub2_hex[65];
bytes_to_hex(pub1, 32, pub1_hex);
bytes_to_hex(pub2, 32, pub2_hex);
printf("📝 pub1: %s\n", pub1_hex);
printf("📝 pub2: %s\n", pub2_hex);
// Step 3: Calculate ECDH shared secret (our raw implementation)
unsigned char shared_secret[32];
if (ecdh_shared_secret(sec1, pub2, shared_secret) != 0) {
printf("❌ Failed to compute ECDH shared secret\n");
return -1;
}
char shared_hex[65];
bytes_to_hex(shared_secret, 32, shared_hex);
printf("🔗 ECDH shared secret: %s\n", shared_hex);
// Step 4: Calculate conversation key using HKDF-extract
unsigned char conversation_key[32];
const char* salt_str = "nip44-v2";
if (nostr_hkdf_extract((const unsigned char*)salt_str, strlen(salt_str),
shared_secret, 32, conversation_key) != 0) {
printf("❌ Failed to derive conversation key\n");
return -1;
}
char conv_key_hex[65];
bytes_to_hex(conversation_key, 32, conv_key_hex);
printf("🗝️ Our conversation key: %s\n", conv_key_hex);
printf("🎯 Expected conv key: %s\n", expected_conversation_key_hex);
if (strcmp(conv_key_hex, expected_conversation_key_hex) == 0) {
printf("✅ Conversation key matches!\n");
} else {
printf("❌ Conversation key MISMATCH!\n");
return -1;
}
// Step 5: Parse nonce
unsigned char nonce[32];
if (hex_to_bytes(nonce_hex, nonce, 32) != 0) {
printf("❌ Failed to parse nonce\n");
return -1;
}
printf("🎲 Nonce: %s\n", nonce_hex);
// Step 6: Derive message keys using HKDF-expand
unsigned char message_keys[76]; // 32 chacha_key + 12 chacha_nonce + 32 hmac_key
if (nostr_hkdf_expand(conversation_key, 32, nonce, 32, message_keys, 76) != 0) {
printf("❌ Failed to derive message keys\n");
return -1;
}
char chacha_key_hex[65], chacha_nonce_hex[25], hmac_key_hex[65];
bytes_to_hex(message_keys, 32, chacha_key_hex);
bytes_to_hex(message_keys + 32, 12, chacha_nonce_hex);
bytes_to_hex(message_keys + 44, 32, hmac_key_hex);
printf("🔐 ChaCha key: %s\n", chacha_key_hex);
printf("🔐 ChaCha nonce: %s\n", chacha_nonce_hex);
printf("🔐 HMAC key: %s\n", hmac_key_hex);
// Step 7: Test encryption with known nonce
char our_payload[8192];
int encrypt_result = nostr_nip44_encrypt_with_nonce(sec1, pub2, plaintext, nonce, our_payload, sizeof(our_payload));
if (encrypt_result == NOSTR_SUCCESS) {
printf("🔒 Our payload: %s\n", our_payload);
printf("🎯 Expected payload: %s\n", expected_payload);
if (strcmp(our_payload, expected_payload) == 0) {
printf("✅ Payload matches perfectly!\n");
} else {
printf("❌ Payload MISMATCH!\n");
// Try to decrypt expected payload with our conversation key
printf("\n🔍 Debugging: Trying to decrypt expected payload...\n");
char decrypted[8192];
int decrypt_result = nostr_nip44_decrypt(sec2, pub1, expected_payload, decrypted, sizeof(decrypted));
if (decrypt_result == NOSTR_SUCCESS) {
printf("✅ Successfully decrypted expected payload!\n");
printf("📝 Decrypted text: \"%s\"\n", decrypted);
if (strcmp(decrypted, plaintext) == 0) {
printf("✅ Decrypted text matches original!\n");
} else {
printf("❌ Decrypted text doesn't match original!\n");
}
} else {
printf("❌ Failed to decrypt expected payload (error: %d)\n", decrypt_result);
}
}
} else {
printf("❌ Encryption failed with error: %d\n", encrypt_result);
return -1;
}
return 0;
}
int main() {
printf("🧪 NIP-44 Debug Test - Step-by-step Vector Comparison\n");
printf("======================================================\n");
// Initialize the library
if (nostr_init() != NOSTR_SUCCESS) {
printf("❌ Failed to initialize NOSTR library\n");
return 1;
}
// Test the simple "a" vector
test_vector_step_by_step(
"Single char 'a'",
"0000000000000000000000000000000000000000000000000000000000000001",
"0000000000000000000000000000000000000000000000000000000000000002",
"c41c775356fd92eadc63ff5a0dc1da211b268cbea22316767095b2871ea1412d",
"0000000000000000000000000000000000000000000000000000000000000001",
"a",
"AgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABee0G5VSK0/9YypIObAtDKfYEAjD35uVkHyB0F4DwrcNaCXlCWZKaArsGrY6M9wnuTMxWfp1RTN9Xga8no+kF5Vsb"
);
// Test the emoji vector
test_vector_step_by_step(
"Emoji 🍕🫃",
"0000000000000000000000000000000000000000000000000000000000000002",
"0000000000000000000000000000000000000000000000000000000000000001",
"c41c775356fd92eadc63ff5a0dc1da211b268cbea22316767095b2871ea1412d",
"f00000000000000000000000000000f00000000000000000000000000000000f",
"🍕🫃",
"AvAAAAAAAAAAAAAAAAAAAPAAAAAAAAAAAAAAAAAAAAAPSKSK6is9ngkX2+cSq85Th16oRTISAOfhStnixqZziKMDvB0QQzgFZdjLTPicCJaV8nDITO+QfaQ61+KbWQIOO2Yj"
);
// Test with get_message_keys test vector to verify HKDF-expand
printf("\n🔍 Testing get_message_keys vector from nostr-tools:\n");
printf("===========================================\n");
unsigned char conv_key[32];
if (hex_to_bytes("a1a3d60f3470a8612633924e91febf96dc5366ce130f658b1f0fc652c20b3b54", conv_key, 32) == 0) {
unsigned char test_nonce[32];
if (hex_to_bytes("e1e6f880560d6d149ed83dcc7e5861ee62a5ee051f7fde9975fe5d25d2a02d72", test_nonce, 32) == 0) {
unsigned char message_keys[76];
if (nostr_hkdf_expand(conv_key, 32, test_nonce, 32, message_keys, 76) == 0) {
char chacha_key_hex[65], chacha_nonce_hex[25], hmac_key_hex[65];
bytes_to_hex(message_keys, 32, chacha_key_hex);
bytes_to_hex(message_keys + 32, 12, chacha_nonce_hex);
bytes_to_hex(message_keys + 44, 32, hmac_key_hex);
printf("📝 Conv key: a1a3d60f3470a8612633924e91febf96dc5366ce130f658b1f0fc652c20b3b54\n");
printf("📝 Nonce: e1e6f880560d6d149ed83dcc7e5861ee62a5ee051f7fde9975fe5d25d2a02d72\n");
printf("🔐 Our ChaCha key: %s\n", chacha_key_hex);
printf("🎯 Expected ChaCha key: f145f3bed47cb70dbeaac07f3a3fe683e822b3715edb7c4fe310829014ce7d76\n");
printf("🔐 Our ChaCha nonce: %s\n", chacha_nonce_hex);
printf("🎯 Expected ChaCha nonce: c4ad129bb01180c0933a160c\n");
printf("🔐 Our HMAC key: %s\n", hmac_key_hex);
printf("🎯 Expected HMAC key: 027c1db445f05e2eee864a0975b0ddef5b7110583c8c192de3732571ca5838c4\n");
if (strcmp(chacha_key_hex, "f145f3bed47cb70dbeaac07f3a3fe683e822b3715edb7c4fe310829014ce7d76") == 0) {
printf("✅ ChaCha key matches!\n");
} else {
printf("❌ ChaCha key MISMATCH!\n");
}
if (strcmp(chacha_nonce_hex, "c4ad129bb01180c0933a160c") == 0) {
printf("✅ ChaCha nonce matches!\n");
} else {
printf("❌ ChaCha nonce MISMATCH!\n");
}
if (strcmp(hmac_key_hex, "027c1db445f05e2eee864a0975b0ddef5b7110583c8c192de3732571ca5838c4") == 0) {
printf("✅ HMAC key matches!\n");
} else {
printf("❌ HMAC key MISMATCH!\n");
}
} else {
printf("❌ Failed to expand message keys\n");
}
} else {
printf("❌ Failed to parse test nonce\n");
}
} else {
printf("❌ Failed to parse conversation key\n");
}
nostr_cleanup();
printf("\n🏁 Debug test completed\n");
return 0;
}

View File

@@ -1,255 +0,0 @@
/*
* NIP-44 Detailed Debug Test - Print every single intermediate step
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "../nostr_core/nostr_core.h"
static int hex_to_bytes(const char* hex, unsigned char* bytes, size_t len) {
if (strlen(hex) != len * 2) return -1;
for (size_t i = 0; i < len; i++) {
if (sscanf(hex + i * 2, "%2hhx", &bytes[i]) != 1) {
return -1;
}
}
return 0;
}
static void bytes_to_hex(const unsigned char* bytes, size_t len, char* hex) {
for (size_t i = 0; i < len; i++) {
sprintf(hex + i * 2, "%02x", bytes[i]);
}
hex[len * 2] = '\0';
}
static void print_bytes(const char* label, const unsigned char* bytes, size_t len) {
char hex[len * 2 + 1];
bytes_to_hex(bytes, len, hex);
printf("%s: %s\n", label, hex);
}
// Test NIP-44 padding calculation (replicate from our crypto.c)
static size_t calc_padded_len(size_t unpadded_len) {
if (unpadded_len <= 32) {
return 32;
}
size_t next_power = 1;
while (next_power < unpadded_len) {
next_power <<= 1;
}
size_t chunk = (next_power <= 256) ? 32 : (next_power / 8);
return chunk * ((unpadded_len - 1) / chunk + 1);
}
// Test NIP-44 padding (replicate from our crypto.c)
static unsigned char* pad_plaintext_debug(const char* plaintext, size_t* padded_len) {
size_t unpadded_len = strlen(plaintext);
if (unpadded_len > 65535) {
return NULL;
}
printf("🔍 PADDING DEBUG:\n");
printf(" unpadded_len: %zu\n", unpadded_len);
*padded_len = calc_padded_len(unpadded_len + 2); // +2 for length prefix
printf(" calculated_padded_len: %zu\n", *padded_len);
unsigned char* padded = malloc(*padded_len);
if (!padded) return NULL;
// Write length prefix (big-endian u16)
padded[0] = (unpadded_len >> 8) & 0xFF;
padded[1] = unpadded_len & 0xFF;
printf(" length_prefix: %02x%02x (big-endian u16 = %zu)\n", padded[0], padded[1], unpadded_len);
// Copy plaintext (if any)
if (unpadded_len > 0) {
memcpy(padded + 2, plaintext, unpadded_len);
}
// Zero-fill padding
memset(padded + 2 + unpadded_len, 0, *padded_len - 2 - unpadded_len);
print_bytes(" padded_plaintext", padded, *padded_len);
return padded;
}
int main() {
printf("🧪 NIP-44 DETAILED DEBUG TEST\n");
printf("===============================\n\n");
// Initialize the library
if (nostr_init() != NOSTR_SUCCESS) {
printf("❌ Failed to initialize NOSTR library\n");
return 1;
}
printf("=== TESTING: Single char 'a' ===\n");
// Step 1: Parse private keys
unsigned char sec1[32], sec2[32];
hex_to_bytes("0000000000000000000000000000000000000000000000000000000000000001", sec1, 32);
hex_to_bytes("0000000000000000000000000000000000000000000000000000000000000002", sec2, 32);
print_bytes("sec1", sec1, 32);
print_bytes("sec2", sec2, 32);
// Step 2: Generate public keys
unsigned char pub1[32], pub2[32];
nostr_ec_public_key_from_private_key(sec1, pub1);
nostr_ec_public_key_from_private_key(sec2, pub2);
print_bytes("pub1", pub1, 32);
print_bytes("pub2", pub2, 32);
// Step 3: ECDH shared secret
unsigned char shared_secret[32];
ecdh_shared_secret(sec1, pub2, shared_secret);
print_bytes("ecdh_shared_secret", shared_secret, 32);
// Step 4: HKDF Extract (conversation key)
unsigned char conversation_key[32];
const char* salt_str = "nip44-v2";
nostr_hkdf_extract((const unsigned char*)salt_str, strlen(salt_str), shared_secret, 32, conversation_key);
print_bytes("conversation_key", conversation_key, 32);
// Step 5: Parse nonce
unsigned char nonce[32];
hex_to_bytes("0000000000000000000000000000000000000000000000000000000000000001", nonce, 32);
print_bytes("nonce", nonce, 32);
// Step 6: HKDF Expand (message keys)
unsigned char message_keys[76];
nostr_hkdf_expand(conversation_key, 32, nonce, 32, message_keys, 76);
print_bytes("chacha_key", message_keys, 32);
print_bytes("chacha_nonce", message_keys + 32, 12);
print_bytes("hmac_key", message_keys + 44, 32);
// Step 7: Pad plaintext
const char* plaintext = "a";
printf("\n🔍 PLAINTEXT: \"%s\" (length: %zu)\n", plaintext, strlen(plaintext));
size_t padded_len;
unsigned char* padded_plaintext = pad_plaintext_debug(plaintext, &padded_len);
if (!padded_plaintext) {
printf("❌ Failed to pad plaintext\n");
return 1;
}
// Step 8: ChaCha20 encrypt
printf("\n🔍 CHACHA20 ENCRYPTION:\n");
unsigned char* ciphertext = malloc(padded_len);
if (!ciphertext) {
printf("❌ Failed to allocate ciphertext buffer\n");
free(padded_plaintext);
return 1;
}
// Use our ChaCha20 function
if (chacha20_encrypt(message_keys, 0, message_keys + 32, padded_plaintext, ciphertext, padded_len) != 0) {
printf("❌ ChaCha20 encryption failed\n");
free(padded_plaintext);
free(ciphertext);
return 1;
}
print_bytes(" ciphertext", ciphertext, padded_len);
// Step 9: HMAC with AAD
printf("\n🔍 HMAC CALCULATION:\n");
unsigned char* aad_data = malloc(32 + padded_len);
if (!aad_data) {
printf("❌ Failed to allocate AAD buffer\n");
free(padded_plaintext);
free(ciphertext);
return 1;
}
memcpy(aad_data, nonce, 32);
memcpy(aad_data + 32, ciphertext, padded_len);
print_bytes(" aad_data", aad_data, 32 + padded_len);
unsigned char mac[32];
nostr_hmac_sha256(message_keys + 44, 32, aad_data, 32 + padded_len, mac);
print_bytes(" mac", mac, 32);
// Step 10: Construct final payload
printf("\n🔍 PAYLOAD CONSTRUCTION:\n");
size_t payload_len = 1 + 32 + padded_len + 32;
unsigned char* payload = malloc(payload_len);
if (!payload) {
printf("❌ Failed to allocate payload buffer\n");
free(padded_plaintext);
free(ciphertext);
free(aad_data);
return 1;
}
payload[0] = 0x02; // NIP-44 version 2
memcpy(payload + 1, nonce, 32);
memcpy(payload + 33, ciphertext, padded_len);
memcpy(payload + 33 + padded_len, mac, 32);
printf(" version: 0x%02x\n", payload[0]);
print_bytes(" payload_nonce", payload + 1, 32);
print_bytes(" payload_ciphertext", payload + 33, padded_len);
print_bytes(" payload_mac", payload + 33 + padded_len, 32);
print_bytes(" raw_payload", payload, payload_len);
// Step 11: Base64 encode
printf("\n🔍 BASE64 ENCODING:\n");
size_t b64_len = ((payload_len + 2) / 3) * 4 + 1;
char* base64_output = malloc(b64_len);
if (!base64_output) {
printf("❌ Failed to allocate base64 buffer\n");
free(padded_plaintext);
free(ciphertext);
free(aad_data);
free(payload);
return 1;
}
// Use our internal base64_encode function from crypto.c
extern size_t base64_encode(const unsigned char* data, size_t len, char* output, size_t output_size);
size_t actual_b64_len = base64_encode(payload, payload_len, base64_output, b64_len);
printf(" payload_length: %zu\n", payload_len);
printf(" base64_length: %zu\n", actual_b64_len);
printf(" our_base64: %s\n", base64_output);
printf(" expected: AgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABee0G5VSK0/9YypIObAtDKfYEAjD35uVkHyB0F4DwrcNaCXlCWZKaArsGrY6M9wnuTMxWfp1RTN9Xga8no+kF5Vsb\n");
if (strcmp(base64_output, "AgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABee0G5VSK0/9YypIObAtDKfYEAjD35uVkHyB0F4DwrcNaCXlCWZKaArsGrY6M9wnuTMxWfp1RTN9Xga8no+kF5Vsb") == 0) {
printf("✅ PERFECT MATCH!\n");
} else {
printf("❌ MISMATCH - need to investigate!\n");
// Let's also try our full encrypt function for comparison
printf("\n🔍 FULL ENCRYPT FUNCTION TEST:\n");
char full_encrypt_output[8192];
int result = nostr_nip44_encrypt_with_nonce(sec1, pub2, plaintext, nonce, full_encrypt_output, sizeof(full_encrypt_output));
if (result == NOSTR_SUCCESS) {
printf(" full_encrypt: %s\n", full_encrypt_output);
} else {
printf(" full_encrypt failed with error: %d\n", result);
}
}
// Cleanup
free(padded_plaintext);
free(ciphertext);
free(aad_data);
free(payload);
free(base64_output);
nostr_cleanup();
printf("\n🏁 Detailed debug test completed\n");
return 0;
}

View File

@@ -1,393 +0,0 @@
/*
* NIP-44 Encryption/Decryption Test
*
* Test suite for NIP-44 versioned encryption functionality
* Uses known test vectors and cross-implementation compatibility tests
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "../nostr_core/nostr_core.h"
// Test vectors for NIP-44 with proper key pairs
typedef struct {
const char* name;
const char* sender_private_key_hex;
const char* recipient_private_key_hex; // FIX: Need proper private key, not public key
const char* plaintext;
const char* expected_encrypted; // Optional - for known test vectors
} nip44_test_vector_t;
// Known test vectors from nostr-tools nip44.vectors.json
static nip44_test_vector_t known_test_vectors[] = {
{
"Known vector: single char 'a'",
"0000000000000000000000000000000000000000000000000000000000000001", // sec1
"0000000000000000000000000000000000000000000000000000000000000002", // sec2
"a",
"AgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABee0G5VSK0/9YypIObAtDKfYEAjD35uVkHyB0F4DwrcNaCXlCWZKaArsGrY6M9wnuTMxWfp1RTN9Xga8no+kF5Vsb"
},
{
"Known vector: emoji",
"0000000000000000000000000000000000000000000000000000000000000002", // sec1
"0000000000000000000000000000000000000000000000000000000000000001", // sec2
"🍕🫃",
"AvAAAAAAAAAAAAAAAAAAAPAAAAAAAAAAAAAAAAAAAAAPSKSK6is9ngkX2+cSq85Th16oRTISAOfhStnixqZziKMDvB0QQzgFZdjLTPicCJaV8nDITO+QfaQ61+KbWQIOO2Yj"
},
{
"Known vector: wide unicode",
"5c0c523f52a5b6fad39ed2403092df8cebc36318b39383bca6c00808626fab3a", // sec1
"4b22aa260e4acb7021e32f38a6cdf4b673c6a277755bfce287e370c924dc936d", // sec2
"表ポあA鷗Œé逍Üߪąñ丂㐀𠀀",
"ArY1I2xC2yDwIbuNHN/1ynXdGgzHLqdCrXUPMwELJPc7s7JqlCMJBAIIjfkpHReBPXeoMCyuClwgbT419jUWU1PwaNl4FEQYKCDKVJz+97Mp3K+Q2YGa77B6gpxB/lr1QgoqpDf7wDVrDmOqGoiPjWDqy8KzLueKDcm9BVP8xeTJIxs="
}
};
// Round-trip test vectors with proper key pairs
static nip44_test_vector_t test_vectors[] = {
{
"Basic short message",
"91ba716fa9e7ea2fcbad360cf4f8e0d312f73984da63d90f524ad61a6a1e7dbe", // Working keys from simple_nip44_test
"96f6fa197aa07477ab88f6981118466ae3a982faab8ad5db9d5426870c73d220",
"Hello, NIP-44!",
NULL
},
{
"Unicode message",
"1111111111111111111111111111111111111111111111111111111111111111",
"2222222222222222222222222222222222222222222222222222222222222222",
"Hello 🌍 World! 🚀",
NULL
},
{
"Empty message",
"3333333333333333333333333333333333333333333333333333333333333333",
"4444444444444444444444444444444444444444444444444444444444444444",
"",
NULL
}
};
static int hex_to_bytes(const char* hex, unsigned char* bytes, size_t len) {
if (strlen(hex) != len * 2) return -1;
for (size_t i = 0; i < len; i++) {
if (sscanf(hex + i * 2, "%2hhx", &bytes[i]) != 1) {
return -1;
}
}
return 0;
}
static void bytes_to_hex(const unsigned char* bytes, size_t len, char* hex) {
for (size_t i = 0; i < len; i++) {
sprintf(hex + i * 2, "%02x", bytes[i]);
}
hex[len * 2] = '\0';
}
static int test_nip44_round_trip(const nip44_test_vector_t* tv) {
printf("Testing: %s\n", tv->name);
// Parse keys - both private keys
unsigned char sender_private_key[32];
unsigned char recipient_private_key[32];
if (hex_to_bytes(tv->sender_private_key_hex, sender_private_key, 32) != 0) {
printf(" ❌ Failed to parse sender private key\n");
return -1;
}
if (hex_to_bytes(tv->recipient_private_key_hex, recipient_private_key, 32) != 0) {
printf(" ❌ Failed to parse recipient private key\n");
return -1;
}
// Generate the public keys from the private keys
unsigned char sender_public_key[32];
unsigned char recipient_public_key[32];
if (nostr_ec_public_key_from_private_key(sender_private_key, sender_public_key) != 0) {
printf(" ❌ Failed to derive sender public key\n");
return -1;
}
if (nostr_ec_public_key_from_private_key(recipient_private_key, recipient_public_key) != 0) {
printf(" ❌ Failed to derive recipient public key\n");
return -1;
}
// Test encryption
char encrypted[8192]; // Large buffer for encrypted data
int encrypt_result = nostr_nip44_encrypt(
sender_private_key,
recipient_public_key,
tv->plaintext,
encrypted,
sizeof(encrypted)
);
if (encrypt_result != NOSTR_SUCCESS) {
printf(" ❌ Encryption failed with error: %d\n", encrypt_result);
return -1;
}
printf(" ✅ Encryption successful\n");
printf(" 📦 Encrypted length: %zu bytes\n", strlen(encrypted));
// Test decryption - use recipient private key + sender public key
char decrypted[8192]; // Large buffer for decrypted data
int decrypt_result = nostr_nip44_decrypt(
recipient_private_key,
sender_public_key,
encrypted,
decrypted,
sizeof(decrypted)
);
if (decrypt_result != NOSTR_SUCCESS) {
printf(" ❌ Decryption failed with error: %d\n", decrypt_result);
return -1;
}
// Verify round-trip
if (strcmp(tv->plaintext, decrypted) != 0) {
printf(" ❌ Round-trip failed!\n");
printf(" 📝 Original: \"%s\"\n", tv->plaintext);
printf(" 📝 Decrypted: \"%s\"\n", decrypted);
return -1;
}
printf(" ✅ Round-trip successful!\n");
printf(" 📝 Message: \"%s\"\n", tv->plaintext);
printf("\n");
return 0;
}
static int test_nip44_error_conditions() {
printf("Testing NIP-44 error conditions:\n");
// Use proper valid secp256k1 private keys
unsigned char valid_sender_key[32];
unsigned char valid_recipient_key[32];
unsigned char valid_recipient_pubkey[32];
hex_to_bytes("0000000000000000000000000000000000000000000000000000000000000001", valid_sender_key, 32);
hex_to_bytes("0000000000000000000000000000000000000000000000000000000000000002", valid_recipient_key, 32);
// Generate the recipient's public key
if (nostr_ec_public_key_from_private_key(valid_recipient_key, valid_recipient_pubkey) != 0) {
printf(" ❌ Failed to generate recipient public key\n");
return -1;
}
char output[1024];
// Test NULL parameters
int result = nostr_nip44_encrypt(NULL, valid_recipient_pubkey, "test", output, sizeof(output));
if (result != NOSTR_ERROR_INVALID_INPUT) {
printf(" ❌ Should reject NULL sender key\n");
return -1;
}
result = nostr_nip44_encrypt(valid_sender_key, NULL, "test", output, sizeof(output));
if (result != NOSTR_ERROR_INVALID_INPUT) {
printf(" ❌ Should reject NULL recipient key\n");
return -1;
}
result = nostr_nip44_encrypt(valid_sender_key, valid_recipient_pubkey, NULL, output, sizeof(output));
if (result != NOSTR_ERROR_INVALID_INPUT) {
printf(" ❌ Should reject NULL plaintext\n");
return -1;
}
result = nostr_nip44_encrypt(valid_sender_key, valid_recipient_pubkey, "test", NULL, sizeof(output));
if (result != NOSTR_ERROR_INVALID_INPUT) {
printf(" ❌ Should reject NULL output buffer\n");
return -1;
}
// Test buffer too small
char small_buffer[10];
result = nostr_nip44_encrypt(valid_sender_key, valid_recipient_pubkey, "test message", small_buffer, sizeof(small_buffer));
if (result != NOSTR_ERROR_NIP44_BUFFER_TOO_SMALL) {
printf(" ❌ Should detect buffer too small, got error: %d\n", result);
return -1;
}
printf(" ✅ All error conditions handled correctly\n\n");
return 0;
}
static int test_nip44_known_vector(const nip44_test_vector_t* tv) {
printf("Testing known vector: %s\n", tv->name);
// Parse keys
unsigned char sender_private_key[32];
unsigned char recipient_private_key[32];
if (hex_to_bytes(tv->sender_private_key_hex, sender_private_key, 32) != 0) {
printf(" ❌ Failed to parse sender private key\n");
return -1;
}
if (hex_to_bytes(tv->recipient_private_key_hex, recipient_private_key, 32) != 0) {
printf(" ❌ Failed to parse recipient private key\n");
return -1;
}
// Generate the public keys from the private keys
unsigned char sender_public_key[32];
if (nostr_ec_public_key_from_private_key(sender_private_key, sender_public_key) != 0) {
printf(" ❌ Failed to derive sender public key\n");
return -1;
}
// Test decryption of known vector
char decrypted[8192];
int decrypt_result = nostr_nip44_decrypt(
recipient_private_key,
sender_public_key,
tv->expected_encrypted,
decrypted,
sizeof(decrypted)
);
if (decrypt_result != NOSTR_SUCCESS) {
printf(" ❌ Decryption of known vector failed with error: %d\n", decrypt_result);
printf(" 📦 Expected payload: %.80s...\n", tv->expected_encrypted);
return -1;
}
// Verify decrypted plaintext matches expected
if (strcmp(tv->plaintext, decrypted) != 0) {
printf(" ❌ Decrypted plaintext doesn't match!\n");
printf(" 📝 Expected: \"%s\"\n", tv->plaintext);
printf(" 📝 Got: \"%s\"\n", decrypted);
return -1;
}
printf(" ✅ Known vector decryption successful!\n");
printf(" 📝 Message: \"%s\"\n", tv->plaintext);
printf("\n");
return 0;
}
static int test_nip44_vs_nip04_comparison() {
printf("Testing NIP-44 vs NIP-04 comparison:\n");
const char* test_message = "This is a test message for comparing NIP-04 and NIP-44 encryption methods.";
unsigned char sender_key[32], recipient_key[32];
memset(sender_key, 0x11, 32);
memset(recipient_key, 0x22, 32);
// Generate proper public keys
unsigned char sender_pubkey[32], recipient_pubkey[32];
if (nostr_ec_public_key_from_private_key(sender_key, sender_pubkey) != 0 ||
nostr_ec_public_key_from_private_key(recipient_key, recipient_pubkey) != 0) {
printf(" ❌ Failed to generate public keys\n");
return -1;
}
// Test NIP-04 encryption
char nip04_encrypted[8192];
int nip04_result = nostr_nip04_encrypt(sender_key, recipient_pubkey,
test_message, nip04_encrypted, sizeof(nip04_encrypted));
// Test NIP-44 encryption
char nip44_encrypted[8192];
int nip44_result = nostr_nip44_encrypt(sender_key, recipient_pubkey,
test_message, nip44_encrypted, sizeof(nip44_encrypted));
if (nip04_result == NOSTR_SUCCESS && nip44_result == NOSTR_SUCCESS) {
printf(" ✅ Both NIP-04 and NIP-44 encryption successful\n");
printf(" 📊 NIP-04 output length: %zu bytes\n", strlen(nip04_encrypted));
printf(" 📊 NIP-44 output length: %zu bytes\n", strlen(nip44_encrypted));
printf(" 📊 Size difference: %+ld bytes\n",
(long)strlen(nip44_encrypted) - (long)strlen(nip04_encrypted));
// Verify they produce different outputs (they use different algorithms)
if (strcmp(nip04_encrypted, nip44_encrypted) == 0) {
printf(" ⚠️ Warning: NIP-04 and NIP-44 produced identical output (unexpected)\n");
} else {
printf(" ✅ NIP-04 and NIP-44 produce different outputs (expected)\n");
}
} else {
if (nip04_result != NOSTR_SUCCESS) {
printf(" ❌ NIP-04 encryption failed: %d\n", nip04_result);
}
if (nip44_result != NOSTR_SUCCESS) {
printf(" ❌ NIP-44 encryption failed: %d\n", nip44_result);
}
return -1;
}
printf("\n");
return 0;
}
int main() {
printf("🧪 NIP-44 Encryption Test Suite\n");
printf("================================\n\n");
// Initialize the library
if (nostr_init() != NOSTR_SUCCESS) {
printf("❌ Failed to initialize NOSTR library\n");
return 1;
}
int total_tests = 0;
int passed_tests = 0;
// Test all vectors
size_t num_vectors = sizeof(test_vectors) / sizeof(test_vectors[0]);
for (size_t i = 0; i < num_vectors; i++) {
total_tests++;
if (test_nip44_round_trip(&test_vectors[i]) == 0) {
passed_tests++;
}
}
// Test known vectors
size_t num_known_vectors = sizeof(known_test_vectors) / sizeof(known_test_vectors[0]);
for (size_t i = 0; i < num_known_vectors; i++) {
total_tests++;
if (test_nip44_known_vector(&known_test_vectors[i]) == 0) {
passed_tests++;
}
}
// Test error conditions
total_tests++;
if (test_nip44_error_conditions() == 0) {
passed_tests++;
}
// Test comparison with NIP-04
total_tests++;
if (test_nip44_vs_nip04_comparison() == 0) {
passed_tests++;
}
// Final results
printf("🏁 Test Results:\n");
printf("================\n");
printf("Tests passed: %d/%d\n", passed_tests, total_tests);
if (passed_tests == total_tests) {
printf("✅ All NIP-44 tests PASSED! 🎉\n");
nostr_cleanup();
return 0;
} else {
printf("❌ Some tests FAILED! 😞\n");
nostr_cleanup();
return 1;
}
}

Some files were not shown because too many files have changed in this diff Show More