Completed
Pull Request — master (#526)
by thomas
83:42 queued 12:15
created

ParsedScript::getPaths()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Script\Path;
4
5
use BitWasp\Bitcoin\Script\ScriptInterface;
6
7
class ParsedScript
8
{
9
    /**
10
     * @var ScriptInterface
11
     */
12
    private $script;
13
14
    /**
15
     * @var LogicOpNode
16
     */
17
    private $ast;
18
19
    /**
20
     * @var array
21
     */
22
    private $descriptorMap;
23
24
    /**
25
     * ParsedScript constructor.
26
     * @param ScriptInterface $script
27
     * @param LogicOpNode $ast
28
     * @param ScriptBranch[] $branches
29
     */
30
    public function __construct(ScriptInterface $script, LogicOpNode $ast, array $branches)
31
    {
32
        if (!$ast->isRoot()) {
33
            throw new \RuntimeException("LogicOpNode was not for root");
34
        }
35
36
        $descriptorIdx = 0;
37
        $keyedIdxMap = []; // descriptor => ScriptBranch
38
        foreach ($branches as $branch) {
39
            $descriptor = $branch->getPath();
40
            $descriptorKey = json_encode($descriptor);
41 136
            if (array_key_exists($descriptorKey, $keyedIdxMap)) {
42
                throw new \RuntimeException("Duplicate logical pathway, invalid ScriptBranch found");
43 136
            }
44
45
            $keyedIdxMap[$descriptorKey] = $branch;
46
            $descriptorIdx++;
47 136
        }
48 136
49 136
        $this->descriptorMap = $keyedIdxMap;
50 136
        $this->script = $script;
51 136
        $this->ast = $ast;
52 136
    }
53 136
54
    /**
55
     * @return bool
56
     */
57 136
    public function hasMultipleBranches()
58 136
    {
59 136
        return $this->ast->hasChildren();
60
    }
61
62 136
    /**
63 136
     * Returns a list of paths for this script. This is not
64 136
     * always guaranteed to be in order, so take care that
65 136
     * you actually work out the paths in advance of signing,
66 136
     * and hard code them somehow.
67
     *
68
     * @return array[] - array of paths
69
     */
70
    public function getPaths()
71 136
    {
72
        return array_map(function (ScriptBranch $branch) {
73 136
            return $branch->getPath();
74
        }, $this->descriptorMap);
75
    }
76
77
    /**
78
     * Look up the branch by it's path
79
     *
80 136
     * @param array $branchDesc
81
     * @return bool|ScriptBranch
82 136
     */
83
    public function getBranchByPath(array $branchDesc)
84 136
    {
85 136
        $key = json_encode($branchDesc);
86
        if (!array_key_exists($key, $this->descriptorMap)) {
87
            throw new \RuntimeException("Unknown logical pathway");
88
        }
89
90
        return $this->descriptorMap[$key];
91
    }
92
}
93