Sha256: 07c45064501c0a000586e976c8710ef5fb05d1cc278b57ac3c59af12217debc3

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

#!/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)
    hrp, data, spec = Bech32.decode(bech32_str, bech32_str.length)
    if hrp
      puts <<~EOS
        HRP: #{hrp}
        DATA: #{data}
        TYPE: #{spec == Bech32::Encoding::BECH32 ? 'bech32' : 'bech32m'}
      EOS
      if [Bech32::SegwitAddr::HRP_MAINNET, Bech32::SegwitAddr::HRP_TESTNET, Bech32::SegwitAddr::HRP_REGTEST].include?(hrp)
        addr = Bech32::SegwitAddr.new(bech32_str)
        puts <<~EOS
          WITNESS VERSION: #{addr.ver}
          WITNESS PROGRAM: #{addr.prog.pack('C*').unpack1('H*')}
        EOS
      elsif Bech32::Nostr::NIP19::ALL_PREFIXES.include?(hrp)
        entity = Bech32::Nostr::NIP19.decode(bech32_str)
        if entity.is_a?(Bech32::Nostr::BareEntity)
          puts <<~EOS
            NIP19 Entity: #{entity.data}
          EOS
        else
          puts 'NIP19 Entities:'
          entity.entries.each do |entry|
            puts "  #{entry.to_s}"
          end
        end
      end
    else
      puts 'Can not decode.'
    end
  end

end

CLI.start(ARGV)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bech32-1.4.1 exe/bech32