Blockchain-Ethereum-Keystore

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

        $keyfile->write_to_file("...");

    Signing a transaction:

        my $transaction = Blockchain::Ethereum::Transaction::EIP1559->new(
            ...
        );
    
        my $keyfile = Blockchain::Ethereum::Keystore::Keyfile->new;
        $keyfile->import_file("...");
        $keyfile->private_key->sign_transaction($transaction);

    Export private key:

        my $keyfile = Blockchain::Ethereum::Keystore::Keyfile->new;
        $keyfile->import_file("...");
    
        # private key bytes
        print $keyfile->private_key->export;

OVERVIEW

    This module provides a collection of Ethereum wallet management
    utilities.

    Core functionalities:

      * Manage Ethereum keyfiles, facilitating import, export, and password
      change.

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

    $keyfile->write_to_file("...");

Signing a transaction:

    my $transaction = Blockchain::Ethereum::Transaction::EIP1559->new(
        ...
    );

    my $keyfile = Blockchain::Ethereum::Keystore::Keyfile->new;
    $keyfile->import_file("...");
    $keyfile->private_key->sign_transaction($transaction);

Export private key:

    my $keyfile = Blockchain::Ethereum::Keystore::Keyfile->new;
    $keyfile->import_file("...");

    # private key bytes
    print $keyfile->private_key->export;

=head1 OVERVIEW

This module provides a collection of Ethereum wallet management utilities.

Core functionalities:

=over 4

=item * Manage Ethereum keyfiles, facilitating import, export, and password change.

lib/Blockchain/Ethereum/Keystore/Key.pm  view on Meta::CPAN

use Carp;
use Crypt::PK::ECC;
use Crypt::Perl::ECDSA::Parse;
use Crypt::Perl::ECDSA::Utils;
use Crypt::Digest::Keccak256 qw(keccak256);
use Crypt::PRNG              qw(random_bytes);

use Blockchain::Ethereum::Keystore::Key::PKUtil;
use Blockchain::Ethereum::Keystore::Address;

field $private_key :reader :writer :param //= undef;
field $_ecc_handler :reader(_ecc_handler) :writer(set_ecc_handler);

ADJUST {
    # if the private key is not set, generate a new one
    $self->set_private_key(random_bytes(32)) unless defined $self->private_key;

    my $importer = Crypt::PK::ECC->new();
    $importer->import_key_raw($self->private_key, 'secp256k1');

    # Crypt::PK::ECC does not provide support for deterministic keys
    $self->set_ecc_handler(bless Crypt::Perl::ECDSA::Parse::private($importer->export_key_der('private')),
        'Blockchain::Ethereum::Keystore::Key::PKUtil');

}

method sign_transaction ($transaction) {

    croak "transaction must be a reference from Blockchain::Ethereum::Transaction"

lib/Blockchain/Ethereum/Keystore/Keyfile.pm  view on Meta::CPAN

use Blockchain::Ethereum::Keystore::Key;
use Blockchain::Ethereum::Keystore::Keyfile::KDF;

field $cipher :reader :writer;
field $ciphertext :reader :writer;
field $mac :reader :writer;
field $version :reader :writer;
field $iv :reader :writer;
field $kdf :reader :writer;
field $id :reader :writer;
field $private_key :reader :writer;

field $_json :reader(_json) = JSON::MaybeXS->new(utf8 => 1);

method import_file ($file_path, $password) {

    my $content = read_file($file_path);
    my $decoded = $self->_json->decode(lc $content);

    return $self->_from_object($decoded, $password);
}

t/key.t  view on Meta::CPAN

#!/usr/bin/env perl

use strict;
use warnings;

use Test::More;
use Blockchain::Ethereum::Keystore::Key;

subtest "0x008AeEda4D805471dF9b2A5B0f38A0C3bCBA786b" => sub {
    my $private_key = pack "H*", "7a28b5ba57c53603b0b07b56bba752f7784bf506fa95edc395f5cf6c7514fe9d";
    my $key         = Blockchain::Ethereum::Keystore::Key->new(private_key => $private_key);

    is $key->address, '0x008AeEda4D805471dF9b2A5B0f38A0C3bCBA786b';
};

subtest "0x9d8A62f656a8d1615C1294fd71e9CFb3E4855A4F" => sub {
    my $private_key = pack "H*", "4646464646464646464646464646464646464646464646464646464646464646";
    my $key         = Blockchain::Ethereum::Keystore::Key->new(private_key => $private_key);

    is $key->address, '0x9d8A62f656a8d1615C1294fd71e9CFb3E4855A4F';
};

done_testing();

t/keyfile.t  view on Meta::CPAN

#!/usr/bin/env perl

use strict;
use warnings;

use Test::More;
use Blockchain::Ethereum::Keystore::Keyfile;

# https://ethereum.org/pt-br/developers/docs/data-structures-and-encoding/web3-secret-storage/#PBKDF2-SHA-256
subtest "v3_pbkdf2_ctr" => sub {
    my $private_key = pack "H*", "7a28b5ba57c53603b0b07b56bba752f7784bf506fa95edc395f5cf6c7514fe9d";
    my $password    = "testpassword";

    my $keyfile = Blockchain::Ethereum::Keystore::Keyfile->new;

    my $key = $keyfile->import_file("./t/resources/pbkdf2_v3.json", $password);
    is $key->private_key->export, $private_key;

    $key = $keyfile->import_key(Blockchain::Ethereum::Keystore::Key->new(private_key => $private_key), $password);
    is $key->private_key->export, $private_key;
};

# https://ethereum.org/pt-br/developers/docs/data-structures-and-encoding/web3-secret-storage/#scrypt
subtest "v3_scrypt_ctr" => sub {
    my $private_key = pack "H*", "7a28b5ba57c53603b0b07b56bba752f7784bf506fa95edc395f5cf6c7514fe9d";
    my $password    = "testpassword";

    my $keyfile = Blockchain::Ethereum::Keystore::Keyfile->new;

    my $key = $keyfile->import_file("./t/resources/scrypt_v3.json", $password);
    is $key->private_key->export, $private_key;

    $key = $keyfile->import_key(Blockchain::Ethereum::Keystore::Key->new(private_key => $private_key), $password);
    is $key->private_key->export, $private_key;
};

done_testing;

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.892 second using v1.00-cache-2.02-grep-82fe00e-cpan-dad7e4baca0 )