Dero_Name_Service_Cli_Tool/dero_nameservice.pl

176 lines
4.3 KiB
Perl
Raw Normal View History

2022-04-21 00:11:25 +01:00
#!/usr/bin/perl
#
# Script Written by Hansen33
# Discord: hansen33#2541
#
# 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;
use FindBin;
use lib "$FindBin::RealBin/../lib";
use Curses::UI;
# options
use Getopt::Long;
my %options;
GetOptions(
'daemon-rpc=s' => \$options{'daemon-rpc'},
'wallet-rpc=s' => \$options{'wallet-rpc'},
'name=s' => \$options{'name'},
);
my $daemon = 'https://dero-node.mysrv.cloud';
$daemon = $options{'daemon-rpc'} if $options{'daemon-rpc'};
my $wallet_rpc = '127.0.0.0:10103';
$wallet_rpc = $options{'wallet-rpc'} if $options{'wallet-rpc'};
my $name = 'MySrvCloud';
$name = $options{'name'} if $options{'name'};
my $check = check_name($name);
if (defined $check and $check->{'status'} eq 'OK') {
printf "Name: (%s) is taken by this address (%s)\n", $check->{'name'}, $check->{'address'};
} else {
printf "Name: (%s) is FREE - Do you want to register it?\n", $name;
}
print "Checking wallet RPC\n";
my $count = 0;
while (not defined check_wallet_rpc() and $count < 10) {
print "Please start wallet with --rpc-server option\n" if $count == 0;
sleep 1;
$count++;
}
#exit
my $reg = register($name);
print Dumper($reg);
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',
'valye' => $name,
}
],
},
};
my $url = sprintf '%s/json_rpc', $daemon;
my $status = decode_json( post_url($url, $data) );
return $status->{'result'};
}
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;
}