Skip to content

Commit b7891f0

Browse files
author
cloudnoize
committed
Add public transfer command
1 parent d9ee55e commit b7891f0

File tree

4 files changed

+89
-18
lines changed

4 files changed

+89
-18
lines changed

utt/wallet-cli/include/wallet.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ class Wallet {
5555
/// @param recipient The user id of the recipient
5656
void transfer(Channel& chan, uint64_t amount, const std::string& recipient);
5757

58+
/// @brief Transfer public (ERC20) funds from wallet to address.
59+
/// @param amount The amount of public funds to transfer
60+
/// @param recipient The user id of the recipient
61+
void publicTransfer(Channel& chan, uint64_t amount, const std::string& recipient);
62+
5863
/// @brief Burns the desired amount of private funds and converts them to public funds.
5964
/// @param amount The amount of private funds to burn.
6065
void burn(Channel& chan, uint64_t amount);
@@ -67,6 +72,9 @@ class Wallet {
6772
void syncState(Channel& chan, uint64_t lastKnownTxNum = 0);
6873
void updateBudget(Channel& chan);
6974

75+
/// @brief Updates the ERC20 balance of the wallet.
76+
void updatePublicBalance(Channel& chan);
77+
7078
struct DummyUserStorage : public utt::client::IUserStorage {};
7179

7280
DummyUserStorage storage_;

utt/wallet-cli/proto/api/v1/wallet-api.proto

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ message GetPublicBalanceResponse {
120120
optional uint64 public_balance = 2;
121121
}
122122

123+
message PublicTransferRequest {
124+
optional string user_id = 1;
125+
optional string recipient = 2;
126+
optional uint64 value = 3;
127+
}
128+
129+
message PublicTransferResponse {
130+
optional string err = 1;
131+
}
132+
133+
123134
message WalletRequest {
124135
oneof req {
125136
// init
@@ -135,6 +146,8 @@ message WalletRequest {
135146
GetSignedTransactionRequest get_signed_tx = 8;
136147

137148
GetPrivacyBudgetRequest get_privacy_budget = 9;
149+
150+
PublicTransferRequest public_transfer = 10;
138151
}
139152
}
140153

@@ -154,5 +167,7 @@ message WalletResponse {
154167
GetSignedTransactionResponse get_signed_tx = 9;
155168

156169
GetPrivacyBudgetResponse get_privacy_budget = 10;
170+
171+
PublicTransferResponse public_transfer = 11;
157172
}
158173
}

utt/wallet-cli/src/main.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ void printHelp() {
5656
std::cout << "register <user-id> -- requests user registration required for spending coins\n";
5757
std::cout << "mint <amount> -- mint the requested amount of public funds.\n";
5858
std::cout << "transfer <amount> <to-user-id> -- transfers the specified amount between users.\n";
59+
std::cout
60+
<< "public-transfer <amount> <to-user-id> -- transfers the specified amount of public funds between users.\n";
5961
std::cout << "burn <amount> -- burns the specified amount of private funds to public funds.\n";
6062
std::cout << '\n';
6163
}
@@ -137,6 +139,20 @@ struct CLIApp {
137139
wallet->showInfo(chan);
138140
}
139141

142+
void publicTransferCmd(const std::vector<std::string>& cmdTokens) {
143+
if (cmdTokens.size() != 3) {
144+
std::cout << "Usage: transfer <amount> <recipient>\n";
145+
return;
146+
}
147+
int amount = std::atoi(cmdTokens[1].c_str());
148+
if (amount <= 0) {
149+
std::cout << "Expected a positive transfer amount!\n";
150+
return;
151+
}
152+
wallet->publicTransfer(chan, (uint64_t)amount, cmdTokens[2]);
153+
wallet->showInfo(chan);
154+
}
155+
140156
void burnCmd(const std::vector<std::string>& cmdTokens) {
141157
if (cmdTokens.size() != 2) {
142158
std::cout << "Usage: burn <amount>\n";
@@ -215,6 +231,8 @@ int main(int argc, char* argv[]) {
215231
app.mintCmd(cmdTokens);
216232
} else if (cmdTokens[0] == "transfer") {
217233
app.transferCmd(cmdTokens);
234+
} else if (cmdTokens[0] == "public-transfer") {
235+
app.publicTransferCmd(cmdTokens);
218236
} else if (cmdTokens[0] == "burn") {
219237
app.burnCmd(cmdTokens);
220238
} else if (cmdTokens[0] == "debug") {

utt/wallet-cli/src/wallet.cpp

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,35 @@ void Wallet::transfer(Channel& chan, uint64_t amount, const std::string& recipie
196196
std::cout << "Anonymous transfer done.\n";
197197
}
198198

199+
void Wallet::publicTransfer(Channel& chan, uint64_t amount, const std::string& recipient) {
200+
updatePublicBalance(chan);
201+
if (publicBalance_ < amount) {
202+
std::cout << "Insufficient public balance!\n";
203+
return;
204+
}
205+
std::cout << "Processing public transfer of " << amount << " to " << recipient << "...\n";
206+
207+
WalletRequest req;
208+
auto& publicTrnasfer = *req.mutable_public_transfer();
209+
publicTrnasfer.set_user_id(userId_);
210+
publicTrnasfer.set_recipient(recipient);
211+
publicTrnasfer.set_value(amount);
212+
chan->Write(req);
213+
214+
WalletResponse resp;
215+
chan->Read(&resp);
216+
217+
if (!resp.has_public_transfer()) throw std::runtime_error("Expected public_transfer response from wallet service!");
218+
const auto& publicTransferResp = resp.public_transfer();
219+
220+
if (publicTransferResp.has_err()) {
221+
std::cout << publicTransferResp.err() << "\n";
222+
return;
223+
}
224+
225+
std::cout << "Public transfer done.\n";
226+
}
227+
199228
void Wallet::burn(Channel& chan, uint64_t amount) {
200229
if (user_->getBalance() < amount) {
201230
std::cout << "Insufficient private balance!\n";
@@ -279,27 +308,28 @@ void Wallet::updateBudget(Channel& chan) {
279308
user_->updatePrivacyBudget(budget, sig);
280309
}
281310

282-
void Wallet::syncState(Channel& chan, uint64_t lastKnownTxNum) {
283-
std::cout << "Synchronizing state...\n";
284-
285-
// Update public balance
286-
{
287-
WalletRequest req;
288-
req.mutable_get_public_balance()->set_user_id(userId_);
289-
chan->Write(req);
311+
void Wallet::updatePublicBalance(Channel& chan) {
312+
WalletRequest req;
313+
req.mutable_get_public_balance()->set_user_id(userId_);
314+
chan->Write(req);
290315

291-
WalletResponse resp;
292-
chan->Read(&resp);
293-
if (!resp.has_get_public_balance())
294-
throw std::runtime_error("Expected get public balance response from wallet service!");
295-
const auto& getPubBalanceResp = resp.get_public_balance();
316+
WalletResponse resp;
317+
chan->Read(&resp);
318+
if (!resp.has_get_public_balance())
319+
throw std::runtime_error("Expected get public balance response from wallet service!");
320+
const auto& getPubBalanceResp = resp.get_public_balance();
296321

297-
if (getPubBalanceResp.has_err()) {
298-
std::cout << "Failed to get public balance:" << resp.err() << '\n';
299-
} else {
300-
publicBalance_ = getPubBalanceResp.public_balance();
301-
}
322+
if (getPubBalanceResp.has_err()) {
323+
std::cout << "Failed to get public balance:" << resp.err() << '\n';
324+
} else {
325+
publicBalance_ = getPubBalanceResp.public_balance();
302326
}
327+
}
328+
329+
void Wallet::syncState(Channel& chan, uint64_t lastKnownTxNum) {
330+
std::cout << "Synchronizing state...\n";
331+
332+
updatePublicBalance(chan);
303333

304334
updateBudget(chan);
305335

0 commit comments

Comments
 (0)