11#pragma once
22
33#include < napi.h>
4+ #include < oxenc/hex.h>
45
56#include < algorithm>
67#include < span>
1415
1516namespace session ::nodeapi {
1617
18+
1719class MultiEncryptWrapper : public Napi ::ObjectWrap<MultiEncryptWrapper> {
1820 public:
1921 MultiEncryptWrapper (const Napi::CallbackInfo& info) :
@@ -36,6 +38,7 @@ class MultiEncryptWrapper : public Napi::ObjectWrap<MultiEncryptWrapper> {
3638 " multiDecryptEd25519" ,
3739 static_cast <napi_property_attributes>(
3840 napi_writable | napi_configurable)),
41+ // Attachments encrypt/decrypt
3942 StaticMethod<&MultiEncryptWrapper::attachmentDecrypt>(
4043 " attachmentDecrypt" ,
4144 static_cast <napi_property_attributes>(
@@ -44,6 +47,25 @@ class MultiEncryptWrapper : public Napi::ObjectWrap<MultiEncryptWrapper> {
4447 " attachmentEncrypt" ,
4548 static_cast <napi_property_attributes>(
4649 napi_writable | napi_configurable)),
50+
51+ // Destination encrypt
52+ StaticMethod<&MultiEncryptWrapper::encryptFor1o1>(
53+ " encryptFor1o1" ,
54+ static_cast <napi_property_attributes>(
55+ napi_writable | napi_configurable)),
56+
57+ // StaticMethod<&MultiEncryptWrapper::encryptForCommunity>(
58+ // "encryptForCommunity",
59+ // static_cast<napi_property_attributes>(
60+ // napi_writable | napi_configurable)),
61+ // StaticMethod<&MultiEncryptWrapper::encryptForCommunityInbox>(
62+ // "encryptForCommunityInbox",
63+ // static_cast<napi_property_attributes>(
64+ // napi_writable | napi_configurable)),
65+ // StaticMethod<&MultiEncryptWrapper::encryptForGroup>(
66+ // "encryptForGroup",
67+ // static_cast<napi_property_attributes>(
68+ // napi_writable | napi_configurable)),
4769 });
4870 }
4971
@@ -181,6 +203,69 @@ class MultiEncryptWrapper : public Napi::ObjectWrap<MultiEncryptWrapper> {
181203 });
182204 };
183205
206+ static Napi::Value encryptFor1o1 (const Napi::CallbackInfo& info) {
207+ return wrapResult (info, [&] {
208+ // we expect an single argument which is an array of objects with the following
209+ // properties:
210+ // {
211+ // "plaintext": Uint8Array,
212+ // "sentTimestampMs": Number,
213+ // "ed25519Privkey": Hexstring,
214+ // "recipientPubkey": Hexstring,
215+ // "proRotatingEd25519Privkey": Hexstring | null,
216+ // }
217+ //
218+
219+ assertInfoLength (info, 1 );
220+ assertIsArray (info[0 ], " encryptFor1o1 info[0]" );
221+
222+ auto array = info[0 ].As <Napi::Array>();
223+
224+ if (array.IsEmpty ())
225+ throw std::invalid_argument (" encryptFor1o1 received empty" );
226+
227+ std::vector<std::vector<uint8_t >> ready_to_send (array.Length ());
228+ for (uint32_t i = 0 ; i < array.Length (); i++) {
229+ auto itemValue = array.Get (i);
230+ if (!itemValue.IsObject ()) {
231+ throw std::invalid_argument (" encryptFor1o1 itemValue is not an object" );
232+ }
233+ auto obj = itemValue.As <Napi::Object>();
234+
235+ assertIsUInt8Array (obj.Get (" plaintext" ), " encryptFor1o1.obj.message" );
236+ auto plaintext = toCppBuffer (obj.Get (" plaintext" ), " encryptFor1o1.obj.message" );
237+
238+ assertIsNumber (obj.Get (" sentTimestampMs" ), " encryptFor1o1.obj.sentTimestampMs" );
239+ auto sentTimestampMs =
240+ toCppMs (obj.Get (" sentTimestampMs" ), " encryptFor1o1.obj.sentTimestampMs" );
241+
242+ assertIsString (obj.Get (" ed25519Privkey" ));
243+ auto ed25519PrivkeyHex =
244+ toCppString (obj.Get (" ed25519Privkey" ), " encryptFor1o1.obj.ed25519Privkey" );
245+
246+ assertIsString (obj.Get (" recipientPubkey" ));
247+ auto recipientPubkeyHex = toCppString (
248+ obj.Get (" recipientPubkey" ), " encryptFor1o1.obj.recipientPubkey" );
249+
250+ assertIsStringOrNull (obj.Get (" proRotatingEd25519Privkey" ));
251+ auto proRotatingEd25519PrivkeyHex = maybeNonemptyString (
252+ obj.Get (" proRotatingEd25519Privkey" ),
253+ " encryptFor1o1.obj.proRotatingEd25519Privkey" );
254+ ready_to_send[i] = session::encode_for_1o1 (
255+ plaintext,
256+ from_hex_to_span (ed25519PrivkeyHex),
257+ sentTimestampMs,
258+ from_hex_to_array<33 >(recipientPubkeyHex),
259+ from_hex_to_span (proRotatingEd25519PrivkeyHex.value_or (" " )));
260+ }
261+
262+ auto ret = Napi::Object::New (info.Env ());
263+ ret.Set (" encryptedData" , toJs (info.Env (), ready_to_send));
264+
265+ return ret;
266+ });
267+ };
268+
184269 static Napi::Value attachmentDecrypt (const Napi::CallbackInfo& info) {
185270 return wrapResult (info, [&] {
186271 assertInfoLength (info, 1 );
0 commit comments