<commit_before><?php

namespace BitWasp\Bitcoin\Script;

use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Script\Parser\Parser;
use BitWasp\Buffertools\Buffer;
use BitWasp\Bitcoin\Crypto\Hash;
use BitWasp\Bitcoin\Serializable;

class Script extends Serializable implements ScriptInterface
{

    /**
     * @var Opcodes
     */
    private $opCodes;

    /**
     * @var null|string
     */
    protected $script;

    /**
     * @param Buffer|null $script
     * @param Opcodes|null $opCodes
     */
    public function __construct(Buffer $script = null, Opcodes $opCodes = null)
    {
        $this->script = $script instanceof Buffer ? $script->getBinary() : '';
        $this->opCodes = $opCodes ?: new Opcodes();
    }

    /**
     * @return Buffer
     */
    public function getBuffer()
    {
        return new Buffer($this->script);
    }

    /**
     * @return Parser
     */
    public function getScriptParser()
    {
        return new Parser(Bitcoin::getMath(), $this);
    }

    /**
     * Get all opcodes
     *
     * @return Opcodes
     */
    public function getOpCodes()
    {
        return $this->opCodes;
    }

    /**
     * Return a buffer containing the hash of this script.
     *
     * @return \BitWasp\Buffertools\Buffer
     */
    public function getScriptHash()
    {
        return Hash::sha256ripe160($this->getBuffer());
    }

    /**
     * @param bool|true $accurate
     * @return int
     */
    public function countSigOps($accurate = true)
    {
        $count = 0;
        $parser = $this->getScriptParser();

        $lastOp = 0xff;
        foreach ($parser as $exec) {
            if ($exec->isPush()) {
                continue;
            }

            $op = $exec->getOp();
            if ($op > Opcodes::OP_PUSHDATA4) {
                // None of these are pushdatas, so just an opcode
                if ($op === Opcodes::OP_CHECKSIG || $op === Opcodes::OP_CHECKSIGVERIFY) {
                    $count++;
                } elseif ($op === Opcodes::OP_CHECKMULTISIG || $op === Opcodes::OP_CHECKMULTISIGVERIFY) {
                    if ($accurate && ($lastOp >= Opcodes::OP_1 && $lastOp <= Opcodes::OP_16)) {
                        $c = ($lastOp - (Opcodes::OP_1 - 1));
                        $count += $c;
                    } else {
                        $count += 20;
                    }
                }

                $lastOp = $op;
            }
        }

        return $count;
    }

    /**
     * @param ScriptInterface $scriptSig
     * @return int
     */
    public function countP2shSigOps(ScriptInterface $scriptSig)
    {
        if (ScriptFactory::scriptPubKey()
            ->classify($this)
            ->isPayToScriptHash() === false
        ) {
            return $this->countSigOps(true);
        }

        $parser = $scriptSig->getScriptParser();

        $data = null;
        foreach ($parser as $exec) {
            if ($exec->getOp() > Opcodes::OP_16) {
                return 0;
            }

            if ($exec->isPush()) {
                $data = $exec->getData();
            }
        }

        if (!$data instanceof Buffer) {
            return 0;
        }

        return (new Script($data))->countSigOps(true);
    }

    /**
     * @return bool
     */
    public function isPushOnly()
    {
        $pushOnly = true;
        foreach ($this->getScriptParser()->parse() as $entity) {
            $pushOnly &= $entity instanceof Buffer;
        }
        return $pushOnly;
    }
}<commit_msg>Script: replace use of Parser::parse() with decode()<commit_after><?php

namespace BitWasp\Bitcoin\Script;

use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Script\Parser\Parser;
use BitWasp\Buffertools\Buffer;
use BitWasp\Bitcoin\Crypto\Hash;
use BitWasp\Bitcoin\Serializable;

class Script extends Serializable implements ScriptInterface
{

    /**
     * @var Opcodes
     */
    private $opCodes;

    /**
     * @var null|string
     */
    protected $script;

    /**
     * @param Buffer|null $script
     * @param Opcodes|null $opCodes
     */
    public function __construct(Buffer $script = null, Opcodes $opCodes = null)
    {
        $this->script = $script instanceof Buffer ? $script->getBinary() : '';
        $this->opCodes = $opCodes ?: new Opcodes();
    }

    /**
     * @return Buffer
     */
    public function getBuffer()
    {
        return new Buffer($this->script);
    }

    /**
     * @return Parser
     */
    public function getScriptParser()
    {
        return new Parser(Bitcoin::getMath(), $this);
    }

    /**
     * Get all opcodes
     *
     * @return Opcodes
     */
    public function getOpCodes()
    {
        return $this->opCodes;
    }

    /**
     * Return a buffer containing the hash of this script.
     *
     * @return \BitWasp\Buffertools\Buffer
     */
    public function getScriptHash()
    {
        return Hash::sha256ripe160($this->getBuffer());
    }

    /**
     * @param bool|true $accurate
     * @return int
     */
    public function countSigOps($accurate = true)
    {
        $count = 0;
        $parser = $this->getScriptParser();

        $lastOp = 0xff;
        foreach ($parser as $exec) {
            if ($exec->isPush()) {
                continue;
            }

            $op = $exec->getOp();
            if ($op > Opcodes::OP_PUSHDATA4) {
                // None of these are pushdatas, so just an opcode
                if ($op === Opcodes::OP_CHECKSIG || $op === Opcodes::OP_CHECKSIGVERIFY) {
                    $count++;
                } elseif ($op === Opcodes::OP_CHECKMULTISIG || $op === Opcodes::OP_CHECKMULTISIGVERIFY) {
                    if ($accurate && ($lastOp >= Opcodes::OP_1 && $lastOp <= Opcodes::OP_16)) {
                        $c = ($lastOp - (Opcodes::OP_1 - 1));
                        $count += $c;
                    } else {
                        $count += 20;
                    }
                }

                $lastOp = $op;
            }
        }

        return $count;
    }

    /**
     * @param ScriptInterface $scriptSig
     * @return int
     */
    public function countP2shSigOps(ScriptInterface $scriptSig)
    {
        if (ScriptFactory::scriptPubKey()
            ->classify($this)
            ->isPayToScriptHash() === false
        ) {
            return $this->countSigOps(true);
        }

        $parser = $scriptSig->getScriptParser();

        $data = null;
        foreach ($parser as $exec) {
            if ($exec->getOp() > Opcodes::OP_16) {
                return 0;
            }

            if ($exec->isPush()) {
                $data = $exec->getData();
            }
        }

        if (!$data instanceof Buffer) {
            return 0;
        }

        return (new Script($data))->countSigOps(true);
    }

    /**
     * @return bool
     */
    public function isPushOnly()
    {
        $pushOnly = true;
        foreach ($this->getScriptParser()->decode() as $entity) {
            $pushOnly &= $entity->isPush();
        }
        return $pushOnly;
    }
}
{'fraction_non_alphanumeric': 0.13552332195676906, 'fraction_numerical': 0.005972696245733789, 'mean_word_length': 1.5875643855776307, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3475647', 'n_tokens_mistral': 2152, 'n_tokens_neox': 2078, 'n_words': 440}