Skip to content

Commit cd627ca

Browse files
committed
first commit
0 parents  commit cd627ca

29 files changed

+1317
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
/vendor
3+
composer.lock

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Andrei Komarov.
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

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
## PHP Bitcoin Address
2+
3+
A simple P2PK, P2PKH, P2SH, P2WPKH, P2WSH output script/address generator.
4+
5+
**Supported types:**
6+
7+
- Pay-To-PubKey (P2PK)
8+
- Pay-To-PubKeyHash (P2PKH)
9+
- Pay-To-Multisig (P2MS)
10+
- Pay-To-ScriptHash (P2SH)
11+
- Pay-To-WitnessPubKeyHash (P2WPKH)
12+
- Pay-To-WitnessScriptHash (P2WSH)
13+
- P2WPKH-over-P2SH
14+
- P2WSH-over-P2SH
15+
- any combination
16+
17+
**Supported networks:**
18+
19+
- Bitcoin
20+
- Bitcoin Testnet
21+
- Bitcoin Regtest
22+
23+
### Installation
24+
25+
```bash
26+
composer require andkom/php-bitcoin-address
27+
```
28+
29+
### Examples
30+
31+
Generate a P2PK/P2PKH address:
32+
33+
```php
34+
$address = OutputFactory::p2pk($pubKey)->address();
35+
$address = OutputFactory::p2pkh($pubKeyHash)->address();
36+
```
37+
38+
Generate a P2MS address:
39+
40+
```php
41+
$address = OutputFactory::p2ms(2, [$pubKey1, $pubKey2, $pubKey3])->address();
42+
```
43+
44+
Generate a P2SH address:
45+
46+
```php
47+
$factory = new OutputFactory();
48+
$p2ms = $factory->p2ms(2, [$pubKey1, $pubKey2, $pubKey3]);
49+
$address = $factory->p2sh($p2ms)->address();
50+
```
51+
52+
Generate a P2WPKH address:
53+
54+
```php
55+
$address = OutputFactory::p2wpkh($pubKeyHash)->address();
56+
```
57+
58+
Generate a P2WSH address:
59+
60+
```php
61+
$factory = new OutputFactory();
62+
$p2ms = $factory->p2ms(2, [$pubKey1, $pubKey2, $pubKey3]);
63+
$address = $factory->p2wsh($p2ms)->address();
64+
```
65+
66+
Generate a P2WPKH-over-P2SH address:
67+
68+
```php
69+
$factory = new OutputFactory();
70+
$p2wpkh = $factory->p2wpkh($pubKeyHash);
71+
$address = $factory->p2sh($p2wpkh)->address();
72+
```
73+
74+
Generate a P2WSH-over-P2SH address:
75+
76+
```php
77+
$factory = new OutputFactory();
78+
$p2ms = $factory->p2ms(2, [$pubKey1, $pubKey2, $pubKey3]);
79+
$p2wsh = $factory->p2wsh($p2ms);
80+
$address = $factory->p2sh($p2wsh)->address();
81+
```

composer.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "andkom/php-bitcoin-address",
3+
"description": "A simple P2PK, P2PKH, P2SH, P2WPKH, P2WSH output script/address generator.",
4+
"type": "library",
5+
"keywords": [
6+
"bitcoin",
7+
"address",
8+
"generator"
9+
],
10+
"license": "MIT",
11+
"authors": [
12+
{
13+
"name": "Andrei Komarov"
14+
}
15+
],
16+
"require": {
17+
"php": "^7.0",
18+
"stephenhill/base58": "^1.1",
19+
"bitwasp/bech32": "^0.0.1"
20+
},
21+
"require-dev": {
22+
"phpunit/phpunit": ">=5.0"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"AndKom\\Bitcoin\\Address\\": "src/"
27+
}
28+
},
29+
"autoload-dev": {
30+
"psr-4": {
31+
"AndKom\\Bitcoin\\Address\\Tests\\": "tests/"
32+
}
33+
}
34+
}

phpunit.xml.dist

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
colors="true"
4+
verbose="true">
5+
<testsuites>
6+
<testsuite name="Unit">
7+
<directory suffix="Test.php">./tests/</directory>
8+
</testsuite>
9+
</testsuites>
10+
<filter>
11+
<whitelist processUncoveredFilesFromWhitelist="true">
12+
<directory suffix=".php">./src</directory>
13+
</whitelist>
14+
</filter>
15+
</phpunit>

src/Exception.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AndKom\Bitcoin\Address;
6+
7+
/**
8+
* Class Exception
9+
* @package AndKom\Bitcoin\Address
10+
*/
11+
class Exception extends \Exception
12+
{
13+
}

src/Network/NetworkFactory.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AndKom\Bitcoin\Address\Network;
6+
7+
use AndKom\Bitcoin\Address\Network\Networks\Bitcoin;
8+
use AndKom\Bitcoin\Address\Network\Networks\BitcoinTestnet;
9+
10+
/**
11+
* Class NetworkFactory
12+
* @package AndKom\Bitcoin\Address\Network
13+
*/
14+
class NetworkFactory
15+
{
16+
/**
17+
* @var NetworkInterface
18+
*/
19+
static protected $defaultNetwork;
20+
21+
/**
22+
* @param NetworkInterface $network
23+
*/
24+
static public function setDefaultNetwork(NetworkInterface $network)
25+
{
26+
static::$defaultNetwork = $network;
27+
}
28+
29+
/**
30+
* @return NetworkInterface
31+
*/
32+
static public function getDefaultNetwork(): NetworkInterface
33+
{
34+
return static::$defaultNetwork ?: static::bitcoin();
35+
}
36+
37+
/**
38+
* @return NetworkInterface
39+
*/
40+
static public function bitcoin(): NetworkInterface
41+
{
42+
return new Bitcoin();
43+
}
44+
45+
/**
46+
* @return NetworkInterface
47+
*/
48+
static public function bitcoinTest(): NetworkInterface
49+
{
50+
return new BitcoinTestnet();
51+
}
52+
53+
/**
54+
* @return NetworkInterface
55+
*/
56+
static public function bitcoinRegtest(): NetworkInterface
57+
{
58+
return new BitcoinTestnet();
59+
}
60+
}

src/Network/NetworkInterface.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AndKom\Bitcoin\Address\Network;
6+
7+
/**
8+
* Interface NetworkInterface
9+
* @package AndKom\Bitcoin\Address\Network
10+
*/
11+
interface NetworkInterface
12+
{
13+
/**
14+
* @param string $hash
15+
* @return string
16+
*/
17+
public function getAddressP2pkh(string $hash): string;
18+
19+
/**
20+
* @param string $hash
21+
* @return string
22+
*/
23+
public function getAddressP2sh(string $hash): string;
24+
25+
/**
26+
* @param string $hash
27+
* @return string
28+
*/
29+
public function getAddressP2wpkh(string $hash): string;
30+
31+
/**
32+
* @param string $hash
33+
* @return string
34+
*/
35+
public function getAddressP2wsh(string $hash): string;
36+
}

src/Network/Networks/Bitcoin.php

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AndKom\Bitcoin\Address\Network\Networks;
6+
7+
use AndKom\Bitcoin\Address\Network\NetworkInterface;
8+
use AndKom\Bitcoin\Address\Utils;
9+
use function BitWasp\Bech32\encodeSegwit;
10+
11+
/**
12+
* Class Bitcoin
13+
* @package AndKom\Bitcoin\Address\Network\Networks
14+
*/
15+
class Bitcoin implements NetworkInterface
16+
{
17+
/**
18+
* @var int
19+
*/
20+
protected $prefixP2pkh = 0x00;
21+
22+
/**
23+
* @var int
24+
*/
25+
protected $prefixP2sh = 0x05;
26+
27+
/**
28+
* @var string
29+
*/
30+
protected $prefixBech32 = 'bc';
31+
32+
/**
33+
* @param string $pubKeyHash
34+
* @return string
35+
* @throws \Exception
36+
*/
37+
public function getAddressP2pkh(string $pubKeyHash): string
38+
{
39+
return Utils::base58address($pubKeyHash, $this->prefixP2pkh);
40+
}
41+
42+
/**
43+
* @param string $scriptHash
44+
* @return string
45+
* @throws \Exception
46+
*/
47+
public function getAddressP2sh(string $scriptHash): string
48+
{
49+
return Utils::base58address($scriptHash, $this->prefixP2sh);
50+
}
51+
52+
/**
53+
* @param string $pubKeyHash
54+
* @return string
55+
* @throws \BitWasp\Bech32\Exception\Bech32Exception
56+
*/
57+
public function getAddressP2wpkh(string $pubKeyHash): string
58+
{
59+
return encodeSegwit($this->prefixBech32, 0, $pubKeyHash);
60+
}
61+
62+
/**
63+
* @param string $witnessScriptHash
64+
* @return string
65+
* @throws \BitWasp\Bech32\Exception\Bech32Exception
66+
*/
67+
public function getAddressP2wsh(string $witnessScriptHash): string
68+
{
69+
return encodeSegwit($this->prefixBech32, 0, $witnessScriptHash);
70+
}
71+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AndKom\Bitcoin\Address\Network\Networks;
6+
7+
/**
8+
* Class BitcoinRegtest
9+
* @package AndKom\Bitcoin\Address\Network\Networks
10+
*/
11+
class BitcoinRegtest extends BitcoinTestnet
12+
{
13+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AndKom\Bitcoin\Address\Network\Networks;
6+
7+
/**
8+
* Class BitcoinTestnet
9+
* @package AndKom\Bitcoin\Address\Network\Networks
10+
*/
11+
class BitcoinTestnet extends Bitcoin
12+
{
13+
/**
14+
* @var int
15+
*/
16+
protected $prefixP2pkh = 0x6f;
17+
18+
/**
19+
* @var int
20+
*/
21+
protected $prefixP2sh = 0xc4;
22+
23+
/**
24+
* @var string
25+
*/
26+
protected $prefixBech32 = 'tb';
27+
}

0 commit comments

Comments
 (0)