|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Blocktrail\SDK\Address; |
|
4
|
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Address\AddressInterface; |
|
6
|
|
|
use BitWasp\Bitcoin\Address\Base58AddressInterface; |
|
7
|
|
|
use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress; |
|
8
|
|
|
use BitWasp\Bitcoin\Address\ScriptHashAddress; |
|
9
|
|
|
use BitWasp\Bitcoin\Address\SegwitAddress; |
|
10
|
|
|
use BitWasp\Bitcoin\Bitcoin; |
|
11
|
|
|
use BitWasp\Bitcoin\Network\NetworkInterface; |
|
12
|
|
|
use BitWasp\Bitcoin\Script\Classifier\OutputClassifier; |
|
13
|
|
|
use BitWasp\Bitcoin\Script\ScriptInterface; |
|
14
|
|
|
use BitWasp\Bitcoin\Script\ScriptType; |
|
15
|
|
|
use BitWasp\Bitcoin\Script\WitnessProgram; |
|
16
|
|
|
use BitWasp\Bitcoin\SegwitBech32; |
|
17
|
|
|
use BitWasp\Buffertools\BufferInterface; |
|
18
|
|
|
use Blocktrail\SDK\Exceptions\BlocktrailSDKException; |
|
19
|
|
|
|
|
20
|
|
|
class BitcoinAddressReader extends AddressReaderBase |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param string $strAddress |
|
25
|
|
|
* @param NetworkInterface $network |
|
26
|
|
|
* @return SegwitAddress|null |
|
27
|
|
|
*/ |
|
28
|
10 |
|
protected function readSegwitAddress($strAddress, NetworkInterface $network) { |
|
29
|
|
|
try { |
|
30
|
10 |
|
return new SegwitAddress(SegwitBech32::decode($strAddress, $network)); |
|
31
|
|
|
} catch (\Exception $e) { |
|
32
|
|
|
// continue on |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
return null; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param ScriptInterface $outputScript |
|
40
|
|
|
* @return AddressInterface|PayToPubKeyHashAddress|ScriptHashAddress|SegwitAddress |
|
41
|
|
|
*/ |
|
42
|
|
|
public function fromOutputScript(ScriptInterface $outputScript) { |
|
43
|
|
|
$wp = null; |
|
44
|
|
|
if ($outputScript->isWitness($wp)) { |
|
45
|
|
|
/** @var WitnessProgram $wp */ |
|
46
|
|
|
return new SegwitAddress($wp); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$decode = (new OutputClassifier())->decode($outputScript); |
|
50
|
|
|
switch ($decode->getType()) { |
|
51
|
|
|
case ScriptType::P2PKH: |
|
52
|
|
|
/** @var BufferInterface $solution */ |
|
53
|
|
|
return new PayToPubKeyHashAddress($decode->getSolution()); |
|
54
|
|
|
case ScriptType::P2SH: |
|
55
|
|
|
/** @var BufferInterface $solution */ |
|
56
|
|
|
return new ScriptHashAddress($decode->getSolution()); |
|
57
|
|
|
default: |
|
58
|
|
|
throw new \RuntimeException('Script type is not associated with an address'); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $strAddress |
|
64
|
|
|
* @param NetworkInterface|null $network |
|
65
|
|
|
* @return Base58AddressInterface|SegwitAddress|null |
|
66
|
|
|
* @throws BlocktrailSDKException |
|
67
|
|
|
*/ |
|
68
|
12 |
|
public function fromString($strAddress, NetworkInterface $network = null) { |
|
69
|
12 |
|
$network = $network ?: Bitcoin::getNetwork(); |
|
70
|
|
|
|
|
71
|
12 |
|
if (($base58Address = $this->readBase58($strAddress, $network))) { |
|
72
|
2 |
|
return $base58Address; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
10 |
|
if (($bech32Address = $this->readSegwitAddress($strAddress, $network))) { |
|
76
|
10 |
|
return $bech32Address; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
throw new BlocktrailSDKException("Address not recognized"); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|