NAME
Bitcoin::Crypto::Network - Management tool for cryptocurrency networks
SYNOPSIS
use Bitcoin::Crypto::Network qw(:all);
# by default network is set to bitcoin
get_default_network()->{name}; # Bitcoin Mainnet
# by default there are two networks specified
# these are identified with keys which you can get with
get_available_networks(); # (mainnet, testnet)
# you can get other network configuration
get_network("testnet")->{name}; # Bitcoin Testnet
# search for the network and get array of keys in return
# there will be multiple results if your search can be matched
# by multiple networks
find_network(name => "Bitcoin Mainnet"); # (mainnet)
find_network(p2pkh_byte => 0x6f); # (testnet)
# if you're working with different cryptocurrency you need to add a new network
# network configuration is important for importing WIF private keys (network
# recognition), generating addresses and serializing extended keys.
# Don't use addresses without validating your configuration first!
# I suggest creating networks by changing values in Bitcoin network
# this way some of the rather default values will be inherited
# configuration keys shown below are required
my $litecoin = get_network("mainnet");
$litecoin->{name} = "Litecoin Mainnet";
$litecoin->{p2pkh_byte} = 0x30;
$litecoin->{wif_byte} = 0xb0;
add_network(litecoin_mainnet => $litecoin);
# 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 will use that
# configuration.
set_default_network("litecoin_mainnet");
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. Right now networks only require three keys, which are marked with *
my $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",
);
After you add_network 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_network 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("your_network");
Some things to consider:
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.
it is entirely possible to add a network that already exists. Because of this, if you don't need bitcoin in your program you can replace existing networks with custom ones.
get_network functions make clones of network configuration at the time of creation, so changing configuration after you've created your keys may not bring the results you're expecting. You probably shouldn't be doing this anyway, but if for some reason you need to update your configuration then you need to either re-create all private and public keys or use set_network method on them all.
FUNCTIONS
set_default_network
set_default_network("network_key");
Sets the network with $name as default one. All newly created private and public keys will be bound to this network. Dies if network doesn't exist
get_default_network
$hashref = get_default_network();
Returns deep clone of currently active network's configuration.
add_network
add_network(name => $hashref);
Adds network "name" with configuration from $hashref. Performs $hashref validation (same as validate_network)
validate_network
validate_network($hashref);
Validates network configuration under $hashref. Dies if configuration is invalid.
find_network
my @found = find_network(key => $value)
Searches for all networks that have configuration "key" set to $value. Returns list. Dies if key doesn't exist.
get_network
my $hashref = get_network($name);
Returns network $name configuration. If $name is omitted behaves like get_default_network(). Dies if network $name doesn't exist.
get_available_networks
my @names = get_available_networks();
Returns all available network names.