|
8 | 8 | use BitWasp\Bitcoin\Tests\AbstractTestCase; |
9 | 9 | use BitWasp\Bitcoin\Transaction\OutPoint; |
10 | 10 | use BitWasp\Bitcoin\Transaction\PSBT\PSBT; |
11 | | -use BitWasp\Bitcoin\Transaction\PSBT\PSBTGlobals; |
12 | 11 | use BitWasp\Bitcoin\Transaction\PSBT\PSBTInput; |
13 | 12 | use BitWasp\Bitcoin\Transaction\PSBT\PSBTOutput; |
14 | 13 | use BitWasp\Bitcoin\Transaction\PSBT\UpdatableInput; |
@@ -103,6 +102,16 @@ public function getInvalidFixtures(): array |
103 | 102 | /*$base64=*/ 'cHNidP8BAFUCAAAAASeaIyOl37UfxF8iD6WLD8E+HjNCeSqF1+Ns1jM7XLw5AAAAAAD/////AaBa6gsAAAAAGXapFP/pwAYQl8w7Y28ssEYPpPxCfStFiKwAAAAAAAEBIJVe6gsAAAAAF6kUY0UgD2jRieGtwN8cTRbqjxTA2+uHIgIEsTQcy6doO2r08SOM1ul+cWfVafrEfx5I1HVBhENVvUZGMEMCIAQktY7/qqaU4VWepck7v9SokGQiQFXN8HC2dxRpRC0HAh9cjrD+plFtYLisszrWTt5g6Hhb+zqpS5m9+GFR25qaAQEEIgAgdx/RitRZZm3Unz1WTj28QvTIR3TjYK2haBao7UiNVoEBBUdSIQSxNBzLp2g7avTxI4zW6X5xZ9Vp+sR/HkjUdUGEQ1W9RiED3lXR4drIBeP4pYwfv5uUwC89uq/hJ/78pJlfJvggg71SriIGA7E0HMunaDtq9PEjjNbpfnFn1Wn6xH8eSNR1QYRDVb1GELSmumcAAACAAAAAgAQAAIAiBgPeVdHh2sgF4/iljB+/m5TALz26r+En/vykmV8m+CCDvRC0prpnAAAAgAAAAIAFAACAAAA=', |
104 | 103 | ], |
105 | 104 |
|
| 105 | + [ // PSBT with duplicate global tx |
| 106 | + /*$hex=*/ '70736274ff01004501000000013412cdab3412cdab3412cdab3412cdab3412cdab3412cdab3412cdab3412cdab0000000000ffffffff020100000000000000000200000000000000000000000001004501000000013412cdab3412cdab3412cdab3412cdab3412cdab3412cdab3412cdab3412cdab0000000000ffffffff0201000000000000000002000000000000000000000000000000', |
| 107 | + /*$base64=*/ 'cHNidP8BAEUBAAAAATQSzas0Es2rNBLNqzQSzas0Es2rNBLNqzQSzas0Es2rAAAAAAD/////AgEAAAAAAAAAAAIAAAAAAAAAAAAAAAABAEUBAAAAATQSzas0Es2rNBLNqzQSzas0Es2rNBLNqzQSzas0Es2rAAAAAAD/////AgEAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAA=', |
| 108 | + ], |
| 109 | + |
| 110 | + [ // PSBT with duplicate global unknown key |
| 111 | + /*$hex=*/ '70736274ff023431023635023431023635000000', |
| 112 | + /*$base64=*/ 'cHNidP8CNDECNjUCNDECNjUAAAA==', |
| 113 | + ], |
| 114 | + |
106 | 115 | ]; |
107 | 116 | } |
108 | 117 |
|
@@ -149,6 +158,13 @@ public function getValidFixtures(): array |
149 | 158 | /*$hex=*/ '70736274ff01003f0200000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000000000ffffffff010000000000000000036a010000000000000a0f0102030405060708090f0102030405060708090a0b0c0d0e0f0000', |
150 | 159 | /*$base64=*/ 'cHNidP8BAD8CAAAAAf//////////////////////////////////////////AAAAAAD/////AQAAAAAAAAAAA2oBAAAAAAAACg8BAgMEBQYHCAkPAQIDBAUGBwgJCgsMDQ4PAAA=', |
151 | 160 | ], |
| 161 | + |
| 162 | + // my own |
| 163 | + |
| 164 | + [ // PSBT with a global unknown key |
| 165 | + /*$hex=*/ '70736274ff01004501000000013412cdab3412cdab3412cdab3412cdab3412cdab3412cdab3412cdab3412cdab0000000000ffffffff0201000000000000000002000000000000000000000000014102363500000000', |
| 166 | + /*$base64=*/ 'cHNidP8BAEUBAAAAATQSzas0Es2rNBLNqzQSzas0Es2rNBLNqzQSzas0Es2rAAAAAAD/////AgEAAAAAAAAAAAIAAAAAAAAAAAAAAAABQQI2NQAAAAA=', |
| 167 | + ], |
152 | 168 | ]; |
153 | 169 | } |
154 | 170 |
|
@@ -203,11 +219,91 @@ public function testUpdate() |
203 | 219 | $psbt = new PSBT($unsignedTx, $unknowns, $inputs, $outputs); |
204 | 220 | $this->assertFalse($psbt->getInputs()[0]->hasWitnessTxOut()); |
205 | 221 | $txOut = new TransactionOutput(1, new Script()); |
206 | | - $psbt->updateInput(0, function(UpdatableInput $input) use ($txOut): UpdatableInput { |
| 222 | + $psbt->updateInput(0, function (UpdatableInput $input) use ($txOut): UpdatableInput { |
207 | 223 | $input->addWitnessTxOut($txOut); |
208 | 224 | return $input; |
209 | 225 | }); |
210 | 226 | $this->assertTrue($psbt->getInputs()[0]->hasWitnessTxOut()); |
211 | 227 | $this->assertSame($txOut, $psbt->getInputs()[0]->getWitnessTxOut()); |
212 | 228 | } |
| 229 | + |
| 230 | + /** |
| 231 | + * @expectedException \BitWasp\Bitcoin\Exceptions\InvalidPSBTException |
| 232 | + * @expectedExceptionMessage Invalid number of inputs |
| 233 | + */ |
| 234 | + public function testChecksNumInputsMatchesGreaterThan() |
| 235 | + { |
| 236 | + $unsignedTx = new Transaction(0, [ |
| 237 | + new TransactionInput(new OutPoint(Buffer::hex('', 32), 0xffffffff), new Script()), |
| 238 | + ], []); |
| 239 | + |
| 240 | + new PSBT($unsignedTx, [], [new PSBTInput(), new PSBTInput()], []); |
| 241 | + } |
| 242 | + |
| 243 | + /** |
| 244 | + * @expectedException \BitWasp\Bitcoin\Exceptions\InvalidPSBTException |
| 245 | + * @expectedExceptionMessage Invalid number of inputs |
| 246 | + */ |
| 247 | + public function testChecksNumInputsMatchesLessThan() |
| 248 | + { |
| 249 | + $unsignedTx = new Transaction(0, [ |
| 250 | + new TransactionInput(new OutPoint(Buffer::hex('', 32), 0xffffffff), new Script()), |
| 251 | + ], []); |
| 252 | + |
| 253 | + new PSBT($unsignedTx, [], [], []); |
| 254 | + } |
| 255 | + |
| 256 | + /** |
| 257 | + * @expectedException \BitWasp\Bitcoin\Exceptions\InvalidPSBTException |
| 258 | + * @expectedExceptionMessage Invalid number of outputs |
| 259 | + */ |
| 260 | + public function testChecksNumOutputsMatchesGreaterThan() |
| 261 | + { |
| 262 | + $unsignedTx = new Transaction(0, [ |
| 263 | + new TransactionInput(new OutPoint(Buffer::hex('', 32), 0xffffffff), new Script()), |
| 264 | + ], [new TransactionOutput(1, new Script()), new TransactionOutput(2, new Script())]); |
| 265 | + |
| 266 | + new PSBT($unsignedTx, [], [new PSBTInput()], [new PSBTOutput()]); |
| 267 | + } |
| 268 | + |
| 269 | + /** |
| 270 | + * @expectedException \BitWasp\Bitcoin\Exceptions\InvalidPSBTException |
| 271 | + * @expectedExceptionMessage Invalid number of outputs |
| 272 | + */ |
| 273 | + public function testChecksNumOutputsMatchesLessThan() |
| 274 | + { |
| 275 | + $unsignedTx = new Transaction(0, [ |
| 276 | + new TransactionInput(new OutPoint(Buffer::hex('', 32), 0xffffffff), new Script()), |
| 277 | + ], [new TransactionOutput(1, new Script()), new TransactionOutput(2, new Script())]); |
| 278 | + |
| 279 | + new PSBT($unsignedTx, [], [new PSBTInput()], [new PSBTOutput(), new PSBTOutput(), new PSBTOutput()]); |
| 280 | + } |
| 281 | + |
| 282 | + /** |
| 283 | + * @expectedException \RuntimeException |
| 284 | + * @expectedExceptionMessage No input at this index |
| 285 | + */ |
| 286 | + public function testUpdateInputChecksInputNum() |
| 287 | + { |
| 288 | + $unsignedTx = new Transaction(0, [ |
| 289 | + new TransactionInput(new OutPoint(Buffer::hex('', 32), 0xffffffff), new Script()), |
| 290 | + ], [new TransactionOutput(1, new Script()), new TransactionOutput(2, new Script())]); |
| 291 | + |
| 292 | + $psbt = new PSBT($unsignedTx, [], [new PSBTInput()], [new PSBTOutput(), new PSBTOutput()]); |
| 293 | + $psbt->updateInput(10, function () { |
| 294 | + }); |
| 295 | + } |
| 296 | + |
| 297 | + /** |
| 298 | + * @expectedException \InvalidArgumentException |
| 299 | + * @expectedExceptionMessage Unknowns must be a map of string keys to Buffer values |
| 300 | + */ |
| 301 | + public function testChecksUnknownsFormat() |
| 302 | + { |
| 303 | + $unsignedTx = new Transaction(0, [ |
| 304 | + new TransactionInput(new OutPoint(Buffer::hex('', 32), 0xffffffff), new Script()), |
| 305 | + ], [new TransactionOutput(1, new Script()), new TransactionOutput(2, new Script())]); |
| 306 | + |
| 307 | + new PSBT($unsignedTx, [1 => $unsignedTx], [new PSBTInput()], [new PSBTOutput(), new PSBTOutput()]); |
| 308 | + } |
213 | 309 | } |
0 commit comments