Completed
Pull Request — master (#99)
by thomas
21:02
created

BitcoinCashAddressReader   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 71.88%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 89
ccs 23
cts 32
cp 0.7188
rs 10
c 1
b 0
f 0
wmc 17
lcom 1
cbo 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B readCashAddress() 0 17 5
B fromString() 0 15 6
B fromOutputScript() 0 24 5
1
<?php
2
3
namespace Blocktrail\SDK\Address;
4
5
use BitWasp\Bitcoin\Address\Base58AddressInterface;
6
use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress;
7
use BitWasp\Bitcoin\Address\ScriptHashAddress;
8
use BitWasp\Bitcoin\Bitcoin;
9
use BitWasp\Bitcoin\Network\NetworkInterface;
10
use BitWasp\Bitcoin\Script\Classifier\OutputClassifier;
11
use BitWasp\Bitcoin\Script\ScriptInterface;
12
use BitWasp\Bitcoin\Script\ScriptType;
13
use BitWasp\Buffertools\Buffer;
14
use BitWasp\Buffertools\BufferInterface;
15
use Blocktrail\SDK\Exceptions\BlocktrailSDKException;
16
use Blocktrail\SDK\Network\BitcoinCashNetworkInterface;
17
18
class BitcoinCashAddressReader extends AddressReaderBase
19
{
20
    /**
21
     * @var bool
22
     */
23
    private $useNewCashAddress;
24
25
    /**
26
     * BitcoinCashAddressReader constructor.
27
     * @param bool $useNewCashAddress
28
     */
29 6
    public function __construct($useNewCashAddress) {
30 6
        $this->useNewCashAddress = (bool) $useNewCashAddress;
31 6
    }
32
33
    /**
34
     * @param string $strAddress
35
     * @param BitcoinCashNetworkInterface $network
36
     * @return CashAddress|null
37
     */
38 4
    protected function readCashAddress($strAddress, BitcoinCashNetworkInterface $network) {
39
        try {
40 4
            list ($prefix, $scriptType, $hash) = \CashAddr\CashAddress::decode($strAddress);
41 4
            if ($prefix !== $network->getCashAddressPrefix()) {
42
                return null;
43
            }
44 4
            if (!($scriptType === ScriptType::P2PKH || $scriptType === ScriptType::P2SH)) {
45
                return null;
46
            }
47
48 4
            return new CashAddress($scriptType, new Buffer($hash, 20));
49
        } catch (\Exception $e) {
50
            // continue on
51
        }
52
53
        return null;
54
    }
55
56
    /**
57
     * @param string $strAddress
58
     * @param NetworkInterface|null $network
59
     * @return Base58AddressInterface|CashAddress
60
     * @throws BlocktrailSDKException
61
     */
62 6
    public function fromString($strAddress, NetworkInterface $network = null) {
63 6
        $network = $network ?: Bitcoin::getNetwork();
64
65 6
        if (($base58Address = $this->readBase58($strAddress, $network))) {
66 4
            return $base58Address;
67
        }
68
69 4
        if ($this->useNewCashAddress && $network instanceof BitcoinCashNetworkInterface) {
70 4
            if (($base32Address = $this->readCashAddress($strAddress, $network))) {
71 4
                return $base32Address;
72
            }
73
        }
74
75
        throw new BlocktrailSDKException("Address not recognized");
76
    }
77
78
    /**
79
     * @param ScriptInterface $script
80
     * @return Base58AddressInterface|CashAddress
81
     */
82 2
    public function fromOutputScript(ScriptInterface $script) {
83 2
        $decode = (new OutputClassifier())->decode($script);
84
85 2
        switch ($decode->getType()) {
86 2
            case ScriptType::P2PKH:
87
                /** @var BufferInterface $solution */
88
                if ($this->useNewCashAddress) {
89
                    return new CashAddress(ScriptType::P2PKH, $decode->getSolution());
90
                } else {
91
                    return new PayToPubKeyHashAddress($decode->getSolution());
92
                }
93
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
94 2
            case ScriptType::P2SH:
95
                /** @var BufferInterface $solution */
96 2
                if ($this->useNewCashAddress) {
97 1
                    return new CashAddress(ScriptType::P2SH, $decode->getSolution());
98
                } else {
99 1
                    return new ScriptHashAddress($decode->getSolution());
100
                }
101
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
102
            default:
103
                throw new \RuntimeException('Script type is not associated with an address');
104
        }
105
    }
106
}
107