Initial commit

This commit is contained in:
Lennart Hansen 2022-04-21 00:11:25 +01:00
parent 724ec6a23d
commit 44f26bff36
2 changed files with 233 additions and 0 deletions

175
dero_nameservice.pl Executable file
View File

@ -0,0 +1,175 @@
#!/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;
}

58
turorial.pl Executable file
View File

@ -0,0 +1,58 @@
#!/usr/bin/perl -w
# Tutorial example
# 2003 (c) by Marcus Thiesen (marcus@cpan.org)
# This file is a part of Curses::UI and might be distributed
# under the same terms as perl itself.
# Use the libraries from the distribution, instead of
# system wide libraries.
use FindBin;
use lib "$FindBin::RealBin/../lib";
use strict;
use Curses::UI;
my $cui = new Curses::UI( -color_support => 1 );
my @menu = (
{ -label => 'File',
-submenu => [
{ -label => 'Exit ^Q', -value => \&exit_dialog }
]
},
);
my $menu = $cui->add(
'menu','Menubar',
-menu => \@menu,
-fg => "blue",
);
my $win1 = $cui->add(
'win1', 'Window',
-border => 1,
-y => 1,
-bfg => 'red',
);
sub exit_dialog()
{
my $return = $cui->dialog(
-message => "Do you really want to quit?",
-title => "Are you sure???",
-buttons => ['yes', 'no'],
);
exit(0) if $return;
}
my $texteditor = $win1->add("text", "TextEditor", -text => "Here is some text\n"
. "And some more");
$cui->set_binding(sub {$menu->focus()}, "\cX");
$cui->set_binding( \&exit_dialog , "\cQ");
$texteditor->focus();
$cui->mainloop();