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