Completed
Pull Request — master (#285)
by thomas
21:56
created

ScriptFactory::sequence()   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 1
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
    }
28
29
    /**
30
     * @param BufferInterface|null $buffer
31
     * @param Opcodes|null $opcodes
32
     * @param Math|null $math
33
     * @return ScriptCreator
34
     */
35 1122
    public static function create(BufferInterface $buffer = null, Opcodes $opcodes = null, Math $math = null)
36
    {
37 1122
        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 84
    public static function sequence(array $sequence)
45
    {
46 84
        return self::create()->sequence($sequence)->getScript();
47
    }
48
49
    /**
50
     * @return OutputScriptFactory
51
     */
52 270
    public static function scriptPubKey()
53
    {
54 270
        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
     * @return ScriptInfo\ScriptInfoInterface
69
     */
70 6
    public static function info(ScriptInterface $script)
71
    {
72 6
        return (new ScriptInfoFactory())->load($script);
73
    }
74
75
    /**
76
     * @return int
77
     */
78 12
    public static function defaultFlags()
79
    {
80
        return
81 12
            InterpreterInterface::VERIFY_P2SH | InterpreterInterface::VERIFY_STRICTENC | InterpreterInterface::VERIFY_DERSIG |
82 12
            InterpreterInterface::VERIFY_LOW_S | InterpreterInterface::VERIFY_NULL_DUMMY | InterpreterInterface::VERIFY_SIGPUSHONLY |
83 12
            InterpreterInterface::VERIFY_DISCOURAGE_UPGRADABLE_NOPS | InterpreterInterface::VERIFY_CLEAN_STACK |
84 12
            InterpreterInterface::VERIFY_CHECKLOCKTIMEVERIFY | InterpreterInterface::VERIFY_WITNESS
85 12
        ;
86
    }
87
88
    /**
89
     * @param int|null $flags
90
     * @param EcAdapterInterface|null $ecAdapter
91
     * @return NativeConsensus
92
     */
93 66
    public static function getNativeConsensus($flags, EcAdapterInterface $ecAdapter = null)
94
    {
95 66
        return new NativeConsensus($ecAdapter ?: Bitcoin::getEcAdapter(), $flags);
96
    }
97
98
    /**
99
     * @param int|null $flags
100
     * @return BitcoinConsensus
101
     */
102
    public static function getBitcoinConsensus($flags)
103
    {
104
        return new BitcoinConsensus($flags);
105
    }
106
107
    /**
108
     * @param int|null $flags
109
     * @param EcAdapterInterface|null $ecAdapter
110
     * @return \BitWasp\Bitcoin\Script\Consensus\ConsensusInterface
111
     */
112 60
    public static function consensus($flags, EcAdapterInterface $ecAdapter = null)
113
    {
114 60
        if (extension_loaded('bitcoinconsensus')) {
115
            return self::getBitcoinConsensus($flags);
116
        } else {
117 60
            return self::getNativeConsensus($flags, $ecAdapter);
118
        }
119
    }
120
}
121