Completed
Push — master ( 7ac9fb...3b1821 )
by thomas
28:43
created

Locktime   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 101
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 11
lcom 0
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isLockedToBlock() 0 4 2
A fromTimestamp() 0 9 2
A toTimestamp() 0 13 3
A fromBlockHeight() 0 8 2
A toBlockHeight() 0 8 2
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 16
    public function isLockedToBlock($nLockTime)
26
    {
27 16
        return $nLockTime > 0 && $nLockTime <= self::BLOCK_MAX;
28
    }
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)
39
    {
40 6
        if ($timestamp > self::TIME_MAX) {
41 2
            throw new \Exception('Timestamp out of range');
42
        }
43
44 6
        $locktime = self::BLOCK_MAX + $timestamp;
45 6
        return $locktime;
46
    }
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)
59
    {
60 6
        if ($lockTime <= self::BLOCK_MAX) {
61 2
            throw new \Exception('Lock time out of range for timestamp');
62
        }
63
64 4
        if ($lockTime > self::INT_MAX) {
65 2
            throw new \Exception('Lock time too large');
66
        }
67
68 4
        $timestamp = $lockTime - self::BLOCK_MAX;
69 4
        return $timestamp;
70
    }
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)
81
    {
82 6
        if ($blockHeight > self::BLOCK_MAX) {
83 2
            throw new \Exception('This block height is too high');
84
        }
85
86 4
        return $blockHeight;
87
    }
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)
98
    {
99 4
        if ($lockTime >= self::BLOCK_MAX) {
100 2
            throw new \Exception('This locktime is out of range for a block height');
101
        }
102
103 2
        return $lockTime;
104
    }
105
}
106