Blockchain-Ethereum-RLP

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

SYNOPSIS

    Allow RLP encoding and decoding

        my $rlp = Blockchain::Ethereum::RLP->new();
    
        my $tx_params  = ['0x9', '0x4a817c800', '0x5208', '0x3535353535353535353535353535353535353535', '0xde0b6b3a7640000', '0x', '0x1', '0x', '0x'];
        my $encoded = $rlp->encode($params); #ec098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a764000080018080
    
        my $encoded_tx_params = 'ec098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a764000080018080';
        my $decoded = $rlp->decode(pack "H*", $encoded_tx_params); #['0x9', '0x4a817c800', '0x5208', '0x3535353535353535353535353535353535353535', '0xde0b6b3a7640000', '0x', '0x1', '0x', '0x']

METHODS

 encode

    Encodes the given input to RLP

      * $input hexadecimal string or reference to an hexadecimal string
      array

lib/Blockchain/Ethereum/RLP.pm  view on Meta::CPAN

=head1 SYNOPSIS

Allow RLP encoding and decoding

    my $rlp = Blockchain::Ethereum::RLP->new();

    my $tx_params  = ['0x9', '0x4a817c800', '0x5208', '0x3535353535353535353535353535353535353535', '0xde0b6b3a7640000', '0x', '0x1', '0x', '0x'];
    my $encoded = $rlp->encode($params); #ec098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a764000080018080

    my $encoded_tx_params = 'ec098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a764000080018080';
    my $decoded = $rlp->decode(pack "H*", $encoded_tx_params); #['0x9', '0x4a817c800', '0x5208', '0x3535353535353535353535353535353535353535', '0xde0b6b3a7640000', '0x', '0x1', '0x', '0x']

=head1 METHODS

=head2 encode

Encodes the given input to RLP

=over 4

=item * C<$input> hexadecimal string or reference to an hexadecimal string array

t/eip-155-example.t  view on Meta::CPAN

    # unsigned
    my $params  = ['0x9', '0x4a817c800', '0x5208', '0x3535353535353535353535353535353535353535', '0xde0b6b3a7640000', '0x', '0x1', '0x', '0x'];
    my $encoded = $rlp->encode($params);

    is(
        unpack("H*", $encoded),
        'ec098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a764000080018080',
        'encoding for unsigned transaction ok'
    );

    my $decoded = $rlp->decode($encoded);

    is_deeply($params, $decoded, 'decoding for unsigned transaction ok');

    #signed
    $params = [
        '0x9', '0x4a817c800', '0x5208', '0x3535353535353535353535353535353535353535',
        '0xde0b6b3a7640000', '0x', '0x25',
        '0x28ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa636276',
        '0x67cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d83'
    ];

    $encoded = $rlp->encode($params);

    is(
        unpack("H*", $encoded),
        'f86c098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a76400008025a028ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa636276a067cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d83',
        'encoding for signed transaction ok'
    );

    $decoded = $rlp->decode($encoded);

    is_deeply($params, $decoded, 'decoding for signed transaction ok');
};

done_testing;

t/ethereum-org-example.t  view on Meta::CPAN

    my $rlp = Blockchain::Ethereum::RLP->new();

    my $dog   = unpack "H*", "dog";
    my $cat   = unpack "H*", "cat";
    my $lorem = unpack "H*", "Lorem ipsum dolor sit amet, consectetur adipisicing elit";

    my $encoded  = $rlp->encode($dog);
    my $expected = "83$dog";
    is(unpack("H*", $encoded), $expected, "correct encoding for dog");

    my $decoded = $rlp->decode($encoded);
    is_deeply $decoded, '0x' . $dog, "correct decoding for dog";

    my $cat_dog = [$cat, $dog];
    $encoded  = $rlp->encode($cat_dog);
    $expected = "c883@{[$cat]}83$dog";
    is(unpack("H*", $encoded), $expected, "correct encoding for cat dog");

    $decoded = $rlp->decode($encoded);
    is_deeply $decoded, ['0x' . $cat, '0x' . $dog], "correct decoding for cat dog";

    $encoded  = $rlp->encode('');
    $expected = "80";
    is(unpack("H*", $encoded), $expected, "correct encoding for empty string");

    $decoded = $rlp->decode($encoded);
    is_deeply $decoded, '0x', "correct decoding for empty string";

    $encoded  = $rlp->encode([]);
    $expected = "c0";
    is(unpack("H*", $encoded), $expected, "correct encoding for empty array reference");

    $decoded = $rlp->decode($encoded);
    is_deeply $decoded, [], "correct decoding for empty array reference";

    $encoded  = $rlp->encode('0');
    $expected = '80';
    is(unpack("H*", $encoded), $expected, "correct encoding for empty = integer 0");

    $decoded = $rlp->decode($encoded);
    is_deeply $decoded, '0x', "correct decoding for empty array empty = integer 0";

    $encoded = $rlp->encode('0x0');
    # 0 is set as null
    $expected = '80';
    is(unpack("H*", $encoded), $expected, "correct encoding for hexadecimal integer 0");

    $decoded = $rlp->decode($encoded);
    is_deeply $decoded, '0x', "correct decoding for hexadecimal integer 0";

    $encoded  = $rlp->encode(sprintf("0x%x", 15));
    $expected = '0f';
    is(unpack("H*", $encoded), $expected, "correct encoding for hexadecimal integer 15");

    $decoded = $rlp->decode($encoded);
    is_deeply $decoded, sprintf("0x%x", 15), "correct decoding for hexadecimal integer 15";

    $encoded  = $rlp->encode(sprintf("0x%x", 1024));
    $expected = '820400';
    is(unpack("H*", $encoded), $expected, "correct encoding for hexadecimal integer 1024");

    $decoded = $rlp->decode($encoded);
    is_deeply $decoded, sprintf("0x%x", 1024), "correct decoding for hexadecimal integer 1024";

    $encoded  = $rlp->encode([[], [[]], [[], [[]]]]);
    $expected = "c7c0c1c0c3c0c1c0";
    is(unpack("H*", $encoded), $expected, "correct encoding for set theoretical representation of three");

    $decoded = $rlp->decode($encoded);
    is_deeply $decoded, [[], [[]], [[], [[]]]], "correct decoding for set theoretical representation of three";

    $encoded  = $rlp->encode($lorem);
    $expected = "b838$lorem";
    is(unpack("H*", $encoded), $expected, "correct encoding for lorem");

    $decoded = $rlp->decode($encoded);
    is_deeply $decoded, '0x' . $lorem, "correct decoding for lorem";
};

done_testing;

t/etherscan_raw_txs.t  view on Meta::CPAN

use Blockchain::Ethereum::RLP;
my $rlp = Blockchain::Ethereum::RLP->new();

# https://etherscan.io/tx/0x784412e6b8ce1e6fcce97d149dea5367a594aef61d6d328398fffac0bb3ab537
subtest "etherscan raw tx => tether transfer" => sub {
    # 0x02 removed from raw tx since the transaction type is not part of the RLP encoding
    my $raw_tx =
        'f8b3018305c6c98405f5e100851d4adefbd882fde894dac17f958d2ee523a2206206994597c13d831ec780b844a9059cbb000000000000000000000000f8dd85f9dae9caee1c3560284715e8933add0de70000000000000000000000000000000000000000000000000000000004c50220c080a0b55239aaa...
    my $tether_transfer = $rlp->decode(pack "H*", $raw_tx);

    my $decoded = [
        '0x1',                                                                   # nonce
        '0x5c6c9',                                                               # chain id
        '0x5f5e100',                                                             # max priority fee per gas
        '0x1d4adefbd8',                                                          # max base fee per gas
        '0xfde8',                                                                # gas limit
        '0xdac17f958d2ee523a2206206994597c13d831ec7',                            # tether contract
        '0x',                                                                    # value
        '0xa9059cbb000000000000000000000000f8dd85f9dae9caee1c3560284715e8933add0de70000000000000000000000000000000000000000000000000000000004c50220'
        ,                                                                        # data
        [],                                                                      # access list
        '0x',                                                                    # v
        '0xb55239aaa8b77ddb8982509ab2bffa71f7b6a313017e4c71c560b7cb57a01c58',    # r
        '0x3f9d03d40ded1c2978a81a1c3c566b830a6a2f884ba238d4c095190793b28d7c'     # s
    ];

    is_deeply $tether_transfer, $decoded;

    is unpack("H*", $rlp->encode($tether_transfer)), $raw_tx;
};

# https://etherscan.io/tx/0x85fdf30e7ce3884b0b6ae4955416bb1dc48eedeef11ed5e4ef8e185fc21b0f4e
subtest "etherscan raw tx => eth transfer" => sub {
    my $raw_tx =
        'f8730181ae8405f5e100850aa1b5e50382520894004af85ea96fd3771ed2e1df6f2b152bc81b47c087ce911cd5adbf7680c080a0e045e7d62f53c4bb03c330a17ba909936f783da5d04af2587fc2d1c48d7e7a15a05554e82b338819fcb288fc28fb1f685b1e84eb5c897bb1c4a4b62a7877c313e2';

    my $eth_transfer = $rlp->decode(pack "H*", $raw_tx);

    my $decoded = [
        '0x1',    #
        '0xae',
        '0x5f5e100',
        '0xaa1b5e503',
        '0x5208',
        '0x04af85ea96fd3771ed2e1df6f2b152bc81b47c0',
        '0xce911cd5adbf76',
        '0x',
        [],
        '0x',
        '0xe045e7d62f53c4bb03c330a17ba909936f783da5d04af2587fc2d1c48d7e7a15',
        '0x5554e82b338819fcb288fc28fb1f685b1e84eb5c897bb1c4a4b62a7877c313e2'
    ];

    is_deeply $eth_transfer, $decoded;

    is unpack("H*", $rlp->encode($eth_transfer)), $raw_tx;

};

done_testing;



( run in 0.431 second using v1.01-cache-2.11-cpan-26ccb49234f )