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.

PREDEFINED NETWORKS

There are a couple of networks that are already defined and can be used without defining them:

  • Bitcoin Mainnet

    defined with id: bitcoin

  • Bitcoin Testnet

    defined with id: bitcoin_testnet

If you want to see more predefined networks added and you're willing to make some research to find out the correct values for the configuration fields, consider opening a pull request on Github.

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",
                wif_byte       => "(*) WIF private key prefix byte, eg. 0x80",
                p2sh_byte      => "p2sh address prefix byte, eg. 0x05",
                segwit_hrp     => "segwit native address human readable part, eg. 'bc'",
                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

        $network_object = $class->register(%config)
        $network_object = $object->register()

Adds the network instance to a list of known networks.

Calls "new" with keys present in %config hash when called in static context.

Returns the network instance.

set_default

        $network_object = $object->set_default()

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

Returns the network instance.

new

        $network_object = $class->new(%config)

Creates a new network instance. See "CONFIGURATION" for a list of possible %config keys.

get

        $network_object = $class->get($id = undef)

Without arguments, returns the default network configuration as 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

        @network_objects = $class->find($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 of network instances.

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