From fc2ca015c6cc4da65c086c1d9899a2791e7864fe Mon Sep 17 00:00:00 2001 From: "Jonathan \"Duke\" Leto" Date: Sun, 29 Oct 2017 22:19:33 -0700 Subject: [PATCH] Start making some stuff --- lib/Hush.pm | 5 ++++ lib/Hush/List.pm | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/Hush/RPC.pm | 34 ++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 lib/Hush.pm create mode 100644 lib/Hush/List.pm create mode 100644 lib/Hush/RPC.pm diff --git a/lib/Hush.pm b/lib/Hush.pm new file mode 100644 index 0000000..d45ae7b --- /dev/null +++ b/lib/Hush.pm @@ -0,0 +1,5 @@ +package Hush; +use strict; +use warnings; + +1; diff --git a/lib/Hush/List.pm b/lib/Hush/List.pm new file mode 100644 index 0000000..bde2f74 --- /dev/null +++ b/lib/Hush/List.pm @@ -0,0 +1,63 @@ +package Hush::List; +use strict; +use warnings; +use Hush::RPC; +use Try::Tiny; + + +#TODO: verify +my $MAX_RECIPIENTS = 55; + +#TODO: create this if not specified +my $ZADDR = $ENV{HUSH_LIST_ZADDR} || die 'No funding zaddr found'; + +sub new { + my $hush_list = {}; + $hush_list->{lists} = {}; + return bless $hush_list, 'Hush::List'; +} + +sub new_list { + my ($self,$name) = @_; + my $lists = $self->{lists}; + die "Hush list $name already exists" if $lists->{$name}; + $lists->{$name}++; + return $self; +} + +# send a message to a Hush List, weeeeee! +sub send_message { + my ($self,$from,$name,$message) = @_; + + # TODO: better validation + die "Invalid Hush list name" unless $name; + die "Hush message cannot be empty" unless $message; + die "Invalid Hush from address: $from" unless $from; + + my $hush_list = $self->{lists}->{$name} || die "No Hush List by the name of '$name' found"; + + my $rpc = Hush::RPC->new; + my $recipients = $hush_list->recipients; + + die "Max recipients of $MAX_RECIPIENTS exceeded" if (@$recipients > $MAX_RECIPIENTS); + + # prevent an easily searchable single xtn amount + my $amount = 1e-4 + (1e-5*rand()); + + # this could blow up for a bajillion reasons... + try { + $rpc->z_sendmany($from, $amount, $recipients, $memo); + } catch { + die "caught RPC error: $_"; + } finally { + # TODO: notekeeping, logging, etc.. + } + + return $self; +} + +sub new_taddr {} +sub new_zaddr {} + + +1; diff --git a/lib/Hush/RPC.pm b/lib/Hush/RPC.pm new file mode 100644 index 0000000..bcb636e --- /dev/null +++ b/lib/Hush/RPC.pm @@ -0,0 +1,34 @@ +package Hush::RPC; +use strict; +use warnings; +use Bitcoin::RPC::Client; + +sub new { + my $rpc = Bitcoin::RPC::Client->new( + user => $ENV{HUSH_RPC_USERNAME} || "hush", + password => $ENV{HUSH_RPC_PASSWORD} || "puppy", + host => "127.0.0.1", + ); + return $rpc, +} + +__DATA__ + +my $chaininfo = $btc->getblockchaininfo; +my $blocks = $chaininfo->{blocks}; + +# Set the transaction fee +# https://bitcoin.org/en/developer-reference#settxfee +my $settx = $btc->settxfee($feerate); + +# Check your balance +# (JSON::Boolean objects must be passed as boolean parameters) +# https://bitcoin.org/en/developer-reference#getbalance +my $account = ''; +my $balance = $btc->getbalance($account, 1, JSON::true); + +# Send to an address +# https://bitcoin.org/en/developer-reference#sendtoaddress +my $txid = $rpc->sendtoaddress("1Ky49cu7FLcfVmuQEHLa1WjhRiqJU2jHxe","0.01"); + +1;