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 | 38 | public static function fromKey(KeyInterface $key) |
|
24 | { |
||
25 | 38 | return new PayToPubKeyHashAddress($key->getPubKeyHash()); |
|
26 | } |
||
27 | |||
28 | /** |
||
29 | * Takes the $p2shScript and generates the scriptHash address. |
||
30 | * |
||
31 | * @param ScriptInterface $p2shScript |
||
32 | * @return ScriptHashAddress |
||
33 | */ |
||
34 | 14 | public static function fromScript(ScriptInterface $p2shScript) |
|
35 | { |
||
36 | 14 | return new ScriptHashAddress($p2shScript->getScriptHash()); |
|
37 | } |
||
38 | |||
39 | /** |
||
40 | * @param ScriptInterface $outputScript |
||
41 | * @return PayToPubKeyHashAddress|ScriptHashAddress |
||
42 | */ |
||
43 | 8 | public static function fromOutputScript(ScriptInterface $outputScript) |
|
44 | { |
||
45 | 8 | $decode = (new OutputClassifier())->decode($outputScript); |
|
46 | 8 | switch ($decode->getType()) { |
|
47 | 8 | case ScriptType::P2PKH: |
|
48 | /** @var BufferInterface $solution */ |
||
49 | 4 | return new PayToPubKeyHashAddress($decode->getSolution()); |
|
50 | 6 | case ScriptType::P2SH: |
|
51 | /** @var BufferInterface $solution */ |
||
52 | 2 | return new ScriptHashAddress($decode->getSolution()); |
|
53 | default: |
||
54 | 4 | throw new \RuntimeException('Script type is not associated with an address'); |
|
55 | } |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @param string $address |
||
60 | * @param NetworkInterface $network |
||
61 | * @return AddressInterface |
||
62 | * @throws \BitWasp\Bitcoin\Exceptions\Base58ChecksumFailure |
||
63 | */ |
||
64 | 42 | public static function fromString($address, NetworkInterface $network = null) |
|
78 | |||
79 | /** |
||
80 | * @param string $address |
||
81 | * @param NetworkInterface $network |
||
82 | * @return bool |
||
83 | * @throws \BitWasp\Bitcoin\Exceptions\Base58ChecksumFailure |
||
84 | */ |
||
85 | 26 | public static function isValidAddress($address, NetworkInterface $network = null) |
|
96 | |||
97 | /** |
||
98 | * Following a loose definition of 'associated', returns |
||
99 | * the current script types, and a PayToPubKeyHash address for P2PK. |
||
100 | * |
||
101 | * @param ScriptInterface $script |
||
102 | * @return AddressInterface |
||
103 | * @throws \RuntimeException |
||
104 | */ |
||
105 | 4 | public static function getAssociatedAddress(ScriptInterface $script) |
|
115 | } |
||
116 |