1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin; |
6
|
|
|
|
7
|
|
|
class Locktime |
8
|
|
|
{ |
9
|
|
|
const INT_MAX = 0xffffffff; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Maximum block height that can be used in locktime, as beyond |
13
|
|
|
* this is reserved for timestamp locktimes |
14
|
|
|
*/ |
15
|
|
|
const BLOCK_MAX = 500000000; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Maximum timestamp that can be encoded in locktime |
19
|
|
|
* (TIME_MAX + BLOCK_MAX = INT_MAX) |
20
|
|
|
*/ |
21
|
|
|
const TIME_MAX = self::INT_MAX - self::BLOCK_MAX; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param int $nLockTime |
25
|
|
|
* @return bool |
26
|
|
|
*/ |
27
|
8 |
|
public function isLockedToBlock(int $nLockTime): bool |
28
|
|
|
{ |
29
|
8 |
|
return $nLockTime > 0 && $nLockTime <= self::BLOCK_MAX; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Convert a $timestamp to a locktime. |
34
|
|
|
* Max timestamp is 3794967296 - 04/04/2090 @ 5:34am (UTC) |
35
|
|
|
* |
36
|
|
|
* @param int $timestamp |
37
|
|
|
* @return int |
38
|
|
|
* @throws \Exception |
39
|
|
|
*/ |
40
|
3 |
|
public function fromTimestamp(int $timestamp): int |
41
|
|
|
{ |
42
|
3 |
|
if ($timestamp > self::TIME_MAX) { |
43
|
1 |
|
throw new \Exception('Timestamp out of range'); |
44
|
|
|
} |
45
|
|
|
|
46
|
3 |
|
$locktime = self::BLOCK_MAX + $timestamp; |
47
|
3 |
|
return $locktime; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Convert a lock time to the timestamp it's locked to. |
52
|
|
|
* Throws an exception when: |
53
|
|
|
* - Lock time appears to be in the block locktime range ( < Locktime::BLOCK_MAX ) |
54
|
|
|
* - When the lock time exceeds the max possible lock time ( > Locktime::INT_MAX ) |
55
|
|
|
* |
56
|
|
|
* @param int $lockTime |
57
|
|
|
* @return int |
58
|
|
|
* @throws \Exception |
59
|
|
|
*/ |
60
|
3 |
|
public function toTimestamp(int $lockTime): int |
61
|
|
|
{ |
62
|
3 |
|
if ($lockTime <= self::BLOCK_MAX) { |
63
|
1 |
|
throw new \Exception('Lock time out of range for timestamp'); |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
if ($lockTime > self::INT_MAX) { |
67
|
1 |
|
throw new \Exception('Lock time too large'); |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
$timestamp = $lockTime - self::BLOCK_MAX; |
71
|
2 |
|
return $timestamp; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Convert $blockHeight to lock time. Doesn't convert anything really, |
76
|
|
|
* but does check the bounds of the given block height. |
77
|
|
|
* |
78
|
|
|
* @param int $blockHeight |
79
|
|
|
* @return int |
80
|
|
|
* @throws \Exception |
81
|
|
|
*/ |
82
|
3 |
|
public function fromBlockHeight(int $blockHeight): int |
83
|
|
|
{ |
84
|
3 |
|
if ($blockHeight > self::BLOCK_MAX) { |
85
|
1 |
|
throw new \Exception('This block height is too high'); |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
return $blockHeight; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Convert locktime to block height tx is locked to. Doesn't convert anything |
93
|
|
|
* really, but does check the bounds of the supplied locktime. |
94
|
|
|
* |
95
|
|
|
* @param int $lockTime |
96
|
|
|
* @return int |
97
|
|
|
* @throws \Exception |
98
|
|
|
*/ |
99
|
2 |
|
public function toBlockHeight(int $lockTime): int |
100
|
|
|
{ |
101
|
2 |
|
if ($lockTime >= self::BLOCK_MAX) { |
102
|
1 |
|
throw new \Exception('This locktime is out of range for a block height'); |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
return $lockTime; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|