{"id":13650669,"url":"https://github.com/azuchi/bech32rb","last_synced_at":"2025-04-22T18:32:13.702Z","repository":{"id":56842830,"uuid":"90827323","full_name":"azuchi/bech32rb","owner":"azuchi","description":"The implementation of Bech32 encoder and decoder for ruby","archived":false,"fork":false,"pushed_at":"2024-01-11T05:55:31.000Z","size":65,"stargazers_count":18,"open_issues_count":1,"forks_count":10,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-03-24T12:21:28.251Z","etag":null,"topics":["bech32","bech32m","bitcoin"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/azuchi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null},"funding":{"github":"azuchi"}},"created_at":"2017-05-10T06:11:32.000Z","updated_at":"2024-04-14T20:45:39.368Z","dependencies_parsed_at":"2024-01-03T05:09:07.653Z","dependency_job_id":"28b61b4d-3d77-4b52-b105-7f5cc79d6cae","html_url":"https://github.com/azuchi/bech32rb","commit_stats":{"total_commits":38,"total_committers":4,"mean_commits":9.5,"dds":0.4473684210526315,"last_synced_commit":"aaece9171085a633d606b76ee7cab86addaee0f2"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azuchi%2Fbech32rb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azuchi%2Fbech32rb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azuchi%2Fbech32rb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azuchi%2Fbech32rb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/azuchi","download_url":"https://codeload.github.com/azuchi/bech32rb/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223903146,"owners_count":17222494,"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":["bech32","bech32m","bitcoin"],"created_at":"2024-08-02T02:00:39.179Z","updated_at":"2025-04-22T18:32:13.688Z","avatar_url":"https://github.com/azuchi.png","language":"Ruby","funding_links":["https://github.com/sponsors/azuchi"],"categories":["Ruby Libraries"],"sub_categories":[],"readme":"# Bech32 [![Build Status](https://github.com/azuchi/bech32rb/actions/workflows/main.yml/badge.svg?branch=master)](https://github.com/azuchi/bech32rb/actions/workflows/main.yml) [![Gem Version](https://badge.fury.io/rb/bech32.svg)](https://badge.fury.io/rb/bech32) [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE) \u003cimg src=\"http://segwit.co/static/public/images/logo.png\" width=\"100\"\u003e\n\nThe implementation of the Bech32/Bech32m encoder and decoder for Ruby.\n\nBech32 is checksummed base32 format that is used in following Bitcoin address format.\n\nhttps://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki\n\nBech32m is checksummed base32m format that is used in following Bitcoin address format.\n\nhttps://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'bech32'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install bech32\n\n## Usage\n\nRequire the Gem:\n\n```ruby\nrequire 'bech32'\n```\n\n### Decode\n\nDecode Bech32-encoded data into hrp part and data part.\n\n```ruby\nhrp, data, spec = Bech32.decode('BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4')\n\n# hrp is human-readable part of Bech32 format\n'bc'\n\n# data is data part of Bech32 format\n[0, 14, 20, 15, 7, 13, 26, 0, 25, 18, 6, 11, 13, 8, 21, 4, 20, 3, 17, 2, 29, 3, 12, 29, 3, 4, 15, 24, 20, 6, 14, 30, 22]\n\n# spec is whether Bech32::Encoding::BECH32 or Bech32::Encoding::BECH32M\n```\n\n#### Advanced\n\nThe maximum number of characters of Bech32 defined in [BIP-173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki) is limited to 90 characters.\nHowever, LN specification [BOLT#11](https://github.com/lightningnetwork/lightning-rfc/blob/master/11-payment-encoding.md) has no limitation.\n\nTo decode data of more than 90 characters, specify `max_length` at decode as below. (The default value of `max_length` is 90.)\n\n```ruby\nMAX_INTEGER = 2**31 - 1\nBech32.decode(bechString, MAX_INTEGER)\n```\n\nNote that between length of the addresses and the error-detection capabilities are [trade-off](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#checksum-design).\n\n### Encode\n\nEncode Bech32 human-readable part and data part into Bech32 string.\n\n```ruby\nhrp = 'bc'\ndata = [0, 14, 20, 15, 7, 13, 26, 0, 25, 18, 6, 11, 13, 8, 21, 4, 20, 3, 17, 2, 29, 3, 12, 29, 3, 4, 15, 24, 20, 6, 14, 30, 22]\n\nbech = Bech32.encode(hrp, data, Bech32::Encoding::BECH32)\n=\u003e 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4'\n```\n\n### Segwit\n\nDecode Bech32-encoded Segwit address into `Bech32::SegwitAddr` instance.\n\n```ruby\naddr = 'BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4'\nsegwit_addr = Bech32::SegwitAddr.new(addr)\n\n# generate script pubkey\nsegwit_addr.to_script_pubkey\n=\u003e '0014751e76e8199196d454941c45d1b3a323f1433bd6'\n```\n\nEncode Segwit script into Bech32 Segwit address.\n\n```ruby\nsegwit_addr = Bech32::SegwitAddr.new\nsegwit_addr.script_pubkey = '0014751e76e8199196d454941c45d1b3a323f1433bd6'\n\n# generate addr\nsegwit_addr.addr\n=\u003e 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4'\n```\n\n### Silent payment address\n\nDecode silent payment address.\n\n```ruby\naddr = Bech32::SilentPaymentAddr.parse('sp1qqgste7k9hx0qftg6qmwlkqtwuy6cycyavzmzj85c6qdfhjdpdjtdgqjuexzk6murw56suy3e0rd2cgqvycxttddwsvgxe2usfpxumr70xc9pkqwv')\n\n# sp version\naddr.version\n=\u003e 0\n# scan public key\naddr.scan_key\n=\u003e '0220bcfac5b99e04ad1a06ddfb016ee13582609d60b6291e98d01a9bc9a16c96d4'\n# spend public key\naddr.spend_key\n=\u003e '025cc9856d6f8375350e123978daac200c260cb5b5ae83106cab90484dcd8fcf36'\n```\n\nEncode sp address.\n\n```ruby\naddr = Bech32::SilentPaymentAddr.new('sp', 0, '0220bcfac5b99e04ad1a06ddfb016ee13582609d60b6291e98d01a9bc9a16c96d4', '025cc9856d6f8375350e123978daac200c260cb5b5ae83106cab90484dcd8fcf36')\n\n# sp address\naddr.to_s\n=\u003e 'sp1qqgste7k9hx0qftg6qmwlkqtwuy6cycyavzmzj85c6qdfhjdpdjtdgqjuexzk6murw56suy3e0rd2cgqvycxttddwsvgxe2usfpxumr70xc9pkqwv'\n```\n\n### Nostr\n\nSupports encoding/decoding of Nostr's [NIP-19](https://github.com/nostr-protocol/nips/blob/master/19.md) entities.\n\n```ruby\n# Decode bare entity\nbech32 = 'npub10elfcs4fr0l0r8af98jlmgdh9c8tcxjvz9qkw038js35mp4dma8qzvjptg'\nentity = Bech32::Nostr::NIP19.parse(bech32)\nentity.hrp\n=\u003e 'npub'\nentity.data\n=\u003e '7e7e9c42a91bfef19fa929e5fda1b72e0ebc1a4c1141673e2794234d86addf4e'\n\n# Decode tlv entity\nbech32 = 'nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p'\nentity = Bech32::Nostr::NIP19.parse(bech32)\nentity.hrp\n=\u003e 'nprofile'\nentity.entries[0].value\n=\u003e '3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d'\nentity.entries[1].value\n=\u003e 'wss://r.x.com'\n\n# Encode bare entity\nentity = Bech32::Nostr::BareEntity.new('npub', '7e7e9c42a91bfef19fa929e5fda1b72e0ebc1a4c1141673e2794234d86addf4e')\nentity.encode\n=\u003e 'npub10elfcs4fr0l0r8af98jlmgdh9c8tcxjvz9qkw038js35mp4dma8qzvjptg'\n\n# Encode tlv entity\nentry_relay = Bech32::Nostr::TLVEntry.new(Bech32::Nostr::TLVEntity::TYPE_RELAY, 'wss://relay.nostr.example')\nentry_author = Bech32::Nostr::TLVEntry.new(Bech32::Nostr::TLVEntity::TYPE_AUTHOR, '97c70a44366a6535c145b333f973ea86dfdc2d7a99da618c40c64705ad98e322')\nentity = Bech32::Nostr::TLVEntity.new(Bech32::Nostr::NIP19::HRP_EVENT, [entry_relay, entry_author])\nentity.encode\n=\u003e 'nevent1qyvhwumn8ghj7un9d3shjtnwdaehgu3wv4uxzmtsd3jsygyhcu9ygdn2v56uz3dnx0uh865xmlwz675emfsccsxxguz6mx8rygstv78u'\n```\n\n### CLI\n\nAfter installing the gem, the `bech32` command will be available. Encoding and decoding features are also available in the cli. \n\n#### Encode\n\nThe `encode` command takes `HRP`, `data`, and type (`bech32` or `bech32m`) as arguments and outputs a bech32/bech32m string.\n\n    $ bech32 encode genesis 000409190707111719041a120308120a060c161408110d091b090009021b1e1d150e190215010d0a1603091e1f0b100609090d1a1c1a13030c120c0c0f1c020d1d1a0e170b1c17021018121e0a021c121a1c0d161b16131d1609130f1d13180f1e081d041d0a110f110d1313081c11100600180b000c08140c1e130e0f090c160e0018191e1d1c0016060c11101214121e19070d1a0c1c15020414001f10100c09090600181806001809080e180507021913031100030216141b100908160c1213120f1912011f07021c13190d1900170c1d1203040f071c1908050504130e080d0a100d130b03000a0a08191310090918120f11031104120c15180c15120a0f1919070c1c12060c1911091010120a0d191b040c1613020e1911081f000b1a0b0514150c1613010d0c1c030b17011805090119061e0b051e130c00030419000112030110181d011a000f020c0901080c1113060201041a0e1b060a15160506181c11140c1c1d0b1b14020c131e0b0f071519181e0b1f0e0a08170f1d0c05060e1c1b0e160c0f0a171d0b101a1a1f1417160e0d1414030e161b0a000709100d15020c000e010c00000000 bech32\n    genesis1qyfe883hey6jrgj2xvk5g3dfmfqfzm7a4wez4pd2krf7ltsxffd6u6nrvjvv0uzda6whtuhzscj72zuj6udkmknakfn0anc07gaya2303dnngu3sxqctqvg5v7nw0fvkwqce7auqkxv3sj5j7e8d6vu4zy5qlssvffxqccxqcfgwc98zenr3qrzk5msfgkvjnj0ejpl8zunedeqhvajry08ueg99ynwgd2sdntrq22gensffcj03r3yjv4cv4j20ee8vujxve3fssj2demyvknzwe3glqt6t954vknpdvurthpc9fpex7t97nvqryeqpjrpscap6q0zvfpgv3nxzpy6wmx24k9xcu35vuatm5zvn7t084ec7tlw2gh0av9xwumwkv02hats66l5hkwd55rwkm2q8fsd4zvqwpvqqqqtdc6rp\n\nNote: `data` must be converted from 8-bit to 5-bit units.\n\n#### Decode\n\nThe `decode` command takes bech32/bech32m string as arguments and outputs `HRP`, `data`, and type.\n\n    $ bech32 decode genesis1qyfe883hey6jrgj2xvk5g3dfmfqfzm7a4wez4pd2krf7ltsxffd6u6nrvjvv0uzda6whtuhzscj72zuj6udkmknakfn0anc07gaya2303dnngu3sxqctqvg5v7nw0fvkwqce7auqkxv3sj5j7e8d6vu4zy5qlssvffxqccxqcfgwc98zenr3qrzk5msfgkvjnj0ejpl8zunedeqhvajry08ueg99ynwgd2sdntrq22gensffcj03r3yjv4cv4j20ee8vujxve3fssj2demyvknzwe3glqt6t954vknpdvurthpc9fpex7t97nvqryeqpjrpscap6q0zvfpgv3nxzpy6wmx24k9xcu35vuatm5zvn7t084ec7tlw2gh0av9xwumwkv02hats66l5hkwd55rwkm2q8fsd4zvqwpvqqqqtdc6rp\n    HRP: genesis\n    DATA: 000409190707111719041a120308120a060c161408110d091b090009021b1e1d150e190215010d0a1603091e1f0b100609090d1a1c1a13030c120c0c0f1c020d1d1a0e170b1c17021018121e0a021c121a1c0d161b16131d1609130f1d13180f1e081d041d0a110f110d1313081c11100600180b000c08140c1e130e0f090c160e0018191e1d1c0016060c11101214121e19070d1a0c1c15020414001f10100c09090600181806001809080e180507021913031100030216141b100908160c1213120f1912011f07021c13190d1900170c1d1203040f071c1908050504130e080d0a100d130b03000a0a08191310090918120f11031104120c15180c15120a0f1919070c1c12060c1911091010120a0d191b040c1613020e1911081f000b1a0b0514150c1613010d0c1c030b17011805090119061e0b051e130c00030419000112030110181d011a000f020c0901080c1113060201041a0e1b060a15160506181c11140c1c1d0b1b14020c131e0b0f071519181e0b1f0e0a08170f1d0c05060e1c1b0e160c0f0a171d0b101a1a1f1417160e0d1414030e161b0a000709100d15020c000e010c00000000\n    TYPE: bech32\n\nIf bech32 string has segwit hrp, it will also output witness version and witness program:\n\n    $ bech32 decode bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kt5nd6y\n    HRP: bc\n    DATA: 010e140f070d1a001912060b0d081504140311021d030c1d03040f1814060e1e160e140f070d1a001912060b0d081504140311021d030c1d03040f1814060e1e16\n    TYPE: bech32m\n    WITNESS VERSION: 1\n    WITNESS PROGRAM: 751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd6\n\nIf bech32 string has NIP-19 hrp, it will also output NIP-19 entry:\n\n    $ bech32 decode nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p\n    HRP: nprofile\n    DATA: 00001003171c0606071f051906111114001e17190f090f051d19120f1502011d02011f0f13190a18081c16041d0e0d0a151b1d040b07080101151b17061c191a051c1717040b1318051911161e1b080102151b17061c191a051c1716081a13020c0519121c1c1b010c111516040b13030d1d1610\n    TYPE: bech32\n    NIP19 Entities:\n      special: 3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d\n      relay: wss://r.x.com\n      relay: wss://djbas.sadkb.com\n\nIf sp address, it will also output scan/spend public key:\n\n    $ bech32 decode sp1qqgste7k9hx0qftg6qmwlkqtwuy6cycyavzmzj85c6qdfhjdpdjtdgqjuexzk6murw56suy3e0rd2cgqvycxttddwsvgxe2usfpxumr70xc9pkqwv\n    HRP: sp\n    DATA: 000008100b191e160517060f00090b081a001b0e1f16000b0e1c041a180418041d0c021b02120714181a000d0917120d010d120b0d0800121c190602161a1b1c030e141a101c0411190f030d0a1808000c0418060b0b0d0d0e100c0806190a1c100901061c1b031e0f0618\n    TYPE: bech32m\n    version: 0\n    scan public key: 0220bcfac5b99e04ad1a06ddfb016ee13582609d60b6291e98d01a9bc9a16c96d4\n    spend public key: 025cc9856d6f8375350e123978daac200c260cb5b5ae83106cab90484dcd8fcf36\n\nNote: `DATA` is data without bit conversion. When used, the `DATA` must be converted from 5-bit to 8-bit and padded as necessary.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fazuchi%2Fbech32rb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fazuchi%2Fbech32rb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fazuchi%2Fbech32rb/lists"}