zano-native 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/CHANGELOG.md +90 -0
  2. package/LICENSE +27 -0
  3. package/README.md +24 -0
  4. package/android/build.gradle +40 -0
  5. package/android/src/main/java/app/edge/rnzano/RnZanoModule.java +73 -0
  6. package/android/src/main/java/app/edge/rnzano/RnZanoPackage.java +21 -0
  7. package/android/src/main/jniLibs/arm64-v8a/librnzano.so +0 -0
  8. package/ios/ZanoModule.h +4 -0
  9. package/ios/ZanoModule.mm +138 -0
  10. package/ios/ZanoModule.xcframework/Info.plist +43 -0
  11. package/ios/ZanoModule.xcframework/ios-arm64/libzano-module.a +0 -0
  12. package/ios/ZanoModule.xcframework/ios-arm64-simulator/libzano-module.a +0 -0
  13. package/ios/react-native-zano.xcodeproj/project.pbxproj +1 -0
  14. package/lib/scripts/build-native-host.d.ts +1 -0
  15. package/lib/scripts/build-native-host.js +175 -0
  16. package/lib/scripts/smoke-node.d.ts +1 -0
  17. package/lib/scripts/smoke-node.js +33 -0
  18. package/lib/scripts/update-sources.d.ts +1 -0
  19. package/lib/scripts/update-sources.js +412 -0
  20. package/lib/scripts/utils/android-tools.d.ts +1 -0
  21. package/lib/scripts/utils/android-tools.js +25 -0
  22. package/lib/scripts/utils/closeWalletPatch.d.ts +23 -0
  23. package/lib/scripts/utils/closeWalletPatch.js +215 -0
  24. package/lib/scripts/utils/common.d.ts +37 -0
  25. package/lib/scripts/utils/common.js +186 -0
  26. package/lib/scripts/utils/ios-tools.d.ts +8 -0
  27. package/lib/scripts/utils/ios-tools.js +26 -0
  28. package/lib/scripts/utils/sdkFolders.d.ts +27 -0
  29. package/lib/scripts/utils/sdkFolders.js +43 -0
  30. package/lib/src/CppBridge.d.ts +142 -0
  31. package/lib/src/CppBridge.js +668 -0
  32. package/lib/src/index.d.ts +4 -0
  33. package/lib/src/index.js +28 -0
  34. package/lib/src/load-addon.d.ts +5 -0
  35. package/lib/src/load-addon.js +50 -0
  36. package/lib/src/node.d.ts +11 -0
  37. package/lib/src/node.js +25 -0
  38. package/lib/src/types.d.ts +292 -0
  39. package/lib/src/types.js +40 -0
  40. package/lib/src/walletFilePassword.d.ts +14 -0
  41. package/lib/src/walletFilePassword.js +74 -0
  42. package/node.d.ts +5 -0
  43. package/node.js +2 -0
  44. package/package.json +105 -0
  45. package/prebuilds/darwin-arm64/zano.node +0 -0
  46. package/src/node/zano-napi.cpp +176 -0
  47. package/src/zano-wrapper/zano-methods.hpp +15 -0
  48. package/zano-native.podspec +27 -0
@@ -0,0 +1,176 @@
1
+ #include <napi.h>
2
+
3
+ #include "zano-methods.hpp"
4
+
5
+ #include <condition_variable>
6
+ #include <functional>
7
+ #include <mutex>
8
+ #include <queue>
9
+ #include <stdexcept>
10
+ #include <string>
11
+ #include <thread>
12
+ #include <utility>
13
+ #include <vector>
14
+
15
+ namespace {
16
+
17
+ struct Job {
18
+ std::string method;
19
+ std::vector<std::string> args;
20
+ std::function<void(std::string result, std::string error)> complete;
21
+ };
22
+
23
+ class SerialExecutor {
24
+ public:
25
+ SerialExecutor() { thread_ = std::thread([this] { run(); }); }
26
+
27
+ ~SerialExecutor() {
28
+ {
29
+ std::lock_guard<std::mutex> lock(mu_);
30
+ stop_ = true;
31
+ }
32
+ cv_.notify_all();
33
+ if (thread_.joinable()) thread_.join();
34
+ }
35
+
36
+ void enqueue(Job job) {
37
+ {
38
+ std::lock_guard<std::mutex> lock(mu_);
39
+ jobs_.push(std::move(job));
40
+ }
41
+ cv_.notify_one();
42
+ }
43
+
44
+ private:
45
+ static std::string dispatch(
46
+ const std::string &method,
47
+ const std::vector<std::string> &args
48
+ ) {
49
+ for (unsigned i = 0; i < zanoMethodCount; ++i) {
50
+ if (zanoMethods[i].name != method) continue;
51
+ if (
52
+ zanoMethods[i].argc != -1 &&
53
+ static_cast<int>(args.size()) != zanoMethods[i].argc
54
+ ) {
55
+ throw std::runtime_error("zano incorrect C++ argument count");
56
+ }
57
+ return zanoMethods[i].method(args);
58
+ }
59
+ throw std::runtime_error("No zano C++ method " + method);
60
+ }
61
+
62
+ void run() {
63
+ while (true) {
64
+ Job job;
65
+ {
66
+ std::unique_lock<std::mutex> lock(mu_);
67
+ cv_.wait(lock, [&] { return stop_ || !jobs_.empty(); });
68
+ if (stop_ && jobs_.empty()) return;
69
+ job = std::move(jobs_.front());
70
+ jobs_.pop();
71
+ }
72
+ try {
73
+ job.complete(dispatch(job.method, job.args), "");
74
+ } catch (const std::exception &e) {
75
+ job.complete("", e.what());
76
+ } catch (...) {
77
+ job.complete("", "zano threw a C++ exception");
78
+ }
79
+ }
80
+ }
81
+
82
+ std::mutex mu_;
83
+ std::condition_variable cv_;
84
+ std::queue<Job> jobs_;
85
+ std::thread thread_;
86
+ bool stop_ = false;
87
+ };
88
+
89
+ SerialExecutor *g_queue = nullptr;
90
+
91
+ struct CallResult {
92
+ Napi::Promise::Deferred deferred;
93
+ std::string value;
94
+ std::string error;
95
+ Napi::ThreadSafeFunction tsfn;
96
+
97
+ CallResult(Napi::Promise::Deferred deferred_, Napi::ThreadSafeFunction tsfn_)
98
+ : deferred(deferred_), tsfn(tsfn_) {}
99
+ };
100
+
101
+ } // namespace
102
+
103
+ static Napi::Value CallZano(const Napi::CallbackInfo &info) {
104
+ Napi::Env env = info.Env();
105
+ if (info.Length() < 2 || !info[0].IsString() || !info[1].IsArray()) {
106
+ Napi::TypeError::New(env, "callZano(method, string[]) expected")
107
+ .ThrowAsJavaScriptException();
108
+ return env.Undefined();
109
+ }
110
+
111
+ const std::string method = info[0].As<Napi::String>().Utf8Value();
112
+ const Napi::Array arr = info[1].As<Napi::Array>();
113
+ std::vector<std::string> args;
114
+ args.reserve(arr.Length());
115
+ for (uint32_t i = 0; i < arr.Length(); ++i) {
116
+ Napi::Value value = arr.Get(i);
117
+ if (!value.IsString()) {
118
+ Napi::TypeError::New(env, "callZano arguments must be strings")
119
+ .ThrowAsJavaScriptException();
120
+ return env.Undefined();
121
+ }
122
+ args.push_back(value.As<Napi::String>().Utf8Value());
123
+ }
124
+
125
+ auto deferred = Napi::Promise::Deferred::New(env);
126
+ Napi::ThreadSafeFunction tsfn = Napi::ThreadSafeFunction::New(
127
+ env,
128
+ Napi::Function::New(env, [](const Napi::CallbackInfo &) {}),
129
+ "zano-call-complete",
130
+ 0,
131
+ 1
132
+ );
133
+
134
+ auto *result = new CallResult(deferred, tsfn);
135
+
136
+ g_queue->enqueue(
137
+ Job{
138
+ method,
139
+ std::move(args),
140
+ [result](std::string value, std::string error) {
141
+ result->value = std::move(value);
142
+ result->error = std::move(error);
143
+ result->tsfn.BlockingCall(result, [](Napi::Env env, Napi::Function, CallResult *r) {
144
+ if (r->error.empty()) {
145
+ r->deferred.Resolve(Napi::String::New(env, r->value));
146
+ } else {
147
+ r->deferred.Reject(Napi::Error::New(env, r->error).Value());
148
+ }
149
+ r->tsfn.Release();
150
+ delete r;
151
+ });
152
+ }
153
+ }
154
+ );
155
+
156
+ return deferred.Promise();
157
+ }
158
+
159
+ static Napi::Value GetMethodNames(const Napi::CallbackInfo &info) {
160
+ Napi::Env env = info.Env();
161
+ Napi::Array out = Napi::Array::New(env, zanoMethodCount);
162
+ for (unsigned i = 0; i < zanoMethodCount; ++i) {
163
+ out.Set(i, Napi::String::New(env, zanoMethods[i].name));
164
+ }
165
+ return out;
166
+ }
167
+
168
+ static Napi::Object Init(Napi::Env env, Napi::Object exports) {
169
+ if (g_queue == nullptr) g_queue = new SerialExecutor();
170
+
171
+ exports.Set("callZano", Napi::Function::New(env, CallZano));
172
+ exports.Set("methodNames", Napi::Function::New(env, GetMethodNames));
173
+ return exports;
174
+ }
175
+
176
+ NODE_API_MODULE(zano, Init)
@@ -0,0 +1,15 @@
1
+ #ifndef ZANO_METHODS_HPP_INCLUDED
2
+ #define ZANO_METHODS_HPP_INCLUDED
3
+
4
+ #include <string>
5
+ #include <vector>
6
+
7
+ struct ZanoMethod {
8
+ const char *name;
9
+ int argc;
10
+ std::string (*method)(const std::vector<std::string> &args);
11
+ };
12
+ extern const ZanoMethod zanoMethods[];
13
+ extern const unsigned zanoMethodCount;
14
+
15
+ #endif
@@ -0,0 +1,27 @@
1
+ require "json"
2
+
3
+ package = JSON.parse(File.read(File.join(__dir__, "package.json")))
4
+
5
+ Pod::Spec.new do |s|
6
+ s.name = package['name']
7
+ s.version = package['version']
8
+ s.summary = package['description']
9
+ s.homepage = package['homepage']
10
+ s.license = package['license']
11
+ s.authors = package['author']
12
+
13
+ s.platform = :ios, "13.0"
14
+ s.requires_arc = true
15
+ s.source = {
16
+ :git => "https://github.com/EdgeApp/zano-native.git",
17
+ :tag => "v#{s.version}"
18
+ }
19
+ s.source_files =
20
+ "ios/ZanoModule.h",
21
+ "ios/ZanoModule.mm",
22
+ "src/zano-wrapper/zano-methods.hpp"
23
+ s.vendored_frameworks = "ios/ZanoModule.xcframework"
24
+
25
+ s.dependency "React-Core"
26
+ s.dependency "OpenSSL-Universal"
27
+ end