cardano-bech32 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 61879ba04ac1f386b78a11a2627b3517b503153030fa714066e46d63ffaf1ca6
4
+ data.tar.gz: 5fdac5c7012890bb2eec29d1ed10162806c13657e7db539b05f38583312eb8da
5
+ SHA512:
6
+ metadata.gz: eaba40171f236c8887414261a9063c9139c016331a1ed37984684c145655dd701c43a0963eb99888f83c2b6a27bf2c1d7a845c88e78582ff2f45b38910b6b8fd
7
+ data.tar.gz: b9f1737243afdfb79c5d19748c7cbbbabcab019ae29262569dc01c79ec21c4e4f3bcddfb1500dac5b6a3d0ef25bce23796ee88a20e1b33b494bd3dfffe69620a
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-12-29
4
+
5
+ ### Added
6
+ - Initial release of `cardano-bech32`
7
+ - Encoding of Cardano Governance Action IDs (`gov_action`) as defined in CIP-0129
8
+ - Decoding of `gov_action` Bech32 identifiers into transaction ID and index
9
+ - Validation helpers for governance action identifiers
10
+ - RSpec test suite with normative CIP-0129 test vectors
@@ -0,0 +1,10 @@
1
+ # Code of Conduct
2
+
3
+ "cardano_bech32" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
4
+
5
+ * Participants will be tolerant of opposing views.
6
+ * Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
7
+ * When interpreting the words and actions of others, participants should always assume good intentions.
8
+ * Behaviour which can be reasonably considered harassment will not be tolerated.
9
+
10
+ If you have any concerns about behaviour within this project, please contact us at ["robin.boening@gmail.com"](mailto:"robin.boening@gmail.com").
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Robin Böning
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,112 @@
1
+ # CardanoBech32
2
+
3
+ A small, focused Ruby library for encoding and decoding Cardano Bech32 identifiers.
4
+ This gem deliberately avoids higher-level ledger concerns and focuses solely on correct, specification-compliant Bech32 handling.
5
+
6
+ Currently supported
7
+
8
+ - [x] Governance Action IDs (gov_action) — [CIP-0129](https://cips.cardano.org/cip/CIP-0129)
9
+ - [ ] Addresses
10
+ - [ ] Stake pool IDs
11
+ - [ ] DRep IDs
12
+
13
+ The API is designed to grow conservatively as additional Cardano Improvement Proposals (CIPs) are implemented.
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application’s Gemfile:
18
+
19
+ ```sh
20
+ gem "cardano-bech32"
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ ```sh
26
+ bundle install
27
+ ```
28
+
29
+ Or install directly:
30
+
31
+ ```sh
32
+ gem install cardano-bech32
33
+ ```
34
+
35
+ ## Usage
36
+
37
+ ### Governance Action IDs (CIP-0129)
38
+
39
+ A Governance Action ID is derived as:
40
+ tx_id (32 bytes) || index (1 byte)
41
+ → Bech32 encode with HRP "gov_action"
42
+
43
+ #### Encoding
44
+
45
+ ```ruby
46
+ require "cardano_bech32"
47
+
48
+ txref = "b2a591ac219ce6dcca5847e0248015209c7cb0436aa6bd6863d0c1f152a60bc5#0"
49
+
50
+ CardanoBech32::GovAction.encode(txref)
51
+ # => "gov_action1k2jertppnnndejjcglszfqq4yzw8evzrd2nt66rr6rqlz54xp0zsq05ecsn"
52
+ ```
53
+
54
+ #### Decoding
55
+
56
+ ```ruby
57
+ CardanoBech32::GovAction.decode(
58
+ "gov_action1k2jertppnnndejjcglszfqq4yzw8evzrd2nt66rr6rqlz54xp0zsq05ecsn"
59
+ )
60
+ # => {
61
+ tx_id: "b2a591ac219ce6dcca5847e0248015209c7cb0436aa6bd6863d0c1f152a60bc5",
62
+ index: 0
63
+ }
64
+ ```
65
+
66
+ #### Validation
67
+
68
+ Validation checks:
69
+ * Bech32 checksum
70
+ * Correct HRP (gov_action)
71
+ * Correct payload length (33 bytes)
72
+
73
+ ```ruby
74
+ CardanoBech32::GovAction.valid?("gov_action1k2jertppnnndejjcglszfqq4yzw8evzrd2nt66rr6rqlz54xp0zsq05ecsn")
75
+ # => true
76
+ ```
77
+
78
+ ### Error Handling
79
+
80
+ The library normalizes all Bech32 decoding and validation failures into
81
+ explicit, typed errors:
82
+
83
+ * InvalidFormat — malformed input, wrong HRP, invalid Bech32
84
+ * InvalidPayload — incorrect byte length, invalid index
85
+
86
+ ```ruby
87
+ # Example
88
+ begin
89
+ CardanoBech32::GovAction.encode("invalid")
90
+ rescue CardanoBech32::GovAction::Error => e
91
+ puts e.message
92
+ end
93
+
94
+ ```
95
+
96
+ ## Development
97
+
98
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
99
+
100
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
101
+
102
+ ## Contributing
103
+
104
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cardano_bech32. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/cardano_bech32/blob/main/CODE_OF_CONDUCT.md).
105
+
106
+ ## License
107
+
108
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
109
+
110
+ ## Code of Conduct
111
+
112
+ Everyone interacting in the CardanoBech32 project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/cardano_bech32/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bech32"
4
+
5
+ module CardanoBech32
6
+ # Bech32 encoding for governance action references.
7
+ # A governance action reference is of the form "txid#index"
8
+ # where txid is a 32-byte hex string and index is a 0..255 integer
9
+ # The Bech32 encoding has HRP "gov_action" and a payload of 33 bytes:
10
+ # - 32 bytes: txid (binary)
11
+ # - 1 byte: index (binary)
12
+ #
13
+ module GovAction
14
+ HRP = "gov_action"
15
+ TX_ID_BYTES = 32
16
+ INDEX_BYTES = 1
17
+ TOTAL_BYTES = TX_ID_BYTES + INDEX_BYTES
18
+
19
+ class Error < StandardError; end
20
+ class InvalidFormat < Error; end
21
+ class InvalidPayload < Error; end
22
+
23
+ def self.encode(tx_ref)
24
+ txid_hex, index_str = tx_ref.split("#", 2)
25
+ raise InvalidFormat, "expected txid#index" unless txid_hex && index_str
26
+
27
+ txid_bytes = hex_to_bytes(txid_hex)
28
+ index = Integer(index_str)
29
+
30
+ raise InvalidPayload, "txid must be 32 bytes" unless txid_bytes.bytesize == TX_ID_BYTES
31
+ raise InvalidPayload, "index must be 0..255" unless index.between?(0, 255)
32
+
33
+ payload = txid_bytes + [index].pack("C")
34
+
35
+ # Convert to 5-bit array
36
+ # args: data (array of integers), from_bits, to_bits, pad (boolean)
37
+ data_5bit = Bech32.convert_bits(payload.bytes, 8, 5, true)
38
+ Bech32.encode(HRP, data_5bit, Bech32::Encoding::BECH32)
39
+ end
40
+
41
+ def self.decode(bech32)
42
+ hrp, data = Bech32.decode(bech32)
43
+ raise InvalidFormat, "invalid HRP" unless hrp == HRP
44
+
45
+ # Convert to 5-bit array
46
+ # args: data (array of integers), from_bits, to_bits, pad (boolean)
47
+ bytes = Bech32.convert_bits(data, 5, 8, false)
48
+ raise InvalidPayload, "invalid payload length" unless bytes.length == TOTAL_BYTES
49
+
50
+ {
51
+ tx_id: bytes_to_hex(bytes[0, TX_ID_BYTES]),
52
+ index: bytes.last
53
+ }
54
+ rescue ArgumentError
55
+ raise InvalidFormat, "invalid bech32 encoding"
56
+ end
57
+
58
+ def self.valid?(bech32)
59
+ decode(bech32)
60
+ true
61
+ rescue Error
62
+ false
63
+ end
64
+
65
+ def self.hex_to_bytes(hex)
66
+ raise InvalidFormat, "invalid hex" unless hex.match?(/\A[0-9a-fA-F]{64}\z/)
67
+
68
+ [hex].pack("H*")
69
+ end
70
+
71
+ def self.bytes_to_hex(bytes)
72
+ bytes.pack("C*").unpack1("H*")
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CardanoBech32
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "cardano_bech32/version"
4
+ require_relative "cardano_bech32/gov_action"
5
+
6
+ module CardanoBech32
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+ end
@@ -0,0 +1,4 @@
1
+ module CardanoBech32
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cardano-bech32
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Robin Böning
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: bech32
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: 1.5.0
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: 1.5.0
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '3.13'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.13'
40
+ description: |
41
+ A small, focused Ruby library for encoding and decoding Cardano Bech32 identifiers.
42
+ The gem intentionally limits its scope to specification-compliant Bech32
43
+ handling and avoids higher-level ledger or transaction concerns.
44
+ email:
45
+ - robin.boening@gmail.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - CHANGELOG.md
51
+ - CODE_OF_CONDUCT.md
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - lib/cardano_bech32.rb
56
+ - lib/cardano_bech32/gov_action.rb
57
+ - lib/cardano_bech32/version.rb
58
+ - sig/cardano_bech32.rbs
59
+ homepage: https://github.com/lacepool/cardano-bech32
60
+ licenses:
61
+ - MIT
62
+ metadata:
63
+ homepage_uri: https://github.com/lacepool/cardano-bech32
64
+ source_code_uri: https://github.com/lacepool/cardano-bech32
65
+ changelog_uri: https://github.com/lacepool/cardano-bech32/blob/main/CHANGELOG.md
66
+ rubygems_mfa_required: 'true'
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 3.2.0
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubygems_version: 4.0.3
82
+ specification_version: 4
83
+ summary: Bech32 encoding and decoding for Cardano identifiers
84
+ test_files: []