{"id":17939411,"url":"https://github.com/libitx/bsv-ex","last_synced_at":"2025-03-24T10:32:08.861Z","repository":{"id":45977803,"uuid":"201769104","full_name":"libitx/bsv-ex","owner":"libitx","description":"Elixir toolset for building Bitcoin applications","archived":false,"fork":false,"pushed_at":"2022-03-22T10:33:01.000Z","size":938,"stargazers_count":29,"open_issues_count":1,"forks_count":8,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-15T22:46:47.963Z","etag":null,"topics":["bitcoin","bsv","elixir"],"latest_commit_sha":null,"homepage":"https://hexdocs.pm/bsv","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/libitx.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-11T13:36:57.000Z","updated_at":"2025-01-05T16:01:47.000Z","dependencies_parsed_at":"2022-07-20T03:48:27.631Z","dependency_job_id":null,"html_url":"https://github.com/libitx/bsv-ex","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/libitx%2Fbsv-ex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/libitx%2Fbsv-ex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/libitx%2Fbsv-ex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/libitx%2Fbsv-ex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/libitx","download_url":"https://codeload.github.com/libitx/bsv-ex/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245252426,"owners_count":20585060,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bitcoin","bsv","elixir"],"created_at":"2024-10-29T00:07:14.299Z","updated_at":"2025-03-24T10:32:08.208Z","avatar_url":"https://github.com/libitx.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BSV\n\n![BSV-ex](https://github.com/libitx/bsv-ex/raw/master/media/poster.png)\n\n![Hex.pm](https://img.shields.io/hexpm/v/bsv?color=informational)\n![License](https://img.shields.io/github/license/libitx/bsv-ex?color=informational)\n![Build](https://img.shields.io/github/workflow/status/libitx/bsv-ex/Elixir%20CI)\n\nBSV-ex is a general purpose library for building Bitcoin SV applications in Elixir. Under the hood, [Curvy](https://hexdocs.pm/curvy) is used for all `secp256k1` flavoured crypto, making BSV-ex the only pure Elixir Bitcoin library.\n\n## Features\n\nCurrently supported features:\n\n- Keypair generation and address encoding and decoding\n- BIP-39 mnemonic phrase generation and BIP-32 hierarchical deterministic keys\n- Script and smart contract builder with built in Script simulator\n- Transaction builder, signing and verification\n- Block and block header parsing and serialization\n- Merkle proof parsing, serialization and verification\n- Bitcoin Signed Messages and ECIES encryption (Electrum compatible)\n\n## Installation\n\nThe package can be installed by adding `bsv` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:bsv, \"~\u003e 2.1\"}\n  ]\nend\n```\n\n## Upgrading\n\nVersion `2.x` of this library is so significantly different to version `0.x` that we skipped an entire version. This is a rewrite from top to bottom, with an entirely new API, and makes no attempt to maintain backwards compatibility.\n\nIf upgrading from a previous version then be prepared to update your code where it interfaces with this library. If this library is depended on by other third party dependencies, then check each dependency and make sure it has been upgraded to support version `2.x`.\n\nMain changes:\n\n* Many modules have been renamed to follow the conventions established in [Money Button's bsv JavaScript library](https://github.com/moneybutton/bsv).\n* `from_binary/2` and `to_binary/2` functions replace the old `parse/2` and `serialize/2` functions.\n* The old `Crypto` namespace and modules have been removed. Crypto code unrelated to Bitcoin is no longer maintained. Hash functions can be found in the `BSV.Hash` module, and ECDSA functions are provided by `Curvy`.\n* An entirely new script and smart contract building API has been created.\n\n## Configuration\n\nOptionally, BSV can be configured for testnet network by editing your application's configuration:\n\n```elixir\nconfig :bsv,\n  network: :test  # defaults to :main\n```\n\n## Usage\n\nBSV-ex is a comprehensive Bitcoin library. Many examples can be found through [the documentation](https://hexdocs.pm/bsv). See the following for some quick-start examples:\n\n### Keypairs, Addresses, BIP-32\n\nGenerate a new random keypair and derive its address.\n\n```elixir\niex\u003e keypair = BSV.KeyPair.new()\n%BSV.KeyPair{\n  privkey: %BSV.PrivKey{\n    compressed: true,\n    d: \u003c\u003c119, 134, 104, 227, 196, 255, 3, 163, 39, 9, 0, 43, 84, 137, 55, 218, 146, 182, 246, 3, 18, 64, 159, 108, 46, 24, 108, 111, 239, 180, 74, 161\u003e\u003e\n  },\n  pubkey: %BSV.PubKey{\n    compressed: true,\n    point: %Curvy.Point{\n      x: 80675204119348790085831157628459085855227400073327708725575496785606354176436,\n      y: 9270420727654506759611377999115473532064051910093243567168505762017618809348\n    }\n  }\n}\n\niex\u003e address = BSV.Address.from_pubkey(keypair.pubkey)\niex\u003e BSV.Address.to_string(address)\n\"19D5DoRKchdZbsP3fXYhopbFDdCJCPaLjr\"\n```\n\nGenerate a BIP-32 HD wallet, derive and child and its address.\n\n```elixir\niex\u003e mnemonic = BSV.Mnemonic.new()\n\"taste canvas eternal brain rent cement fat dilemma duty fame floor defy\"\n\niex\u003e seed = BSV.Mnemonic.to_seed(mnemonic)\niex\u003e extkey = BSV.ExtKey.from_seed!(seed)\n%BSV.ExtKey{\n  chain_code: \u003c\u003c110, 26, 215, 117, 61, 123, 141, 33, 144, 225, 219, 244, 190, 61, 102, 123, 48, 131, 110, 209, 3, 193, 247, 57, 46, 72, 196, 13, 33, 189, 61, 6\u003e\u003e,\n  child_index: 0,\n  depth: 0,\n  fingerprint: \u003c\u003c0, 0, 0, 0\u003e\u003e,\n  privkey: %BSV.PrivKey{\n    compressed: true,\n    d: \u003c\u003c177, 226, 248, 91, 203, 59, 219, 9, 27, 117, 171, 67, 62, 138, 86, 122, 9, 215, 241, 4, 118, 97, 110, 174, 141, 2, 86, 116, 186, 32, 155, 133\u003e\u003e\n  },\n  pubkey: %BSV.PubKey{\n    compressed: true,\n    point: %Curvy.Point{\n      x: 13957581247370416663735268664956755789623055115850818561656783044351458532461,\n      y: 13811705978617564383043442008879108616838570537839984416253146804416417872149\n    }\n  },\n  version: \u003c\u003c4, 136, 173, 228\u003e\u003e\n}\n\n# Derive child key and address\niex\u003e child = BSV.ExtKey.derive(extkey, \"m/0/1\")\niex\u003e address = BSV.Address.from_pubkey(child.pubkey)\niex\u003e BSV.Address.to_string(address)\n\"1Cax2dCtapJZtwzYXCdLuTkZ1egG8JSugA\"\n```\n\n### Building transactions\n\nThe `TxBuilder` module provides a simple declarative way to build any type of transaction.\n\n```elixir\niex\u003e alias BSV.Contract.{P2PKH, OpReturn}\n\niex\u003e utxo = BSV.UTXO.from_params(utxo_params)\niex\u003e builder = %BSV.TxBuilder{\n...\u003e   inputs: [\n...\u003e     P2PKH.unlock(utxo, %{keypair: keypair})\n...\u003e   ],\n...\u003e   outputs: [\n...\u003e     P2PKH.lock(10000, %{address: address}),\n...\u003e     OpReturn.lock(0, %{data: [\"hello\", \"world\"]})\n...\u003e   ]\n...\u003e }\n\niex\u003e tx = BSV.TxBuilder.to_tx(builder)\niex\u003e rawtx = BSV.Tx.to_binary(tx, encoding: :hex)\n\"0100000001121a9ac1e0...\"\n```\n\n### Creating custom contracts\n\nThe `BSV.Contract` module provides a way to define a locking script and unlocking script in a pure Elixir function.\n\n```elixir\n# Define a module that implements the `Contract` behaviour\ndefmodule P2PKH do\n  use BSV.Contract\n  \n  def locking_script(ctx, %{address: address}) do\n    ctx\n    |\u003e op_dup\n    |\u003e op_hash160\n    |\u003e push(address.pubkey_hash)\n    |\u003e op_equalverify\n    |\u003e op_checksig\n  end\n\n  def unlocking_script(ctx, %{keypair: keypair}) do\n    ctx\n    |\u003e sig(keypair.privkey)\n    |\u003e push(BSV.PubKey.to_binary(keypair.pubkey))\n  end\nend\n```\n\nContracts can be tested using the built-in simulator.\n\n```elixir\niex\u003e keypair = BSV.KeyPair.new()\niex\u003e lock_params = %{address: BSV.Address.from_pubkey(keypair.pubkey)}\niex\u003e unlock_params = %{keypair: keypair}\niex\u003e {:ok, vm} = BSV.Contract.simulate(P2PKH, lock_params, unlock_params)\niex\u003e BSV.VM.valid?(vm)\ntrue\n```\n\n## License\n\nBSV-ex is open source and released under the [Apache-2 License](https://github.com/libitx/bsv-elixir/blob/master/LICENSE).\n\n© Copyright 2019-2021 Chronos Labs Ltd.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flibitx%2Fbsv-ex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flibitx%2Fbsv-ex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flibitx%2Fbsv-ex/lists"}