From 44f26bff36f885d4b3ad12fe93889e1468a330f6 Mon Sep 17 00:00:00 2001 From: Lennart Hansen Date: Thu, 21 Apr 2022 00:11:25 +0100 Subject: [PATCH] Initial commit --- dero_nameservice.pl | 175 ++++++++++++++++++++++++++++++++++++++++++++ turorial.pl | 58 +++++++++++++++ 2 files changed, 233 insertions(+) create mode 100755 dero_nameservice.pl create mode 100755 turorial.pl diff --git a/dero_nameservice.pl b/dero_nameservice.pl new file mode 100755 index 0000000..ccdbb17 --- /dev/null +++ b/dero_nameservice.pl @@ -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; + +} + diff --git a/turorial.pl b/turorial.pl new file mode 100755 index 0000000..58702f5 --- /dev/null +++ b/turorial.pl @@ -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();