Skip to content

Commit 37c750c

Browse files
committed
Add all function wallet.
Signed-off-by: Markuu-s <[email protected]>
1 parent 03fd9bd commit 37c750c

File tree

8 files changed

+881
-0
lines changed

8 files changed

+881
-0
lines changed

CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ include_directories(
113113
${PROJECT_SOURCE_DIR}/libs
114114
)
115115

116+
<<<<<<< Updated upstream
116117
if (BUILD_INTERNAL_DEPS)
117118
include_directories(
118119
SYSTEM
@@ -122,6 +123,16 @@ if (BUILD_INTERNAL_DEPS)
122123
)
123124
endif()
124125

126+
=======
127+
include_directories(
128+
SYSTEM
129+
# system includes
130+
deps/indicators/include
131+
deps/libsecp256k1/include
132+
)
133+
134+
add_subdirectory(core)
135+
>>>>>>> Stashed changes
125136
add_subdirectory(libs)
126137
add_subdirectory(core)
127138

core/api/wallet/local_wallet.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ namespace fc::api {
6565
}
6666
return std::move(address);
6767
}};
68+
api->WalletList = [=]() -> outcome::result<std::vector<Address>> {
69+
70+
};
6871
api->WalletSetDefault = [=](auto &address) -> outcome::result<void> {
6972
wallet_default_address->setCbor(address);
7073
return outcome::success();

core/api/wallet/wallet_api.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ namespace fc::api {
5151
const Address &,
5252
const Bytes &,
5353
const Signature &)
54+
55+
API_METHOD(WalletList, jwt::kAdminPermission, std::vector<Address>)
56+
57+
API_METHOD(WalletDelete, jwt::kAdminPermission, void, const Address &)
5458
};
5559

5660
template <typename A, typename F>
@@ -63,6 +67,8 @@ namespace fc::api {
6367
f(a.WalletSetDefault);
6468
f(a.WalletSign);
6569
f(a.WalletVerify);
70+
f(a.WalletList);
71+
f(a.WalletDelete);
6672
}
6773

6874
} // namespace fc::api

core/cli/cli.hpp

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#pragma once
7+
8+
#include <boost/optional.hpp>
9+
#include <boost/program_options/options_description.hpp>
10+
#include <map>
11+
#include <memory>
12+
#include <typeindex>
13+
14+
#define CLI_TRY(valueName, maybeResult) \
15+
auto valueName##OUTCOME_TRY = maybeResult; \
16+
if (not valueName##OUTCOME_TRY.has_value()) throw std::invalid_argument(""); \
17+
auto valueName = valueName##OUTCOME_TRY.value();
18+
19+
#define CLI_TRY1(maybeResult) \
20+
auto valueName##OUTCOME_TRY = maybeResult; \
21+
if (not valueName##OUTCOME_TRY.has_value()) throw std::invalid_argument("");
22+
23+
#define CLI_TRY_TEXT(valueName, maybeResult, textError) \
24+
auto valueName##OUTCOME_TRY = maybeResult; \
25+
if (not valueName##OUTCOME_TRY.has_value()) \
26+
throw std::invalid_argument(textError); \
27+
auto valueName = valueName##OUTCOME_TRY.value();
28+
29+
#define CLI_TRY_TEXT1(maybeResult, textError) \
30+
auto valueName##OUTCOME_TRY = maybeResult; \
31+
if (not valueName##OUTCOME_TRY.has_value()) \
32+
throw std::invalid_argument(textError);
33+
34+
#define CLI_OPTS() ::fc::cli::Opts opts()
35+
#define CLI_RUN() \
36+
static ::fc::cli::RunResult run(const ::fc::cli::ArgsMap &argm, \
37+
const Args &args, \
38+
const ::fc::cli::Argv &argv)
39+
#define CLI_NO_RUN() constexpr static nullptr_t run{nullptr};
40+
41+
namespace fc::cli {
42+
namespace po = boost::program_options;
43+
using Opts = po::options_description;
44+
45+
using RunResult = void;
46+
struct ArgsMap {
47+
std::map<std::type_index, std::shared_ptr<void>> _;
48+
template <typename Args>
49+
void add(Args &&v) {
50+
_.emplace(typeid(Args), std::make_shared<Args>(std::forward<Args>(v)));
51+
}
52+
template <typename Cmd>
53+
const typename Cmd::Args &of() const {
54+
return *reinterpret_cast<const typename Cmd::Args *>(
55+
_.at(typeid(typename Cmd::Args)).get());
56+
}
57+
};
58+
// note: Args is defined inside command
59+
using Argv = std::vector<std::string>;
60+
61+
struct Empty {
62+
struct Args {
63+
CLI_OPTS() {
64+
return {};
65+
}
66+
};
67+
CLI_NO_RUN();
68+
};
69+
using Group = Empty;
70+
71+
struct ShowHelp {};
72+
73+
class TableWriter {
74+
struct Column {
75+
std::string name;
76+
bool separate_line;
77+
int lines = 0;
78+
};
79+
using Row = std::map<int, std::string>;
80+
81+
std::vector<Column> columns;
82+
std::vector<Row> rows;
83+
84+
public:
85+
static Column newColumn(const std::string &name) {
86+
Column column{.name = name, .separate_line = false};
87+
return column;
88+
}
89+
90+
static Column newLineColumn(const std::string &name) {
91+
Column column{.name = name, .separate_line = true};
92+
return column;
93+
}
94+
95+
explicit TableWriter(const std::vector<Column> &cols) {
96+
columns = cols;
97+
}
98+
99+
void write(std::map<std::string, std::string> range) {
100+
std::map<int, std::string> byColumnId;
101+
bool flag = false;
102+
103+
while (not flag) {
104+
for (auto &[columnName, value] : range) {
105+
for (size_t i = 0; i < columns.size(); ++i) {
106+
Column &column = columns[i];
107+
if (column.name == columnName) {
108+
byColumnId[i] = std::move(value);
109+
range.erase(columnName);
110+
column.lines++;
111+
flag = true;
112+
break;
113+
}
114+
}
115+
116+
if (flag) {
117+
break;
118+
}
119+
byColumnId[columns.size()] = std::move(value);
120+
range.erase(columnName);
121+
columns.push_back(
122+
Column{.name = columnName, .separate_line = false, .lines = 1});
123+
}
124+
flag = not flag;
125+
}
126+
127+
rows.push_back(byColumnId);
128+
}
129+
130+
void flush() {
131+
std::vector<int> colLenghts;
132+
colLenghts.reserve(columns.size());
133+
134+
std::map<int, std::string> header;
135+
for (size_t i = 0; i < columns.size(); ++i) {
136+
Column &column = columns[i];
137+
if (column.separate_line) {
138+
continue;
139+
}
140+
header[i] = column.name;
141+
}
142+
143+
rows.insert(rows.begin(), header);
144+
145+
for (size_t i = 0; i < columns.size(); ++i) {
146+
Column &column = columns[i];
147+
if (column.lines == 0) {
148+
continue;
149+
}
150+
151+
for (size_t j = 0; j < rows.size(); ++j) {
152+
Row &row = rows[j];
153+
if (row.find(i) == row.end()) {
154+
continue;
155+
}
156+
std::string value = row[i];
157+
if (value.size() > colLenghts[i]) {
158+
colLenghts[i] = value.size();
159+
}
160+
}
161+
}
162+
163+
for (Row &row : rows) {
164+
std::vector<std::string> cols;
165+
cols.reserve(cols.size());
166+
167+
for (int i = 0; i < columns.size(); ++i) {
168+
Column &column = columns[i];
169+
if (column.lines == 0) {
170+
continue;
171+
}
172+
173+
std::string e = row[i];
174+
int pad = colLenghts[i] - e.size() + 2;
175+
if (not column.separate_line && column.lines > 0) {
176+
e += std::string(pad, ' ');
177+
std::cout << e;
178+
}
179+
180+
cols.push_back(e);
181+
}
182+
183+
std::cout << '\n';
184+
185+
for (int i = 0; i < columns.size(); ++i) {
186+
Column &column = columns[i];
187+
if (not column.separate_line || cols[i].size() == 0) {
188+
continue;
189+
}
190+
191+
std::cout << " " << column.name << ": " << cols[i] << '\n';
192+
}
193+
}
194+
}
195+
};
196+
} // namespace fc::cli

core/cli/node/_tree.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#pragma once
7+
8+
#include "cli/node/client.hpp"
9+
#include "cli/node/net.hpp"
10+
#include "cli/node/wallet.hpp"
11+
#include "cli/tree.hpp"
12+
13+
namespace fc::cli::_node {
14+
const auto _tree{tree<Node>(
15+
{{"net",
16+
tree<Group>({
17+
{"listen", tree<Node_net_listen>()},
18+
})},
19+
{"client",
20+
tree<Group>({
21+
{"retrieve", tree<clientRetrive>()},
22+
})},
23+
{"wallet",
24+
tree<Group>({
25+
{"new", tree<walletNew>()}, // TODO DONE)))
26+
{"list", tree<walletList>()}, // TODO DONE)))
27+
{"balance", tree<walletBalance>()}, // TODO DONE)))
28+
{"default", tree<walletDefault>()}, // TODO DONE)))
29+
{"set-default", tree<walletSetDefault>()}, // TODO DONE)))
30+
{"import", tree<walletImport>()}, // TODO 20% StateGetActor
31+
{"sign", tree<walletSign>()}, // TODO DONE)))
32+
{"verify", tree<walletVerify>()}, // TODO DONE)))
33+
{"delete",
34+
tree<walletDelete>()}, // TODO DONE))) (p.S api._->WalletDelete add)
35+
{"market",
36+
tree<Group>({
37+
{"withdraw", tree<walletWithdraw>()}, // TODO 99% (p.S api._->MarketGetReserved add, api._->MarketWithdraw)
38+
{"add", tree<walletAdd>()}, // TODO DONE))) (p.S api._->MarketAddBalance add)
39+
})},
40+
})}})};
41+
} // namespace fc::cli::_node

core/cli/node/client.hpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// Created by Ruslan Gilvanov on 12.02.2022.
3+
4+
#pragma once
5+
#include "cli/node/node.hpp"
6+
7+
namespace fc::cli::_node {
8+
9+
struct clientRetrive {
10+
struct Args {
11+
bool car{};
12+
bool export_merkle_root{};
13+
std::string data_selector;
14+
15+
CLI_OPTS() {
16+
Opts opts;
17+
auto option{opts.add_options()};
18+
option("car",
19+
po::bool_switch(&car),
20+
"Export to a car file instead of a regular file");
21+
option("data-selector",
22+
po::value(&data_selector),
23+
"IPLD datamodel text-path selector, or IPLD json selector");
24+
option("car-export-merkle-proof",
25+
po::bool_switch(&export_merkle_root),
26+
"(requires --data-selector and --car) Export data-selector "
27+
"merkle proof");
28+
return opts;
29+
}
30+
};
31+
32+
CLI_RUN() {
33+
std::cout << args.data_selector << "\n";
34+
}
35+
};
36+
37+
} // namespace fc::cli::_node

0 commit comments

Comments
 (0)