andkom / php-bitcoin-address
A simple P2PK, P2PKH, P2SH, P2WPKH, P2WSH output script/address generator.
Installs: 820
Dependents: 1
Suggesters: 0
Security: 0
Stars: 22
Watchers: 2
Forks: 12
Open Issues: 1
Requires
- php: >=7.1
- ext-gmp: *
- brooksyang/bech32m: ^1.0
- btccom/cashaddress: ^0.0.3
- shanecurran/phpecc: ^0.0.1
- stephenhill/base58: ^1.1
Requires (Dev)
- phpunit/phpunit: >=5.7
This package is auto-updated.
Last update: 2025-03-16 20:21:38 UTC
README
A simple P2PK, P2PKH, P2SH, P2WPKH, P2WSH, P2TR output script/address parser/generator/validator.
Supported types:
- Pay-To-PubKey (P2PK)
- Pay-To-PubKeyHash (P2PKH)
- Pay-To-Multisig (P2MS)
- Pay-To-ScriptHash (P2SH)
- Pay-To-WitnessPubKeyHash (P2WPKH)
- Pay-To-WitnessScriptHash (P2WSH)
- Pay-To-Taproot (P2TR)
- P2WPKH-over-P2SH
- P2WSH-over-P2SH
- any combination
Supported networks:
- Bitcoin
- Bitcoin Testnet
- Bitcoin Gold
- Bitcoin Cash
- Litecoin
- Litecoin Testnet
- Dogecoin
- Dogecoin Testnet
- Viacoin
- Viacoin Testnet
- Dash
- Dash Testnet
- Zcash
Installation
composer require andkom/php-bitcoin-address
Examples
Generate a P2PK/P2PKH address:
$address = OutputFactory::p2pk($pubKey)->address(); $address = OutputFactory::p2pkh($pubKeyHash)->address();
Generate a P2MS address:
$address = OutputFactory::p2ms(2, [$pubKey1, $pubKey2, $pubKey3])->address();
Generate a P2SH address:
$factory = new OutputFactory(); $p2ms = $factory->p2ms(2, [$pubKey1, $pubKey2, $pubKey3]); $address = $factory->p2sh($p2ms)->address();
Generate a P2WPKH address:
$address = OutputFactory::p2wpkh($pubKeyHash)->address();
Generate a P2WSH address:
$factory = new OutputFactory(); $p2ms = $factory->p2ms(2, [$pubKey1, $pubKey2, $pubKey3]); $address = $factory->p2wsh($p2ms)->address();
Generate a P2WPKH-over-P2SH address:
$factory = new OutputFactory(); $p2wpkh = $factory->p2wpkh($pubKeyHash); $address = $factory->p2sh($p2wpkh)->address();
Generate a P2WSH-over-P2SH address:
$factory = new OutputFactory(); $p2ms = $factory->p2ms(2, [$pubKey1, $pubKey2, $pubKey3]); $p2wsh = $factory->p2wsh($p2ms); $address = $factory->p2sh($p2wsh)->address();
Generate a P2TR address:
$taprootPubKey = Taproot::construct($pubKey); $address = OutputFactory::p2tr($taprootPubKey)->address();
Generate an address from an output script:
$address = OutputFactory::fromScript($script)->address();
Decode a Bitcoin address:
$output = NetworkFactory::bitcoin()->decodeAddress('1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH');
Get a Bitcoin address type:
$type = NetworkFactory::bitcoin()->decodeAddress('1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH')->type(); // p2pkh
Validate a Bitcoin address:
NetworkFactory::bitcoin()->validateAddress('1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH'); // true