Completed
Pull Request — master (#404)
by thomas
22:17
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\Address\AddressFactory;
6
use BitWasp\Bitcoin\Address\AddressInterface;
7
use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress;
8
use BitWasp\Bitcoin\Address\ScriptHashAddress;
9
use BitWasp\Bitcoin\Bitcoin;
10
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface;
11
use BitWasp\Bitcoin\Math\Math;
12
use BitWasp\Bitcoin\Network\NetworkInterface;
13
use BitWasp\Bitcoin\Script\Consensus\BitcoinConsensus;
14
use BitWasp\Bitcoin\Script\Consensus\NativeConsensus;
15
use BitWasp\Bitcoin\Script\Factory\OutputScriptFactory;
16
use BitWasp\Bitcoin\Script\Factory\P2shScriptFactory;
17
use BitWasp\Bitcoin\Script\Factory\ScriptCreator;
18
use BitWasp\Buffertools\Buffer;
19
use BitWasp\Buffertools\BufferInterface;
20
21
class ScriptFactory
22
{
23
    /**
24
     * @var OutputScriptFactory
25
     */
26
    private static $outputScriptFactory = null;
27
28
    /**
29
     * @param BufferInterface|string $string
30
     * @return ScriptInterface
31
     */
32 96
    public static function fromHex($string)
33
    {
34 96
        return self::create($string instanceof BufferInterface ? $string : Buffer::hex($string))->getScript();
35
    }
36
37
    /**
38
     * @param BufferInterface|null $buffer
39
     * @param Opcodes|null $opcodes
40
     * @param Math|null $math
41
     * @return ScriptCreator
42
     */
43 2866
    public static function create(BufferInterface $buffer = null, Opcodes $opcodes = null, Math $math = null)
44
    {
45 2866
        return new ScriptCreator($math ?: Bitcoin::getMath(), $opcodes ?: new Opcodes(), $buffer);
46
    }
47
48
    /**
49
     * @param int[]|\BitWasp\Bitcoin\Script\Interpreter\Number[]|BufferInterface[] $sequence
50
     * @return ScriptInterface
51
     */
52 2632
    public static function sequence(array $sequence)
53
    {
54 2632
        return self::create()->sequence($sequence)->getScript();
55
    }
56
57
    /**
58
     * @return OutputScriptFactory
59
     */
60 330
    public static function scriptPubKey()
61
    {
62 330
        if (self::$outputScriptFactory === null) {
63
            self::$outputScriptFactory = new OutputScriptFactory();
64
        }
65
66 330
        return self::$outputScriptFactory;
67
    }
68
69
    /**
70
     * @param EcAdapterInterface|null $ecAdapter
71
     * @return NativeConsensus
72
     */
73 36
    public static function getNativeConsensus(EcAdapterInterface $ecAdapter = null)
74
    {
75 36
        return new NativeConsensus($ecAdapter ?: Bitcoin::getEcAdapter());
76
    }
77
78
    /**
79
     * @return BitcoinConsensus
80
     */
81 33
    public static function getBitcoinConsensus()
82
    {
83 33
        return new BitcoinConsensus();
84
    }
85
86
    /**
87
     * @param EcAdapterInterface|null $ecAdapter
88
     * @return \BitWasp\Bitcoin\Script\Consensus\ConsensusInterface
89
     */
90 60
    public static function consensus(EcAdapterInterface $ecAdapter = null)
91
    {
92 60
        if (extension_loaded('bitcoinconsensus')) {
93 30
            return self::getBitcoinConsensus();
94
        } else {
95 30
            return self::getNativeConsensus($ecAdapter);
96
        }
97
    }
98
}
99