Browse Source

Migrate some code into Hush::Contact which was lonely

master
Jonathan "Duke" Leto 7 years ago
parent
commit
58ef021695
  1. 122
      bin/hushlist
  2. 136
      lib/Hush/App.pm
  3. 38
      lib/Hush/Contact.pm
  4. 38
      lib/Hush/List.pm

122
bin/hushlist

@ -7,125 +7,9 @@ use Hush::List;
use Hush::Util qw/barf/;
use Data::Dumper;
use Hush::RPC;
use Hush::App;
my $rpc = Hush::RPC->new;
my $chaininfo = $rpc->getblockchaininfo;
my $walletinfo = $rpc->getwalletinfo;
my $chain = $chaininfo->{chain};
my $blocks = $chaininfo->{blocks};
my $balance = $rpc->z_gettotalbalance;
my $tbalance = sprintf "%.8f", $balance->{transparent};
my $zbalance = sprintf "%.8f", $balance->{private};
my $total_balance = $balance->{total};
my $blockchain = "HUSH";
print "Hushlist v$Hush::List::VERSION running on $blockchain ${chain}net, $blocks blocks\n";
print "Balances: transparent $tbalance HUSH, private $zbalance HUSH\n";
# we only need one
my $options = {};
my $list = Hush::List->new($rpc, $options);
my $rpc = Hush::RPC->new;
my $command = shift || help();
my $COMMANDS = {
"add" => \&add,
"contact" => \&Hush::List::contact,
"help" => \&help,
"new" => \&new,
"remove" => \&remove,
"send" => \&send,
"show" => \&show,
"status" => \&status,
#"public" => \&public,
};
# TODO: translations
my %HELP = (
"add" => "Add a contact to a Hushlist",
"contact" => "Manage Hushlist contacts",
"help" => "Get your learn on",
"new" => "Create a new Hushlist",
"remove" => "Remove a Hushlist",
"send" => "Send a new new Hushlist memo",
"status" => "Get overview of Hushlists or a specific Hushlist",
"show" => "Show Hushlist memos",
#"public" => "Make a private Hushlist public",
);
run();
Hush::App::run($command);
exit;
sub help {
print "Available Hushlist commands:\n";
my @cmds = sort keys %$COMMANDS;
for my $cmd (@cmds) {
print "\t$cmd\t: $HELP{$cmd}\n";
}
}
sub show {
my ($name,@args) = @_;
if ($name) {
my $status = $list->show($name);
} else {
my $status = $list->global_show;
}
}
sub usage {
print "Usage: $0 command [subcommand] [options]\n";
print "$0 help for more details :)\n";
}
sub status {
my $name = shift;
if ($name) {
my $status = $list->status($name);
} else {
my $status = $list->global_status;
}
}
# make a hushlist public by publishing its privkey in OP_RETURN data
# This operation costs HUSH and cannot be undone!!! It sends private
# keys to the PUBLIC BLOCKCHAIN
sub publicize {
my ($name) = @_;
$list->publicize($name);
}
sub new {
my $name = shift || '';
#TODO: better validation and allow safe unicode stuff
barf "Invalid hushlist name '$name' !" unless $name && ($name =~ m/^[A-z0-9_-]{0,64}/);
$list->new_list($name);
print "hushlist '$name' created, enjoy your private comms ;)\n";
}
sub run {
#print "Running command $command\n";
my $cmd = $COMMANDS->{$command};
if ($cmd) {
$cmd->(@ARGV);
} else {
usage();
}
}
sub add {
my ($list_name,$zaddr) = @_;
$list->add_zaddr($list_name,$zaddr);
}
sub remove {
my ($list_name,$zaddr) = @_;
$list->remove_zaddr($list_name,$zaddr);
}
sub send {
my ($list_name,$memo) = @_;
$list->send_memo($rpc, $list_name, $memo);
}

136
lib/Hush/App.pm

@ -0,0 +1,136 @@
package Hush::App;
use strict;
use warnings;
use Try::Tiny;
use lib 'lib';
use Hush::List;
use Hush::Util qw/barf/;
use Data::Dumper;
use Hush::RPC;
my $COMMANDS = {
"add" => \&add,
"contact" => \&Hush::List::contact,
"help" => \&help,
"new" => \&new,
"remove" => \&remove,
"send" => \&send,
"show" => \&show,
"status" => \&status,
#"public" => \&public,
};
# TODO: translations
my %HELP = (
"add" => "Add a contact to a Hushlist",
"contact" => "Manage Hushlist contacts",
"help" => "Get your learn on",
"new" => "Create a new Hushlist",
"remove" => "Remove a Hushlist",
"send" => "Send a new new Hushlist memo",
"status" => "Get overview of Hushlists or a specific Hushlist",
"show" => "Show Hushlist memos",
#"public" => "Make a private Hushlist public",
);
my $rpc = Hush::RPC->new;
sub show_status {
my $chaininfo = $rpc->getblockchaininfo;
my $walletinfo = $rpc->getwalletinfo;
my $chain = $chaininfo->{chain};
my $blocks = $chaininfo->{blocks};
my $balance = $rpc->z_gettotalbalance;
my $tbalance = sprintf "%.8f", $balance->{transparent};
my $zbalance = sprintf "%.8f", $balance->{private};
my $total_balance = $balance->{total};
my $blockchain = "HUSH";
print "Hushlist v$Hush::List::VERSION running on $blockchain ${chain}net, $blocks blocks\n";
print "Balances: transparent $tbalance HUSH, private $zbalance HUSH\n";
}
my $options = {};
my $list = Hush::List->new($rpc, $options);
my $command = shift || help();
sub run {
my ($command) = @_;
#print "Running command $command\n";
my $cmd = $COMMANDS->{$command};
show_status();
if ($cmd) {
$cmd->(@ARGV);
} else {
usage();
}
}
sub add {
my ($list_name,$zaddr) = @_;
$list->add_zaddr($list_name,$zaddr);
}
sub remove {
my ($list_name,$zaddr) = @_;
$list->remove_zaddr($list_name,$zaddr);
}
sub send {
my ($list_name,$memo) = @_;
$list->send_memo($rpc, $list_name, $memo);
}
sub help {
print "Available Hushlist commands:\n";
my @cmds = sort keys %$COMMANDS;
for my $cmd (@cmds) {
print "\t$cmd\t: $HELP{$cmd}\n";
}
}
sub show {
my ($name,@args) = @_;
if ($name) {
my $status = $list->show($name);
} else {
my $status = $list->global_show;
}
}
sub usage {
print "Usage: $0 command [subcommand] [options]\n";
print "$0 help for more details :)\n";
}
sub status {
my $name = shift;
if ($name) {
my $status = $list->status($name);
} else {
my $status = $list->global_status;
}
}
# make a hushlist public by publishing its privkey in OP_RETURN data
# This operation costs HUSH and cannot be undone!!! It sends private
# keys to the PUBLIC BLOCKCHAIN
sub publicize {
my ($name) = @_;
$list->publicize($name);
}
sub new {
my $name = shift || '';
#TODO: better validation and allow safe unicode stuff
barf "Invalid hushlist name '$name' !" unless $name && ($name =~ m/^[A-z0-9_-]{0,64}/);
$list->new_list($name);
print "hushlist '$name' created, enjoy your private comms ;)\n";
}

38
lib/Hush/Contact.pm

@ -3,4 +3,42 @@ use strict;
use warnings;
use Hush::Util qw/barf/;
sub contact {
my $cmd = shift || '';
my $subcommands = {
"add" => sub {
# add a hush contact, yay
my ($cmd,$name,$zaddr) = @ARGV;
#barf Dumper [ $cmd, $name, $zaddr ];
#TODO: give user ability to choose
my $chain = "hush";
my $contacts_file = catdir($HUSHLIST_CONFIG_DIR,"$chain-contacts.txt");
if (-e $contacts_file) {
my %contacts = read_file( $contacts_file ) =~ /^(z[a-z0-9]+) (.*)$/mgi ;
# TODO: check if zaddr OR nickname exists
if ($contacts{$zaddr}) {
} else {
# TODO: see if this contact exists already in this chain
open my $fh, ">>", $contacts_file or barf "Could not write file $contacts_file ! : $!";
#TODO: validation?
print $fh "$zaddr $name\n";
close $fh;
}
}
},
"rm" => sub {
my ($cmd,$name,$zaddr) = @ARGV;
barf Dumper [ $cmd, $name, $zaddr ];
},
};
my $subcmd = $subcommands->{$cmd};
if ($subcmd) {
$subcmd->();
} else {
barf "Invalid hushlist contact subcommand!";
}
}
1;

38
lib/Hush/List.pm

@ -5,6 +5,7 @@ use Hush::RPC;
use Try::Tiny;
use File::Spec::Functions;
use Hush::Util qw/barf/;
use Hush::Contact;
use File::Slurp;
use Hush::Logger qw/debug/;
use JSON;
@ -17,43 +18,6 @@ my $HUSHLIST_CONFIG_DIR = $ENV{HUSH_CONFIG_DIR} || catdir($HUSH_CONFIG_DIR, 'lis
our $VERSION = 20171031;
my $rpc = Hush::RPC->new;
sub contact {
my $cmd = shift || '';
my $subcommands = {
"add" => sub {
# add a hush contact, yay
my ($cmd,$name,$zaddr) = @ARGV;
#barf Dumper [ $cmd, $name, $zaddr ];
#TODO: give user ability to choose
my $chain = "hush";
my $contacts_file = catdir($HUSHLIST_CONFIG_DIR,"$chain-contacts.txt");
if (-e $contacts_file) {
my %contacts = read_file( $contacts_file ) =~ /^(z[a-z0-9]+) (.*)$/mgi ;
# TODO: check if zaddr OR nickname exists
if ($contacts{$zaddr}) {
} else {
# TODO: see if this contact exists already in this chain
open my $fh, ">>", $contacts_file or barf "Could not write file $contacts_file ! : $!";
#TODO: validation?
print $fh "$zaddr $name\n";
close $fh;
}
}
},
"rm" => sub {
my ($cmd,$name,$zaddr) = @ARGV;
barf Dumper [ $cmd, $name, $zaddr ];
},
};
my $subcmd = $subcommands->{$cmd};
if ($subcmd) {
$subcmd->();
} else {
barf "Invalid hushlist contact subcommand!";
}
}
sub _sanity_checks {
if (!-e $HUSH_CONFIG_DIR ) {

Loading…
Cancel
Save