Dero_Name_Service_Cli_Tool/dero_ns_cli.pl
2022-04-21 03:54:56 +01:00

261 lines
6.7 KiB
Perl
Executable File

#!/usr/bin/perl
#
# Script Written by Hansen33
# Discord: hansen33#2541
#
# Let's have some fun and register some more names.
#
# 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;
use warnings;
use strict;
use JSON;
use Data::Dumper;
use WWW::Curl::Easy;
# options
use Getopt::Long;
my $name;
my %options;
GetOptions(
'daemon-rpc=s' => \$options{'daemon-rpc'},
'wallet-rpc=s' => \$options{'wallet-rpc'},
'register+' => \$options{'register'},
'name=s' => \$name,
'check-list=s' => \$options{'list'},
'help+' => \$options{'help'},
);
my $daemon = 'https://dero-node.mysrv.cloud';
$daemon = $options{'daemon-rpc'} if $options{'daemon-rpc'};
my $wallet_rpc = '127.0.0.1:10103';
$wallet_rpc = $options{'wallet-rpc'} if $options{'wallet-rpc'};
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";
exit;
}
usage() if $options{'help'};
my @check_list;
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();
}
}
foreach my $check_name (@check_list) {
my $check = check_name($check_name);
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'}) {
printf "Name: (%s) is AVAILABLE - and will be registered in a little while\n", $name;
} else {
printf "Name: (%s) is AVAILABLE to register\n", $name;
}
}
}
if ($options{'register'}) {
my $max_wait = 120;
usage() if $options{'list'};
if (check_name($name)) {
printf "Name: (%s) is already registered, nothing to do\n", $name;
exit 0;
}
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 = register($name);
if (defined $reg and $reg->{'result'}) {
printf "Name: (%s) registered on the DERO Stargate with TX ID (%s)\n", $name, $reg->{'result'}->{'txid'};
} else {
print "Error, something went wrong!\n";
print Dumper($reg);
exit 1;
}
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) - Congratulations!\n", $reg_check->{'name'}, $reg_check->{'address'};
last;
} else {
printf "\rWaiting for registration to complete [%d/%d]... ", $count, $max_wait;
}
die "Something went wrong, it took too long to register!..\n" if $count > $max_wait;
sleep 1;
$count++;
}
} else {
print "Failed to connect with your wallet, please check your settings!\n";
}
}
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',
'value' => $name,
}
],
},
};
my $url = sprintf 'http://%s/json_rpc', $wallet_rpc;
my $status = decode_json( post_url($url, $data) );
return $status;
}
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;
my $status = decode_json( post_url($url, $data) );
return $status->{'result'};
}
sub post_url {
my $url = shift;
my $data = shift;
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;
}