Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utxo api #112

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion service/tools/utxo/service_tools/start_utxo_service.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
killall -9 utxo_server
killall -9 utxo_service

SERVER_PATH=./bazel-bin/service/utxo/utxo_service
SERVER_CONFIG=service/tools/config/server/server.config
Expand Down
62 changes: 47 additions & 15 deletions service/tools/utxo/wallet_tool/cpp/utxo_client_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ void ShowUsage() {
}

void Transfer(UTXOClient* client, int64_t transaction_id,
const std::string& address, const std::string& to_address,
const int value, const std::string& private_key,
const std::string& to_pub_key) {
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) {
if (private_key.empty() || to_pub_key.empty()) {
printf("no private key or public key\n");
return;
Expand All @@ -60,13 +61,16 @@ void Transfer(UTXOClient* client, int64_t transaction_id,
in->set_out_idx(0);
nonce += transaction_id;

UTXOOut* out = utxo.add_out();
out->set_address(to_address);
out->set_value(value);
out->set_pub_key(to_pub_key);
utxo.set_address(address);
utxo.set_sig(resdb::utils::ECDSASignString(private_key,
address + std::to_string(nonce)));
for(int i = 0; i < to_address.size();++i){
UTXOOut* out = utxo.add_out();
out->set_address(to_address[i]);
out->set_value(values[i]);
out->set_pub_key(to_pub_key[i]);
utxo.set_address(address);
utxo.set_sig(resdb::utils::ECDSASignString(private_key,
address + std::to_string(nonce)));
LOG(ERROR)<<"transfer from:"<<address<<" to:"<<to_address[i]<<" value:"<<values[i];
}

auto output = client->Transfer(utxo);
LOG(ERROR) << "execute result:\n" << output;
Expand All @@ -87,6 +91,35 @@ void GetWallet(UTXOClient* client, const std::string& address) {
LOG(ERROR) << "address:" << address << " get wallet value:" << ret;
}

std::vector<std::string> ParseString(std::string str){
std::vector<std::string> ret;
while(true){
size_t pos = str.find(",");
if(pos == std::string::npos){
ret.push_back(str);
break;
}
ret.push_back(str.substr(0,pos));
str = str.substr(pos+1);
}
return ret;
}

std::vector<int64_t> ParseValue(std::string str){
std::vector<int64_t> ret;
while(true){
size_t pos = str.find(",");
if(pos == std::string::npos){
ret.push_back(strtoull(str.c_str(), NULL, 10));
break;
}
ret.push_back(strtoull(str.substr(0,pos).c_str(), NULL, 10));
str = str.substr(pos+1);
}
return ret;
}


int main(int argc, char** argv) {
if (argc < 3) {
printf("-d <cmd> -c [config]\n");
Expand All @@ -100,7 +133,7 @@ int main(int argc, char** argv) {
int num = 10;
int c;
std::string cmd;
int64_t value = 0;
std::string value;
std::string client_config_file;
while ((c = getopt(argc, argv, "c:d:t:x:m:h:e:v:n:p:b:")) != -1) {
switch (c) {
Expand All @@ -126,7 +159,7 @@ int main(int argc, char** argv) {
num = strtoull(optarg, NULL, 10);
break;
case 'v':
value = strtoull(optarg, NULL, 10);
value = optarg;
break;
case 'p':
private_key = optarg;
Expand All @@ -142,11 +175,10 @@ int main(int argc, char** argv) {

ResDBConfig config = GenerateResDBConfig(client_config_file);
config.SetClientTimeoutMs(100000);

UTXOClient client(config);
if (cmd == "transfer") {
Transfer(&client, transaction_id, address, to_address, value, private_key,
to_pub_key);
Transfer(&client, transaction_id, address, ParseString(to_address), ParseValue(value), private_key,
ParseString(to_pub_key));
} else if (cmd == "list") {
GetList(&client, end_id, num);
} else if (cmd == "wallet") {
Expand Down