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