Completed
Pull Request — master (#393)
by thomas
87:52 queued 16:38
created

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