|
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
|
36 |
|
protected function readBase58($strAddress, NetworkInterface $network) { |
|
25
|
|
|
try { |
|
26
|
36 |
|
$data = Base58::decodeCheck($strAddress); |
|
27
|
24 |
|
$prefixByte = $data->slice(0, 1)->getHex(); |
|
28
|
|
|
|
|
29
|
24 |
|
if ($prefixByte === $network->getP2shByte()) { |
|
30
|
22 |
|
return new ScriptHashAddress($data->slice(1)); |
|
31
|
2 |
|
} else if ($prefixByte === $network->getAddressByte()) { |
|
32
|
2 |
|
return new PayToPubKeyHashAddress($data->slice(1)); |
|
33
|
|
|
} |
|
34
|
15 |
|
} catch (\Exception $e) { |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
15 |
|
return null; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $strAddress |
|
41
|
|
|
* @param NetworkInterface $network |
|
42
|
|
|
* @return SegwitAddress|null |
|
43
|
|
|
*/ |
|
44
|
11 |
|
protected function readBech32($strAddress, NetworkInterface $network) { |
|
45
|
|
|
try { |
|
46
|
11 |
|
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
|
4 |
|
protected function readBase32($strAddress, BitcoinCashNetworkInterface $network) { |
|
60
|
|
|
try { |
|
61
|
4 |
|
list ($prefix, $scriptType, $hash) = \CashAddr\CashAddress::decode($strAddress); |
|
62
|
4 |
|
if ($prefix !== $network->getCashAddressPrefix()) { |
|
63
|
|
|
return null; |
|
64
|
|
|
} |
|
65
|
4 |
|
if (!($scriptType === ScriptType::P2PKH || $scriptType === ScriptType::P2SH)) { |
|
66
|
|
|
return null; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
4 |
|
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 $strAddress |
|
79
|
|
|
* @param NetworkInterface|null $network |
|
80
|
|
|
* @return mixed |
|
81
|
|
|
*/ |
|
82
|
|
|
abstract public function fromString($strAddress, NetworkInterface $network = null); |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param ScriptInterface $script |
|
86
|
|
|
* @return AddressInterface |
|
87
|
|
|
*/ |
|
88
|
|
|
abstract public function fromOutputScript(ScriptInterface $script); |
|
89
|
|
|
} |
|
90
|
|
|
|