Crypto-NanoRPC

 view release on metacpan or  search on metacpan

lib/Crypto/NanoRPC.pm  view on Meta::CPAN

    key_expand => [ 'key' ],
    ledger => [ 'account', 'count', [ 'representative', 'weight', 'pending', 'modified_since', 'sorting' ] ],
    node_id => [],
    node_id_delete => [],
    peers => [ [ 'peer_details' ] ],
    pending => [ 'account', [ 'count', 'threshold', 'source', 'include_active', 'sorting', 'include_only_confirmed' ] ],
    pending_exists => [ 'hash', [ 'include_active', 'include_only_confirmed' ] ],
    process => [ 'block', [ 'force', 'subtype', 'json_block' ] ],
    representatives => [ [ 'count', 'sorting' ] ],
    representatives_online => [ [ 'weight' ] ],
    republish => [ 'hash', [ 'sources', 'destinations' ] ],
    sign => [ 'block', [ 'key', 'wallet', 'account', 'json_block' ] ],
    stats => [ 'type' ],
    stats_clear => [],
    stop => [],
    successors => [ 'block', 'count', [ 'offset', 'reverse' ] ],
    validate_account_number => [ 'account' ],
    version => [],
    unchecked => [ 'count' ],
    unchecked_clear => [],
    unchecked_get => [ 'hash', [ 'json_block' ] ],
    unchecked_keys => [ 'key', 'count', [ 'json_block' ] ],
    unopened => [ [ 'account', 'count' ] ],
    uptime => [],
    work_cancel => [ 'hash' ],
    work_generate => [ 'hash', [ 'use_peers', 'difficulty' ] ],
    work_peer_add => [ 'address', 'port' ],
    work_peers => [],
    work_peers_clear => [],
    work_validate => [ 'work', 'hash', [ 'difficulty' ] ],

    # Wallet RPC's
    account_create => [ 'wallet', [ 'index', 'work' ] ],
    account_list => [ 'wallet' ],
    account_move => [ 'wallet', 'source', 'accounts' ],
    account_remove => [ 'wallet', 'account' ],
    account_representative_set => [ 'wallet', 'account', 'representative', [ 'work' ] ],
    accounts_create => [ 'wallet', 'count', [ 'work' ] ],
    password_change => [ 'wallet', 'password' ],
    password_enter => [ 'wallet', 'password' ],
    password_valid => [ 'wallet' ],
    receive => [ 'wallet', 'account', 'block', [ 'work' ] ],
    receive_minimum => [],
    receive_minimum_set => [ 'amount' ],
    search_pending => [ 'wallet' ],
    search_pending_all => [],
    send => [ 'wallet', 'source', 'destination', 'amount', [ 'work' ] ],
    wallet_add => [ 'wallet', 'key', [ 'work' ] ],
    wallet_add_watch => [ 'wallet', 'accounts' ],
    wallet_balances => [ 'wallet', [ 'threshold' ] ],
    wallet_change_seed => [ 'wallet', 'seed', [ 'count' ] ],
    wallet_contains => [ 'wallet', 'account' ],
    wallet_create => [ [ 'seed' ] ],
    wallet_destroy => [ 'wallet' ],
    wallet_export => [ 'wallet' ],
    wallet_frontiers => [ 'wallet' ],
    wallet_history => [ 'wallet', [ 'modified_since' ] ],
    wallet_info => [ 'wallet' ],
    wallet_ledger => [ 'wallet', [ 'representative', 'weight', 'pending', 'modified_since' ] ],
    wallet_lock => [ 'wallet' ],
    wallet_locked => [ 'wallet' ],
    wallet_pending => [ 'wallet', 'count', [ 'threshold', 'source', 'include_active', 'include_only_confirmed' ] ],
    wallet_representative => [ 'wallet' ],
    wallet_representative_set => [ 'wallet', 'representative', [ 'update_existing_accounts' ] ],
    wallet_republish => [ 'wallet', 'count' ],
    wallet_work_get => [ 'wallet' ],
    work_get => [ 'wallet', 'account' ],
    work_set => [ 'wallet', 'account', 'work' ],

    # Conversion RPC's  
    krai_from_raw => [ 'amount' ],
    krai_to_raw => [ 'amount' ],
    mrai_from_raw => [ 'amount' ],
    mrai_to_raw => [ 'amount' ],
    rai_from_raw => [ 'amount' ],
    rai_to_raw => [ 'amount' ],
};

sub new {
    my $class = shift;
    my $self = {
        url       => shift,
    };
    $self->{url} = 'http://[::1]:7076' unless defined $self->{url};
    $self->{request} = HTTP::Request->new( 'POST', $self->{url} );
    $self->{request}->content_type('application/json');
    $self->{ua} = LWP::UserAgent->new;
    bless $self, $class;
    return $self;
}

sub set_wallet {
    my $self = shift;
    $self->{wallet} = shift;
    return $self;
}

sub set_account {
    my $self = shift;
    $self->{account} = shift;
    return $self;
}

sub set_params {
    my ($self,$extra) = @_;
    if (ref $extra eq 'HASH') {
        $self->{$_} = $extra->{$_} for (keys %$extra);
    } else {
        # assume key/values were specified as array
        shift @_;
        while (@_) {
            my ($key,$value) = (shift,shift);
            $self->{$key} = $value;
        }
    }
    return $self;
}

sub AUTOLOAD {
    my ($self,$extra) = @_;
    if (ref $extra eq 'HASH') {

lib/Crypto/NanoRPC.pm  view on Meta::CPAN

sub raw_to_mrai {
    my $raw = shift;
    return $raw / 1000000000000000000000000000000;
}

sub __do_rpc {
    my ($self,$json) = @_;
    $self->{request}->content($json);
    my $response = $self->{ua}->request($self->{request});
    if ($response->is_success) {
        return decode_json($response->decoded_content);
    }
    return { error => "RPC call failed" };
}

1;
__END__

=head1 NAME

Crypto::NanoRPC - Perl module for interacting with Nano node

=head1 SYNOPSIS

  use Crypto::NanoRPC;
  $rpc = NanoRPC->new();
  $rpc = NanoRPC->new( 'http://[::1]:7076' );
 
  $rpc->set_params(
                        wallet => '000XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
                        account => 'nano_111111111111111111111111111111111111111111111111111111111111',
                  );
 
  $response = $rpc->account_balance();
  
  printf "Balance: %s\n", $response->{balance} unless defined $response->{error};


=head1 DESCRIPTION

=over 1

Object Oriented perl class for interacting with a Nano (rai) node

Implemented RPC calls are defined in the array rpc_actions in NanoRPC.pm. The required arguments can
be set using the set_params() method. The most common arguments, "wallet" and "account", have their own
set_ methods.

=back

=head1 METHODS

See L<https://docs.nano.org/commands/rpc-protocol/> for a list of RPC calls. This module implements the following RPCs:

=head2 Node RPCs

account_balance account_block_count account_get account_history account_info account_key account_representative account_weight accounts_balances accounts_frontiers accounts_pending active_difficulty available_supply block_account block_count block_co...

=head2 Wallet RPCs

account_create account_list account_move account_remove account_representative_set accounts_create password_change password_enter password_valid receive receive_minimum receive_minimum_set search_pending search_pending_all send wallet_add wallet_add_...

=head2 Unit Conversion RPCs

krai_from_raw krai_to_raw mrai_from_raw mrai_to_raw rai_from_raw rai_to_raw

=head1 DEPENDENCIES

These modules are required:

=over 1

=item HTTP::Request

=item LWP::UserAgent

=item JSON

=back

=head1 AUTHOR

Ruben de Groot, ruben at hacktor.com

Git Repository: L<https://github.com/hacktor/Crypto-NanoRPC>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2020 by Ruben de Groot

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.16.1 or,
at your option, any later version of Perl 5 you may have available.


=cut



( run in 3.177 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )