btcaddressvalidator

package module
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 23, 2021 License: MIT Imports: 6 Imported by: 0

README

Build Status Codacy Badge Codacy Badge

bitcoin-address-validator

Golang implementation of a Bitcoin address validator.

Install

To install as a library:

go get github.com/josemiguelmelo/btcaddressvalidator

Usage

The btc address validator return as result a BitcoinAddressType if valid and an error in case of invalid address.

The type BitcoindAddressType contains the following information about the bitcoin address address:

  • Address - Bitcoin address string
  • IsBech32 - Address has Bech32 format
  • Type - Address formats
    • For base58 addresses:
      • p2pkh - Begins with the number 1. Example: 1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2
      • p2sh - Begins with the number 3. Example: 3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy
    • For Bech32 addresses - begins with bc1:
      • p2wpkh
      • p2wsh
  • Network - The bitcoin network where the address belongs to
    • mainnet
    • testnet
    • regtest
type BitcoinAddressType struct {
	Address  string
	IsBech32 bool
	Type     string
	Network  string
}
Base58 Address

To validate a Base58 btc address:

address := "1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2"
addressType, err := btcaddressvalidator.CheckBase58Address(address)
Bech32 Address

To validate a Bech32 btc address:

address := "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"
addressType, err := CheckBech32Address(address)
Btc Address (independent of the type)

If you don't want to check for a specific type of btc address, you can use the CheckBtcAddress. This method checks for both bech32 and base58 addresses.

base58Address := "1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2"
base58AddressType, err := CheckBtcAddress(base58Address)

bech32Address := "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"
bech32AddressType, err := CheckBtcAddress(bech32Address)

License

The MIT License (MIT). Please see LICENSE file for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BitcoinAddressType

type BitcoinAddressType struct {
	Address  string
	IsBech32 bool
	Type     string
	Network  string
}

BitcoinAddressType contains information about a bitcoin address

func CheckBase58Address

func CheckBase58Address(address string) (*BitcoinAddressType, error)

CheckBase58Address validates an address against base58 format and returns the address information if valid.

func CheckBech32Address

func CheckBech32Address(address string) (*BitcoinAddressType, error)

CheckBech32Address validates an address against bech32 format and returns the address information if valid.

func CheckBtcAddress

func CheckBtcAddress(address string) (*BitcoinAddressType, error)

CheckBtcAddress validates a bitcoin address and returns the address information if valid.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL