sgns::neoswarm::security::NodeIdentity¶
Manages a secp256k1 keypair and derives the node's PeerId. More...
#include <node_identity.hpp>
Public Classes¶
| Name | |
|---|---|
| struct | Impl |
Public Types¶
| Name | |
|---|---|
| using std::array< uint8_t, kPrivKeySize > | PrivKey |
| using std::array< uint8_t, kPubKeySize > | PubKey |
Public Functions¶
| Name | |
|---|---|
| NodeIdentity() | |
| ~NodeIdentity() | |
| outcome::result< void > | Generate() Generate a new random secp256k1 keypair. |
| outcome::result< void > | LoadFromFile(const std::string & path) Load a keypair from a hex file. |
| outcome::result< void > | SaveToFile(const std::string & path) const Save the current keypair to a hex file. |
| outcome::result< void > | SaveEncrypted(const std::string & path, const std::string & passphrase) const Save the current keypair encrypted with AES-256-GCM. |
| outcome::result< void > | LoadEncrypted(const std::string & path, const std::string & passphrase) Load an encrypted keypair and decrypt it. |
| std::string | GetPeerId() const Derive the PeerId string from the public key. |
| const PubKey & | GetPublicKey() const |
| const PrivKey & | GetPrivateKey() const |
| bool | IsLoaded() const |
| outcome::result< std::vector< uint8_t > > | Sign(const std::vector< uint8_t > & message) const Sign a message with the node's private key. |
| bool | Verify(const std::vector< uint8_t > & message, const std::vector< uint8_t > & signature) const Verify a signature against this node's public key. |
Public Attributes¶
| Name | |
|---|---|
| constexpr size_t | kPrivKeySize |
| constexpr size_t | kPubKeySize compressed |
| constexpr size_t | kPeerIdSize |
Detailed Description¶
Manages a secp256k1 keypair and derives the node's PeerId.
PeerId = hex( SHA-256( compressed_public_key ) )
This is the NEO-SWARM P2P identity — used for encrypting swarm inter-node communication and verifying peer messages. It is separate from GeniusSDK identity: the SDK generates its own keypair internally via GeniusSDKInit() for blockchain identity. NEO-SWARM does NOT derive SDK keys from this class.
Public Types Documentation¶
using PrivKey¶
using PubKey¶
Public Functions Documentation¶
function NodeIdentity¶
function ~NodeIdentity¶
function Generate¶
Generate a new random secp256k1 keypair.
Return: outcome::success or IdentityError.
function LoadFromFile¶
Load a keypair from a hex file.
Parameters:
- path Path to the key file.
Return: outcome::success or IdentityError.
function SaveToFile¶
Save the current keypair to a hex file.
Parameters:
- path Destination file path.
Return: outcome::success or IdentityError.
function SaveEncrypted¶
outcome::result< void > SaveEncrypted(
const std::string & path,
const std::string & passphrase
) const
Save the current keypair encrypted with AES-256-GCM.
Parameters:
- path Destination file path (typically "node.key").
- passphrase User-supplied encryption passphrase.
Return: outcome::success or IdentityError.
Derives a 256-bit encryption key from passphrase using PBKDF2-HMAC-SHA256 (600,000 iterations) with a random salt. The key is encrypted and written in a self-describing binary format: [4-byte salt length][salt][12-byte IV][ciphertext][16-byte GCM tag].
function LoadEncrypted¶
Load an encrypted keypair and decrypt it.
Parameters:
- path Path to the encrypted key file.
- passphrase Decryption passphrase.
Return: outcome::success or IdentityError.
Reads the binary format written by SaveEncrypted, derives the decryption key from passphrase, decrypts, and verifies the GCM authentication tag. If the tag does not match (wrong passphrase or tampered file), returns IdentityError.
On success, the public key is derived and PeerId is available.
function GetPeerId¶
Derive the PeerId string from the public key.
Return: Hex-encoded SHA-256 of the compressed public key.
function GetPublicKey¶
Return: The compressed public key bytes.
function GetPrivateKey¶
Return: The 32-byte secp256k1 private key.
Precondition: IsLoaded() must return true.
function IsLoaded¶
Return: True if a keypair has been loaded or generated.
function Sign¶
Sign a message with the node's private key.
Parameters:
- message Raw bytes to sign.
Return: DER-encoded signature or IdentityError.
function Verify¶
bool Verify(
const std::vector< uint8_t > & message,
const std::vector< uint8_t > & signature
) const
Verify a signature against this node's public key.
Parameters:
- message Original message bytes.
- signature DER-encoded signature to verify.
Return: True if the signature is valid.
Public Attributes Documentation¶
variable kPrivKeySize¶
variable kPubKeySize¶
compressed
variable kPeerIdSize¶
Updated on 2026-07-25 at 22:56:57 +0000