| 1 | # PODNAME: Finance::Dogecoin::Utils::ProxyActions | ||
| 2 | # ABSTRACT: proxy and enhance RPC made to a Dogecoin Core node | ||
| 3 | use Object::Pad; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | class Finance::Dogecoin::Utils::ProxyActions { | ||
| 9 | use JSON; | ||
| 10 | use Path::Tiny; | ||
| 11 | use Digest::MD5 'md5_hex'; | ||
| 12 | |||
| 13 | use feature 'say'; | ||
| 14 | |||
| 15 | field $rpc :param; | ||
| 16 | field $json :param; | ||
| 17 | field $addresses :param; | ||
| 18 | field $address_file :param; | ||
| 19 | field $conf_dir :param; | ||
| 20 | |||
| 21 | sub BUILDARGS( $class, %args ) { | ||
| 22 | $args{json} //= JSON->new->utf8(1); | ||
| 23 | $args{address_file} = path( $args{address_file} ); | ||
| 24 | $args{addresses} //= do { | ||
| 25 | if ($args{address_file}->exists) { | ||
| 26 | $args{json}->decode( $args{address_file}->slurp_utf8 ); | ||
| 27 | } | ||
| 28 | else { | ||
| 29 | {} | ||
| 30 | } | ||
| 31 | }; | ||
| 32 | |||
| 33 | return %args; | ||
| 34 | } | ||
| 35 | |||
| 36 | method setlabel( $address, $label ) { | ||
| 37 | $addresses->{$label} = $address; | ||
| 38 | return 1; | ||
| 39 | } | ||
| 40 | |||
| 41 | method getreceivedbylabel( $label, @args ) { | ||
| 42 | if (my $address = $addresses->{$label}) { | ||
| 43 | my $output = $rpc->call_method( getreceivedbyaddress => $address, @args ); | ||
| 44 | say $json->encode( $output ); | ||
| 45 | } else { | ||
| 46 | warn "No address label '$label' found\n"; | ||
| 47 | return 0; | ||
| 48 | }; | ||
| 49 | |||
| 50 | return 1; | ||
| 51 | } | ||
| 52 | |||
| 53 | method exportaddresses { | ||
| 54 | my $dumpfile = md5_hex( $$ . time() ); | ||
| 55 | my $result = $rpc->call_method( 'dumpwallet', $dumpfile ); | ||
| 56 | |||
| 57 | my $file = $conf_dir->child($dumpfile); | ||
| 58 | |||
| 59 | for my $line ($file->lines_utf8) { | ||
| 60 | # skip header, blank, and comment lines | ||
| 61 | next unless $line =~ /\S/; | ||
| 62 | next if $line =~ /^#/; | ||
| 63 | |||
| 64 | my ($private_key, $timestamp, $label, $comment, @rest) = split /\s+/, $line; | ||
| 65 | |||
| 66 | for my $rest (@rest) { | ||
| 67 | next unless $rest =~ /^addr=(\S+)/; | ||
| 68 | say $1; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | method DESTROY { | ||
| 74 | return unless $address_file; | ||
| 75 | $address_file->spew_utf8( $json->encode( $addresses ) ); | ||
| 76 | undef $address_file; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | __END__ |