Loading...
Searching...
No Matches
utxo_client_tools.cpp
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#include <glog/logging.h>
21#include <google/protobuf/util/json_util.h>
22
26
27using google::protobuf::util::MessageToJsonString;
30using resdb::utxo::UTXO;
32using resdb::utxo::UTXOIn;
33using resdb::utxo::UTXOOut;
34
35void ShowUsage() {
36 printf(
37 "<cmd> -c <config> -m <caller address> -n <contract name> -p <contact "
38 "path> -a <params> \n");
39 exit(0);
40}
41
42void Transfer(UTXOClient* client, int64_t transaction_id,
43 const std::string& address,
44 const std::vector<std::string>& to_address,
45 const std::vector<int64_t>& values,
46 const std::string& private_key,
47 const std::vector<std::string>& to_pub_key) {
48 if (private_key.empty() || to_pub_key.empty()) {
49 printf("no private key or public key\n");
50 return;
51 }
52 UTXO utxo;
53 int64_t nonce = 0;
54 UTXOIn* in = utxo.add_in();
55 in->set_prev_id(transaction_id);
56 in->set_out_idx(0);
57 nonce += transaction_id;
58
59 for (size_t i = 0; i < to_address.size(); ++i) {
60 UTXOOut* out = utxo.add_out();
61 out->set_address(to_address[i]);
62 out->set_value(values[i]);
63 out->set_pub_key(to_pub_key[i]);
64 utxo.set_address(address);
66 private_key, address + std::to_string(nonce)));
67 LOG(ERROR) << "transfer from:" << address << " to:" << to_address[i]
68 << " value:" << values[i];
69 }
70
71 auto output = client->Transfer(utxo);
72 LOG(ERROR) << "execute result:\n" << output;
73}
74
75void GetList(UTXOClient* client, int64_t end_id, int num) {
76 std::vector<UTXO> list = client->GetList(end_id, num);
77 LOG(ERROR) << "get utxo:";
78 for (const UTXO& utxo : list) {
79 std::string str;
80 MessageToJsonString(utxo, &str);
81 printf("%s\n", str.c_str());
82 }
83}
84
85void GetWallet(UTXOClient* client, const std::string& address) {
86 int64_t ret = client->GetWallet(address);
87 LOG(ERROR) << "address:" << address << " get wallet value:" << ret;
88}
89
90std::vector<std::string> ParseString(std::string str) {
91 std::vector<std::string> ret;
92 while (true) {
93 size_t pos = str.find(",");
94 if (pos == std::string::npos) {
95 ret.push_back(str);
96 break;
97 }
98 ret.push_back(str.substr(0, pos));
99 str = str.substr(pos + 1);
100 }
101 return ret;
102}
103
104std::vector<int64_t> ParseValue(std::string str) {
105 std::vector<int64_t> ret;
106 while (true) {
107 size_t pos = str.find(",");
108 if (pos == std::string::npos) {
109 ret.push_back(strtoull(str.c_str(), NULL, 10));
110 break;
111 }
112 ret.push_back(strtoull(str.substr(0, pos).c_str(), NULL, 10));
113 str = str.substr(pos + 1);
114 }
115 return ret;
116}
117
118int main(int argc, char** argv) {
119 if (argc < 3) {
120 printf("-d <cmd> -c [config]\n");
121 return 0;
122 }
123
124 int64_t transaction_id = 0;
125 std::string address, to_address, params, contract_address, func_name,
126 private_key, to_pub_key;
127 int64_t end_id = 0;
128 int num = 10;
129 int c;
130 std::string cmd;
131 std::string value;
132 std::string client_config_file;
133 while ((c = getopt(argc, argv, "c:d:t:x:m:h:e:v:n:p:b:")) != -1) {
134 switch (c) {
135 case 'c':
136 client_config_file = optarg;
137 break;
138 case 'd':
139 address = optarg;
140 break;
141 case 't':
142 to_address = optarg;
143 break;
144 case 'x':
145 transaction_id = strtoull(optarg, NULL, 10);
146 break;
147 case 'm':
148 cmd = optarg;
149 break;
150 case 'e':
151 end_id = strtoull(optarg, NULL, 10);
152 break;
153 case 'n':
154 num = strtoull(optarg, NULL, 10);
155 break;
156 case 'v':
157 value = optarg;
158 break;
159 case 'p':
160 private_key = optarg;
161 break;
162 case 'b':
163 to_pub_key = optarg;
164 break;
165 case 'h':
166 ShowUsage();
167 break;
168 }
169 }
170
171 ResDBConfig config = GenerateResDBConfig(client_config_file);
172 config.SetClientTimeoutMs(100000);
173 UTXOClient client(config);
174 if (cmd == "transfer") {
175 Transfer(&client, transaction_id, address, ParseString(to_address),
176 ParseValue(value), private_key, ParseString(to_pub_key));
177 } else if (cmd == "list") {
178 GetList(&client, end_id, num);
179 } else if (cmd == "wallet") {
180 GetWallet(&client, to_address);
181 }
182}
void SetClientTimeoutMs(int timeout_ms)
std::vector< UTXO > GetList(int64_t end_id, int num)
int Transfer(const UTXO &utxo)
int64_t GetWallet(const std::string &address)
std::string ECDSASignString(const std::string &private_key, const std::string &message)
std::unique_ptr< ResDBConfig > GenerateResDBConfig(const std::string &config_file, const std::string &private_key_file, const std::string &cert_file, std::optional< ReplicaInfo > self_info, std::optional< ConfigGenFunc > gen_func)
void GetList(UTXOClient *client, int64_t end_id, int num)
void GetWallet(UTXOClient *client, const std::string &address)
int main(int argc, char **argv)
std::vector< std::string > ParseString(std::string str)
void ShowUsage()
void Transfer(UTXOClient *client, int64_t transaction_id, const std::string &address, const std::vector< std::string > &to_address, const std::vector< int64_t > &values, const std::string &private_key, const std::vector< std::string > &to_pub_key)
std::vector< int64_t > ParseValue(std::string str)