|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <napi.h> |
| 4 | + |
| 5 | +#include <algorithm> |
| 6 | + |
| 7 | +#include "../meta/meta_base_wrapper.hpp" |
| 8 | +#include "../utilities.hpp" |
| 9 | +#include "oxenc/hex.h" |
| 10 | +#include "session/blinding.hpp" |
| 11 | +#include "session/config/user_profile.hpp" |
| 12 | +#include "session/platform.hpp" |
| 13 | +#include "session/random.hpp" |
| 14 | + |
| 15 | +namespace session::nodeapi { |
| 16 | + |
| 17 | +class BlindingWrapper : public Napi::ObjectWrap<BlindingWrapper> { |
| 18 | + |
| 19 | + public: |
| 20 | + BlindingWrapper(const Napi::CallbackInfo& info) : Napi::ObjectWrap<BlindingWrapper>{info} { |
| 21 | + throw std::invalid_argument( |
| 22 | + "BlindingWrapper is all static and don't need to be constructed"); |
| 23 | + } |
| 24 | + |
| 25 | + static void Init(Napi::Env env, Napi::Object exports) { |
| 26 | + MetaBaseWrapper::NoBaseClassInitHelper<BlindingWrapper>( |
| 27 | + env, |
| 28 | + exports, |
| 29 | + "BlindingWrapperNode", |
| 30 | + { |
| 31 | + StaticMethod<&BlindingWrapper::blindVersionPubkey>( |
| 32 | + "blindVersionPubkey", |
| 33 | + static_cast<napi_property_attributes>( |
| 34 | + napi_writable | napi_configurable)), |
| 35 | + StaticMethod<&BlindingWrapper::blindVersionSign>( |
| 36 | + "blindVersionSign", |
| 37 | + static_cast<napi_property_attributes>( |
| 38 | + napi_writable | napi_configurable)), |
| 39 | + |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + private: |
| 44 | + static Napi::Value blindVersionPubkey(const Napi::CallbackInfo& info) { |
| 45 | + return wrapResult(info, [&] { |
| 46 | + assertInfoLength(info, 1); |
| 47 | + assertIsObject(info[0]); |
| 48 | + auto obj = info[0].As<Napi::Object>(); |
| 49 | + |
| 50 | + if (obj.IsEmpty()) |
| 51 | + throw std::invalid_argument("blindVersionPubkey received empty"); |
| 52 | + |
| 53 | + assertIsUInt8Array(obj.Get("ed25519SecretKey")); |
| 54 | + auto ed25519_secret_key = |
| 55 | + toCppBuffer(obj.Get("ed25519SecretKey"), "blindVersionPubkey.ed25519SecretKey"); |
| 56 | + |
| 57 | + auto keypair = session::blind_version_key_pair(ed25519_secret_key); |
| 58 | + session::uc32 pk_arr = std::get<0>(keypair); |
| 59 | + ustring blinded_pk = session::ustring( |
| 60 | + session::to_unsigned_sv(std::string(pk_arr.begin(), pk_arr.end()))); |
| 61 | + std::string blinded_pk_hex; |
| 62 | + blinded_pk_hex.reserve(66); |
| 63 | + blinded_pk_hex += "07"; |
| 64 | + oxenc::to_hex(blinded_pk.begin(), blinded_pk.end(), std::back_inserter(blinded_pk_hex)); |
| 65 | + |
| 66 | + return blinded_pk_hex; |
| 67 | + }); |
| 68 | + }; |
| 69 | + |
| 70 | + static Napi::Value blindVersionSign(const Napi::CallbackInfo& info) { |
| 71 | + return wrapResult(info, [&] { |
| 72 | + assertInfoLength(info, 1); |
| 73 | + assertIsObject(info[0]); |
| 74 | + auto obj = info[0].As<Napi::Object>(); |
| 75 | + |
| 76 | + if (obj.IsEmpty()) |
| 77 | + throw std::invalid_argument("blindVersionSign received empty"); |
| 78 | + |
| 79 | + assertIsUInt8Array(obj.Get("ed25519SecretKey")); |
| 80 | + auto ed25519_secret_key = |
| 81 | + toCppBuffer(obj.Get("ed25519SecretKey"), "blindVersionSign.ed25519SecretKey"); |
| 82 | + |
| 83 | + assertIsNumber(obj.Get("sigTimestampSeconds")); |
| 84 | + auto sig_timestamp = toCppInteger( |
| 85 | + obj.Get("sigTimestampSeconds"), "blindVersionSign.sigTimestampSeconds", false); |
| 86 | + |
| 87 | + return session::blind_version_sign( |
| 88 | + ed25519_secret_key, Platform::desktop, sig_timestamp); |
| 89 | + }); |
| 90 | + }; |
| 91 | +}; |
| 92 | + |
| 93 | +} // namespace session::nodeapi |
0 commit comments