1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin; |
6
|
|
|
|
7
|
|
|
use BitWasp\Bitcoin\Address\AddressInterface; |
8
|
|
|
use BitWasp\Bitcoin\Network\NetworkInterface; |
9
|
|
|
|
10
|
|
|
class Uri |
11
|
|
|
{ |
12
|
|
|
const BIP0021 = 0; |
13
|
|
|
const BIP0072 = 1; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var AddressInterface |
17
|
|
|
*/ |
18
|
|
|
private $bip21Address; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var AddressInterface|null |
22
|
|
|
*/ |
23
|
|
|
private $bip72Address; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var null|string |
27
|
|
|
*/ |
28
|
|
|
private $amount; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string|null |
32
|
|
|
*/ |
33
|
|
|
private $label; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $message; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $request; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var int |
47
|
|
|
*/ |
48
|
|
|
private $rule; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Uri constructor. |
52
|
|
|
* @param AddressInterface|null $address |
53
|
|
|
* @param int $convention |
54
|
|
|
*/ |
55
|
8 |
|
public function __construct(AddressInterface $address = null, int $convention = self::BIP0021) |
56
|
|
|
{ |
57
|
8 |
|
if ($convention === self::BIP0021) { |
58
|
7 |
|
if ($address === null) { |
59
|
1 |
|
throw new \InvalidArgumentException('Cannot provide a null address with bip0021'); |
60
|
|
|
} |
61
|
6 |
|
$this->bip21Address = $address; |
62
|
1 |
|
} else if ($convention === self::BIP0072) { |
63
|
1 |
|
$this->bip72Address = $address; |
64
|
|
|
} else { |
65
|
|
|
throw new \InvalidArgumentException("Invalid convention for bitcoin uri"); |
66
|
|
|
} |
67
|
|
|
|
68
|
7 |
|
$this->rule = $convention; |
69
|
7 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $value |
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
1 |
|
public function setAmountBtc(string $value) |
76
|
|
|
{ |
77
|
1 |
|
$this->amount = $value; |
78
|
1 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param Amount $amount |
83
|
|
|
* @param int $value |
84
|
|
|
* @return $this |
85
|
|
|
*/ |
86
|
1 |
|
public function setAmount(Amount $amount, int $value) |
87
|
|
|
{ |
88
|
1 |
|
$this->amount = $amount->toBtc($value); |
89
|
1 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $label |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
1 |
|
public function setLabel(string $label) |
97
|
|
|
{ |
98
|
1 |
|
$this->label = $label; |
99
|
1 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $message |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
1 |
|
public function setMessage(string $message) |
107
|
|
|
{ |
108
|
1 |
|
$this->message = $message; |
109
|
1 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $url |
114
|
|
|
* @return $this |
115
|
|
|
*/ |
116
|
2 |
|
public function setRequestUrl(string $url) |
117
|
|
|
{ |
118
|
2 |
|
$this->request = $url; |
119
|
2 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param NetworkInterface|null $network |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
7 |
|
public function uri(NetworkInterface $network = null): string |
127
|
|
|
{ |
128
|
7 |
|
if ($this->rule === self::BIP0072) { |
129
|
1 |
|
$address = $this->bip72Address === null ? '' : $this->bip72Address->getAddress($network); |
130
|
|
|
} else { |
131
|
6 |
|
$address = $this->bip21Address->getAddress($network); |
132
|
|
|
} |
133
|
|
|
|
134
|
7 |
|
$url = 'bitcoin:' . $address; |
135
|
|
|
|
136
|
7 |
|
$params = []; |
137
|
7 |
|
if (null !== $this->amount) { |
138
|
2 |
|
$params['amount'] = $this->amount; |
139
|
|
|
} |
140
|
|
|
|
141
|
7 |
|
if (null !== $this->label) { |
142
|
1 |
|
$params['label'] = $this->label; |
143
|
|
|
} |
144
|
|
|
|
145
|
7 |
|
if (null !== $this->message) { |
146
|
1 |
|
$params['message'] = $this->message; |
147
|
|
|
} |
148
|
|
|
|
149
|
7 |
|
if (null !== $this->request) { |
150
|
2 |
|
$params['r'] = $this->request; |
151
|
|
|
} |
152
|
|
|
|
153
|
7 |
|
if (count($params) > 0) { |
154
|
6 |
|
$url .= '?' . http_build_query($params); |
155
|
|
|
} |
156
|
|
|
|
157
|
7 |
|
return $url; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|