Completed
Pull Request — master (#240)
by thomas
73:04
created
Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
require "vendor/autoload.php";
4
5
use BitWasp\Buffertools\Buffer;
6
use BitWasp\Bitcoin\Key\PrivateKeyFactory;
7
use BitWasp\Bitcoin\Transaction\OutPoint;
8
use BitWasp\Bitcoin\Script\Interpreter\InterpreterInterface as I;
0 ignored issues
show
This use statement conflicts with another class in this namespace, I.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
use BitWasp\Bitcoin\Transaction\TransactionFactory;
10
use BitWasp\Bitcoin\Script\WitnessProgram;
11
12
$wif = 'QP3p9tRpTGTefG4a8jKoktSWC7Um8qzvt8wGKMxwWyW3KTNxMxN7';
13
14
\BitWasp\Bitcoin\Bitcoin::setNetwork(\BitWasp\Bitcoin\Network\NetworkFactory::bitcoinSegnet());
15
16
17
$key = PrivateKeyFactory::fromWif($wif);
18
$pub = $key->getPublicKey();
19
$addr = $pub->getAddress();
20
21
echo $addr->getAddress() . "\n";
22
23
$redeemScript = \BitWasp\Bitcoin\Script\ScriptFactory::p2sh()->multisig(1, [$pub]);
24
$os = $redeemScript->getOutputScript();
25
$addrSH = $redeemScript->getAddress();
26
echo $addrSH->getAddress() . PHP_EOL;
27
28
$outpoint = new OutPoint(Buffer::hex('9065ff2553b170d0159a6781654a6e43c9aea883cef757b016bff2685ef8504b'), 0);
29
$value = (new \BitWasp\Bitcoin\Amount())->toSatoshis('0.5');
30
$scriptPubKey = $os;
31
32
$spend = TransactionFactory::build()
33
    ->spendOutPoint($outpoint)
34
    ->payToAddress('49000000', $addr)
35
    ->getAndReset();
36
37
$spend = (new \BitWasp\Bitcoin\Transaction\Factory\TxWitnessSigner($spend, \BitWasp\Bitcoin\Bitcoin::getEcAdapter()))
38
    ->sign(0, '500000000', $key, $scriptPubKey, $redeemScript, \BitWasp\Bitcoin\Transaction\SignatureHash\SigHash::ALL)
39
    ->get();
40
41
$consensus = \BitWasp\Bitcoin\Script\ScriptFactory::consensus(\BitWasp\Bitcoin\Script\Interpreter\InterpreterInterface::VERIFY_P2SH);
42
$validator = $spend->validator()->checkSignature($consensus, 0, '50000000', $scriptPubKey);
43
echo $spend->getHex() . "\n";
44
var_dump($validator);