Code Monkey home page Code Monkey logo

adequate_crypto_address's Introduction

AdequateCryptoAddress

Gem Version Build Status Code Climate Coverage Status Downloads Docs License

Simple wallet address validator and normalizer for cryptocurrencies addresses in Ruby.

Inspired by ognus/wallet-address-validator.

Installation

Add this line to your application's Gemfile:

gem 'adequate_crypto_address'

Or install it yourself as:

gem install adequate_crypto_address

Main API

.valid? (address, currency [, type = :prod])
Parameters
  • address - Wallet address to validate.
  • currency - Currency name string or symbol in any case, :bitcoin or 'BTC' or :btc or 'BitCoin'
  • type - Optional. You can enforce validation with specific type. Not all currencies support types.

Returns true if the address (string) is a valid wallet address for the crypto currency specified, see below for supported currencies.

Supported crypto currencies

  • Bitcoin/BTC, 'bitcoin' or 'BTC' types: :segwit_v0_keyhash :segwit_v0_scripthash :hash160 :p2sh
  • BitcoinCash/BCH, 'bitcoincash' or 'BCH' types: :p2sh :p2pkh :p2pkhtest :p2shtest
  • Dash, 'dash' or 'DASH' types: :prod :test
  • Zcash/ZEC, 'zcash' or 'ZEC' types: :prod :test
  • Ethereum/ETH, 'ethereum' or 'ETH'
  • Ripple/XRP, 'ripple' or 'XRP'
  • Toncoin, 'TON'
  • Monero/XRM, 'monero'

Usage

Validation

require 'adequate_crypto_address'
# BTC
AdequateCryptoAddress.valid?('12QeMLzSrB8XH8FvEzPMVoRxVAzTr5XM2y', 'BTC') #=> true
AdequateCryptoAddress.valid?('3NJZLcZEEYBpxYEUGewU4knsQRn1WM5Fkt', :bitcoin, :p2sh) #=> true

# BCH
AdequateCryptoAddress.valid?('bitcoincash:qrtj3rd8524cndt2eew3s6wljqggmne00sgh4kfypk', :bch) #=> true
AdequateCryptoAddress.valid?('mmRH4e9WW4ekZUP5HvBScfUyaSUjfQRyvD', :BCH, :p2pkhtest) #=> true

# ETH
AdequateCryptoAddress.valid?('0xde709f2102306220921060314715629080e2fb77', :ETH) #=> true
AdequateCryptoAddress.valid?('de709f2102306220921060314715629080e2fb77', :ethereum) #=> true

Normalization

*Not all currencies support this feature.
require 'adequate_crypto_address'

# BCH
AdequateCryptoAddress.address('mmRH4e9WW4ekZUP5HvBScfUyaSUjfQRyvD', 'bch').cash_address #=> "bchtest:qpqtmmfpw79thzq5z7s0spcd87uhn6d34uqqem83hf"
AdequateCryptoAddress.address('bitcoincash:qrtj3rd8524cndt2eew3s6wljqggmne00sgh4kfypk', 'bch').legacy_address #=> "1LcerwTc1oPsMtByDCNUXFxReZpN1EXHoe"

address_string = 'qrtj3rd8524cndt2eew3s6wljqggmne00sgh4kfypk'
addr = AdequateCryptoAddress.address(address_string, 'bch')
addr.prefix #=> "bitcoincash"
addr.type #=> :p2pkh
addr.address #=> "bitcoincash:qrtj3rd8524cndt2eew3s6wljqggmne00sgh4kfypk"

# ETH
AdequateCryptoAddress.address('D1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb', 'eth').address #=> "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb"

ActiveRecord validation example

class Model < ActiveRecord::Base
  attribute :address, :string
  attribute :dest_tag, :string
  attribute :currency, :string

  validate  :validate_address_type
  validate  :validate_destination_tag

  def validate_address_type
    errors.add(:address, 'invalid address') unless AdequateCryptoAddress.valid?(address, currency)
  end

  # for Ripple
  def validate_destination_tag
    errors.add(:dest_tag, 'invalid destination tag') if dest_tag.present? && !(dest_tag =~ /\A\d{1,10}\z/)
  end
end

Add your currnecy

# frozen_string_literal: true
# for Rails /config/initializers/adequate_crypto_address.rb
module AdequateCryptoAddress
  class Coin
    attr_reader :address

    def initialize(address_sring)
      @address = address_sring
    end

    def valid?(_type)
      address.present?
    end
  end
end

AdequateCryptoAddress.valid?('addr', :coin) #=> true

Development

Run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

Contributing

  1. Fork the repo
  2. Grab dependencies: bundle install
  3. Make sure everything is working: bundle exec rake spec
  4. Make your changes
  5. Test your changes
  6. Create a Pull Request
  7. Celebrate!!!!!

Notes

Bug reports and pull requests are welcome on GitHub at https://github.com/vtm9/adequate_crypto_address/issues

adequate_crypto_address's People

Contributors

nepod1988 avatar q9f avatar vtm9 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

adequate_crypto_address's Issues

AdequateCryptoAddress::UnknownCurrency: Wrong currency btc

AdequateCryptoAddress.valid?('12QeMLzSrB8XH8FvEzPMVoRxVAzTr5XM2y', 'BTC')
Any response on that? why i am facing this issue while having testing btc testnet!
errors.add(:address, 'invalid address') unless AdequateCryptoAddress.valid?(address, currency)

Code gap between this repo and rubygems?

Today I tried to run this gem on ruby 3 and it's doesn't install

#Gemfile
gem 'adequate_crypto_address'

sh > bundle install
...
adequate_crypto_address (~> 0.1.5) was resolved to 0.1.5, which depends on
      digest-sha3 (~> 1.1.0) was resolved to 1.1.0, which depends on
        Ruby (~> 2.2)

I looked to gemspec and there is no digest-sha3 dependency
But digest-sha3 dependency exists on rubygems

As I can see version 0.1.5 released to rubygems on May 13, 2019
But on 1 Dec 2021 @q9f switch gem from digest-sha3 to keccak (commit)

Could you be so kind to download this changes on rubygems?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.