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

AddressReaderBase::readBase32()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 7.1941

Importance

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