1 | <?php |
||
12 | class EcAdapterFactory |
||
13 | { |
||
14 | /** |
||
15 | * @var EcAdapterInterface |
||
16 | */ |
||
17 | private static $adapter; |
||
18 | |||
19 | /** |
||
20 | * @var |
||
21 | */ |
||
22 | private static $context; |
||
23 | |||
24 | /** |
||
25 | * @param null $flags |
||
26 | * @return resource |
||
27 | */ |
||
28 | public static function getSecp256k1Context($flags = null) |
||
29 | { |
||
30 | if (!extension_loaded('secp256k1')) { |
||
31 | throw new \RuntimeException('Secp256k1 not installed'); |
||
32 | } |
||
33 | |||
34 | if (self::$context === null) { |
||
35 | self::$context = secp256k1_context_create($flags ?: SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY); |
||
36 | } |
||
37 | |||
38 | return self::$context; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @param Math $math |
||
43 | * @param GeneratorPoint $generator |
||
44 | * @return EcAdapterInterface |
||
45 | */ |
||
46 | public static function getAdapter(Math $math, GeneratorPoint $generator) |
||
47 | { |
||
48 | if (self::$adapter !== null) { |
||
49 | return self::$adapter; |
||
50 | } |
||
51 | |||
52 | if (extension_loaded('secp256k1') && Semver::satisfies(phpversion('secp256k1'), "~0.1.0")) { |
||
53 | self::$adapter = self::getSecp256k1($math, $generator); |
||
54 | } else { |
||
55 | self::$adapter = self::getPhpEcc($math, $generator); |
||
56 | } |
||
57 | |||
58 | return self::$adapter; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param EcAdapterInterface $ecAdapter |
||
63 | */ |
||
64 | public static function setAdapter(EcAdapterInterface $ecAdapter) |
||
68 | |||
69 | /** |
||
70 | * @param Math $math |
||
71 | * @param GeneratorPoint $generator |
||
72 | * @return PhpEcc |
||
73 | */ |
||
74 | public static function getPhpEcc(Math $math, GeneratorPoint $generator) |
||
78 | |||
79 | /** |
||
80 | * @param Math $math |
||
81 | * @param GeneratorPoint $generator |
||
82 | * @return Secp256k1 |
||
83 | */ |
||
84 | public static function getSecp256k1(Math $math, GeneratorPoint $generator) |
||
88 | } |
||
89 |