The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Bitcoin::Crypto::Network - Management tool for cryptocurrency networks

SYNOPSIS

        use Bitcoin::Crypto::Network;

        # by default network is set to bitcoin
        # get() without arguments returns default network

        Bitcoin::Crypto::Network->get->name; # Bitcoin Mainnet

        # by default there are two networks specified
        # find() without arguments returns a list of all network ids

        Bitcoin::Crypto::Network->find; # (mainnet, testnet)

        # you can get full network configuration with get() using network id

        Bitcoin::Crypto::Network->get("bitcoin_testnet")->name; # Bitcoin Testnet

        # search for network and get array of keys in return
        # there will be multiple results if your search is matched
        # by multiple networks

        Bitcoin::Crypto::Network->find(sub { shift->name eq "Bitcoin Mainnet" }); # (mainnet)
        Bitcoin::Crypto::Network->find(sub { shift->p2pkh_byte eq "\x6f" }); # (testnet)

        # if you're working with cryptocurrency other than Bitcoin you need to add a new network

        # network configuration is important for importing WIF private keys (network
        # recognition), generating addresses and serializing extended keys.
        # It may also hold other data specific to a network

        # register() can be used to create a network

        my $litecoin = Bitcoin::Crypto::Network->register(
                id => "litecoin",
                name => "Litecoin Mainnet",
                p2pkh_byte => "\x30",
                wif_byte => "\xb0",
        );

        # after you've added your network you can set it as default. This means that
        # all extended keys generated by other means than importing serialized key and
        # all private keys generated by other means than importing WIF / extended keys
        # will use that configuration.

        $litecoin->set_default;

DESCRIPTION

This package allows you to manage non-bitcoin cryptocurrencies. Before you start producing keys and addresses for your favorite crypto you have to configure it's network first.

CONFIGURATION

Right now networks only require four keys, which are marked with (*)

        my %config = (
                id             => "(*) identifier for the network",
                name           => "(*) human-readable network name",
                p2pkh_byte     => "(*) p2pkh address prefix byte, eg. 0x00",
                p2sh_byte      => "p2sh address prefix byte, eg. 0x05",
                segwit_hrp     => "segwit native address human readable part, eg. 'bc'",
                wif_byte       => "(*) WIF private key prefix byte, eg. 0x80",
                extprv_version => "version of extended private keys, eg. 0x0488ade4",
                extpub_version => "version of extended public keys, eg. 0x0488b21e",
                bip44_coin     => "bip44 coin number, eg. 0",
        );

After you register a network with this hashref your program will be able to import keys for that network but all keys created from other sources will be treated as Bitcoin. You need to set_default to make all new keys use it. If you use many networks it might be better to set a network with key's set_network method:

        $priv->set_network("network_id");

Remember that if you don't specify network field for some feature you won't be able to use it. For example the module will complain if you try to generate segwit address with custom network without segwit_hrp field set.

METHODS

register

        sig: register($self, %config)

Calls Moose's new with keys present in $config hash when called in static context. Adds the newly created network instance or the one that the method was called on to a list of known networks. The hash %config is ignored in object context.

set_default

        sig: set_default($self)

Sets the network as default one. All newly created private and public keys will be bound to this network.

STATIC METHODS

new

        sig: new($class, %config)

Works the same as register does in static context, but does not add the newly created network to a list of known networks.

get

        sig: get($class, $id = undef)

Without arguments, returns default network configuration, the Bitcoin::Crypto::Network instance With the $id argument (string), returns the instance of a configuration matching the id. Throws an exception if network doesn't exist.

find

        sig: find($class, $sub = undef)

Without arguments, returns a list of all registered network ids. With the $sub argument (coderef), searches for all networks that pass the criteria and returns their ids. Returns list.

The $sub will be passed all the instances of registered networks, one at a time. If must perform required checks and return a boolean value. All the networks that pass this test will be returned. Example:

        sub {
                my $instance = shift;
                return $instance->name eq "Some name";
        }

SEE ALSO

Bitcoin::Crypto::Key::ExtPrivate
Bitcoin::Crypto::Key::Private