├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml ├── src └── AddressValidator.php └── tests └── AddressValidatorTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | composer.phar 2 | vendor/ 3 | 4 | # Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file 5 | # You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file 6 | # composer.lock -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Linus Unnebäck 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # php-bitcoin-address-validator 2 | 3 | A simple, easy to use PHP Bitcoin address validator 4 | 5 | ## Usage 6 | 7 | Quick start: 8 | 9 | ```php 10 | use \LinusU\Bitcoin\AddressValidator; 11 | 12 | // This will return false, indicating invalid address. 13 | AddressValidator::isValid('blah'); 14 | 15 | // This is a valid address and will thus return true. 16 | AddressValidator::isValid('1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i'); 17 | 18 | // This is a Testnet address, it's valid and the function will return true. 19 | AddressValidator::isValid('mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs', AddressValidator::TESTNET); 20 | ``` 21 | 22 | ## API 23 | 24 | ### `isValid($addr, $version)` 25 | 26 | - `$addr`: A bitcoin address 27 | - `$version`: The version to test against, defaults to `MAINNET` 28 | 29 | Returns a boolean indicating if the address is valid or not. 30 | 31 | ### `typeOf($addr)` 32 | 33 | - `$addr`: A bitcoin address 34 | 35 | Returns the type of the address. 36 | 37 | ## Constants 38 | 39 | The library exposes the following constants. 40 | 41 | - `MAINNET`: Indicates any mainnet address type 42 | - `TESTNET`: Indicates any testnet address type 43 | - `MAINNET_PUBKEY`: Indicates a mainnet pay to pubkey hash address 44 | - `MAINNET_SCRIPT`: Indicates a mainnet pay to script hash address 45 | - `TESTNET_PUBKEY`: Indicates a testnet pay to pubkey hash address 46 | - `TESTNET_SCRIPT`: Indicates a testnet pay to script hash address 47 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "linusu/bitcoin-address-validator", 3 | "type": "library", 4 | "description": "A simple, easy to use PHP Bitcoin address validator", 5 | "keywords": ["bitcoin", "address", "validation", "validator"], 6 | "homepage": "http://github.com/LinusU/bitcoin-address-validator", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Linus Unnebäck", 11 | "email": "linus@folkdatorn.se", 12 | "homepage": "http://linusu.se/", 13 | "role": "Developer" 14 | } 15 | ], 16 | "require": { 17 | "php": ">=5.3.0" 18 | }, 19 | "autoload": { 20 | "psr-4": { 21 | "LinusU\\Bitcoin\\": "src/" 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ./tests/ 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /src/AddressValidator.php: -------------------------------------------------------------------------------- 1 | invalidAddrs as $addr) { 109 | $this->assertEquals(false, AddressValidator::isValid($addr), 'Should be invalid: ' . $addr); 110 | } 111 | } 112 | 113 | function testValidAddrsMainnet() { 114 | foreach ($this->validAddrs as $addr) { 115 | $this->assertEquals(true, AddressValidator::isValid($addr), 'Should be valid: ' . $addr); 116 | } 117 | } 118 | 119 | function testInvalidAddrsTestnet() { 120 | foreach ($this->invalidAddrs as $addr) { 121 | $this->assertEquals(false, AddressValidator::isValid($addr, AddressValidator::TESTNET), 'Should be invalid: ' . $addr); 122 | } 123 | } 124 | 125 | function testValidAddrsTestnet() { 126 | foreach ($this->validTestnet as $addr) { 127 | $this->assertEquals(true, AddressValidator::isValid($addr, AddressValidator::TESTNET), 'Should be valid: ' . $addr); 128 | } 129 | } 130 | 131 | function testScriptAddrsMainnet() { 132 | foreach ($this->scriptAddrs as $addr) { 133 | $this->assertEquals(AddressValidator::typeOf($addr), AddressValidator::MAINNET_SCRIPT, 'Should be script: ' . $addr); 134 | } 135 | } 136 | 137 | function testScriptAddrsTestnet() { 138 | foreach ($this->scriptTestnet as $addr) { 139 | $this->assertEquals(AddressValidator::typeOf($addr), AddressValidator::TESTNET_SCRIPT, 'Should be script: ' . $addr); 140 | } 141 | } 142 | 143 | } 144 | --------------------------------------------------------------------------------