GithubHelp home page GithubHelp logo

kielabokkie / bitcoin-address-validator Goto Github PK

View Code? Open in Web Editor NEW
38.0 2.0 8.0 58 KB

PHP package to validate legacy, segwit, native segwit (bech32) and taproot Bitcoin addresses

License: MIT License

PHP 100.00%
bitcoin bitcoin-validator php php-package php-library segwit bitcoin-addresses taproot address

bitcoin-address-validator's Introduction

Bitcoin Address Validator

Author Build Packagist Version Software License Total Downloads

Validate legacy, segwit, native segwit (bech32), and taproot Bitcoin addresses.

Requirements

  • PHP >= 7.3
PHP Package Version
7.x v1.0
8.x v2.0+

Please note that taproot addresses are supported from v2.1 of this package.

Installation

Install the package via composer:

composer require kielabokkie/bitcoin-address-validator

Usage

First you instantiate the validator class:

$addressValidator = new \Kielabokkie\Bitcoin\AddressValidator;

Validate any kind of address (legacy, segwit, native segwit and taproot):

$addressValidator->isValid('1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i');

Legacy (P2PKH) address:

$addressValidator->isPayToPublicKeyHash('1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i');

Segwit (P2SH) address:

$addressValidator->isPayToScriptHash('3ALJH9Y951VCGcVZYAdpA3KchoP9McEj1G');

Native segwit (bech32) address:

$addressValidator->isBech32('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4');

Taproot (P2TR) address:

$addressValidator->isPayToTaproot('bc1pveaamy78cq5hvl74zmfw52fxyjun3lh7lgt44j03ygx02zyk8lesgk06f6');

Testnet

By default, the validator only passes mainnet addresses as valid. If you would like to validate both mainnet and testnet addresses you can use method chaining:

// Both valid
$addressValidator->includeTestnet()->isBech32('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4');
$addressValidator->includeTestnet()->isBech32('tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx');

If you want to validate only testnet addresses you can do that as follows:

// Invalid
$addressValidator->onlyTestnet()->isBech32('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4');
// Valid
$addressValidator->onlyTestnet()->isBech32('tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx');

Testing

This package is tested against the test data of the official bitcoin/bitcoin repo. If you come across an address that is not validated correctly please open an issue for it.

Run the tests with:

composer test

Credits

This package is based on the following packages and uses a lot of their code:

All credit goes to the original authors.

Donate

Did this package made you lots of money, save you some time or just sparked joy?

A donation would be much appreciated: 32vtWJSomccxQ6y1tgSwSHXN5PChpdYy27

bitcoin-address-validator's People

Contributors

kielabokkie avatar sh41 avatar

Stargazers

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

Watchers

 avatar  avatar

bitcoin-address-validator's Issues

PHP 8 Support

Hello!

Please update package to PHP 8 compatibility (at least in composer.json)

Thank you :)

support for taproot adresses

since last BTC update including Taproot,
there is new type of addresses, i believe Bech version 1. for example:
bc1pveaamy78cq5hvl74zmfw52fxyjun3lh7lgt44j03ygx02zyk8lesgk06f6
problem is these addresses does not pass validation

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.