Bit-Wasp /
bitcoin-php
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
|
|||
| 9 | use BitWasp\Bitcoin\Transaction\TransactionFactory; |
||
| 10 | use BitWasp\Bitcoin\Script\WitnessProgram; |
||
| 11 | use BitWasp\Bitcoin\Script\Interpreter\InterpreterInterface; |
||
| 12 | |||
| 13 | $wif = 'QP3p9tRpTGTefG4a8jKoktSWC7Um8qzvt8wGKMxwWyW3KTNxMxN7'; |
||
| 14 | |||
| 15 | \BitWasp\Bitcoin\Bitcoin::setNetwork(\BitWasp\Bitcoin\Network\NetworkFactory::bitcoinSegnet()); |
||
| 16 | |||
| 17 | |||
| 18 | $key = PrivateKeyFactory::fromWif($wif); |
||
| 19 | $pub = $key->getPublicKey(); |
||
| 20 | $addr = $pub->getAddress(); |
||
| 21 | |||
| 22 | $redeemScript = \BitWasp\Bitcoin\Script\ScriptFactory::scriptPubKey()->multisig(1, [$pub]); |
||
| 23 | echo $redeemScript->getHex() . "\n"; |
||
| 24 | |||
| 25 | $p2sh = \BitWasp\Bitcoin\Script\ScriptFactory::p2sh()->parse($redeemScript); |
||
| 26 | $addrSH = $p2sh->getAddress()->getHash(); |
||
| 27 | echo $addrSH. PHP_EOL; |
||
| 28 | |||
| 29 | $witness = new WitnessProgram(0, \BitWasp\Bitcoin\Crypto\Hash::sha256($redeemScript->getBuffer())); |
||
| 30 | |||
| 31 | $outpoint = new OutPoint(Buffer::hex('9065ff2553b170d0159a6781654a6e43c9aea883cef757b016bff2685ef8504b'), 0); |
||
| 32 | $value = (new \BitWasp\Bitcoin\Amount())->toSatoshis('0.5'); |
||
| 33 | $scriptPubKey = $p2sh->getOutputScript(); |
||
| 34 | |||
| 35 | $spend = TransactionFactory::build() |
||
| 36 | ->spendOutPoint($outpoint) |
||
| 37 | ->output('49000000', $p2sh->getOutputScript()) |
||
| 38 | ->getAndReset(); |
||
| 39 | |||
| 40 | $spend = (new \BitWasp\Bitcoin\Transaction\Factory\TxWitnessSigner($spend, \BitWasp\Bitcoin\Bitcoin::getEcAdapter())) |
||
| 41 | ->sign(0, '500000000', $key, $scriptPubKey, $redeemScript, \BitWasp\Bitcoin\Transaction\SignatureHash\SigHash::ALL) |
||
| 42 | ->get(); |
||
| 43 | |||
| 44 | $consensus = \BitWasp\Bitcoin\Script\ScriptFactory::consensus(InterpreterInterface::VERIFY_P2SH | InterpreterInterface::VERIFY_WITNESS); |
||
| 45 | $validator = $spend->validator()->checkSignature($consensus, 0, '50000000', $scriptPubKey); |
||
| 46 | echo $spend->getWitnessBuffer()->getHex() . "\n"; |
||
| 47 | var_dump($validator); |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/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: