1 | <?php |
||
20 | class AddressCreator extends BaseAddressCreator |
||
21 | { |
||
22 | /** |
||
23 | * @param string $strAddress |
||
24 | * @param NetworkInterface $network |
||
25 | * @return Base58Address|null |
||
26 | */ |
||
27 | 39 | protected function readBase58Address(string $strAddress, NetworkInterface $network) |
|
28 | { |
||
29 | try { |
||
30 | 39 | $data = Base58::decodeCheck($strAddress); |
|
31 | 33 | $prefixByte = $data->slice(0, $network->getP2shPrefixLength())->getHex(); |
|
32 | |||
33 | 33 | if ($prefixByte === $network->getP2shByte()) { |
|
34 | 10 | return new ScriptHashAddress($data->slice(1)); |
|
35 | 23 | } else if ($prefixByte === $network->getAddressByte()) { |
|
36 | 23 | return new PayToPubKeyHashAddress($data->slice($network->getAddressPrefixLength())); |
|
37 | } |
||
38 | 6 | } catch (\Exception $e) { |
|
39 | // Just return null |
||
40 | } |
||
41 | |||
42 | 7 | return null; |
|
43 | } |
||
44 | |||
45 | /** |
||
46 | * @param string $strAddress |
||
47 | * @param NetworkInterface $network |
||
48 | * @return SegwitAddress|null |
||
49 | */ |
||
50 | 7 | protected function readSegwitAddress(string $strAddress, NetworkInterface $network) |
|
51 | { |
||
52 | try { |
||
53 | 7 | return new SegwitAddress(SegwitBech32::decode($strAddress, $network)); |
|
54 | 1 | } catch (\Exception $e) { |
|
55 | // Just return null |
||
56 | } |
||
57 | |||
58 | 1 | return null; |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param ScriptInterface $outputScript |
||
63 | * @return Address |
||
64 | */ |
||
65 | 41 | public function fromOutputScript(ScriptInterface $outputScript): Address |
|
66 | { |
||
67 | 41 | if ($outputScript instanceof P2shScript || $outputScript instanceof WitnessScript) { |
|
68 | throw new \RuntimeException("P2shScript & WitnessScript's are not accepted by fromOutputScript"); |
||
69 | } |
||
70 | |||
71 | 41 | $wp = null; |
|
72 | 41 | if ($outputScript->isWitness($wp)) { |
|
73 | /** @var WitnessProgram $wp */ |
||
74 | 10 | return new SegwitAddress($wp); |
|
75 | } |
||
76 | |||
77 | 31 | $decode = (new OutputClassifier())->decode($outputScript); |
|
78 | 31 | switch ($decode->getType()) { |
|
79 | case ScriptType::P2PKH: |
||
80 | /** @var BufferInterface $solution */ |
||
81 | 20 | return new PayToPubKeyHashAddress($decode->getSolution()); |
|
82 | case ScriptType::P2SH: |
||
83 | /** @var BufferInterface $solution */ |
||
84 | 11 | return new ScriptHashAddress($decode->getSolution()); |
|
85 | default: |
||
86 | 1 | throw new \RuntimeException('Script type is not associated with an address'); |
|
87 | } |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param string $strAddress |
||
92 | * @param NetworkInterface|null $network |
||
93 | * @return Address |
||
94 | * @throws UnrecognizedAddressException |
||
95 | */ |
||
96 | 39 | public function fromString(string $strAddress, NetworkInterface $network = null): Address |
|
110 | } |
||
111 |