aboutsummaryrefslogtreecommitdiff
path: root/src/signer/noscrypt_signer.cpp
blob: 26f8e548e887cad92d5196a61f34f8fcb44c8d13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <noscrypt.h>

#include "signer.hpp"

using namespace std;

namespace nostr
{
namespace signer
{
class NoscryptSigner : public INostrConnectSigner
{
public:
    NoscryptSigner(shared_ptr<plog::IAppender> appender)
    {
        // Set up the logger.
        plog::init(plog::debug, appender.get());

        // Set up the noscrypt library.
        this->context = make_shared<NCContext>();
        auto contextStructSize = NCGetContextStructSize();
        unique_ptr<uint8_t[]> randomEntropy(new uint8_t[contextStructSize]);

        random_device device;
        mt19937 seed(device());
        uniform_int_distribution<int> distribution(1, NC_CONTEXT_ENTROPY_SIZE);
        generate_n(randomEntropy.get(), NC_CONTEXT_ENTROPY_SIZE, [&]() { return distribution(seed); });

        NCResult result = NCInitContext(context.get(), randomEntropy.get());
        this->handleNoscryptInitResult(result);
    };

    ~NoscryptSigner()
    {
        NCDestroyContext(context.get());
    };

    void receiveConnection(string connectionToken) override
    {
        // Receive the connection token here.
    };

    void initiateConnection(
        string relay,
        string name,
        string url,
        string description) override
    {
        // Initiate the connection here.
    };

    void sign(shared_ptr<data::Event> event) override
    {
        // Sign the event here.
    };

private:
    shared_ptr<NCContext> context;

    void handleNoscryptInitResult(NCResult result)
    {
        switch (result) {
        case NC_SUCCESS:
            PLOG_INFO << "Successfully initialized noscrypt.";
            break;
        
        case E_NULL_PTR:
            PLOG_ERROR << "Failed to initialize noscrypt: A null pointer was passed to the initializer.";
            break;

        case E_INVALID_ARG:
            PLOG_ERROR << "Failed to initialize noscrypt: An invalid argument was passed to the initializer.";
            break;
        
        case E_INVALID_CONTEXT:
            PLOG_ERROR << "Failed to initialize noscrypt: The NCContext struct is in an invalid state.";
            break;

        case E_ARGUMENT_OUT_OF_RANGE:
            PLOG_ERROR << "Failed to initialize noscrypt: An initializer argument was outside the range of acceptable values.";
            break;

        case E_OPERATION_FAILED:
            PLOG_ERROR << "Failed to initialize noscrypt.";
            break;
        }
    };
};
} // namespace signer
} // namespace nostr