Completed
Pull Request — master (#99)
by thomas
42:14 queued 39:21
created

CashAddress::getLegacyAddress()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Blocktrail\SDK\Address;
4
5
use BitWasp\Bitcoin\Address\Address;
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\ScriptFactory;
11
use BitWasp\Bitcoin\Script\ScriptType;
12
use BitWasp\Buffertools\BufferInterface;
13
use Blocktrail\SDK\Exceptions\BlocktrailSDKException;
14
use Blocktrail\SDK\Network\BitcoinCashNetworkInterface;
15
16
class CashAddress extends Address implements Base32AddressInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $type;
22
23
    /**
24
     * @var BufferInterface
25
     */
26
    protected $hash;
27
28
    /**
29
     * CashAddress constructor.
30
     * @param string $type
31
     * @param BufferInterface $hash
32
     * @throws BlocktrailSDKException
33
     */
34 7
    public function __construct($type, BufferInterface $hash) {
35 7
        if ($type !== ScriptType::P2PKH && $type !== ScriptType::P2SH) {
36
            throw new BlocktrailSDKException("Invalid type for bitcoin cash address");
37
        }
38
39 7
        $this->type = $type;
40
41 7
        parent::__construct($hash);
42 7
    }
43
44
    /**
45
     * @param BitcoinCashNetworkInterface $network
46
     * @return string
47
     */
48 3
    public function getPrefix(BitcoinCashNetworkInterface $network) {
49 3
        return $network->getCashAddressPrefix();
50
    }
51
52
    /**
53
     * @return string
54
     */
55 4
    public function getType() {
56 4
        return $this->type;
57
    }
58
59
    /**
60
     * @param NetworkInterface|null $network
61
     * @return string
62
     * @throws BlocktrailSDKException
63
     * @throws \CashAddr\Exception\Base32Exception
64
     * @throws \CashAddr\Exception\CashAddressException
65
     */
66 5
    public function getAddress(NetworkInterface $network = null) {
67 5
        if (null === $network) {
68 1
            $network = Bitcoin::getNetwork();
69
        }
70
71 5
        if (!($network instanceof BitcoinCashNetworkInterface)) {
72
            throw new BlocktrailSDKException("Invalid network - must implement BitcoinCashNetworkInterface");
73
        }
74
75 5
        return \CashAddr\CashAddress::encode(
76 5
            $network->getCashAddressPrefix(),
77 5
            $this->type,
78 5
            $this->hash->getBinary()
79
        );
80
    }
81
82
    /**
83
     * @return PayToPubKeyHashAddress|ScriptHashAddress
84
     */
85
    public function getLegacyAddress() {
86
        if ($this->type === ScriptType::P2PKH) {
87
            return new PayToPubKeyHashAddress($this->hash);
88
        } else {
89
            return new ScriptHashAddress($this->hash);
90
        }
91
    }
92
93
    /**
94
     * @return \BitWasp\Bitcoin\Script\ScriptInterface
95
     */
96 3
    public function getScriptPubKey() {
97 3
        if ($this->type === ScriptType::P2PKH) {
98
            return ScriptFactory::scriptPubKey()->p2pkh($this->hash);
99
        } else {
100 3
            return ScriptFactory::scriptPubKey()->p2sh($this->hash);
101
        }
102
    }
103
}
104