Hush/App.pm untested

filter: Statementstmt 0.0% 0 / 101 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 166 CC 11 SCAR 18.4
CC
11
Cov
0%
send_file
20.0
subscribe
12.0
validate_hushlist_url
6.0
CRAP 6.3
1 package Hush::App;
2 use strict;
3 use warnings;
4 use Try::Tiny;
5 use lib 'lib';
6 use Hush::List;
7 use Hush::Util qw/barf/;
8 use Data::Dumper;
9 use Hush::RPC;
10 use File::Slurp;
11  
12 my $COMMANDS = {
13 "add" => \&add,
14 "contact" => \&Hush::Contact::contact,
15 "help" => \&help,
16 "new" => \&new,
17 "remove" => \&remove,
18 "send" => \&send,
19 "send-file" => \&send_file,
20 "show" => \&show,
21 "status" => \&status,
22 #"public" => \&public,
23 "subscribe" => \&subscribe,
24 };
25 # TODO: translations
26 my %HELP = (
27 "add" => "Add a contact to a Hushlist",
28 "contact" => "Manage Hushlist contacts",
29 "help" => "Get your learn on",
30 "new" => "Create a new Hushlist",
31 "remove" => "Remove a Hushlist",
32 "send" => "Send a new Hushlist memo, specified on command-line",
33 "send-file" => "Send a new Hushlist memo from a file on disk",
34 "status" => "Get overview of Hushlists or a specific Hushlist",
35 "show" => "Show Hushlist memos",
36 #"public" => "Make a private Hushlist public",
37 );
38  
39 my $options = {};
40 my $rpc = Hush::RPC->new;
41 my $list = Hush::List->new($rpc, $options);
42  
43 sub validate_hushlist_url {
44 my ($url) = @_;
45 # TODO: allow full syntax and bare privkeys
46 # and other chains
47 if ($url =~ m!^hushlist://(SK[A-z0-9]+)(\?height=(\d+))?!i) {
48 my ($key,$height) = ($1,$3);
49 my ($chain,$net) = ("hush","");
50 return ($chain, $net, $key,$height);
51 }
52 return undef;
53 }
54  
55 sub subscribe {
56 my ($url) = @_;
57  
58 my $status;
59 if ($url) {
60 if (my @hushlist = validate_hushlist_url($url)) {
61 $status = $list->subscribe(@hushlist);
62 } else {
63 die "Invalid hushlist URL!\n";
64 }
65 } else {
66 die "Usage: hushlist subscribe URL\n";
67 }
68 return $status;
69 }
70  
71 sub show_status {
72 my $chaininfo = $rpc->getblockchaininfo;
73 my $walletinfo = $rpc->getwalletinfo;
74 my $chain = $chaininfo->{chain};
75 my $blocks = $chaininfo->{blocks};
76 my $balance = $rpc->z_gettotalbalance;
77 my $tbalance = sprintf "%.8f", $balance->{transparent};
78 my $zbalance = sprintf "%.8f", $balance->{private};
79 my $total_balance = $balance->{total};
80 my $blockchain = "HUSH";
81  
82 print "Hushlist v$Hush::List::VERSION running on $blockchain ${chain}net, $blocks blocks\n";
83 print "Balances: transparent $tbalance HUSH, private $zbalance HUSH\n";
84 }
85  
86  
87 sub run {
88 my ($command) = @_;
89 #print "Running command $command\n";
90 my $cmd = $COMMANDS->{$command};
91  
92 show_status();
93  
94 if ($cmd) {
95 return $cmd->(@ARGV);
96 } else {
97 usage();
98 }
99 }
100  
101 sub add {
102 my ($list_name,$contact) = @_;
103  
104 # we add contacts to a list, not a zaddr
105 $list->add_contact($list_name,$contact);
106 }
107  
108 sub remove {
109 my ($list_name,$zaddr) = @_;
110  
111 $list->remove_zaddr($list_name,$zaddr);
112 return $list;
113 }
114  
115 # send a Hushlist memo from a file
116 sub send_file {
117 my ($list_name,$file) = @_;
118  
119 barf "You must specify a Hushlist to send to" unless $list_name;
120 barf "You must specify a file to attach" unless $file;
121  
122 if (-e $file) {
123 my $memo = read_file($file);
124 $list->send_memo($rpc, $list_name, $memo);
125 } else {
126 barf "Cannot find $file to attach to Hushlist memo to $list_name!";
127 }
128 return $list;
129 }
130  
131 # send a Hushlist memo to list of contacts on the chain
132 # specified for this list
133 sub send {
134 my ($list_name,$memo) = @_;
135  
136 $list->send_memo($rpc, $list_name, $memo);
137 }
138  
139 sub help {
140 print "Available Hushlist commands:\n";
141 my @cmds = sort keys %$COMMANDS;
142 for my $cmd (@cmds) {
143 print "\t$cmd\t: $HELP{$cmd}\n";
144 }
145 }
146  
147 sub show {
148 my ($name,@args) = @_;
149 if ($name) {
150 my $status = $list->show($name);
151 } else {
152 my $status = $list->global_show;
153 }
154 }
155  
156 sub usage {
157 print "Usage: $0 command [subcommand] [options]\n";
158 print "$0 help for more details :)\n";
159 return 1;
160 }
161  
162 sub status {
163 my $name = shift;
164 if ($name) {
165 my $status = $list->status($name);
166 } else {
167 my $status = $list->global_status;
168 }
169 }
170  
171 # make a hushlist public by publishing its privkey in OP_RETURN data
172 # This operation costs HUSH and cannot be undone!!! It sends private
173 # keys to the PUBLIC BLOCKCHAIN
174 sub publicize {
175 my ($name) = @_;
176  
177 $list->publicize($name);
178 }
179  
180 sub new {
181 my $name = shift || '';
182 #TODO: better validation and allow safe unicode stuff
183 barf "Invalid hushlist name '$name' !" unless $name && ($name =~ m/^[A-z0-9_-]{0,64}/);
184  
185 $list->new_list($name);
186 print "hushlist '$name' created, enjoy your private comms ;)\n";
187 }
188  
189 1;