btctool v0.1.1 BtcTool View Source

Bitcoin utils related to Elliptic curve cryptography (ECC) algorithms used in bitcoin to create addresses or public keys from private keys, brainwallets, WIFs, etc.

Link to this section Summary

Types

Wallet Import Format including the base58check containing the private key

Functions

Create Wallet Import Format (WIF) private key from raw private key. A raw private key can be presented by a binary of 32 bytes or in 64 hexadecimal characters (0-9a-fA-F)

Link to this section Types

Link to this type wif_type() View Source
wif_type() :: %{wif: <<_::408>> | <<_::416>>, network: :testnet | :mainnet, compressed: boolean()}

Wallet Import Format including the base58check containing the private key.

WIF will be a base58check string of 51 characters (408 bits) if user want to use uncompressed public keys in the bitcoin addresses or 52 characters (416 bits) if want to use compressed public keys.

Metadata like network or compressed can also be deducted from the WIP string, but make them visible anyway here:

  • network. Which is instended to be used on.
  • compressed. Which state if a compressed public key will be used

Link to this section Functions

Link to this function privkey_to_wif(privkey, options \\ []) View Source
privkey_to_wif(<<_::512>> | <<_::256>>, [{atom(), any()}]) ::
  {:ok, wif_type()} |
  {:error, atom()}

Create Wallet Import Format (WIF) private key from raw private key. A raw private key can be presented by a binary of 32 bytes or in 64 hexadecimal characters (0-9a-fA-F)

It assumes you want the compressed WIF version by default. That way you are signalling that the bitcoin address which should be used when imported into a wallet will be also compressed.

Options

  • compressed - Generate a WIF which signals that a compressed public key should be used if true. Deafault is true.
  • network - Specifies the network is this private key intended to be used on. Can be :mainnet or :testnet. Default is :mainnet.
  • case - Specifies the character case to accept when decoding. Valid values are: :upper, :lower, :mixed. Only useful when the raw private key is passed in hex format. Default is :mixed

Examples

iex> hexprivkey = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"
iex> binprivkey = hexprivkey |> Base.decode16!()
<<1, 35, 69, 103, 137, 171, 205, 239, 1, 35, 69, 103, 137, 171, 205, 239, 1, 35, 69, 103, 137, 171, 205, 239, 1, 35, 69, 103, 137, 171, 205, 239>>
iex> privkey_to_wif(hexprivkey)
{:ok, %{
  wif: "KwFvTne98E1t3mTNAr8pKx67eUzFJWdSNPqPSfxMEtrueW7PcQzL",
  compressed: true,
  network: :mainnet
}}
iex> privkey_to_wif(binprivkey)
{:ok, %{
  wif: "KwFvTne98E1t3mTNAr8pKx67eUzFJWdSNPqPSfxMEtrueW7PcQzL",
  compressed: true,
  network: :mainnet
}}
iex> privkey_to_wif(binprivkey, compressed: false, network: :testnet)
{:ok, %{
  wif: "91bRE5Duv5h8kYhhTLhYRXijCiXWSpWwFNX6nndfuntBdPV2idD",
  compressed: false,
  network: :testnet
}}

When binary private key has an unexpected length (not 64 bytes for hex format or 32 bytes for binary format) returns error:

iex> privkey_to_wif(<<1, 35, 69>>)
{:error, :incorrect_privkey_length}

When private key is out of recommended range will return error:

iex> maxprivkey = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364140" |> Base.decode16!()
iex> privkey_to_wif(maxprivkey)
{:error, :ecc_out_range}