Coverage Summary for Class: Secp256k1 (org.ethereum.crypto.signature)
Class |
Class, %
|
Method, %
|
Line, %
|
Secp256k1 |
100%
(1/1)
|
40%
(2/5)
|
16%
(4/25)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2020 RSK Labs Ltd.
4 * (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>)
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package org.ethereum.crypto.signature;
21
22 import co.rsk.config.RskSystemProperties;
23 import com.google.common.annotations.VisibleForTesting;
24 import org.bitcoin.Secp256k1Context;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import javax.annotation.Nullable;
29
30 /**
31 * Class in charge of being the access point to implementations of all the Signature related functionality.
32 * It returns an instance of {@link Secp256k1Service}
33 * Is implemented as a Singleton, so the only way to access an instance is through getInstance().
34 */
35 public final class Secp256k1 {
36
37 private static final String NATIVE_LIB = "native";
38 private static final Logger logger = LoggerFactory.getLogger(Secp256k1.class);
39
40 private static Secp256k1Service instance = new Secp256k1ServiceBC();
41 private static boolean initialized = false;
42
43 private Secp256k1() {
44 }
45
46 /**
47 * <p> It should be called only once in Node Startup.</p>
48 *
49 * <p> It reads "crypto.library" property to decide which impl to initialize.</p>
50 *
51 * <p> By default it initialize Bouncy Castle impl.</p>
52 *
53 * @param rskSystemProperties {@link Nullable} = Could be null in tests.
54 */
55 public static synchronized void initialize(@Nullable RskSystemProperties rskSystemProperties) {
56 // Just a warning for duplicate initialization.
57 if (initialized) {
58 logger.warn("Instance was already initialized. This could be either for duplicate initialization or calling to getInstance before init.");
59 } else {
60 initialized = true;
61 }
62 if (rskSystemProperties != null) {
63 String cryptoLibrary = rskSystemProperties.cryptoLibrary();
64 logger.debug("Trying to initialize Signature Service: {}.", cryptoLibrary);
65 if (NATIVE_LIB.equals(cryptoLibrary)) {
66 if(Secp256k1Context.isEnabled()){
67 instance = new Secp256k1ServiceNative();
68 logger.debug("Native Service initialized.");
69 } else {
70 instance = new Secp256k1ServiceBC();
71 logger.debug("Signature Service {} not available, initialized Bouncy Castle.", cryptoLibrary);
72 }
73 } else {
74 instance = new Secp256k1ServiceBC();
75 }
76 } else {
77 logger.warn("Empty system properties.");
78 }
79 }
80
81 /**
82 * As a singleton this should be the only entry point for creating instances of SignatureService classes.
83 *
84 * @return either {@link Secp256k1ServiceBC} or Native Signature (future) implementation.
85 */
86 public static Secp256k1Service getInstance() {
87 return instance;
88 }
89
90 @VisibleForTesting
91 static void reset() {
92 instance = new Secp256k1ServiceBC();
93 initialized = false;
94 }
95 }