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\Flags; |
8
|
|
|
use BitWasp\Bitcoin\Math\Math; |
9
|
|
|
use BitWasp\Bitcoin\Script\Consensus\BitcoinConsensus; |
10
|
|
|
use BitWasp\Bitcoin\Script\Consensus\NativeConsensus; |
11
|
|
|
use BitWasp\Bitcoin\Script\Factory\InputScriptFactory; |
12
|
|
|
use BitWasp\Bitcoin\Script\Factory\OutputScriptFactory; |
13
|
|
|
use BitWasp\Bitcoin\Script\Factory\P2shScriptFactory; |
14
|
|
|
use BitWasp\Bitcoin\Script\Factory\ScriptCreator; |
15
|
|
|
use BitWasp\Bitcoin\Script\Factory\ScriptInfoFactory; |
16
|
|
|
use BitWasp\Bitcoin\Script\Factory\WitnessScriptFactory; |
17
|
|
|
use BitWasp\Bitcoin\Script\Interpreter\InterpreterInterface; |
18
|
|
|
use BitWasp\Buffertools\Buffer; |
19
|
|
|
use BitWasp\Buffertools\BufferInterface; |
20
|
|
|
|
21
|
|
|
class ScriptFactory |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @param BufferInterface|string $string |
25
|
|
|
* @return Script |
26
|
|
|
*/ |
27
|
708 |
|
public static function fromHex($string) |
28
|
|
|
{ |
29
|
708 |
|
return self::create($string instanceof BufferInterface ? $string : Buffer::hex($string))->getScript(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param BufferInterface|null $buffer |
34
|
|
|
* @param Opcodes|null $opcodes |
35
|
|
|
* @param Math|null $math |
36
|
|
|
* @return ScriptCreator |
37
|
|
|
*/ |
38
|
1137 |
|
public static function create(BufferInterface $buffer = null, Opcodes $opcodes = null, Math $math = null) |
39
|
|
|
{ |
40
|
1137 |
|
return new ScriptCreator($math ?: Bitcoin::getMath(), $opcodes ?: new Opcodes(), $buffer); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return InputScriptFactory |
45
|
|
|
*/ |
46
|
93 |
|
public static function scriptSig() |
47
|
|
|
{ |
48
|
93 |
|
return new InputScriptFactory(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return OutputScriptFactory |
53
|
|
|
*/ |
54
|
345 |
|
public static function scriptPubKey() |
55
|
|
|
{ |
56
|
345 |
|
return new OutputScriptFactory(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Opcodes|null $opcodes |
61
|
|
|
* @return P2shScriptFactory |
62
|
|
|
*/ |
63
|
42 |
|
public static function p2sh(Opcodes $opcodes = null) |
64
|
|
|
{ |
65
|
42 |
|
return new P2shScriptFactory(self::scriptPubKey(), $opcodes ?: new Opcodes()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param Opcodes|null $opcodes |
70
|
|
|
* @return WitnessScriptFactory |
71
|
|
|
*/ |
72
|
|
|
public static function witness(Opcodes $opcodes = null) |
73
|
|
|
{ |
74
|
|
|
return new WitnessScriptFactory(self::scriptPubKey(), $opcodes ?: new Opcodes()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param ScriptInterface $script |
79
|
|
|
* @param ScriptInterface|null $redeemScript |
80
|
|
|
* @return ScriptInfo\ScriptInfoInterface |
81
|
|
|
*/ |
82
|
135 |
|
public static function info(ScriptInterface $script, ScriptInterface $redeemScript = null) |
83
|
|
|
{ |
84
|
135 |
|
return (new ScriptInfoFactory())->load($script, $redeemScript); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return Flags |
89
|
|
|
*/ |
90
|
24 |
|
public static function defaultFlags() |
91
|
|
|
{ |
92
|
24 |
|
return new Flags( |
93
|
24 |
|
InterpreterInterface::VERIFY_P2SH | InterpreterInterface::VERIFY_STRICTENC | InterpreterInterface::VERIFY_DERSIG | |
94
|
24 |
|
InterpreterInterface::VERIFY_LOW_S | InterpreterInterface::VERIFY_NULL_DUMMY | InterpreterInterface::VERIFY_SIGPUSHONLY | |
95
|
24 |
|
InterpreterInterface::VERIFY_DISCOURAGE_UPGRADABLE_NOPS | InterpreterInterface::VERIFY_CLEAN_STACK | |
96
|
24 |
|
InterpreterInterface::VERIFY_CHECKLOCKTIMEVERIFY | InterpreterInterface::VERIFY_CHECKSEQUENCEVERIFY |
97
|
24 |
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param Flags|null $flags |
102
|
|
|
* @param EcAdapterInterface|null $ecAdapter |
103
|
|
|
* @return NativeConsensus |
104
|
|
|
*/ |
105
|
12 |
|
public static function getNativeConsensus(Flags $flags = null, EcAdapterInterface $ecAdapter = null) |
106
|
|
|
{ |
107
|
12 |
|
return new NativeConsensus($ecAdapter ?: Bitcoin::getEcAdapter(), $flags ?: self::defaultFlags()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param Flags|null $flags |
112
|
|
|
* @return BitcoinConsensus |
113
|
|
|
*/ |
114
|
|
|
public static function getBitcoinConsensus(Flags $flags = null) |
115
|
|
|
{ |
116
|
|
|
return new BitcoinConsensus($flags ?: self::defaultFlags()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param Flags|null $flags |
121
|
|
|
* @param EcAdapterInterface|null $ecAdapter |
122
|
|
|
* @return \BitWasp\Bitcoin\Script\Consensus\ConsensusInterface |
123
|
|
|
*/ |
124
|
6 |
|
public static function consensus(Flags $flags = null, EcAdapterInterface $ecAdapter = null) |
125
|
|
|
{ |
126
|
6 |
|
if (extension_loaded('bitcoinconsensus')) { |
127
|
|
|
return self::getBitcoinConsensus($flags); |
128
|
|
|
} else { |
129
|
6 |
|
return self::getNativeConsensus($flags, $ecAdapter); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|