forked from Bit-Wasp/bitcoin-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransactionInput.php
165 lines (141 loc) · 3.96 KB
/
TransactionInput.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
declare(strict_types=1);
namespace BitWasp\Bitcoin\Transaction;
use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Script\ScriptInterface;
use BitWasp\Bitcoin\Serializable;
use BitWasp\Bitcoin\Serializer\Transaction\OutPointSerializer;
use BitWasp\Bitcoin\Serializer\Transaction\TransactionInputSerializer;
use BitWasp\Buffertools\BufferInterface;
class TransactionInput extends Serializable implements TransactionInputInterface
{
/**
* @var OutPointInterface
*/
private $outPoint;
/**
* @var ScriptInterface
*/
private $script;
/**
* @var int
*/
private $sequence;
/**
* @param OutPointInterface $outPoint
* @param ScriptInterface $script
* @param int $sequence
*/
public function __construct(OutPointInterface $outPoint, ScriptInterface $script, int $sequence = self::SEQUENCE_FINAL)
{
$this->outPoint = $outPoint;
$this->script = $script;
$this->sequence = $sequence;
}
/**
* @return OutPointInterface
*/
public function getOutPoint(): OutPointInterface
{
return $this->outPoint;
}
/**
* @return ScriptInterface
*/
public function getScript(): ScriptInterface
{
return $this->script;
}
/**
* @return int
*/
public function getSequence(): int
{
return $this->sequence;
}
/**
* @param TransactionInputInterface $other
* @return bool
*/
public function equals(TransactionInputInterface $other): bool
{
if (!$this->outPoint->equals($other->getOutPoint())) {
return false;
}
if (!$this->script->equals($other->getScript())) {
return false;
}
return gmp_cmp(gmp_init($this->sequence), gmp_init($other->getSequence())) === 0;
}
/**
* Check whether this transaction is a Coinbase transaction
*
* @return bool
*/
public function isCoinbase(): bool
{
$outpoint = $this->outPoint;
return $outpoint->getTxId()->getBinary() === str_pad('', 32, "\x00")
&& $outpoint->getVout() == 0xffffffff;
}
/**
* @return bool
*/
public function isFinal(): bool
{
$math = Bitcoin::getMath();
return $math->cmp(gmp_init($this->getSequence(), 10), gmp_init(self::SEQUENCE_FINAL, 10)) === 0;
}
/**
* @return bool
*/
public function isSequenceLockDisabled(): bool
{
if ($this->isCoinbase()) {
return true;
}
return ($this->sequence & self::SEQUENCE_LOCKTIME_DISABLE_FLAG) !== 0;
}
/**
* @return bool
*/
public function isLockedToTime(): bool
{
return !$this->isSequenceLockDisabled() && (($this->sequence & self::SEQUENCE_LOCKTIME_TYPE_FLAG) === self::SEQUENCE_LOCKTIME_TYPE_FLAG);
}
/**
* @return bool
*/
public function isLockedToBlock(): bool
{
return !$this->isSequenceLockDisabled() && (($this->sequence & self::SEQUENCE_LOCKTIME_TYPE_FLAG) === 0);
}
/**
* @return int
*/
public function getRelativeTimeLock(): int
{
if (!$this->isLockedToTime()) {
throw new \RuntimeException('Cannot decode time based locktime when disable flag set/timelock flag unset/tx is coinbase');
}
// Multiply by 512 to convert locktime to seconds
return ($this->sequence & self::SEQUENCE_LOCKTIME_MASK) * 512;
}
/**
* @return int
*/
public function getRelativeBlockLock(): int
{
if (!$this->isLockedToBlock()) {
throw new \RuntimeException('Cannot decode block locktime when disable flag set/timelock flag set/tx is coinbase');
}
return $this->sequence & self::SEQUENCE_LOCKTIME_MASK;
}
/**
* @return BufferInterface
*/
public function getBuffer(): BufferInterface
{
return (new TransactionInputSerializer(new OutPointSerializer()))->serialize($this);
}
}