1 | <?php |
||
15 | class AddressFactory |
||
16 | { |
||
17 | /** |
||
18 | * Returns a pay-to-pubkey-hash address for the given public key |
||
19 | * |
||
20 | * @param KeyInterface $key |
||
21 | * @return PayToPubKeyHashAddress |
||
22 | */ |
||
23 | 210 | public static function fromKey(KeyInterface $key) |
|
27 | |||
28 | /** |
||
29 | * Takes the $p2shScript and generates the scriptHash address. |
||
30 | * |
||
31 | * @param ScriptInterface $p2shScript |
||
32 | * @return ScriptHashAddress |
||
33 | */ |
||
34 | 90 | public static function fromScript(ScriptInterface $p2shScript) |
|
38 | |||
39 | /** |
||
40 | * @param ScriptInterface $outputScript |
||
41 | * @return PayToPubKeyHashAddress|ScriptHashAddress |
||
42 | */ |
||
43 | 18 | public static function fromOutputScript(ScriptInterface $outputScript) |
|
44 | { |
||
45 | 18 | $classifier = new OutputClassifier($outputScript); |
|
46 | 18 | $type = $classifier->classify(); |
|
47 | 18 | $parsed = $outputScript->getScriptParser()->decode(); |
|
48 | |||
49 | 18 | if ($type === OutputClassifier::PAYTOPUBKEYHASH) { |
|
50 | /** @var \BitWasp\Buffertools\BufferInterface $hash */ |
||
51 | 12 | $hash = $parsed[2]->getData(); |
|
52 | 12 | return new PayToPubKeyHashAddress($hash); |
|
53 | 12 | } else if ($type === OutputClassifier::PAYTOSCRIPTHASH) { |
|
54 | /** @var \BitWasp\Buffertools\BufferInterface $hash */ |
||
55 | 6 | $hash = $parsed[1]->getData(); |
|
56 | 6 | return new ScriptHashAddress($hash); |
|
57 | } |
||
58 | |||
59 | 12 | throw new \RuntimeException('Script type is not associated with an address'); |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param string $address |
||
64 | * @param NetworkInterface $network |
||
65 | * @return AddressInterface |
||
66 | * @throws \BitWasp\Bitcoin\Exceptions\Base58ChecksumFailure |
||
67 | */ |
||
68 | 162 | public static function fromString($address, NetworkInterface $network = null) |
|
82 | |||
83 | /** |
||
84 | * @param string $address |
||
85 | * @param NetworkInterface $network |
||
86 | * @return AddressInterface |
||
87 | * @throws \BitWasp\Bitcoin\Exceptions\Base58ChecksumFailure |
||
88 | */ |
||
89 | 12 | public static function isValidAddress($address, NetworkInterface $network = null) |
|
100 | 6 | ||
101 | 6 | /** |
|
102 | 6 | * @param ScriptInterface $script |
|
103 | * @param NetworkInterface $network |
||
104 | * @return String |
||
105 | * @throws \RuntimeException |
||
106 | */ |
||
107 | public static function getAssociatedAddress(ScriptInterface $script, NetworkInterface $network = null) |
||
123 | } |
||
124 |