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 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) and generating addresses. Don't use addresses without validating
  # your configuration first!

  # keys below are required. You can add extra keys and it'll work, but you won't
  # be able to use find_network function with them

  my $litecoin = {
      name => "Litecoin Mainnet",
      p2pkh_byte => 0x30,
      wif_byte => 0xb0
  };
  add_network(litecoin_mainnet => $litecoin);

  # after you've added your network you can set it as default. This means that
  # all private keys generated by other means than WIF importing 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 use three keys, but they may grow in the future (for p2sh, bech32 etc.)

  my $network = (
      name => "human-readable network name",
      p2pkh_byte => "p2pkh address prefix byte, eg. 0x00",
      wif_byte => "WIF private key prefix byte, eg. 0x80"
  );

After you add_network your program will be able to import WIF 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 setNetwork method:

  $priv->setNetwork("your_network");

Some things to consider:

  • 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 setNetwork method on them all.

FUNCTIONS

set_default_network($str)

Sets the network with $name as default one. All newly created private and public keys will be bound to this network. Croaks if network doesn't exist

get_default_network()

Returns deep clone of currently active network's configuration.

add_network(name => $hashref)

Adds network "name" with configuration from $hashref. Performs $hashref validation.

validate_network($hashref)

Validates network configuration under $hashref. Croaks if configuration is invalid.

find_network(key => $value)

Searches for all networks that have configuration "key" set to $value. Returns list. Croaks if key doesn't exist.

get_network($name)

Returns network $name configuration. If $name is omitted behaves like get_default_network(). Croaks if network $name doesn't exist.

get_available_networks()

Returns all available network names.

SEE ALSO

Bitcoin::Crypto::PrivateKey
Bitcoin::Crypto::PublicKey