Socket
Book a DemoInstallSign in
Socket

crypto_address_validator

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

crypto_address_validator

0.2.1
bundlerRubygems
Version published
Maintainers
1
Created
Source

CryptoAddressValidator

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

Inspired by vtm9/adequate_crypto_address.

Installation

Add this line to your application's Gemfile:

gem 'crypto_address_validator'

Or install it yourself as:

gem install crypto_address_validator

Main API

.valid? (address, currency [, type = :prod, type = [])
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 or an array of types. 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'

Usage

Validation

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

# BTC(non SegWit address validation)
CryptoAddressValidator.valid?('3NJZLcZEEYBpxYEUGewU4knsQRn1WM5Fkt', :bitcoin, [:p2sh, :hash160]) #=> true

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

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

Normalization

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

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

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

# ETH
CryptoAddressValidator.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 CryptoAddressValidator.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/crypto_address_validator.rb
module CryptoAddressValidator
  class Coin
    attr_reader :address

    def initialize(address_sring)
      @address = address_sring
    end

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

CryptoAddressValidator.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

  • Fork the repo
  • Grab dependencies: bundle install
  • Make sure everything is working: bundle exec rake spec
  • Make your changes
  • Test your changes
  • Create a Pull Request

Notes

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

FAQs

Package last updated on 12 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.