Completed
Pull Request — master (#127)
by thomas
19:50
created

CashAddress::getAddress()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 15
ccs 8
cts 9
cp 0.8889
crap 3.0123
rs 9.7666
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 8
    public function __construct($type, BufferInterface $hash) {
35 8
        if ($type !== ScriptType::P2PKH && $type !== ScriptType::P2SH) {
36
            throw new BlocktrailSDKException("Invalid type for bitcoin cash address");
37
        }
38
39 8
        $this->type = $type;
40
41 8
        parent::__construct($hash);
42 8
    }
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 5
    public function getType() {
56 5
        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 6
    public function getAddress(NetworkInterface $network = null) {
67 6
        if (null === $network) {
68 2
            $network = Bitcoin::getNetwork();
69
        }
70
71 6
        if (!($network instanceof BitcoinCashNetworkInterface)) {
72
            throw new BlocktrailSDKException("Invalid network - must implement BitcoinCashNetworkInterface");
73
        }
74
75 6
        return \CashAddr\CashAddress::encode(
76 6
            $network->getCashAddressPrefix(),
77 6
            $this->type,
78 6
            $this->hash->getBinary()
79
        );
80
    }
81
82
    /**
83
     * @return PayToPubKeyHashAddress|ScriptHashAddress
84
     */
85 1
    public function getLegacyAddress() {
86 1
        if ($this->type === ScriptType::P2PKH) {
87
            return new PayToPubKeyHashAddress($this->hash);
88
        } else {
89 1
            return new ScriptHashAddress($this->hash);
90
        }
91
    }
92
93
    /**
94
     * @return \BitWasp\Bitcoin\Script\ScriptInterface
95
     */
96 4
    public function getScriptPubKey() {
97 4
        if ($this->type === ScriptType::P2PKH) {
98
            return ScriptFactory::scriptPubKey()->p2pkh($this->hash);
99
        } else {
100 4
            return ScriptFactory::scriptPubKey()->p2sh($this->hash);
101
        }
102
    }
103
}
104