1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Script; |
4
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Collection\StaticCollection; |
6
|
|
|
use BitWasp\Bitcoin\Serializer\Script\ScriptWitnessSerializer; |
7
|
|
|
use BitWasp\Buffertools\BufferInterface; |
8
|
|
|
|
9
|
|
|
class ScriptWitness extends StaticCollection implements ScriptWitnessInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* ScriptWitness constructor. |
13
|
|
|
* @param BufferInterface[] $sigValues |
14
|
|
|
*/ |
15
|
90 |
|
public function __construct(array $sigValues) |
16
|
|
|
{ |
17
|
90 |
|
$this->set = new \SplFixedArray(count($sigValues)); |
18
|
90 |
|
foreach ($sigValues as $idx => $push) { |
19
|
30 |
|
if (!$push instanceof BufferInterface) { |
20
|
|
|
throw new \InvalidArgumentException('Must provide BufferInterface[] to ScriptWitness'); |
21
|
|
|
} |
22
|
30 |
|
$this->set->offsetSet($idx, $push); |
23
|
90 |
|
} |
24
|
90 |
|
} |
25
|
|
|
|
26
|
|
|
public function __clone() |
27
|
|
|
{ |
28
|
|
|
$outputs = $this->set; |
29
|
|
|
$this->set = new \SplFixedArray(count($outputs)); |
30
|
|
|
|
31
|
|
|
foreach ($outputs as $idx => $output) { |
32
|
|
|
$this->set->offsetSet($idx, $output); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return BufferInterface |
38
|
|
|
*/ |
39
|
|
|
public function bottom() |
40
|
|
|
{ |
41
|
|
|
return parent::offsetGet(count($this) - 1); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return BufferInterface |
46
|
|
|
*/ |
47
|
30 |
|
public function current() |
48
|
|
|
{ |
49
|
30 |
|
return $this->set->current(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param int $offset |
54
|
|
|
* @return BufferInterface |
55
|
|
|
*/ |
56
|
30 |
|
public function offsetGet($offset) |
57
|
|
|
{ |
58
|
30 |
|
if (!$this->set->offsetExists($offset)) { |
59
|
|
|
throw new \OutOfRangeException('No offset found'); |
60
|
|
|
} |
61
|
|
|
|
62
|
30 |
|
return $this->set->offsetGet($offset); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param int $start |
67
|
|
|
* @param int $length |
68
|
|
|
* @return ScriptWitness |
69
|
|
|
*/ |
70
|
18 |
|
public function slice($start, $length) |
71
|
|
|
{ |
72
|
18 |
|
$end = $this->set->getSize(); |
73
|
18 |
|
if ($start > $end || $length > $end) { |
74
|
|
|
throw new \RuntimeException('Invalid start or length'); |
75
|
|
|
} |
76
|
|
|
|
77
|
18 |
|
$sliced = array_slice($this->set->toArray(), $start, $length); |
78
|
18 |
|
return new self($sliced); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param ScriptWitnessInterface $witness |
83
|
|
|
* @return bool |
84
|
54 |
|
*/ |
85
|
|
|
public function equals(ScriptWitnessInterface $witness) |
86
|
54 |
|
{ |
87
|
|
|
$nStack = count($this); |
88
|
|
|
if ($nStack !== count($witness)) { |
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
for ($i = 0; $i < $nStack; $i++) { |
93
|
|
|
if (false === $this->offsetGet($i)->equals($witness->offsetGet($i))) { |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return \BitWasp\Buffertools\BufferInterface |
103
|
|
|
*/ |
104
|
|
|
public function getBuffer() |
105
|
|
|
{ |
106
|
|
|
return (new ScriptWitnessSerializer())->serialize($this); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.