Skip to content

securecrdt/SecureCrdtRegistry.hpp

Static registry mapping a base_key pattern to {signer-set source, required signature count, ISignedCRDTData factory}, resolvable at startup/runtime by key. Header-only, mirrors IInputValidator's static Register/UnregisterIf/Get idiom. More...

Namespaces

Name
sgns
sgns::securecrdt

Classes

Name
struct sgns::securecrdt::SignerSetSnapshot
Snapshot of an authorized signer set and its required signature count, as produced by an injected SignerSetSource.
struct sgns::securecrdt::SecureCrdtRegistryEntry
Policy entry describing how a registered key pattern is verified and instantiated.
class sgns::securecrdt::SecureCrdtRegistry
Static registry resolving a CRDT key to its SecureCrdtRegistryEntry.

Types

Name
using std::function< outcome::result< SignerSetSnapshot >(const std::string &base_key)> SignerSetSource
Injectable callback resolving the current authorized signer set for a given base_key. NOT hard-wired to TrustedPeerRegistry (which does not exist until Phase 10) - tests inject a fixed-list lambda.

Detailed Description

Static registry mapping a base_key pattern to {signer-set source, required signature count, ISignedCRDTData factory}, resolvable at startup/runtime by key. Header-only, mirrors IInputValidator's static Register/UnregisterIf/Get idiom.

Date: 2026-07-23 Henrique A. Klein ([email protected])

Types Documentation

using SignerSetSource

using sgns::securecrdt::SignerSetSource = typedef std::function<outcome::result<SignerSetSnapshot>( const std::string &base_key )>;

Injectable callback resolving the current authorized signer set for a given base_key. NOT hard-wired to TrustedPeerRegistry (which does not exist until Phase 10) - tests inject a fixed-list lambda.

Source code

#ifndef SGNS_SECURECRDT_SECURECRDTREGISTRY_HPP
#define SGNS_SECURECRDT_SECURECRDTREGISTRY_HPP

#include <cstdint>
#include <functional>
#include <memory>
#include <regex>
#include <string>
#include <unordered_map>
#include <vector>

#include "outcome/outcome.hpp"
#include "securecrdt/ISignedCRDTData.hpp"

namespace sgns::securecrdt
{
    struct SignerSetSnapshot
    {
        std::vector<std::string> signer_set;
        uint64_t                 required_signatures = 0;
    };

    using SignerSetSource = std::function<outcome::result<SignerSetSnapshot>( const std::string &base_key )>;

    struct SecureCrdtRegistryEntry
    {
        std::string                                          key_pattern;
        SignerSetSource                                      signer_set_source;
        std::function<std::shared_ptr<ISignedCRDTData>()>    make_instance;
        std::regex                                           compiled_pattern;
        const void                                          *owner_token = nullptr;
    };

    class SecureCrdtRegistry
    {
    public:
        static void Register( const std::string &key_pattern, SecureCrdtRegistryEntry entry )
        {
            entry.key_pattern     = key_pattern;
            entry.compiled_pattern = std::regex( "/?" + key_pattern + "(/sig/[^/]+)?" );
            registry()[key_pattern] = std::move( entry );
        }

        static void UnregisterIf( const std::string &key_pattern, const void *expected_token )
        {
            auto it = registry().find( key_pattern );
            if ( it != registry().end() && it->second.owner_token == expected_token )
            {
                registry().erase( it );
            }
        }

        static const SecureCrdtRegistryEntry *Resolve( const std::string &key )
        {
            for ( const auto &[pattern, entry] : registry() )
            {
                if ( std::regex_match( key, entry.compiled_pattern ) )
                {
                    return &entry;
                }
            }
            return nullptr;
        }

        static std::vector<SecureCrdtRegistryEntry> AllEntries()
        {
            std::vector<SecureCrdtRegistryEntry> entries;
            entries.reserve( registry().size() );
            for ( const auto &[pattern, entry] : registry() )
            {
                entries.push_back( entry );
            }
            return entries;
        }

    private:
        static std::unordered_map<std::string, SecureCrdtRegistryEntry> &registry()
        {
            static std::unordered_map<std::string, SecureCrdtRegistryEntry> map;
            return map;
        }
    };
} // namespace sgns::securecrdt

#endif // SGNS_SECURECRDT_SECURECRDTREGISTRY_HPP

Updated on 2026-08-06 at 13:59:20 +0000