Completed
Pull Request — master (#118)
by thomas
02:37
created

AddressReaderBase::fromString()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
1
<?php
2
3
namespace Blocktrail\SDK\Address;
4
5
use BitWasp\Bitcoin\Address\AddressInterface;
6
use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress;
7
use BitWasp\Bitcoin\Address\ScriptHashAddress;
8
use BitWasp\Bitcoin\Base58;
9
use BitWasp\Bitcoin\Network\NetworkInterface;
10
use BitWasp\Bitcoin\Script\ScriptInterface;
11
12
abstract class AddressReaderBase
13
{
14
    /**
15
     * @param string $strAddress
16
     * @param NetworkInterface $network
17
     * @return PayToPubKeyHashAddress|ScriptHashAddress|null
18
     */
19
    protected function readBase58($strAddress, NetworkInterface $network) {
20
        try {
21
            $data = Base58::decodeCheck($strAddress);
22
            $prefixByte = $data->slice(0, 1)->getHex();
23
24
            if ($prefixByte === $network->getP2shByte()) {
25
                return new ScriptHashAddress($data->slice(1));
26
            } else if ($prefixByte === $network->getAddressByte()) {
27
                return new PayToPubKeyHashAddress($data->slice(1));
28
            }
29
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
30
        }
31
        return null;
32
    }
33
34
    /**
35
     * @param $strAddress
36
     * @param NetworkInterface|null $network
37
     * @return AddressInterface
38
     */
39
    abstract public function fromString($strAddress, NetworkInterface $network = null);
40
41
    /**
42
     * @param ScriptInterface $script
43
     * @return AddressInterface
44
     */
45
    abstract public function fromOutputScript(ScriptInterface $script);
46
}
47