1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Script\Interpreter; |
4
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Bitcoin; |
6
|
|
|
use BitWasp\Bitcoin\Math\Math; |
7
|
|
|
use BitWasp\Bitcoin\Serializable; |
8
|
|
|
use BitWasp\Buffertools\Buffer; |
9
|
|
|
use BitWasp\Buffertools\BufferInterface; |
10
|
|
|
|
11
|
|
|
class Number extends Serializable |
12
|
|
|
{ |
13
|
|
|
const MAX_NUM_SIZE = 4; |
14
|
|
|
const MAX = 2147483647; // 2^31-1 |
15
|
|
|
const MIN = -2147483647; // -2^31+1 |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Math |
19
|
|
|
*/ |
20
|
|
|
private $math; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var int |
24
|
|
|
*/ |
25
|
|
|
private $number; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Number constructor. |
29
|
|
|
* @param int $number |
30
|
|
|
* @param Math $math |
31
|
|
|
*/ |
32
|
1590 |
|
public function __construct($number, Math $math) |
33
|
|
|
{ |
34
|
1590 |
|
$this->number = $number; |
35
|
1590 |
|
$this->math = $math; |
36
|
1590 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param int $number |
40
|
|
|
* @param Math|null $math |
41
|
|
|
* @return self |
42
|
|
|
*/ |
43
|
582 |
|
public static function int($number, Math $math = null) |
44
|
|
|
{ |
45
|
582 |
|
return new self( |
46
|
582 |
|
$number, |
47
|
582 |
|
$math ?: Bitcoin::getMath() |
48
|
582 |
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param BufferInterface $vch |
53
|
|
|
* @param bool $fRequireMinimal |
54
|
|
|
* @param int $maxNumSize |
55
|
|
|
* @param Math|null $math |
56
|
|
|
* @return self |
57
|
|
|
*/ |
58
|
1524 |
|
public static function buffer(BufferInterface $vch, $fRequireMinimal, $maxNumSize = self::MAX_NUM_SIZE, Math $math = null) |
59
|
|
|
{ |
60
|
1524 |
|
$size = $vch->getSize(); |
61
|
1524 |
|
if ($size > $maxNumSize) { |
62
|
|
|
throw new \RuntimeException('Script number overflow'); |
63
|
|
|
} |
64
|
|
|
|
65
|
1524 |
|
if ($fRequireMinimal && $size > 0) { |
66
|
|
|
$binary = $vch->getBinary(); |
67
|
|
|
if (ord($binary[$size - 1]) & 0x7f === 0) { |
68
|
|
|
if ($size <= 1 || ord($binary[$size - 2]) & 0x80 === 0) { |
69
|
|
|
throw new \RuntimeException('Non-minimally encoded script number'); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
1524 |
|
$math = $math ?: Bitcoin::getMath(); |
75
|
1524 |
|
$number = new self(0, $math); |
76
|
1524 |
|
$number->number = $number->parseBuffer($vch); |
|
|
|
|
77
|
1524 |
|
return $number; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param BufferInterface $buffer |
82
|
|
|
* @return int |
83
|
|
|
*/ |
84
|
1524 |
|
private function parseBuffer(BufferInterface $buffer) |
85
|
|
|
{ |
86
|
1524 |
|
$size = $buffer->getSize(); |
87
|
1524 |
|
if ($size === 0) { |
88
|
1464 |
|
return '0'; |
89
|
|
|
} |
90
|
|
|
|
91
|
1518 |
|
$chars = array_map(function ($binary) { |
92
|
1518 |
|
return ord($binary); |
93
|
|
|
|
94
|
1518 |
|
}, str_split($buffer->getBinary(), 1)); |
95
|
|
|
|
96
|
1518 |
|
$result = 0; |
97
|
1518 |
|
for ($i = 0; $i < $size; $i++) { |
98
|
1518 |
|
$mul = $this->math->mul($i, 8); |
99
|
1518 |
|
$byte = $this->math->leftShift($chars[$i], $mul); |
100
|
1518 |
|
$result = $this->math->bitwiseOr($result, $byte); |
101
|
1518 |
|
} |
102
|
|
|
|
103
|
1518 |
|
if ($chars[count($chars)-1] & 0x80) { |
104
|
30 |
|
$mask = gmp_strval(gmp_com($this->math->leftShift(0x80, (8 * ($size - 1)))), 10); |
105
|
30 |
|
return $this->math->sub(0, $this->math->bitwiseAnd($result, $mask)); |
106
|
|
|
} |
107
|
|
|
|
108
|
1488 |
|
return $result; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return BufferInterface |
113
|
|
|
*/ |
114
|
1524 |
|
private function serialize() |
115
|
|
|
{ |
116
|
1524 |
|
if ($this->math->cmp($this->number, '0') === 0) { |
117
|
1464 |
|
return new Buffer('', 0); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// Using array of integers instead of bytes |
121
|
1518 |
|
$result = []; |
122
|
1518 |
|
$negative = $this->math->cmp($this->number, 0) < 0; |
123
|
1518 |
|
$abs = $negative ? $this->math->sub(0, $this->number) : $this->number; |
124
|
|
|
|
125
|
1518 |
|
while ($this->math->cmp($abs, 0) > 0) { |
126
|
|
|
//array_unshift($result, (int)$this->math->bitwiseAnd($abs, 0xff)); |
127
|
1518 |
|
$result[] = (int)$this->math->bitwiseAnd($abs, 0xff); |
128
|
1518 |
|
$abs = $this->math->rightShift($abs, 8); |
129
|
1518 |
|
} |
130
|
|
|
|
131
|
1518 |
|
if ($result[count($result) - 1] & 0x80) { |
132
|
|
|
//array_unshift($result, $negative ? 0x80 : 0); |
133
|
12 |
|
$result[] = $negative ? 0x80 : 0; |
134
|
1518 |
|
} else if ($negative) { |
135
|
|
|
//$result[0] |= 0x80; |
136
|
24 |
|
$result[count($result) - 1] |= 0x80; |
137
|
24 |
|
} |
138
|
|
|
|
139
|
1518 |
|
$s = ''; |
140
|
1518 |
|
foreach ($result as $i) { |
141
|
1518 |
|
$s .= chr($i); |
142
|
1518 |
|
} |
143
|
|
|
|
144
|
1518 |
|
return new Buffer($s, null, $this->math); |
145
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return BufferInterface |
150
|
|
|
*/ |
151
|
1524 |
|
public function getBuffer() |
152
|
|
|
{ |
153
|
1524 |
|
return $this->serialize(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return int |
158
|
|
|
*/ |
159
|
480 |
|
public function getInt() |
160
|
|
|
{ |
161
|
480 |
|
if ($this->math->cmp($this->number, self::MAX) > 0) { |
162
|
|
|
return self::MAX; |
163
|
480 |
|
} else if ($this->math->cmp($this->number, self::MIN) < 0) { |
164
|
|
|
return self::MIN; |
165
|
|
|
} |
166
|
|
|
|
167
|
480 |
|
return $this->number; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.