1 | <?php |
||
5 | class Locktime |
||
6 | { |
||
7 | const INT_MAX = 0xffffffff; |
||
8 | |||
9 | /** |
||
10 | * Maximum block height that can be used in locktime, as beyond |
||
11 | * this is reserved for timestamp locktimes |
||
12 | */ |
||
13 | const BLOCK_MAX = 500000000; |
||
14 | |||
15 | /** |
||
16 | * Maximum timestamp that can be encoded in locktime |
||
17 | * (TIME_MAX + BLOCK_MAX = INT_MAX) |
||
18 | */ |
||
19 | const TIME_MAX = self::INT_MAX - self::BLOCK_MAX; |
||
20 | |||
21 | /** |
||
22 | * @param int $nLockTime |
||
23 | * @return bool |
||
24 | */ |
||
25 | 16 | public function isLockedToBlock($nLockTime) |
|
29 | |||
30 | /** |
||
31 | * Convert a $timestamp to a locktime. |
||
32 | * Max timestamp is 3794967296 - 04/04/2090 @ 5:34am (UTC) |
||
33 | * |
||
34 | * @param int $timestamp |
||
35 | * @return int |
||
36 | * @throws \Exception |
||
37 | */ |
||
38 | 6 | public function fromTimestamp($timestamp) |
|
47 | |||
48 | /** |
||
49 | * Convert a lock time to the timestamp it's locked to. |
||
50 | * Throws an exception when: |
||
51 | * - Lock time appears to be in the block locktime range ( < Locktime::BLOCK_MAX ) |
||
52 | * - When the lock time exceeds the max possible lock time ( > Locktime::INT_MAX ) |
||
53 | * |
||
54 | * @param int $lockTime |
||
55 | * @return int |
||
56 | * @throws \Exception |
||
57 | */ |
||
58 | 6 | public function toTimestamp($lockTime) |
|
71 | |||
72 | /** |
||
73 | * Convert $blockHeight to lock time. Doesn't convert anything really, |
||
74 | * but does check the bounds of the given block height. |
||
75 | * |
||
76 | * @param int $blockHeight |
||
77 | * @return int |
||
78 | * @throws \Exception |
||
79 | */ |
||
80 | 6 | public function fromBlockHeight($blockHeight) |
|
88 | |||
89 | /** |
||
90 | * Convert locktime to block height tx is locked to. Doesn't convert anything |
||
91 | * really, but does check the bounds of the supplied locktime. |
||
92 | * |
||
93 | * @param int $lockTime |
||
94 | * @return int |
||
95 | * @throws \Exception |
||
96 | */ |
||
97 | 4 | public function toBlockHeight($lockTime) |
|
105 | } |
||
106 |