Completed
Pull Request — master (#392)
by thomas
191:09 queued 188:38
created

ScriptFactory::fromHex()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
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\Buffertools\Buffer;
14
use BitWasp\Buffertools\BufferInterface;
15
16
class ScriptFactory
17
{
18
    /**
19
     * @param BufferInterface|string $string
20
     * @return ScriptInterface
21
     */
22 96
    public static function fromHex($string)
23
    {
24 96
        return self::create($string instanceof BufferInterface ? $string : Buffer::hex($string))->getScript();
25
    }
26
27
    /**
28
     * @param BufferInterface|null $buffer
29
     * @param Opcodes|null $opcodes
30
     * @param Math|null $math
31
     * @return ScriptCreator
32
     */
33 4904
    public static function create(BufferInterface $buffer = null, Opcodes $opcodes = null, Math $math = null)
34
    {
35 4904
        return new ScriptCreator($math ?: Bitcoin::getMath(), $opcodes ?: new Opcodes(), $buffer);
36
    }
37
38
    /**
39
     * @param int[]|\BitWasp\Bitcoin\Script\Interpreter\Number[]|BufferInterface[] $sequence
40
     * @return ScriptInterface
41
     */
42 4670
    public static function sequence(array $sequence)
43
    {
44 4670
        return self::create()->sequence($sequence)->getScript();
45
    }
46
47
    /**
48
     * @return OutputScriptFactory
49
     */
50 270
    public static function scriptPubKey()
51
    {
52 270
        return new OutputScriptFactory();
53
    }
54
55
    /**
56
     * @param EcAdapterInterface|null $ecAdapter
57
     * @return NativeConsensus
58
     */
59 36
    public static function getNativeConsensus(EcAdapterInterface $ecAdapter = null)
60
    {
61 36
        return new NativeConsensus($ecAdapter ?: Bitcoin::getEcAdapter());
62
    }
63
64
    /**
65
     * @return BitcoinConsensus
66
     */
67 33
    public static function getBitcoinConsensus()
68
    {
69 33
        return new BitcoinConsensus();
70
    }
71
72
    /**
73
     * @param EcAdapterInterface|null $ecAdapter
74
     * @return \BitWasp\Bitcoin\Script\Consensus\ConsensusInterface
75
     */
76 60
    public static function consensus(EcAdapterInterface $ecAdapter = null)
77
    {
78 60
        if (extension_loaded('bitcoinconsensus')) {
79 30
            return self::getBitcoinConsensus();
80
        } else {
81 30
            return self::getNativeConsensus($ecAdapter);
82
        }
83
    }
84
}
85