1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Russell Michell 2018 <[email protected]> |
5
|
|
|
* @package silverstripe-verifiable |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace PhpTek\Verifiable\Backend\Chainpoint; |
9
|
|
|
|
10
|
|
|
use SilverStripe\Core\Injector\Injector; |
11
|
|
|
use SilverStripe\Core\Injector\Injectable; |
12
|
|
|
use SilverStripe\Core\Config\Configurable; |
13
|
|
|
use PhpTek\Verifiable\Exception\VerifiableBackendException; |
14
|
|
|
use PhpTek\Verifiable\Backend\GatewayProvider; |
15
|
|
|
use PhpTek\Verifiable\Backend\ServiceProvider; |
16
|
|
|
use PhpTek\Verifiable\Backend\Chainpoint\Gateway; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Service class that works as an intermediary between any data model and the |
20
|
|
|
* currently selected Merkle Tree backend gateway. |
21
|
|
|
*/ |
22
|
|
|
class Service implements ServiceProvider |
23
|
|
|
{ |
24
|
|
|
use Injectable; |
25
|
|
|
use Configurable; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var GatewayProvider |
29
|
|
|
*/ |
30
|
|
|
protected $backend; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $extra = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
|
|
public function __construct() |
42
|
|
|
{ |
43
|
|
|
$this->setGateway(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function name() : string |
50
|
|
|
{ |
51
|
|
|
return $this->getGateway()->name(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Wrapper around all gateway methods. |
56
|
|
|
* |
57
|
|
|
* @param string $method The name of the method to call |
58
|
|
|
* @param mixed $arg The argument to pass to $method |
59
|
|
|
* @return mixed |
60
|
|
|
* @throws InvalidArgumentException |
61
|
|
|
*/ |
62
|
|
|
public function call($method, $arg) |
63
|
|
|
{ |
64
|
|
|
if (!method_exists($this, $method)) { |
65
|
|
|
throw new \InvalidArgumentException("$method doesn't exist."); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->gateway->setDiscoveredNodes($this->getExtra()); |
69
|
|
|
|
70
|
|
|
return $this->$method($arg); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Write a hash of data as per the "verifiable_fields" config static on each |
75
|
|
|
* {@link DataObject}. |
76
|
|
|
* |
77
|
|
|
* @param array $data |
78
|
|
|
* @return mixed The result of this call to the backend. |
79
|
|
|
*/ |
80
|
|
|
protected function write(array $data) |
81
|
|
|
{ |
82
|
|
|
return $this->gateway->hashes([$this->hash($data)]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Fetch a chainpoint proof for the passed $uuid. |
87
|
|
|
* |
88
|
|
|
* @param mixed string | array $uuid |
89
|
|
|
* @return string The JSON-LD chainpoint proof. |
90
|
|
|
*/ |
91
|
|
|
protected function read($uuid) : string |
92
|
|
|
{ |
93
|
|
|
if (is_array($uuid)) { |
94
|
|
|
return $this->gateway->proofs(array_unique($uuid)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->gateway->proofs($uuid); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Verify the given JSON-LD chainpoint proof against the backend. |
102
|
|
|
* |
103
|
|
|
* @param string $proof A JSON-LD chainpoint proof. |
104
|
|
|
* @return mixed |
105
|
|
|
*/ |
106
|
|
|
protected function verify(string $proof) |
107
|
|
|
{ |
108
|
|
|
return $this->gateway->verify($proof); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set some arbitrary data onto the service. Used as a way of acting as |
113
|
|
|
* an intermediary or broker between DataOBjects and the backend. |
114
|
|
|
* |
115
|
|
|
* @param array $extra |
116
|
|
|
* @return VerifiableService |
|
|
|
|
117
|
|
|
*/ |
118
|
|
|
public function setExtra(array $extra = []) |
119
|
|
|
{ |
120
|
|
|
if (!$extra) { |
|
|
|
|
121
|
|
|
$this->gateway->setDiscoveredNodes(); |
122
|
|
|
$this->extra = $this->gateway->getDiscoveredNodes(); |
123
|
|
|
} else { |
124
|
|
|
$this->extra = $extra; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this; |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Return arbitrary data set to this service. |
132
|
|
|
* |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
public function getExtra() |
136
|
|
|
{ |
137
|
|
|
return $this->extra; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Set, configure and return a new Merkle Tree storage backend. |
142
|
|
|
* |
143
|
|
|
* @param GatewayProvider $provider Optional manually passed backend. |
144
|
|
|
* @return VerifiableService |
145
|
|
|
* @throws VerifiableBackendException |
146
|
|
|
*/ |
147
|
|
|
public function setGateway(GatewayProvider $provider = null) |
148
|
|
|
{ |
149
|
|
|
if ($provider) { |
150
|
|
|
$this->gateway = $provider; |
|
|
|
|
151
|
|
|
|
152
|
|
|
return $this; |
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$this->gateway = Injector::inst()->create(Gateway::class); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return GatewayProvider |
160
|
|
|
*/ |
161
|
|
|
public function getGateway() |
162
|
|
|
{ |
163
|
|
|
return $this->gateway; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Hashes the data passed into the $hash param. |
168
|
|
|
* |
169
|
|
|
* @param array $data An array of data who's values should be hashed. |
170
|
|
|
* @return string The resulting hashed data. |
171
|
|
|
*/ |
172
|
|
|
public function hash(array $data) : string |
173
|
|
|
{ |
174
|
|
|
$func = $this->gateway->hashFunc(); |
175
|
|
|
$text = json_encode($data, true); |
|
|
|
|
176
|
|
|
|
177
|
|
|
return hash($func, $text); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths