1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Transaction\Factory\ScriptInfo; |
4
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Locktime; |
6
|
|
|
use BitWasp\Bitcoin\Script\Interpreter\Number; |
7
|
|
|
use BitWasp\Bitcoin\Script\Opcodes; |
8
|
|
|
use BitWasp\Bitcoin\Script\Parser\Operation; |
9
|
|
|
use BitWasp\Bitcoin\Script\ScriptInterface; |
10
|
|
|
|
11
|
|
|
class CheckLocktimeVerify |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var int |
15
|
|
|
*/ |
16
|
|
|
private $nLockTime; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var bool |
20
|
|
|
*/ |
21
|
|
|
private $toBlock; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* CheckLocktimeVerify constructor. |
25
|
|
|
* @param int $nLockTime |
26
|
|
|
*/ |
27
|
16 |
|
public function __construct($nLockTime) |
28
|
|
|
{ |
29
|
16 |
|
if ($nLockTime < 0) { |
30
|
|
|
throw new \RuntimeException("locktime cannot be negative"); |
31
|
|
|
} |
32
|
|
|
|
33
|
16 |
|
if ($nLockTime > Locktime::INT_MAX) { |
34
|
|
|
throw new \RuntimeException("nLockTime exceeds maximum value"); |
35
|
|
|
} |
36
|
|
|
|
37
|
16 |
|
$this->nLockTime = $nLockTime; |
38
|
16 |
|
$this->toBlock = (new Locktime())->isLockedToBlock($nLockTime); |
39
|
16 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param Operation[] $chunks |
43
|
|
|
* @param bool $fMinimal |
44
|
|
|
* @return static |
45
|
|
|
*/ |
46
|
166 |
|
public static function fromDecodedScript(array $chunks, $fMinimal = false) |
47
|
|
|
{ |
48
|
166 |
|
if (count($chunks) !== 3) { |
49
|
166 |
|
throw new \RuntimeException("Invalid number of items for CLTV"); |
50
|
|
|
} |
51
|
|
|
|
52
|
132 |
|
if (!$chunks[0]->isPush()) { |
53
|
102 |
|
throw new \InvalidArgumentException('CLTV script had invalid value for time'); |
54
|
|
|
} |
55
|
|
|
|
56
|
30 |
|
if ($chunks[1]->getOp() !== Opcodes::OP_CHECKLOCKTIMEVERIFY) { |
57
|
14 |
|
throw new \InvalidArgumentException('CLTV script invalid opcode'); |
58
|
|
|
} |
59
|
|
|
|
60
|
16 |
|
if ($chunks[2]->getOp() !== Opcodes::OP_DROP) { |
61
|
|
|
throw new \InvalidArgumentException('CLTV script invalid opcode'); |
62
|
|
|
} |
63
|
|
|
|
64
|
16 |
|
$numLockTime = Number::buffer($chunks[0]->getData(), $fMinimal, 5); |
65
|
|
|
|
66
|
16 |
|
return new static($numLockTime->getInt()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param ScriptInterface $script |
71
|
|
|
* @return CheckLocktimeVerify |
72
|
|
|
*/ |
73
|
|
|
public static function fromScript(ScriptInterface $script) |
74
|
|
|
{ |
75
|
|
|
return static::fromDecodedScript($script->getScriptParser()->decode()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return int |
80
|
|
|
*/ |
81
|
16 |
|
public function getLocktime() |
82
|
|
|
{ |
83
|
16 |
|
return $this->nLockTime; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
2 |
|
public function isLockedToBlock() |
90
|
|
|
{ |
91
|
2 |
|
return $this->toBlock; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|