From 0031788d7338c8a5bec944a8ef56f5d463b20592 Mon Sep 17 00:00:00 2001 From: azuchi Date: Thu, 1 Jul 2021 22:00:04 +0900 Subject: [PATCH] Add CLI --- README.md | 16 ++++++++++++++++ bech32.gemspec | 23 ++++++++++++----------- exe/bech32 | 31 +++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 11 deletions(-) create mode 100755 exe/bech32 diff --git a/README.md b/README.md index 82ee5c6..d9e7c8b 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,22 @@ segwit_addr.addr => bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4 ``` +### CLI + +After installing the gem, the `bech32` command will be available. Encoding and decoding features are also available in the cli. + +#### Encode + + $ bech32 encode genesis 000409190707111719041a120308120a060c161408110d091b090009021b1e1d150e190215010d0a1603091e1f0b100609090d1a1c1a13030c120c0c0f1c020d1d1a0e170b1c17021018121e0a021c121a1c0d161b16131d1609130f1d13180f1e081d041d0a110f110d1313081c11100600180b000c08140c1e130e0f090c160e0018191e1d1c0016060c11101214121e19070d1a0c1c15020414001f10100c09090600181806001809080e180507021913031100030216141b100908160c1213120f1912011f07021c13190d1900170c1d1203040f071c1908050504130e080d0a100d130b03000a0a08191310090918120f11031104120c15180c15120a0f1919070c1c12060c1911091010120a0d191b040c1613020e1911081f000b1a0b0514150c1613010d0c1c030b17011805090119061e0b051e130c00030419000112030110181d011a000f020c0901080c1113060201041a0e1b060a15160506181c11140c1c1d0b1b14020c131e0b0f071519181e0b1f0e0a08170f1d0c05060e1c1b0e160c0f0a171d0b101a1a1f1417160e0d1414030e161b0a000709100d15020c000e010c00000000 bech32 + genesis1qyfe883hey6jrgj2xvk5g3dfmfqfzm7a4wez4pd2krf7ltsxffd6u6nrvjvv0uzda6whtuhzscj72zuj6udkmknakfn0anc07gaya2303dnngu3sxqctqvg5v7nw0fvkwqce7auqkxv3sj5j7e8d6vu4zy5qlssvffxqccxqcfgwc98zenr3qrzk5msfgkvjnj0ejpl8zunedeqhvajry08ueg99ynwgd2sdntrq22gensffcj03r3yjv4cv4j20ee8vujxve3fssj2demyvknzwe3glqt6t954vknpdvurthpc9fpex7t97nvqryeqpjrpscap6q0zvfpgv3nxzpy6wmx24k9xcu35vuatm5zvn7t084ec7tlw2gh0av9xwumwkv02hats66l5hkwd55rwkm2q8fsd4zvqwpvqqqqtdc6rp + +# Decode + + $ bech32 decode genesis1qyfe883hey6jrgj2xvk5g3dfmfqfzm7a4wez4pd2krf7ltsxffd6u6nrvjvv0uzda6whtuhzscj72zuj6udkmknakfn0anc07gaya2303dnngu3sxqctqvg5v7nw0fvkwqce7auqkxv3sj5j7e8d6vu4zy5qlssvffxqccxqcfgwc98zenr3qrzk5msfgkvjnj0ejpl8zunedeqhvajry08ueg99ynwgd2sdntrq22gensffcj03r3yjv4cv4j20ee8vujxve3fssj2demyvknzwe3glqt6t954vknpdvurthpc9fpex7t97nvqryeqpjrpscap6q0zvfpgv3nxzpy6wmx24k9xcu35vuatm5zvn7t084ec7tlw2gh0av9xwumwkv02hats66l5hkwd55rwkm2q8fsd4zvqwpvqqqqtdc6rp + HRP: genesis + DATA: 000409190707111719041a120308120a060c161408110d091b090009021b1e1d150e190215010d0a1603091e1f0b100609090d1a1c1a13030c120c0c0f1c020d1d1a0e170b1c17021018121e0a021c121a1c0d161b16131d1609130f1d13180f1e081d041d0a110f110d1313081c11100600180b000c08140c1e130e0f090c160e0018191e1d1c0016060c11101214121e19070d1a0c1c15020414001f10100c09090600181806001809080e180507021913031100030216141b100908160c1213120f1912011f07021c13190d1900170c1d1203040f071c1908050504130e080d0a100d130b03000a0a08191310090918120f11031104120c15180c15120a0f1919070c1c12060c1911091010120a0d191b040c1613020e1911081f000b1a0b0514150c1613010d0c1c030b17011805090119061e0b051e130c00030419000112030110181d011a000f020c0901080c1113060201041a0e1b060a15160506181c11140c1c1d0b1b14020c131e0b0f071519181e0b1f0e0a08170f1d0c05060e1c1b0e160c0f0a171d0b101a1a1f1417160e0d1414030e161b0a000709100d15020c000e010c00000000 + TYPE: bech32 + ## License The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). diff --git a/bech32.gemspec b/bech32.gemspec index e7f3294..dae2a67 100644 --- a/bech32.gemspec +++ b/bech32.gemspec @@ -4,21 +4,22 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'bech32/version' Gem::Specification.new do |spec| - spec.name = "bech32" + spec.name = 'bech32' spec.version = Bech32::VERSION - spec.authors = ["Shigeyuki Azuchi"] - spec.email = ["azuchi@haw.co.jp"] + spec.authors = ['Shigeyuki Azuchi'] + spec.email = ['techmedia.think@gmail.com'] - spec.summary = %q{The implementation of Bech32 encoder and decoder.} - spec.description = %q{The implementation of Bech32 encoder and decoder.} - spec.homepage = "https://github.com/azuchi/bech32rb" - spec.license = "MIT" + spec.summary = %q(The implementation of Bech32 encoder and decoder.) + spec.description = %q(The implementation of Bech32 encoder and decoder.) + spec.homepage = 'https://github.com/azuchi/bech32rb' + spec.license = 'MIT' spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } - spec.bindir = "exe" + spec.bindir = 'exe' spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } - spec.require_paths = ["lib"] + spec.require_paths = ['lib'] + spec.add_runtime_dependency 'thor', '>= 1.1.0' - spec.add_development_dependency "rake", ">= 12.3.3" - spec.add_development_dependency "rspec", "~> 3.0" + spec.add_development_dependency 'rake', '>= 12.3.3' + spec.add_development_dependency 'rspec', '~> 3.0' end \ No newline at end of file diff --git a/exe/bech32 b/exe/bech32 new file mode 100755 index 0000000..150c8d4 --- /dev/null +++ b/exe/bech32 @@ -0,0 +1,31 @@ +#!/usr/bin/env ruby + +require 'bech32' +require 'thor' + +class CLI < Thor + + desc 'encode hrp data bech32/bech32m', 'Encoding data to bech32/bech32m string.' + def encode(hrp, data, type) + if type && !['bech32', 'bech32m'].include?(type) + puts 'Invalid type value. type must be bech32 or bech32m.' + else + spec = type.nil? || type == 'bech32' ? 1 : 2 + data = [data].pack('H*').unpack('C*') + puts Bech32.encode(hrp, data, spec) + end + end + + desc 'decode "bech32string"', 'Decoding bech32/bech32m string.' + def decode(bech32_str) + bech32 = Bech32.decode(bech32_str, bech32_str.length) + puts <<~EOS + HRP: #{bech32[0]} + DATA: #{bech32[1].pack('C*').unpack1('H*')} + TYPE: #{bech32[2] == Bech32::Encoding::BECH32 ? 'bech32' : 'bech32m'} + EOS + end + +end + +CLI.start(ARGV)