View Source Examples

livebook-examples

LiveBook examples

Interact with Bitcoin straight from your browser with these Livebook documents.

prerequisite

Prerequisite

Let's define some aliases, to simplify this document.

alias BitcoinLib.Key.{PrivateKey, PublicKey, PublicKeyHash, Address}
alias BitcoinLib.Key.HD.SeedPhrase

seed-phrase-generation-from-dice-rolls

Seed phrase generation from dice rolls

Randomness is of utmost importance when creating Bitcoin keys. That's why we use multiple dice rolls to make sure private keys are unique. There's a choice of 50 or 99 dice rolls to generate a 12 or 24 word seed phrase.

{:ok, seed_phrase} =
  "12345612345612345612345612345612345612345612345612"
  |> SeedPhrase.from_dice_rolls()
{:ok, "blue involve cook print twist crystal razor february caution private slim medal"}

master-private-key-from-a-seed-phrase

Master private key from a seed phrase

The master private key can monitor and spend funds on a range of addresses.

master_private_key = 
  seed_phrase
  |> PrivateKey.from_seed_phrase()
%BitcoinLib.Key.PrivateKey{
  key: <<0xe870b890337e36ad866db34824b47a026db4a03f7f2df48eaf6ede61b1fcbfea::256>>,
  chain_code: <<0xda54909d3b6b377d2654e0887596b49f8e36c4300fdb9eb9e3e43a47dceda789::256>>,
  depth: 0,
  index: 0,
  parent_fingerprint: <<0x00000000::32>>,
  fingerprint: <<0x2aafc40e::32>>
}

master-public-key-from-a-master-private-key

Master public key from a master private key

master_public_key =
  master_private_key
  |> PublicKey.from_private_key()
%BitcoinLib.Key.PublicKey{
  key: <<0x03254ed681b40913a4a9c4dc22b920f4bf56cc93ad442f8f9f7e976e166fe9cc56::264>>,
  uncompressed_key: <<0x04254ed681b40913a4a9c4dc22b920f4bf56cc93ad442f8f9f7e976e166fe9cc56ce17fd672862c25f33812262e2792f3082ae91e5480e8cad2a483a018bd7c0d1::520>>,
  chain_code: <<0xda54909d3b6b377d2654e0887596b49f8e36c4300fdb9eb9e3e43a47dceda789::256>>,
  depth: 0,
  index: 0,
  parent_fingerprint: <<0x00000000::32>>,
  fingerprint: <<0x2aafc40e::32>>
}

generate-a-p2pkh-from-a-public-key

Generate a P2PKH from a public key

address =
  master_public_key
  |> PublicKeyHash.from_public_key()
  |> Address.from_public_key_hash(:mainnet)
"14thwUvxWWJRZipWUgk1j45LG8qpWu8AxH"