|
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\Interpreter\InterpreterInterface; |
|
17
|
|
|
use BitWasp\Buffertools\Buffer; |
|
18
|
|
|
use BitWasp\Buffertools\BufferInterface; |
|
19
|
|
|
|
|
20
|
|
|
class ScriptFactory |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @param Buffer|string $string |
|
24
|
|
|
* @return Script |
|
25
|
|
|
*/ |
|
26
|
708 |
|
public static function fromHex($string) |
|
27
|
6 |
|
{ |
|
28
|
708 |
|
return self::create($string instanceof BufferInterface ? $string : Buffer::hex($string))->getScript(); |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param BufferInterface|null $buffer |
|
33
|
|
|
* @param Opcodes|null $opcodes |
|
34
|
|
|
* @param Math|null $math |
|
35
|
|
|
* @return ScriptCreator |
|
36
|
|
|
*/ |
|
37
|
1137 |
|
public static function create(BufferInterface $buffer = null, Opcodes $opcodes = null, Math $math = null) |
|
38
|
|
|
{ |
|
39
|
1137 |
|
return new ScriptCreator($math ?: Bitcoin::getMath(), $opcodes ?: new Opcodes(), $buffer); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return InputScriptFactory |
|
44
|
|
|
*/ |
|
45
|
93 |
|
public static function scriptSig() |
|
46
|
|
|
{ |
|
47
|
93 |
|
return new InputScriptFactory(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return OutputScriptFactory |
|
52
|
|
|
*/ |
|
53
|
345 |
|
public static function scriptPubKey() |
|
54
|
|
|
{ |
|
55
|
345 |
|
return new OutputScriptFactory(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return P2shScriptFactory |
|
60
|
|
|
*/ |
|
61
|
42 |
|
public static function p2sh() |
|
62
|
|
|
{ |
|
63
|
42 |
|
return new P2shScriptFactory(self::scriptPubKey()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param ScriptInterface $script |
|
68
|
|
|
* @param ScriptInterface|null $redeemScript |
|
69
|
|
|
* @return ScriptInfo\ScriptInfoInterface |
|
70
|
|
|
*/ |
|
71
|
135 |
|
public static function info(ScriptInterface $script, ScriptInterface $redeemScript = null) |
|
72
|
|
|
{ |
|
73
|
135 |
|
return (new ScriptInfoFactory())->load($script, $redeemScript); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return Flags |
|
79
|
|
|
*/ |
|
80
|
24 |
|
public static function defaultFlags() |
|
81
|
|
|
{ |
|
82
|
24 |
|
return new Flags( |
|
83
|
24 |
|
InterpreterInterface::VERIFY_P2SH | InterpreterInterface::VERIFY_STRICTENC | InterpreterInterface::VERIFY_DERSIG | |
|
84
|
24 |
|
InterpreterInterface::VERIFY_LOW_S | InterpreterInterface::VERIFY_NULL_DUMMY | InterpreterInterface::VERIFY_SIGPUSHONLY | |
|
85
|
24 |
|
InterpreterInterface::VERIFY_DISCOURAGE_UPGRADABLE_NOPS | InterpreterInterface::VERIFY_CLEAN_STACK | |
|
86
|
24 |
|
InterpreterInterface::VERIFY_CHECKLOCKTIMEVERIFY | InterpreterInterface::VERIFY_CHECKSEQUENCEVERIFY |
|
87
|
24 |
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param Flags|null $flags |
|
92
|
|
|
* @param EcAdapterInterface|null $ecAdapter |
|
93
|
|
|
* @return NativeConsensus |
|
94
|
|
|
*/ |
|
95
|
12 |
|
public static function getNativeConsensus(Flags $flags = null, EcAdapterInterface $ecAdapter = null) |
|
96
|
|
|
{ |
|
97
|
12 |
|
return new NativeConsensus($ecAdapter ?: Bitcoin::getEcAdapter(), $flags ?: self::defaultFlags()); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param Flags|null $flags |
|
102
|
|
|
* @return BitcoinConsensus |
|
103
|
|
|
*/ |
|
104
|
|
|
public static function getBitcoinConsensus(Flags $flags = null) |
|
105
|
|
|
{ |
|
106
|
|
|
return new BitcoinConsensus($flags ?: self::defaultFlags()); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param Flags|null $flags |
|
111
|
|
|
* @param EcAdapterInterface|null $ecAdapter |
|
112
|
|
|
* @return \BitWasp\Bitcoin\Script\Consensus\ConsensusInterface |
|
113
|
|
|
*/ |
|
114
|
6 |
|
public static function consensus(Flags $flags = null, EcAdapterInterface $ecAdapter = null) |
|
115
|
|
|
{ |
|
116
|
6 |
|
if (extension_loaded('bitcoinconsensus')) { |
|
117
|
|
|
return self::getBitcoinConsensus($flags); |
|
118
|
|
|
} else { |
|
119
|
6 |
|
return self::getNativeConsensus($flags, $ecAdapter); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.