Completed
Pull Request — master (#99)
by thomas
18:12
created

AddressReaderBase::readBase58()   A

Complexity

Conditions 4
Paths 9

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.1755

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 9
nop 2
dl 0
loc 15
ccs 7
cts 9
cp 0.7778
crap 4.1755
rs 9.2
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\Address\SegwitAddress;
9
use BitWasp\Bitcoin\Base58;
10
use BitWasp\Bitcoin\Network\NetworkInterface;
11
use BitWasp\Bitcoin\Script\ScriptInterface;
12
use BitWasp\Bitcoin\SegwitBech32;
13
use Blocktrail\SDK\Network\BitcoinCashNetworkInterface;
14
15
abstract class AddressReaderBase
16
{
17
    /**
18
     * @param string $strAddress
19
     * @param NetworkInterface $network
20
     * @return PayToPubKeyHashAddress|ScriptHashAddress|null
21
     */
22 18
    protected function readBase58($strAddress, NetworkInterface $network) {
23
        try {
24 18
            $data = Base58::decodeCheck($strAddress);
25 18
            $prefixByte = $data->slice(0, 1)->getHex();
26
27 18
            if ($prefixByte === $network->getP2shByte()) {
28 18
                return new ScriptHashAddress($data->slice(1));
29
            } else if ($prefixByte === $network->getAddressByte()) {
30
                return new PayToPubKeyHashAddress($data->slice(1));
31
            }
32 1
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
33
        }
34
35 1
        return null;
36
    }
37
38
    /**
39
     * @param string $strAddress
40
     * @param NetworkInterface $network
41
     * @return SegwitAddress|null
42
     */
43 1
    protected function readBech32($strAddress, NetworkInterface $network) {
44
        try {
45 1
            return new SegwitAddress(SegwitBech32::decode($strAddress, $network));
46
        } catch (\Exception $e) {
47
            // continue on
48
        }
49
50
        return null;
51
    }
52
53
    /**
54
     * @param string $strAddress
55
     * @param BitcoinCashNetworkInterface $network
56
     * @return CashAddress|null
57
     */
58
    protected function readBase32($strAddress, BitcoinCashNetworkInterface $network) {
59
        try {
60
            list ($prefix, $scriptType, $hash) = \CashAddr\CashAddress::decode($strAddress);
61
            if ($prefix !== $network->getCashAddressPrefix()) {
62
                return null;
63
            }
64
            return new CashAddress($scriptType, $hash);
65
        } catch (\Exception $e) {
66
            // continue on
67
        }
68
69
        return null;
70
    }
71
72
    /**
73
     * @param string $strAddress
74
     * @return AddressInterface
75
     */
76
    abstract public function fromString($strAddress);
77
78
    /**
79
     * @param ScriptInterface $script
80
     * @return AddressInterface
81
     */
82
    abstract public function fromOutputScript(ScriptInterface $script);
83
}
84