Skip to content

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

class sgns::neoswarm::security::NodeIdentity;

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 sgns::neoswarm::security::NodeIdentity::PrivKey =  std::array<uint8_t, kPrivKeySize>;

using PubKey

using sgns::neoswarm::security::NodeIdentity::PubKey =  std::array<uint8_t, kPubKeySize>;

Public Functions Documentation

function NodeIdentity

NodeIdentity()

function ~NodeIdentity

~NodeIdentity()

function Generate

outcome::result< void > Generate()

Generate a new random secp256k1 keypair.

Return: outcome::success or IdentityError.

function LoadFromFile

outcome::result< void > LoadFromFile(
    const std::string & path
)

Load a keypair from a hex file.

Parameters:

  • path Path to the key file.

Return: outcome::success or IdentityError.

function SaveToFile

outcome::result< void > SaveToFile(
    const std::string & path
) const

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

outcome::result< void > LoadEncrypted(
    const std::string & path,
    const std::string & passphrase
)

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

std::string GetPeerId() const

Derive the PeerId string from the public key.

Return: Hex-encoded SHA-256 of the compressed public key.

function GetPublicKey

inline const PubKey & GetPublicKey() const

Return: The compressed public key bytes.

function GetPrivateKey

inline const PrivKey & GetPrivateKey() const

Return: The 32-byte secp256k1 private key.

Precondition: IsLoaded() must return true.

function IsLoaded

inline bool IsLoaded() const

Return: True if a keypair has been loaded or generated.

function Sign

outcome::result< std::vector< uint8_t > > Sign(
    const std::vector< uint8_t > & message
) const

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

static constexpr size_t kPrivKeySize = 32;

variable kPubKeySize

static constexpr size_t kPubKeySize = 33;

compressed

variable kPeerIdSize

static constexpr size_t kPeerIdSize = 32;

Updated on 2026-07-25 at 22:56:57 +0000