Dero_Name_Service_Cli_Tool/dero_ns_cli.pl

287 lines
7.5 KiB
Perl
Raw Permalink Normal View History

2022-04-21 00:11:25 +01:00
#!/usr/bin/perl
#
# Script Written by Hansen33
# Discord: hansen33#2541
#
2022-04-21 02:04:20 +01:00
# Let's have some fun and register some more names.
#
2022-04-21 00:11:25 +01:00
# Dero name service
# https://docs.dero.io/rtd_pages/dev_dvm.html#nameservice
# Check if name is registered
# curl http://127.0.0.1:40402/json_rpc -d '{"jsonrpc":"2.0","id":"0","method":"nametoaddress","params":{"name":"TESTUSERNAME" }}' -H 'Content-Type: application/json'
# Register
# curl http://127.0.0.1:40403/json_rpc -d '{"jsonrpc":"2.0","id":"0","method":"scinvoke","params":{"scid":"0000000000000000000000000000000000000000000000000000000000000001","ringsize":2, "sc_rpc":[{"name":"entrypoint","datatype":"S","value":"Register"}, {"name":"name","datatype":"S","value":"TESTUSERNAME" }] }}' -H 'Content-Type: application/json'
$|=1;
2022-04-21 17:37:01 +01:00
2022-04-21 00:11:25 +01:00
use warnings;
use strict;
2022-04-21 17:37:01 +01:00
use utf8::all;
2022-04-21 00:11:25 +01:00
use JSON;
use Data::Dumper;
use WWW::Curl::Easy;
# options
use Getopt::Long;
2022-04-21 19:01:18 +01:00
my ($name,$debug);
2022-04-21 00:11:25 +01:00
my %options;
GetOptions(
2022-04-21 02:14:29 +01:00
'daemon-rpc=s' => \$options{'daemon-rpc'},
'wallet-rpc=s' => \$options{'wallet-rpc'},
2022-04-21 02:04:20 +01:00
'register+' => \$options{'register'},
2022-04-21 02:14:29 +01:00
'name=s' => \$name,
'check-list=s' => \$options{'list'},
'help+' => \$options{'help'},
2022-04-21 19:01:18 +01:00
'debug+' => \$debug,
2022-04-21 00:11:25 +01:00
);
my $daemon = 'https://dero-node.mysrv.cloud';
$daemon = $options{'daemon-rpc'} if $options{'daemon-rpc'};
2022-04-21 02:04:20 +01:00
my $wallet_rpc = '127.0.0.1:10103';
2022-04-21 00:11:25 +01:00
$wallet_rpc = $options{'wallet-rpc'} if $options{'wallet-rpc'};
2022-04-21 02:04:20 +01:00
print "Dero Name Service Cli Tool - register name to wallet\n";
sub usage {
print "usage: $0 --name <name> [--register] [--list <path>] [--daemon-rpc] [--wallet-rpc]\n";
print "\t--name name to check or register\n";
print "\t--register will attempt to register name to your wallet\n";
print "\t--check-list Check file with list of names - list can not be used with --register\n";
print "\t--daemon-rpc Specify URL for daemon if you don't want to use https://dero-node.mysrv.cloud/\n";
print "\t This is just to check if the name is registered already\n";
print "\t--wallet-rpc Specify wallet RPC if other than 127.0.0.1:10103 (local wallet)\n";
2022-04-21 19:01:18 +01:00
print "\t--debug Show debug information\n";
2022-04-21 02:04:20 +01:00
exit;
}
usage() if $options{'help'};
2022-04-21 00:11:25 +01:00
2022-04-21 02:04:20 +01:00
my @check_list;
2022-04-21 00:11:25 +01:00
2022-04-21 02:04:20 +01:00
push @check_list, $name if defined $name;
if ($options{'list'}) {
# Building list
if (-f $options{'list'}) {
open my $FILE, '<', $options{'list'} or die ("Failed to open file list for reading....");
while (my $line = <$FILE>) {
chomp $line;
push @check_list, $line;
}
} else {
usage();
}
2022-04-21 00:11:25 +01:00
}
2022-04-21 02:04:20 +01:00
foreach my $check_name (@check_list) {
2022-04-21 00:11:25 +01:00
2022-04-21 19:01:18 +01:00
if (length $check_name < 6 or length $check_name >= 64) {
print "Name: ($check_name) not compatible, need to longer than 6 charaters and shorter than 64\n";
next;
}
2022-04-21 02:04:20 +01:00
my $check = check_name($check_name);
2022-04-21 00:11:25 +01:00
2022-04-21 02:04:20 +01:00
if (defined $check and $check->{'status'} eq 'OK') {
printf "Name: (%s) is taken by this address (%s)\n", $check->{'name'}, $check->{'address'};
} else {
if ($options{'register'}) {
2022-04-21 03:57:48 +01:00
printf "Name: (%s) is AVAILABLE - and will be registered in a little while\n", $check_name;
2022-04-21 19:01:18 +01:00
register_name($check_name);
2022-04-21 02:04:20 +01:00
} else {
2022-04-21 03:57:48 +01:00
printf "Name: (%s) is AVAILABLE to register\n", $check_name;
2022-04-21 02:04:20 +01:00
}
}
2022-04-21 00:11:25 +01:00
}
2022-04-21 19:01:18 +01:00
sub register_name {
my $name = shift;
2022-04-21 02:04:20 +01:00
my $max_wait = 120;
usage() if $options{'list'};
printf "Checking wallet RPC (%s)\n", $wallet_rpc;
my $count = 0;
while (not defined check_wallet_rpc() and $count < $max_wait) {
print "Please start wallet with --rpc-server option\n" if $count == 0;
printf "\rWaiting [%d/%d]... ", $count, $max_wait;
sleep 1;
$count++;
}
if (check_wallet_rpc()) {
printf "Wallet connected (%s)\n", $wallet_rpc;
my $reg;
my $reg_count = 0;
while ((not defined $reg or not exists $reg->{'result'}) and $reg_count < 3) {
$reg = register($name);
2022-04-21 02:04:20 +01:00
if (defined $reg and $reg->{'result'}) {
printf "Name: (%s) registered on the DERO Stargate with TX ID (%s)\n", $name, $reg->{'result'}->{'txid'};
} else {
printf "Error, something went wrong! log saved -> (%s.log)\n", $0;
open my $FILE, '>>', sprintf '%s.log', $0 or die "Faile to open logfile for writing";
print $FILE Dumper($reg);
close $FILE;
}
$reg_count++;
}
if (not defined $reg or not exists $reg->{'result'}) {
print "Failed to register, please check log file..\n";
2022-04-21 02:04:20 +01:00
print Dumper($reg);
exit 1;
}
2022-04-21 02:04:20 +01:00
my $reg_check;
$count = 0;
while (not defined $reg_check) {
my $reg_check = check_name($name);
if (defined $reg_check and $reg_check->{'status'} eq 'OK') {
printf "\rName: (%s) is now registered to this address (%s)\nCongratulations your name was successfully registered!\n", $reg_check->{'name'}, $reg_check->{'address'};
2022-04-21 02:04:20 +01:00
last;
} else {
2022-04-21 02:14:29 +01:00
printf "\rWaiting for registration to complete [%d/%d]... ", $count, $max_wait;
2022-04-21 02:04:20 +01:00
}
2022-04-21 02:14:29 +01:00
die "Something went wrong, it took too long to register!..\n" if $count > $max_wait;
2022-04-21 02:04:20 +01:00
sleep 1;
$count++;
}
} else {
print "Failed to connect with your wallet, please check your settings!\n";
}
}
2022-04-21 00:11:25 +01:00
sub register {
my $name = shift;
# Register
# curl http://127.0.0.1:40403/json_rpc -d '{"jsonrpc":"2.0","id":"0","method":"scinvoke","params":{"scid":"0000000000000000000000000000000000000000000000000000000000000001","ringsize":2, "sc_rpc":[{"name":"entrypoint","datatype":"S","value":"Register"}, {"name":"name","datatype":"S","value":"TESTUSERNAME" }] }}' -H 'Content-Type: application/json'
my $data = {
'jsonrpc' => '2.0',
'id' => 0,
'method' => 'scinvoke',
'params' => {
'scid' => '0000000000000000000000000000000000000000000000000000000000000001',
'ringsize' => 2,
'sc_rpc' => [
{
'name' => 'entrypoint',
'datatype' => 'S',
'value' => 'Register',
},
{
'name' => 'name',
'datatype' => 'S',
2022-04-21 02:04:20 +01:00
'value' => $name,
2022-04-21 00:11:25 +01:00
}
],
},
};
2022-04-21 02:04:20 +01:00
my $url = sprintf 'http://%s/json_rpc', $wallet_rpc;
2022-04-21 00:11:25 +01:00
my $status = decode_json( post_url($url, $data) );
2022-04-21 00:11:25 +01:00
2022-04-21 19:01:18 +01:00
print Dumper($status) if $debug;
2022-04-21 02:04:20 +01:00
return $status;
2022-04-21 00:11:25 +01:00
}
sub check_wallet_rpc {
my ($ip,$port) = split /:/, $wallet_rpc;
my $cmd = sprintf 'timeout 1 /usr/bin/nc -z %s %d', $ip, $port;
system "$cmd >/dev/null 2>&1";
if ( $? == 0 ) {
return 1;
}
return;
}
sub check_name {
my $name = shift;
# Check if name is registered
# curl http://127.0.0.1:40402/json_rpc -d '{"jsonrpc":"2.0","id":"0","method":"nametoaddress","params":{"name":"TESTUSERNAME" }}' -H 'Content-Type: application/json'
my $data = {
'jsonrpc' => '2.0',
'id' => 0,
'method' => 'nametoaddress',
'params' => {
'name' => $name,
},
};
my $url = sprintf '%s/json_rpc', $daemon;
2022-04-21 19:01:18 +01:00
my $response = decode_json( post_url($url, $data) );
2022-04-21 00:11:25 +01:00
2022-04-21 19:01:18 +01:00
print Dumper($response) if $debug;
return $response->{'result'} if defined $response and exists $response->{'result'};
return;
2022-04-21 00:11:25 +01:00
}
sub post_url {
my $url = shift;
my $data = shift;
2022-04-21 19:01:18 +01:00
print Dumper($data) if $debug;
2022-04-21 00:11:25 +01:00
my $response_data;
my $curl = WWW::Curl::Easy->new();
$curl->setopt( CURLOPT_URL, $url );
$curl->setopt( CURLOPT_POSTFIELDS, encode_json($data) );
$curl->setopt( CURLOPT_HTTPHEADER, [ 'Content-Type: application/json' ]);
$curl->setopt( CURLOPT_FOLLOWLOCATION, 1);
$curl->setopt( CURLOPT_WRITEDATA, \$response_data );
my $return_code = $curl->perform();
my $response_code = $curl->getinfo(CURLINFO_RESPONSE_CODE);
# If curl fails to get the URL - report the error and exit
if ( $return_code != 0 ) {
die "Failed to contact RPC API : $url";
}
return $response_data;
}