forked from Bit-Wasp/bitcoin-php
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathEcSerializer.php
More file actions
142 lines (120 loc) · 4.01 KB
/
EcSerializer.php
File metadata and controls
142 lines (120 loc) · 4.01 KB
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
<?php
declare(strict_types=1);
namespace BitWasp\Bitcoin\Crypto\EcAdapter;
use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface;
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PrivateKeySerializerInterface;
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PublicKeySerializerInterface;
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Signature\CompactSignatureSerializerInterface;
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Signature\DerSignatureSerializerInterface;
class EcSerializer
{
const PATH_PHPECC = 'BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\\';
const PATH_SECP256K1 = 'BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\\';
/**
* @var string[]
*/
private static $serializerInterface = [
PrivateKeySerializerInterface::class,
PublicKeySerializerInterface::class,
CompactSignatureSerializerInterface::class,
DerSignatureSerializerInterface::class,
];
/**
* @var string[]
*/
private static $serializerImpl = [
'Serializer\Key\PrivateKeySerializer',
'Serializer\Key\PublicKeySerializer',
'Serializer\Signature\CompactSignatureSerializer',
'Serializer\Signature\DerSignatureSerializer'
];
/**
* @var array
*/
private static $map = [];
/**
* @var bool
*/
private static $useCache = true;
/**
* @var array
*/
private static $cache = [];
/**
* @param string $interface
* @return string
*/
public static function getImplRelPath(string $interface): string
{
if (0 === count(self::$map)) {
if (!in_array($interface, self::$serializerInterface, true)) {
throw new \InvalidArgumentException('Interface not known');
}
$cInterface = count(self::$serializerInterface);
if ($cInterface !== count(self::$serializerImpl)) {
throw new \InvalidArgumentException('Invalid serializer interface map');
}
for ($i = 0; $i < $cInterface; $i++) {
/** @var string $iface */
$iface = self::$serializerInterface[$i];
$ipath = self::$serializerImpl[$i];
self::$map[$iface] = $ipath;
}
}
return self::$map[$interface];
}
/**
* @return array
*/
public static function getImplPaths(): array
{
return [
'BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Adapter\EcAdapter' => 'BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\\',
'BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Adapter\EcAdapter' => 'BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\\'
];
}
/**
* @param EcAdapterInterface $adapter
* @return string
*/
public static function getAdapterImplPath(EcAdapterInterface $adapter): string
{
$paths = static::getImplPaths();
$class = get_class($adapter);
if (!isset($paths[$class])) {
throw new \RuntimeException('Unknown EcAdapter');
}
return $paths[$class];
}
/**
* @param string $interface
* @param bool $useCache
* @param EcAdapterInterface $adapter
* @return mixed
*/
public static function getSerializer(string $interface, $useCache = true, ?EcAdapterInterface $adapter = null)
{
if (null === $adapter) {
$adapter = Bitcoin::getEcAdapter();
}
$key = get_class($adapter) . ":" . $interface;
if (array_key_exists($key, self::$cache)) {
return self::$cache[$key];
}
$classPath = self::getAdapterImplPath($adapter) . self::getImplRelPath($interface);
$class = new $classPath($adapter);
if ($useCache && self::$useCache) {
self::$cache[$key] = $class;
}
return $class;
}
/**
* Disables caching of serializers
*/
public static function disableCache()
{
self::$useCache = false;
self::$cache = [];
}
}