Web3-Tiny

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN

    address => '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
    abi     => {
        symbol    => { sig => 'symbol()',           returns => ['string']  },
        balanceOf => { sig => 'balanceOf(address)', returns => ['uint256'] },
    },
);
print $weth->call('symbol'), "\n";                       # WETH
print $weth->call('balanceOf', $web3_address), "\n";     # raw wei, as a Math::BigInt

# sign and send a transaction
my $wallet = $web3->wallet(private_key => '0x...');
my $erc20  = $web3->contract(
    address => '0x...',
    abi     => { transfer => { sig => 'transfer(address,uint256)', returns => ['bool'] } },
);
my $tx_hash = $erc20->send($wallet, 'transfer', $to_address, to_wei('1.5'));
```

## Why

A handful of CPAN modules already talk to Ethereum's JSON-RPC API --

lib/Web3/Tiny.pm  view on Meta::CPAN

    my ($class, %opts) = @_;
    die "Web3::Tiny: 'rpc_url' is required\n" unless $opts{rpc_url};

    return bless {
        rpc => Web3::Tiny::RPC->new(url => $opts{rpc_url}, timeout => $opts{timeout}),
    }, $class;
}

sub rpc { return $_[0]->{rpc} }

# wallet(private_key => '0x...') -> Web3::Tiny::Wallet
sub wallet {
    my ($self, %opts) = @_;
    return Web3::Tiny::Wallet->new(%opts);
}

# contract(address => '0x...', abi => { ... }) -> Web3::Tiny::Contract
sub contract {
    my ($self, %opts) = @_;
    return Web3::Tiny::Contract->new(%opts, rpc => $self->{rpc});
}

lib/Web3/Tiny.pm  view on Meta::CPAN

        address => '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
        abi     => {
            symbol    => { sig => 'symbol()',           returns => ['string']  },
            balanceOf => { sig => 'balanceOf(address)', returns => ['uint256'] },
        },
    );
    print $weth->call('symbol'), "\n";
    print $weth->call('balanceOf', '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'), "\n";

    # --- sign and send a transaction ---
    my $wallet = $web3->wallet(private_key => '0x...');
    my $erc20  = $web3->contract(
        address => '0x...',
        abi     => { transfer => { sig => 'transfer(address,uint256)', returns => ['bool'] } },
    );
    my $tx_hash = $erc20->send($wallet, 'transfer', $to_address, to_wei('1.5'));

=head1 DESCRIPTION

Web3::Tiny is a minimal Ethereum toolkit: JSON-RPC transport, Solidity
ABI encoding/decoding, secp256k1/Keccak256/RLP for signing legacy

lib/Web3/Tiny/Wallet.pm  view on Meta::CPAN

    }
    return $val;
}

sub _strip_leading_zeros {
    my ($bytes) = @_;
    $bytes =~ s/^\x00+//;
    return $bytes;
}

# Web3::Tiny::Wallet->new(private_key => '0x...' or 32 raw bytes)
sub new {
    my ($class, %opts) = @_;
    die "Web3::Tiny::Wallet: 'private_key' is required\n" unless defined $opts{private_key};

    my $priv = _hex_to_bytes($opts{private_key});
    die "Web3::Tiny::Wallet: private_key must be 32 bytes\n" unless length($priv) == 32;

    my ($x, $y) = privkey_to_pubkey($priv);
    my $addr = pubkey_to_address($x, $y);

    return bless {
        priv    => $priv,
        pub_x   => $x,
        pub_y   => $y,
        address => $addr,
    }, $class;

lib/Web3/Tiny/Wallet.pm  view on Meta::CPAN

__END__

=head1 NAME

Web3::Tiny::Wallet - Private key handling and transaction signing

=head1 SYNOPSIS

    use Web3::Tiny::Wallet;

    my $wallet = Web3::Tiny::Wallet->new(private_key => '0x' . ('11' x 32));
    print $wallet->address, "\n";    # EIP-55 checksummed

    my $raw_tx = $wallet->sign_transaction(
        nonce     => 0,
        gas_price => '20000000000',
        gas       => 21000,
        to        => '0x5B38Da6a701c568545dCfcB03FcB875f56beddC4',
        value     => '1000000000000000000',
        chain_id  => 1,
    );

t/50-wallet.t  view on Meta::CPAN

for my $addr (
    '0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed',
    '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359',
    '0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB',
    '0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb',
) {
    is(Web3::Tiny::Wallet::to_checksum_address(lc $addr), $addr, "EIP-55 checksum for $addr");
}

my $priv_hex = '0x' . ('11' x 32);
my $wallet   = Web3::Tiny::Wallet->new(private_key => $priv_hex);
like($wallet->address, qr/^0x[0-9a-fA-F]{40}$/, 'wallet address looks right shape');

my $to = '35' x 20;
my $raw = $wallet->sign_transaction(
    nonce     => 9,
    gas_price => '20000000000',
    gas       => 21000,
    to        => "0x$to",
    value     => '1000000000000000000',
    chain_id  => 1,



( run in 0.926 second using v1.01-cache-2.11-cpan-6aa56a78535 )