-
Notifications
You must be signed in to change notification settings - Fork 446
Expand file tree
/
Copy pathPSBT.php
More file actions
232 lines (207 loc) · 7.21 KB
/
PSBT.php
File metadata and controls
232 lines (207 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
declare(strict_types=1);
namespace BitWasp\Bitcoin\Transaction\PSBT;
use BitWasp\Bitcoin\Exceptions\InvalidPSBTException;
use BitWasp\Bitcoin\Serializer\Types;
use BitWasp\Bitcoin\Transaction\TransactionFactory;
use BitWasp\Bitcoin\Transaction\TransactionInterface;
use BitWasp\Buffertools\Buffer;
use BitWasp\Buffertools\BufferInterface;
use BitWasp\Buffertools\Parser;
class PSBT
{
const PSBT_GLOBAL_UNSIGNED_TX = 0;
/**
* @var TransactionInterface
*/
private $tx;
/**
* Remaining PSBTGlobals key/value pairs we
* didn't know how to parse. map[string]BufferInterface
* @var BufferInterface[]
*/
private $unknown = [];
/**
* @var PSBTInput[]
*/
private $inputs;
/**
* @var PSBTOutput[]
*/
private $outputs;
/**
* PSBT constructor.
* @param TransactionInterface $tx
* @param BufferInterface[] $unknowns
* @param PSBTInput[] $inputs
* @param PSBTOutput[] $outputs
* @throws InvalidPSBTException
*/
public function __construct(TransactionInterface $tx, array $unknowns, array $inputs, array $outputs)
{
if (count($tx->getInputs()) !== count($inputs)) {
throw new InvalidPSBTException("Invalid number of inputs");
}
if (count($tx->getOutputs()) !== count($outputs)) {
throw new InvalidPSBTException("Invalid number of outputs");
}
$numInputs = count($tx->getInputs());
$witnesses = $tx->getWitnesses();
for ($i = 0; $i < $numInputs; $i++) {
$input = $tx->getInput($i);
if ($input->getScript()->getBuffer()->getSize() > 0 || (array_key_exists($i, $witnesses) && count($witnesses[$i]) > 0)) {
throw new InvalidPSBTException("Unsigned tx does not have empty script sig or witness");
}
}
foreach ($unknowns as $key => $unknown) {
if (!is_string($key) || !($unknown instanceof BufferInterface)) {
throw new \InvalidArgumentException("Unknowns must be a map of string keys to Buffer values");
}
}
$this->tx = $tx;
$this->unknown = $unknowns;
$this->inputs = $inputs;
$this->outputs = $outputs;
}
/**
* @param BufferInterface $in
* @return PSBT
* @throws InvalidPSBTException
*/
public static function fromBuffer(BufferInterface $in): PSBT
{
$byteString5 = Types::bytestring(5);
$vs = Types::varstring();
$parser = new Parser($in);
try {
$prefix = $byteString5->read($parser);
if ($prefix->getBinary() !== "psbt\xff") {
throw new InvalidPSBTException("Incorrect bytes");
}
} catch (\Exception $e) {
throw new InvalidPSBTException("Invalid PSBT magic", 0, $e);
}
$tx = null;
$unknown = [];
try {
do {
$key = $vs->read($parser);
if ($key->getSize() === 0) {
break;
}
$value = $vs->read($parser);
$dataType = ord(substr($key->getBinary(), 0, 1));
switch ($dataType) {
case self::PSBT_GLOBAL_UNSIGNED_TX:
if ($tx !== null) {
throw new \RuntimeException("Duplicate global tx");
} else if ($key->getSize() !== 1) {
throw new \RuntimeException("Invalid key length");
}
$tx = TransactionFactory::fromBuffer($value);
break;
default:
if (array_key_exists($key->getBinary(), $unknown)) {
throw new InvalidPSBTException("Duplicate unknown key");
}
$unknown[$key->getBinary()] = $value;
break;
}
} while ($parser->getPosition() < $parser->getSize());
} catch (\Exception $e) {
throw new InvalidPSBTException("Failed to parse global section", 0, $e);
}
if (!$tx) {
throw new InvalidPSBTException("Missing global tx");
}
$numInputs = count($tx->getInputs());
$inputs = [];
for ($i = 0; $parser->getPosition() < $parser->getSize() && $i < $numInputs; $i++) {
try {
$input = PSBTInput::fromParser($parser, $vs);
$inputs[] = $input;
} catch (\Exception $e) {
throw new InvalidPSBTException("Failed to parse inputs section", 0, $e);
}
}
if ($numInputs !== count($inputs)) {
throw new InvalidPSBTException("Missing inputs");
}
$numOutputs = count($tx->getOutputs());
$outputs = [];
for ($i = 0; $parser->getPosition() < $parser->getSize() && $i < $numOutputs; $i++) {
try {
$output = PSBTOutput::fromParser($parser, $vs);
$outputs[] = $output;
} catch (\Exception $e) {
throw new InvalidPSBTException("Failed to parse outputs section", 0, $e);
}
}
if ($numOutputs !== count($outputs)) {
throw new InvalidPSBTException("Missing outputs");
}
return new PSBT($tx, $unknown, $inputs, $outputs);
}
/**
* @return TransactionInterface
*/
public function getTransaction(): TransactionInterface
{
return $this->tx;
}
/**
* @return BufferInterface[]
*/
public function getUnknowns(): array
{
return $this->unknown;
}
/**
* @return PSBTInput[]
*/
public function getInputs(): array
{
return $this->inputs;
}
/**
* @return PSBTOutput[]
*/
public function getOutputs(): array
{
return $this->outputs;
}
public function updateInput(int $input, \Closure $modifyPsbtIn)
{
if (!array_key_exists($input, $this->inputs)) {
throw new \RuntimeException("No input at this index");
}
$updatable = new UpdatableInput($this, $input, $this->inputs[$input]);
$modifyPsbtIn($updatable);
$this->inputs[$input] = $updatable->input();
}
/**
* @return BufferInterface
*/
public function getBuffer(): BufferInterface
{
$vs = Types::varstring();
$parser = new Parser();
$parser->appendBinary("psbt\xff");
$parser->appendBinary($vs->write(new Buffer(chr(self::PSBT_GLOBAL_UNSIGNED_TX))));
$parser->appendBinary($vs->write($this->tx->getBuffer()));
foreach ($this->unknown as $key => $value) {
$parser->appendBinary($vs->write(new Buffer($key)));
$parser->appendBinary($vs->write($value));
}
$parser->appendBinary($vs->write(new Buffer()));
$numInputs = count($this->tx->getInputs());
for ($i = 0; $i < $numInputs; $i++) {
$this->inputs[$i]->writeToParser($parser, $vs);
}
$numOutputs = count($this->tx->getOutputs());
for ($i = 0; $i < $numOutputs; $i++) {
$this->outputs[$i]->writeToParser($parser, $vs);
}
return $parser->getBuffer();
}
}