bech32 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 746f17b48aafeb175a66ea2d5e5d75adcb2a4bc7
4
+ data.tar.gz: 345ef0c51a1e399871d316e196a7d25e7fa6caf2
5
+ SHA512:
6
+ metadata.gz: 8b8df48e2f10f6a00e27e3f9f19332e83eacd8aca2659299b24508a59f688ebfad9585a126373396a9e69fab53a3790d99f0d0b732f1d19621a4964fb5cf0aca
7
+ data.tar.gz: 114e4c62ec06132002b30c17b51884875d475f5d33aaec2c4a73436a331180984f10e644ea3762d0c3d4302d71503fe801fdd7622c9eb9e4b8d8752482ac5c2f
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1 @@
1
+ bech32
@@ -0,0 +1 @@
1
+ 2.3.0
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1
4
+ - 2.2.2
5
+ - 2.3.0
6
+ - 2.4.0
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at azuchi@haw.co.jp. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bech32.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Shigeyuki Azuchi
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.
@@ -0,0 +1,83 @@
1
+ # Bech32 [![Build Status](https://travis-ci.org/azuchi/bech32rb.svg?branch=master)](https://travis-ci.org/azuchi/bech32rb) [![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)
2
+
3
+ The implementation of the Bech32 encoder and decoder for Ruby.
4
+
5
+ Bech32 is checksummed base32 format that is used in following Bitcoin address format.
6
+
7
+ https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'bech32'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install bech32
24
+
25
+ ## Usage
26
+
27
+ Require the Gem:
28
+
29
+ ```ruby
30
+ require 'bech32'
31
+ ```
32
+
33
+ ### Decode
34
+
35
+ Decode Bech32-encoded data into hrp part and data part.
36
+
37
+ ```ruby
38
+ hrp, data = Bech32.decode('BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4')
39
+
40
+ # hrp is human-readable part of Bech32 format
41
+ 'bc'
42
+
43
+ # data is data part of Bech32 format
44
+ [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]
45
+ ```
46
+
47
+ Decode Bech32-encoded Segwit address into `Bech32::SegwitAddr` instance.
48
+
49
+ ```ruby
50
+ segwit_addr = Bech32::SegwitAddr.new(addr)
51
+
52
+ # generate script pubkey
53
+ segwit_addr.to_script_pubkey
54
+ => 0014751e76e8199196d454941c45d1b3a323f1433bd6
55
+ ```
56
+
57
+ ### Encode
58
+
59
+ Encode Bech32 human-readable part and data part into Bech32 string.
60
+
61
+ ```ruby
62
+ hrp = 'bc'
63
+ data = [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]
64
+
65
+ bech = Bech32.encode(hrp, data)
66
+ => bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4
67
+ ```
68
+
69
+ Encode Segwit script into Bech32 Segwit address.
70
+
71
+ ```ruby
72
+ segwit_addr = Bech32::SegwitAddr.new
73
+ segwit_addr.script_pubkey = '0014751e76e8199196d454941c45d1b3a323f1433bd6'
74
+
75
+ # generate addr
76
+ segwit_addr.addr
77
+ => bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4
78
+ ```
79
+
80
+ ## License
81
+
82
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
83
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "bech32"
7
+ spec.version = "1.0.0"
8
+ spec.authors = ["Shigeyuki Azuchi"]
9
+ spec.email = ["azuchi@haw.co.jp"]
10
+
11
+ spec.summary = %q{The implementation of Bech32 encoder and decoder.}
12
+ spec.description = %q{The implementation of Bech32 encoder and decoder.}
13
+ spec.homepage = "https://github.com/azuchi/bech32rb"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec", "~> 3.0"
23
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "bech32"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,85 @@
1
+ module Bech32
2
+
3
+ autoload :SegwitAddr, 'bech32/segwit_addr'
4
+
5
+ SEPARATOR = '1'
6
+
7
+ CHARSET = %w(q p z r y 9 x 8 g f 2 t v d w 0 s 3 j n 5 4 k h c e 6 m u a 7 l)
8
+
9
+ module_function
10
+
11
+ # Returns the encoded Bech32 string.
12
+ #
13
+ # require 'bech32'
14
+ #
15
+ # bech = Bech32.encode('bc', [])
16
+ #
17
+ # <i>Generates:</i>
18
+ # 'BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4' # bech
19
+ #
20
+ def encode(hrp, data)
21
+ checksummed = data + create_checksum(hrp, data)
22
+ hrp + SEPARATOR + checksummed.map{|i|CHARSET[i]}.join
23
+ end
24
+
25
+ # Returns the Bach32 decoded hrp and data.
26
+ #
27
+ # require 'bech32'
28
+ #
29
+ # addr = 'BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4'
30
+ # hrp, data = Bech32.decode(addr)
31
+ #
32
+ # <i>Generates:</i>
33
+ # 'bc' # hrp
34
+ # [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] # data
35
+ #
36
+ def decode(bech)
37
+ # check uppercase/lowercase
38
+ return nil if (bech.downcase != bech && bech.upcase != bech)
39
+ bech.each_char{|c|return nil if c.ord < 33 || c.ord > 126}
40
+ bech = bech.downcase
41
+ # check data length
42
+ pos = bech.rindex(SEPARATOR)
43
+ return nil if pos.nil? || pos + 7 > bech.length || bech.length > 90
44
+ # check valid charset
45
+ bech[pos+1..-1].each_char{|c|return nil unless CHARSET.include?(c)}
46
+ # split hrp and data
47
+ hrp = bech[0..pos-1]
48
+ data = bech[pos+1..-1].each_char.map{|c|CHARSET.index(c)}
49
+ # check checksum
50
+ return nil unless verify_checksum(hrp, data)
51
+ [hrp, data[0..-7]]
52
+ end
53
+
54
+ # Returns computed checksum values of +hrp+ and +data+
55
+ def create_checksum(hrp, data)
56
+ values = expand_hrp(hrp) + data
57
+ polymod = polymod(values + [0, 0, 0, 0, 0, 0]) ^ 1
58
+ (0..5).map{|i|(polymod >> 5 * (5 - i)) & 31}
59
+ end
60
+
61
+ # Verify a checksum given Bech32 string
62
+ def verify_checksum(hrp, data)
63
+ polymod(expand_hrp(hrp) + data) == 1
64
+ end
65
+
66
+ # Expand the hrp into values for checksum computation.
67
+ def expand_hrp(hrp)
68
+ hrp.each_char.map{|c|c.ord >> 5} + [0] + hrp.each_char.map{|c|c.ord & 31}
69
+ end
70
+
71
+ # Compute Bech32 checksum
72
+ def polymod(values)
73
+ generator = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3]
74
+ chk = 1
75
+ values.each do |v|
76
+ top = chk >> 25
77
+ chk = (chk & 0x1ffffff) << 5 ^ v
78
+ (0..4).each{|i|chk ^= ((top >> i) & 1) == 0 ? 0 : generator[i]}
79
+ end
80
+ chk
81
+ end
82
+
83
+ private_class_method :polymod, :expand_hrp
84
+
85
+ end
@@ -0,0 +1,67 @@
1
+ module Bech32
2
+
3
+ class SegwitAddr
4
+ attr_accessor :hrp # human-readable part
5
+ attr_accessor :ver # witness version
6
+ attr_accessor :prog # witness program
7
+
8
+ def initialize(addr = nil)
9
+ @hrp = 'bc'
10
+ parse_addr(addr) if addr
11
+ end
12
+
13
+ # Returns segwit script pubkey which generated from witness version and witness program.
14
+ def to_script_pubkey
15
+ v = ver == 0 ? ver : ver + 0x80
16
+ ([v, prog.length].pack("CC") + prog.map{|p|[p].pack("C")}.join).unpack('H*').first
17
+ end
18
+
19
+ # parse script pubkey into witness version and witness program
20
+ def script_pubkey=(script_pubkey)
21
+ values = [script_pubkey].pack('H*').unpack("C*")
22
+ @ver = values[0]
23
+ @prog = values[2..-1]
24
+ end
25
+
26
+ # Returns segwit address string which generated from hrp, witness version and witness program.
27
+ def addr
28
+ Bech32.encode(hrp, [ver] + convert_bits(prog, 8, 5))
29
+ end
30
+
31
+ private
32
+
33
+ def parse_addr(addr)
34
+ @hrp, data = Bech32.decode(addr)
35
+ raise 'Invalid address.' if @hrp.nil? || (@hrp != 'bc' && @hrp != 'tb')
36
+ @ver = data[0]
37
+ raise 'Invalid witness version' if @ver > 16
38
+ @prog = convert_bits(data[1..-1], 5, 8, false)
39
+ raise 'Invalid witness program' if @prog.nil? || @prog.length < 2 || @prog.length > 40
40
+ raise 'Invalid witness program with version 0' if @ver == 0 && (@prog.length != 20 && @prog.length != 32)
41
+ end
42
+
43
+ def convert_bits(data, from, to, padding=true)
44
+ acc = 0
45
+ bits = 0
46
+ ret = []
47
+ maxv = (1 << to) - 1
48
+ max_acc = (1 << (from + to - 1)) - 1
49
+ data.each do |v|
50
+ return nil if v < 0 || (v >> from) != 0
51
+ acc = ((acc << from) | v) & max_acc
52
+ bits += from
53
+ while bits >= to
54
+ bits -= to
55
+ ret << ((acc >> bits) & maxv)
56
+ end
57
+ end
58
+ if padding
59
+ ret << ((acc << (to - bits)) & maxv) unless bits == 0
60
+ elsif bits >= from || ((acc << (to - bits)) & maxv) != 0
61
+ return nil
62
+ end
63
+ ret
64
+ end
65
+
66
+ end
67
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bech32
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Shigeyuki Azuchi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-05-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '10.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '10.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ description: The implementation of Bech32 encoder and decoder.
42
+ email:
43
+ - azuchi@haw.co.jp
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".ruby-gemset"
51
+ - ".ruby-version"
52
+ - ".travis.yml"
53
+ - CODE_OF_CONDUCT.md
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - bech32.gemspec
59
+ - bin/console
60
+ - bin/setup
61
+ - lib/bech32.rb
62
+ - lib/bech32/segwit_addr.rb
63
+ homepage: https://github.com/azuchi/bech32rb
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.5.1
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: The implementation of Bech32 encoder and decoder.
87
+ test_files: []