Completed
Push — master ( e1c430...ecdee9 )
by thomas
26:24
created

ScriptFactory::info()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Script;
4
5
use BitWasp\Bitcoin\Bitcoin;
6
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface;
7
use BitWasp\Bitcoin\Math\Math;
8
use BitWasp\Bitcoin\Script\Consensus\BitcoinConsensus;
9
use BitWasp\Bitcoin\Script\Consensus\NativeConsensus;
10
use BitWasp\Bitcoin\Script\Factory\OutputScriptFactory;
11
use BitWasp\Bitcoin\Script\Factory\P2shScriptFactory;
12
use BitWasp\Bitcoin\Script\Factory\ScriptCreator;
13
use BitWasp\Bitcoin\Script\Factory\ScriptInfoFactory;
14
use BitWasp\Bitcoin\Script\Interpreter\InterpreterInterface;
15
use BitWasp\Buffertools\Buffer;
16
use BitWasp\Buffertools\BufferInterface;
17
18
class ScriptFactory
19
{
20
    /**
21
     * @param BufferInterface|string $string
22
     * @return Script
23
     */
24 738
    public static function fromHex($string)
25
    {
26 738
        return self::create($string instanceof BufferInterface ? $string : Buffer::hex($string))->getScript();
27 6
    }
28
29
    /**
30
     * @param BufferInterface|null $buffer
31
     * @param Opcodes|null $opcodes
32
     * @param Math|null $math
33
     * @return ScriptCreator
34
     */
35 1209
    public static function create(BufferInterface $buffer = null, Opcodes $opcodes = null, Math $math = null)
36
    {
37 1209
        return new ScriptCreator($math ?: Bitcoin::getMath(), $opcodes ?: new Opcodes(), $buffer);
38
    }
39
40
    /**
41
     * @param int[]|\BitWasp\Bitcoin\Script\Interpreter\Number[]|BufferInterface[] $sequence
42
     * @return ScriptInterface
43
     */
44 117
    public static function sequence(array $sequence)
45
    {
46 117
        return self::create()->sequence($sequence)->getScript();
47
    }
48
49
    /**
50
     * @return OutputScriptFactory
51
     */
52 387
    public static function scriptPubKey()
53
    {
54 387
        return new OutputScriptFactory();
55
    }
56
57
    /**
58
     * @param Opcodes|null $opcodes
59
     * @return P2shScriptFactory
60
     */
61 42
    public static function p2sh(Opcodes $opcodes = null)
62
    {
63 42
        return new P2shScriptFactory(self::scriptPubKey(), $opcodes ?: new Opcodes());
64
    }
65
66
    /**
67
     * @param ScriptInterface $script
68
     * @param ScriptInterface|null $redeemScript
69
     * @return ScriptInfo\ScriptInfoInterface
70
     */
71 135
    public static function info(ScriptInterface $script, ScriptInterface $redeemScript = null)
72
    {
73 135
        return (new ScriptInfoFactory())->load($script, $redeemScript);
74
    }
75
76
    /**
77
     * @return int
78
     */
79 12
    public static function defaultFlags()
80
    {
81
        return
82 12
            InterpreterInterface::VERIFY_P2SH | InterpreterInterface::VERIFY_STRICTENC | InterpreterInterface::VERIFY_DERSIG |
83 12
            InterpreterInterface::VERIFY_LOW_S | InterpreterInterface::VERIFY_NULL_DUMMY | InterpreterInterface::VERIFY_SIGPUSHONLY |
84 12
            InterpreterInterface::VERIFY_DISCOURAGE_UPGRADABLE_NOPS | InterpreterInterface::VERIFY_CLEAN_STACK |
85 12
            InterpreterInterface::VERIFY_CHECKLOCKTIMEVERIFY | InterpreterInterface::VERIFY_WITNESS
86 12
        ;
87
    }
88
89
    /**
90
     * @param int|null $flags
91
     * @param EcAdapterInterface|null $ecAdapter
92
     * @return NativeConsensus
93
     */
94 66
    public static function getNativeConsensus($flags = null, EcAdapterInterface $ecAdapter = null)
95
    {
96 66
        if ($flags === null) {
97
            $flags = self::defaultFlags();
98
        }
99
100 66
        return new NativeConsensus($ecAdapter ?: Bitcoin::getEcAdapter(), $flags);
101
    }
102
103
    /**
104
     * @param int|null $flags
105
     * @return BitcoinConsensus
106
     */
107
    public static function getBitcoinConsensus($flags = null)
108
    {
109
        if ($flags === null) {
110
            $flags = self::defaultFlags();
111
        }
112
113
        return new BitcoinConsensus($flags);
114
    }
115
116
    /**
117
     * @param int|null $flags
118
     * @param EcAdapterInterface|null $ecAdapter
119
     * @return \BitWasp\Bitcoin\Script\Consensus\ConsensusInterface
120
     */
121 60
    public static function consensus($flags, EcAdapterInterface $ecAdapter = null)
122
    {
123 60
        if (extension_loaded('bitcoinconsensus')) {
124
            return self::getBitcoinConsensus($flags);
125
        } else {
126 60
            return self::getNativeConsensus($flags, $ecAdapter);
127
        }
128
    }
129
}
130